Exemplo n.º 1
0
// NewHelper ...
func NewHelper(destinationDir, remoteURI string, resetRepository bool) (Helper, error) {
	if destinationDir == "" {
		return Helper{}, errors.New("destination dir path is empty")
	}

	if remoteURI == "" {
		return Helper{}, errors.New("remote URI is empty")
	}

	// Expand destination dir
	fullDestinationDir, err := pathutil.AbsPath(destinationDir)
	if err != nil {
		return Helper{}, err
	}

	helper := Helper{
		destinationDir: fullDestinationDir,
		remoteURI:      remoteURI,
	}

	// Check if .git exist
	gitDirPth := filepath.Join(fullDestinationDir, ".git")
	if exist, err := pathutil.IsDirExists(gitDirPth); err != nil {
		return Helper{}, err
	} else if exist {
		if remotes, err := helper.RemoteList(); err != nil {
			return Helper{}, err
		} else {
			if !strings.Contains(remotes, remoteURI) {
				return Helper{}, fmt.Errorf(".git folder already exists in the destination dir: %s, using a different remote", fullDestinationDir)
			} else {
				if resetRepository {
					if err = helper.Clean(); err != nil {
						return Helper{}, err
					}
				}
				helper.originPresent = true
			}
		}
	}

	// Create destination dir if not exist
	if exist, err := pathutil.IsDirExists(fullDestinationDir); err != nil {
		return Helper{}, err
	} else if !exist {
		if err := os.MkdirAll(fullDestinationDir, 0777); err != nil {
			return Helper{}, err
		}
	}

	return helper, nil
}
Exemplo n.º 2
0
func saveAttachements(projectPath, scheme string) error {
	projectName := filepath.Base(projectPath)
	projectExt := filepath.Ext(projectName)
	projectName = strings.TrimSuffix(projectName, projectExt)

	userHome := pathutil.UserHomeDir()
	deviedDataDir := filepath.Join(userHome, "Library/Developer/Xcode/DerivedData")
	projectDerivedDataDirPattern := filepath.Join(deviedDataDir, fmt.Sprintf("%s-*", projectName))
	projectDerivedDataDirs, err := filepath.Glob(projectDerivedDataDirPattern)
	if err != nil {
		return err
	}

	if len(projectDerivedDataDirs) > 1 {
		return fmt.Errorf("more than 1 project derived data dir found: %v, with pattern: %s", projectDerivedDataDirs, projectDerivedDataDirPattern)
	} else if len(projectDerivedDataDirs) == 0 {
		return fmt.Errorf("no project derived data dir found with pattern: %s", projectDerivedDataDirPattern)
	}
	projectDerivedDataDir := projectDerivedDataDirs[0]

	testLogDir := filepath.Join(projectDerivedDataDir, "Logs", "Test")
	if exist, err := pathutil.IsDirExists(testLogDir); err != nil {
		return err
	} else if !exist {
		return fmt.Errorf("no test logs found at: %s", projectDerivedDataDir)
	}

	testLogAttachmentsDir := filepath.Join(testLogDir, "Attachments")
	if exist, err := pathutil.IsDirExists(testLogAttachmentsDir); err != nil {
		return err
	} else if !exist {
		return fmt.Errorf("no test attachments found at: %s", testLogAttachmentsDir)
	}

	deployDir := os.Getenv("BITRISE_DEPLOY_DIR")
	if deployDir == "" {
		return errors.New("No BITRISE_DEPLOY_DIR found")
	}

	zipedTestsDerivedDataPath := filepath.Join(deployDir, fmt.Sprintf("%s-xc-test-Attachments.zip", scheme))
	if err := cmd.Zip(testLogDir, "Attachments", zipedTestsDerivedDataPath); err != nil {
		return err
	}

	if err := cmd.ExportEnvironmentWithEnvman("BITRISE_XCODE_TEST_ATTACHMENTS_PATH", zipedTestsDerivedDataPath); err != nil {
		log.Warn("Failed to export: BITRISE_XCODE_TEST_ATTACHMENTS_PATH, error: %s", err)
	}
	return nil
}
Exemplo n.º 3
0
func TestNewHelper(t *testing.T) {
	t.Log("it fails if destinationDir empty")
	{
		helper, err := NewHelper("", "https://github.com/bitrise-samples/git-clone-test.git", false)
		require.Error(t, err)
		require.Equal(t, "", helper.destinationDir)
		require.Equal(t, "", helper.remoteURI)
	}

	t.Log("it fails if remote URI empty")
	{
		helper, err := NewHelper("./", "", false)
		require.Error(t, err)
		require.Equal(t, "", helper.destinationDir)
		require.Equal(t, "", helper.remoteURI)
	}

	t.Log("it fails if remote URI empty")
	{
		helper, err := NewHelper("./", "", false)
		require.Error(t, err)
		require.Equal(t, "", helper.destinationDir)
		require.Equal(t, "", helper.remoteURI)
	}

	t.Log("it creates destination dir if not exist")
	{
		tmpDir, err := pathutil.NormalizedOSTempDirPath("__test__")
		require.NoError(t, err)

		destinationDir := filepath.Join(tmpDir, "dst")
		exist, err := pathutil.IsDirExists(destinationDir)
		require.NoError(t, err)
		require.Equal(t, false, exist)

		helper, err := NewHelper(destinationDir, "https://github.com/bitrise-samples/git-clone-test.git", false)
		require.NoError(t, err)
		require.Equal(t, destinationDir, helper.destinationDir)
		require.Equal(t, "https://github.com/bitrise-samples/git-clone-test.git", helper.remoteURI)

		exist, err = pathutil.IsDirExists(destinationDir)
		require.NoError(t, err)
		require.Equal(t, true, exist)

		require.NoError(t, os.RemoveAll(tmpDir))
	}
}
Exemplo n.º 4
0
func ensureEnvmanConfigDirExists() error {
	confDirPth := getEnvmanConfigsDirPath()
	isExists, err := pathutil.IsDirExists(confDirPth)
	if !isExists || err != nil {
		if err := os.MkdirAll(confDirPth, 0777); err != nil {
			return err
		}
	}
	return nil
}
// CleanAll ...
func (builder Model) CleanAll(callback ClearCommandCallback) error {
	whitelistedProjects := builder.whitelistedProjects()

	for _, proj := range whitelistedProjects {

		projectDir := filepath.Dir(proj.Pth)

		{
			binPth := filepath.Join(projectDir, "bin")
			if exist, err := pathutil.IsDirExists(binPth); err != nil {
				return err
			} else if exist {
				if callback != nil {
					callback(proj, binPth)
				}

				if err := os.RemoveAll(binPth); err != nil {
					return err
				}
			}
		}

		{
			objPth := filepath.Join(projectDir, "obj")
			if exist, err := pathutil.IsDirExists(objPth); err != nil {
				return err
			} else if exist {
				if callback != nil {
					callback(proj, objPth)
				}

				if err := os.RemoveAll(objPth); err != nil {
					return err
				}
			}
		}
	}

	return nil
}
Exemplo n.º 6
0
// DeletePlugin ...
func DeletePlugin(name string) error {
	pluginDir := GetPluginDir(name)

	if exists, err := pathutil.IsDirExists(pluginDir); err != nil {
		return err
	} else if !exists {
		return fmt.Errorf("Plugin (%s) not installed", name)
	}

	if err := os.RemoveAll(pluginDir); err != nil {
		return fmt.Errorf("failed to delete dir (%s)", pluginDir)
	}

	return DeletePluginRoute(name)
}
Exemplo n.º 7
0
func initExportOutputDir() (string, error) {
	absExportOutputDirPath, err := pathutil.AbsPath(confExportOutputDirPath)
	log.Debugf("absExportOutputDirPath: %s", absExportOutputDirPath)
	if err != nil {
		return absExportOutputDirPath, fmt.Errorf("Failed to determin Absolute path of export dir: %s", confExportOutputDirPath)
	}
	if exist, err := pathutil.IsDirExists(absExportOutputDirPath); err != nil {
		return absExportOutputDirPath, fmt.Errorf("Failed to determin whether the export directory already exists: %s", err)
	} else if !exist {
		if err := os.Mkdir(absExportOutputDirPath, 0777); err != nil {
			return absExportOutputDirPath, fmt.Errorf("Failed to create export output directory at path: %s | error: %s", absExportOutputDirPath, err)
		}
	} else {
		log.Infof("Export output dir already exists at path: %s", absExportOutputDirPath)
	}
	return absExportOutputDirPath, nil
}
Exemplo n.º 8
0
// LoadPlugin ...
func LoadPlugin(name string) (Plugin, bool, error) {
	pluginDir := GetPluginDir(name)

	if exists, err := pathutil.IsDirExists(pluginDir); err != nil {
		return Plugin{}, false, fmt.Errorf("Failed to check dir (%s), err: %s", pluginDir, err)
	} else if !exists {
		return Plugin{}, false, nil
	}

	pluginYMLPath := GetPluginYMLPath(name)

	plugin, err := NewPluginFromYML(pluginYMLPath)
	if err != nil {
		return Plugin{}, true, err
	}

	return plugin, true, nil
}
Exemplo n.º 9
0
func convert(c *cli.Context) {
	// Input validation
	src := c.String(SourceKey)
	if src == "" {
		log.Fatal("Missing source")
	}

	sources := []string{}
	srcSlice := strings.Split(src, ",")
	if len(srcSlice) > 1 {
		// Comma separated sources
		log.Info("Converting workflows at:", srcSlice)
		fmt.Println()

		sources = srcSlice
	} else {
		isDir, err := pathutil.IsDirExists(src)
		if err != nil {
			log.Fatal("Failed to check path:", err)
		}
		if isDir {
			// Converting workflows in directory
			log.Info("Converting workflows in dir:", src)
			fmt.Println()

			if err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error {
				if filepath.Ext(path) == ".yml" {
					sources = append(sources, path)
				}
				return nil
			}); err != nil {
				log.Fatal("Faild to collect workflow pathes")
			}
			log.Info("Converting workflows at:", sources)
			fmt.Println()

		} else {
			// Converting single workflow
			log.Info("Converting single workflows at:", src)
			fmt.Println()

			sources = append(sources, src)
		}
	}

	dstPth := c.String(DestinationKey)
	if dstPth == "" {
		log.Fatal("Missing destination")
	}

	// Read old workflow
	oldWorkflowMap := map[string]oldModels.WorkflowModel{}
	for _, srcPth := range sources {
		log.Infoln("Converting workflow at:", srcPth)
		fmt.Println()

		oldWorkflow, err := utils.ReadOldWorkflowModel(srcPth)
		if err != nil {
			log.Fatal("Failed to read old workflow:", err)
		}
		oldWorkflowID := getWorkflowNameFromPath(srcPth)

		log.Debugln("Old workflow:")
		log.Debugf("%#v", oldWorkflow)
		oldWorkflowMap[oldWorkflowID] = oldWorkflow
	}

	// Convert workflow
	newConfig, err := converter.ConvertOldWorkfowModels(oldWorkflowMap)
	if err != nil {
		log.Fatal("Failed to convert old workflow:", err)
	}
	log.Debugln("New workflow:")
	log.Debugf("%#v", newConfig)

	// Write new wokrflow to file
	if err := utils.WriteNewWorkflowModel(dstPth, newConfig); err != nil {
		if err != nil {
			log.Fatal("Failed to write new workflow:", err)
		}
	}

	log.Infoln("Converted workflow path:", dstPth)
	fmt.Println()
}
Exemplo n.º 10
0
func export(c *cli.Context) error {
	// Input validation
	steplibURI := c.String("steplib")
	outputPth := c.String("output")
	exportTypeStr := c.String("export-type")

	if steplibURI == "" {
		return fmt.Errorf("Missing required input: steplib")
	}

	if outputPth == "" {
		return fmt.Errorf("Missing required input: output")
	}

	exportType := exportTypeFull
	if exportTypeStr != "" {
		var err error
		exportType, err = parseExportType(exportTypeStr)
		if err != nil {
			return err
		}
	}

	log.Infof("Exporting StepLib (%s) spec, export-type: %s, output: %s", steplibURI, exportTypeStr, outputPth)

	// Setup StepLib
	if exist, err := stepman.RootExistForCollection(steplibURI); err != nil {
		return fmt.Errorf("Failed to check if setup was done for StepLib, error: %s", err)
	} else if !exist {
		log.Infof("StepLib does not exist, setup...")
		if err := setupSteplib(steplibURI, false); err != nil {
			return fmt.Errorf("Failed to setup StepLib, error: %s", err)
		}
	}

	// Prepare spec
	stepLibSpec, err := stepman.ReadStepSpec(steplibURI)
	if err != nil {
		log.Fatalln("Failed to read StepLib spec, error: %s", err)
	}

	switch exportType {
	case exportTypeMinimal:
		stepLibSpec = convertToMinimalSpec(stepLibSpec)
	case exportTypeLatest:
		stepLibSpec = convertToLatestSpec(stepLibSpec)
	}

	stepLibSpecBytes, err := json.Marshal(stepLibSpec)
	if err != nil {
		return fmt.Errorf("Failed to marshal StepLib, error: %s", err)
	}

	// Export spec
	outputDir := filepath.Dir(outputPth)

	exist, err := pathutil.IsDirExists(outputDir)
	if err != nil {
		return fmt.Errorf("Failed to check if dir (%s) exist, error: %s", outputDir, err)
	}
	if !exist {
		if err := os.MkdirAll(outputDir, 0777); err != nil {
			return fmt.Errorf("Failed to create dir (%s), error: %s", outputDir, err)
		}
	}

	if err := fileutil.WriteBytesToFile(outputPth, stepLibSpecBytes); err != nil {
		return fmt.Errorf("Failed to write StepLib spec to: %s, error: %s", outputPth, err)
	}

	log.Infof("StepLib spec exported to: %s", outputPth)

	return nil
}