// InitializeProject initializes a Gauge project with specified template func InitializeProject(templateName string) { wd, err := os.Getwd() if err != nil { logger.Fatalf("Failed to find working directory. %s", err.Error()) } config.ProjectRoot = wd exists, _ := common.UrlExists(getTemplateURL(templateName)) if exists { err = initializeTemplate(templateName) } else { err = createProjectTemplate(templateName) } if err != nil { logger.Fatalf("Failed to initialize project. %s", err.Error()) } logger.Info("Successfully initialized the project. Run specifications with \"gauge specs/\"\n") language := getTemplateLangauge(templateName) if !install.IsCompatiblePluginInstalled(language, true) { logger.Info("Compatible langauge plugin %s is not installed. Installing plugin...", language) install.HandleInstallResult(install.InstallPlugin(language, ""), language, true) } }
func createProjectTemplate(language string) error { if !install.IsCompatibleLanguagePluginInstalled(language) { logger.Info("Compatible %s plugin is not installed \n", language) logger.Info("Installing plugin => %s ... \n\n", language) if result := install.InstallPlugin(language, ""); !result.Success { return fmt.Errorf("Failed to install plugin %s . %s \n", language, result.Error.Error()) } } // Create the project manifest showMessage("create", common.ManifestFile) if common.FileExists(common.ManifestFile) { showMessage("skip", common.ManifestFile) } manifest := &manifest.Manifest{Language: language, Plugins: defaultPlugins} if err := manifest.Save(); err != nil { return err } // creating the spec directory showMessage("create", specsDirName) if !common.DirExists(specsDirName) { err := os.Mkdir(specsDirName, common.NewDirectoryPermissions) if err != nil { showMessage("error", fmt.Sprintf("Failed to create %s. %s", specsDirName, err.Error())) } } else { showMessage("skip", specsDirName) } // Copying the skeleton file skelFile, err := common.GetSkeletonFilePath(skelFileName) if err != nil { return err } specFile := path.Join(specsDirName, skelFileName) showMessage("create", specFile) if common.FileExists(specFile) { showMessage("skip", specFile) } else { err = common.CopyFile(skelFile, specFile) if err != nil { showMessage("error", fmt.Sprintf("Failed to create %s. %s", specFile, err.Error())) } } // Creating the env directory showMessage("create", common.EnvDirectoryName) if !common.DirExists(common.EnvDirectoryName) { err = os.Mkdir(common.EnvDirectoryName, common.NewDirectoryPermissions) if err != nil { showMessage("error", fmt.Sprintf("Failed to create %s. %s", common.EnvDirectoryName, err.Error())) } } defaultEnv := path.Join(common.EnvDirectoryName, envDefaultDirName) showMessage("create", defaultEnv) if !common.DirExists(defaultEnv) { err = os.Mkdir(defaultEnv, common.NewDirectoryPermissions) if err != nil { showMessage("error", fmt.Sprintf("Failed to create %s. %s", defaultEnv, err.Error())) } } defaultJSON, err := common.GetSkeletonFilePath(path.Join(common.EnvDirectoryName, common.DefaultEnvFileName)) if err != nil { return err } defaultJSONDest := path.Join(defaultEnv, common.DefaultEnvFileName) showMessage("create", defaultJSONDest) err = common.CopyFile(defaultJSON, defaultJSONDest) if err != nil { showMessage("error", fmt.Sprintf("Failed to create %s. %s", defaultJSONDest, err.Error())) } return runner.ExecuteInitHookForRunner(language) }
func main() { flag.Parse() util.SetWorkingDir(*workingDir) initPackageFlags() validGaugeProject := true err := config.SetProjectRoot(flag.Args()) if err != nil { validGaugeProject = false } env.LoadEnv(*currentEnv) logger.Initialize(*logLevel) if *gaugeVersion && *machineReadable { printJSONVersion() } else if *machineReadable { fmt.Printf("flag '--machine-readable' can only be used with '--version' or '-v'\n\n") fmt.Printf("Usage:\n\n") flag.PrintDefaults() os.Exit(1) } else if *gaugeVersion { printVersion() } else if *initialize != "" { projectInit.InitializeProject(*initialize) } else if *installZip != "" && *installPlugin != "" { install.HandleInstallResult(install.InstallPluginFromZipFile(*installZip, *installPlugin), *installPlugin, true) } else if *installPlugin != "" { install.HandleInstallResult(install.InstallPlugin(*installPlugin, *pluginVersion), *installPlugin, true) } else if *uninstallPlugin != "" { install.UninstallPlugin(*uninstallPlugin, *pluginVersion) } else if *installAll { install.InstallAllPlugins() } else if *update != "" { install.HandleUpdateResult(install.InstallPlugin(*update, *pluginVersion), *update, true) } else if *updateAll { install.UpdatePlugins() } else if *checkUpdates { install.PrintUpdateInfoWithDetails() } else if *addPlugin != "" { install.AddPluginToProject(*addPlugin, *pluginArgs) } else if *listTemplates { projectInit.ListTemplates() } else if flag.NFlag() == 0 && len(flag.Args()) == 0 { printUsage() os.Exit(0) } else if validGaugeProject { var specDirs = []string{common.SpecsDirectoryName} if len(flag.Args()) > 0 { specDirs = flag.Args() } if *refactorSteps != "" { refactorInit(flag.Args()) } else if *daemonize { api.RunInBackground(*apiPort, specDirs) } else if *specFilesToFormat != "" { formatter.FormatSpecFilesIn(*specFilesToFormat) } else if *validate { validation.Validate(flag.Args()) } else { exitCode := execution.ExecuteSpecs(specDirs) os.Exit(exitCode) } } else { logger.Fatalf(err.Error()) } }