// buildAllPackages builds each package without vendor packages. func buildAllPackages(buildPath string, args []string, packages []string) error { // Clean buildPath if exists before build if err := utils.CleanDirectory(buildPath); err != nil { return err } // Save root and change to build directory root, _ := os.Getwd() os.Chdir(buildPath) // Wait group var wg sync.WaitGroup wg.Add(len(packages)) // Error channel chErr := make(chan error) // Build the packages for _, pkg := range packages { // Routine for package build go func(pkg string, args []string, chErr chan error) { defer wg.Done() // Add package to arguments args = append(args, pkg) // Shell command go build [flags] [binary] [package] cmdName := "go" cmdArgs := []string{"build"} // Add arguments cmdArgs = append(cmdArgs, args...) // Run go build with arguments command, send error to channel chErr <- utils.CmdRun(cmdName, cmdArgs...) }(pkg, args, chErr) } // Wait for channel errors for i := 0; i < len(packages); i++ { err := <-chErr if err != nil { return err } } // Wait for routines wg.Wait() // Change back to root os.Chdir(root) return nil }
// createFilesFromTemplates generate files from templates func createFilesFromTemplates(settings *Settings) error { // For templates var lxbPath string // Loop through GOPATHs and find the LxbPackageName directory. for _, v := range settings.GoPaths { // Checking for LxbPackage in this directory checkPath := filepath.Join(v, "src", lxb.LxbPackageName) if _, err := os.Stat(checkPath); os.IsNotExist(err) { continue } else { lxbPath = checkPath break } } // Check whether LxbPackage was found if len(lxbPath) == 0 { return fmt.Errorf("Package path %s not found in gopaths.", lxb.LxbPackageName) } // Set binary name binaryName := settings.ProjectName if runtime.GOOS == "windows" { binaryName += ".exe" } // Project package name, if name with package-name than replace with package_name projectPackageName := strings.Replace(settings.ProjectName, "-", "_", -1) // Template vars tplVars := map[string]string{"ProjectName": settings.ProjectName, "ProjectPackageName": projectPackageName, "PackagePath": settings.PackagePath, "LxbPackageName": lxb.LxbPackageName, "BinaryName": binaryName} ftpls := map[string]FileTemplates{ "changelog": FileTemplates{ "changelog.tpl", "CHANGELOG.md", settings.Root, tplVars, }, "cmd_doc": FileTemplates{ "cmd_doc.tpl", "doc.go", filepath.Join(settings.Root, "cmd", settings.ProjectName), tplVars, }, "cmd_main": FileTemplates{ "cmd_main.tpl", "main.go", filepath.Join(settings.Root, "cmd", settings.ProjectName), tplVars, }, "cmd_main_test": FileTemplates{ "cmd_main_test.tpl", "main_test.go", filepath.Join(settings.Root, "cmd", settings.ProjectName), tplVars, }, "editorconfig": FileTemplates{ "editorconfig.tpl", ".editorconfig", settings.Root, nil, }, "gitignore": FileTemplates{ "gitignore.tpl", ".gitignore", settings.Root, nil, }, "project_package": FileTemplates{ "project_package.tpl", settings.ProjectName + ".go", settings.Root, tplVars, }, "project_package_test": FileTemplates{ "project_package_test.tpl", settings.ProjectName + "_test.go", settings.Root, tplVars, }, "readme": FileTemplates{ "readme.tpl", "README.md", settings.Root, tplVars, }, "task-runner_build": FileTemplates{ "task-runner_build.tpl", "build.go", filepath.Join(settings.Root, "_task-runner"), tplVars, }, "task-runner_coverage": FileTemplates{ "task-runner_coverage.tpl", "coverage.go", filepath.Join(settings.Root, "_task-runner"), tplVars, }, "task-runner_get": FileTemplates{ "task-runner_get.tpl", "get.go", filepath.Join(settings.Root, "_task-runner"), tplVars, }, "task-runner_main": FileTemplates{ "task-runner_main.tpl", "main.go", filepath.Join(settings.Root, "_task-runner"), tplVars, }, "task-runner_run": FileTemplates{ "task-runner_run.tpl", "run.go", filepath.Join(settings.Root, "_task-runner"), tplVars, }, "task-runner_test": FileTemplates{ "task-runner_test.tpl", "test.go", filepath.Join(settings.Root, "_task-runner"), tplVars, }, } // Delete vendor if exists and create new if err := utils.CleanDirectory(filepath.Join(settings.Root, "vendor")); err != nil { return err } // Path to templates tplPath := filepath.Join(lxbPath, "_templates") // Read all templates tpl := template.Must(template.New("name").ParseGlob(filepath.Join(tplPath, "*.tpl"))) // done channel with error done := make(chan error, len(ftpls)) // start the template worker as go routines for _, ftpl := range ftpls { go templateWorker(tpl, ftpl, done) } // wait of end all channels for i := 0; i < len(ftpls); i++ { <-done } return nil }
// Coverage packages and create report // // Example for coverage all packages with html report // $lxb coverage -html func coverageTask(c *taskRunner.Context) { report := "" reportPath := filepath.Join("_build", "reports") packages := []string{} // Clean reports if err := utils.CleanDirectory(reportPath); err != nil { log.Fatal(err) } // Check arguments for package or report options if len(c.Args) > 0 { // If the first argument a package name? // Check this by loading with getPackage. pkgName, err := utils.GetPackage(c.Args[0]) if err != nil { log.Fatal(err) } // Check loaded package name if pkgName != "" { // First argument is valid package name. // Check report argument if len(c.Args) > 1 { // Overwrite with html report settings if c.Args[1] == "-html" || c.Args[1] == "-xml" { report = c.Args[1][1:] } } // Add the package name to packages for coverage. packages = append(packages, pkgName) } else { // First argument is not valid package name // Check if first argument a report setting if c.Args[0] == "-html" || c.Args[0] == "-xml" { // Set report report = c.Args[0][1:] // First argument is not valid package name but // a valid report setting. Set all packages for coverage packages, err = utils.GetAllPackages("/vendor/") if err != nil { log.Fatal(err) } } else { // First argument is not valid package name and // not a valid report setting. Exit with error. log.Fatalf("package %s not found", c.Args[0]) } } } else { // Pass no arguments, use all packages var err error packages, err = utils.GetAllPackages("/vendor/") if err != nil { log.Fatal(err) } } // Start coverage and create report if err := lxCoverage.GocovCoverage(report, packages, reportPath, OpenHtmlReport); err != nil { log.Fatal(err) } }