From 97dab6aa97e329aca143e0e013b689c09a0f56f2 Mon Sep 17 00:00:00 2001 From: Bernd Ahlers Date: Sat, 20 Jan 2018 01:56:51 +0100 Subject: [PATCH] Update package.json files during release and add npm-version command Closes #2 --- apply/apply.go | 4 +++ apply/execute.go | 68 +++++++++++++++++++++++++++++++++++++++++++ apply/noop.go | 8 +++++ cmd/apply_manifest.go | 34 ++++++++++++++++++++++ cmd/npm_version.go | 57 ++++++++++++++++++++++++++++++++++++ utils/utils.go | 65 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 236 insertions(+) create mode 100644 cmd/npm_version.go diff --git a/apply/apply.go b/apply/apply.go index 602cc23..26c92c4 100644 --- a/apply/apply.go +++ b/apply/apply.go @@ -30,6 +30,10 @@ type Applier interface { MavenSetParent(module project.Module, parentVersion string) MavenSetProperty(module project.Module, name string, value string) + + NpmVersionSet(module project.Module, newVersion string) + + NpmVersionCommit(module project.Module, newVersion string) } // Ensures that the server module gets handled first. diff --git a/apply/execute.go b/apply/execute.go index 12d1082..6487149 100644 --- a/apply/execute.go +++ b/apply/execute.go @@ -2,13 +2,17 @@ package apply import ( "fmt" + "github.com/Graylog2/graylog-project-cli/git" "github.com/Graylog2/graylog-project-cli/logger" "github.com/Graylog2/graylog-project-cli/pom" "github.com/Graylog2/graylog-project-cli/pomparse" "github.com/Graylog2/graylog-project-cli/project" + "github.com/Graylog2/graylog-project-cli/utils" "github.com/fatih/color" "os" e "os/exec" + "path/filepath" + "regexp" "strings" ) @@ -54,3 +58,67 @@ func (execute executeApplier) MavenExec(commands []string) { logger.Fatal("Command failed: %v", err) } } + +func (execute executeApplier) NpmVersionSet(module project.Module, version string) { + versionRe := regexp.MustCompile(`^\d+\.\d+\.\d+-?.*?$`) + filename := "package.json" + + if !versionRe.MatchString(version) { + logger.Fatal("Invalid version: %s", version) + } + + if module.Server { + // The server module needs special treatment + // TODO: Move list of package.json files to the manifest and default to "package.json" + files := []string{ + filepath.Join("graylog2-web-interface", filename), + filepath.Join("graylog2-web-interface/manifests", filename), + filepath.Join("graylog2-web-interface/packages/graylog-web-plugin", filename), + } + for _, file := range files { + fmt.Println("set version in " + filepath.Join(module.Path, file) + ": " + version) + + err := utils.SetPackageJsonVersion(file, version) + if err != nil { + logger.Fatal("Couldn't set version in file %s: %s", filepath.Join(module.Path, file), err) + } + } + return + } + + if !module.IsNpmModule() { + return + } + + absFilename := filepath.Join(module.Path, filename) + + fmt.Println("set version in " + absFilename + ": " + version) + + err := utils.SetPackageJsonVersion(filename, version) + if err != nil { + logger.Fatal("Couldn't set version in file %s: %s", absFilename, err) + } +} + +func (execute executeApplier) NpmVersionCommit(module project.Module, version string) { + commitMsg := fmt.Sprintf("Bump package.json version to %s", version) + + utils.InDirectory(module.Path, func() { + if module.Server { + // The server module needs special treatment + // TODO: Move list of package.json files to the manifest and default to "package.json" + file1 := "graylog2-web-interface/package.json" + file2 := "graylog2-web-interface/manifests/package.json" + file3 := "graylog2-web-interface/packages/graylog-web-plugin/package.json" + + git.Git("commit", "-m", commitMsg, file1, file2, file3) + return + } + + if !module.IsNpmModule() { + return + } + + git.Git("commit", "-m", commitMsg, "package.json") + }) +} diff --git a/apply/noop.go b/apply/noop.go index 0bf120b..70876bd 100644 --- a/apply/noop.go +++ b/apply/noop.go @@ -29,3 +29,11 @@ func (noop noopApplier) MavenSetProperty(module project.Module, name string, val func (noop noopApplier) MavenExec(commands []string) { fmt.Println(strings.Join(commands, " ")) } + +func (noop noopApplier) NpmVersionSet(module project.Module, newVersion string) { + fmt.Println("set web module version: " + newVersion) +} + +func (noop noopApplier) NpmVersionCommit(module project.Module, newVersion string) { + fmt.Println("commit web module version: " + newVersion) +} diff --git a/cmd/apply_manifest.go b/cmd/apply_manifest.go index bcdf00c..51fe6e4 100644 --- a/cmd/apply_manifest.go +++ b/cmd/apply_manifest.go @@ -114,6 +114,14 @@ func applyManifestCommand(cmd *cobra.Command, args []string) { }) }) + // Set release version in all web modules + msg("Setting release version in all web modules") + apply.ForEachModule(proj, true, func(module project.Module) { + applyManifestInDirectory(module.Path, func() { + applier.NpmVersionSet(module, module.Revision) + }) + }) + // Set release version in all modules msg("Setting release version in all modules") apply.ForEachModule(proj, false, func(module project.Module) { @@ -134,6 +142,15 @@ func applyManifestCommand(cmd *cobra.Command, args []string) { logger.ColorInfo(color.FgMagenta, "[%s]", utils.GetCwd()) applier.MavenRun("clean", "package") + // Committing new version in web modules + // Run this before the maven scm checkin is pushing to GitHub + msg("Committing new version in web modules") + apply.ForEachModule(proj, true, func(module project.Module) { + applyManifestInDirectory(module.Path, func() { + applier.NpmVersionCommit(module, module.Revision) + }) + }) + // Commit and push new versions and create and push tag msg("Committing and pushing new versions and tags") apply.ForEachModule(proj, false, func(module project.Module) { @@ -148,6 +165,14 @@ func applyManifestCommand(cmd *cobra.Command, args []string) { logger.ColorInfo(color.FgMagenta, "[%s]", utils.GetCwd()) applier.MavenRunWithProfiles([]string{"release"}, "-DskipTests", "clean", "deploy") + // Set development version in all web modules + msg("Setting development version in all web modules") + apply.ForEachModule(proj, true, func(module project.Module) { + applyManifestInDirectory(module.Path, func() { + applier.NpmVersionSet(module, module.ApplyNewVersion()) + }) + }) + // Set development version msg("Set development versions") apply.ForEachModule(proj, false, func(module project.Module) { @@ -159,6 +184,15 @@ func applyManifestCommand(cmd *cobra.Command, args []string) { applyManifestUpdateVersions(msg, proj, applier) }) + // Committing new development version in web modules + // Run this before the maven scm checkin is pushing to GitHub + msg("Committing new development version in web modules") + apply.ForEachModule(proj, true, func(module project.Module) { + applyManifestInDirectory(module.Path, func() { + applier.NpmVersionCommit(module, module.Revision) + }) + }) + // Commit development version msg("Commit development versions") apply.ForEachModule(proj, false, func(module project.Module) { diff --git a/cmd/npm_version.go b/cmd/npm_version.go new file mode 100644 index 0000000..a6fea5c --- /dev/null +++ b/cmd/npm_version.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "github.com/Graylog2/graylog-project-cli/apply" + "github.com/Graylog2/graylog-project-cli/config" + "github.com/Graylog2/graylog-project-cli/logger" + "github.com/Graylog2/graylog-project-cli/manifest" + p "github.com/Graylog2/graylog-project-cli/project" + "github.com/Graylog2/graylog-project-cli/utils" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "os" +) + +var npmVersionCmd = &cobra.Command{ + Use: "npm-version", + Aliases: []string{"nv"}, + Short: "Set package.json version", + Long: ` +Sets version in all package.json files. +`, + Run: npmVersionCommand, +} + +func init() { + RootCmd.AddCommand(npmVersionCmd) + + npmVersionCmd.Flags().StringP("set", "s", "", "New version") + npmVersionCmd.Flags().BoolP("commit", "c", false, "If new version should be committed with Git") + + viper.BindPFlag("npm-version.set", npmVersionCmd.Flags().Lookup("set")) + viper.BindPFlag("npm-version.commit", npmVersionCmd.Flags().Lookup("commit")) +} + +func npmVersionCommand(cmd *cobra.Command, args []string) { + version := viper.GetString("npm-version.set") + shouldCommit := viper.GetBool("npm-version.commit") + + if version == "" { + logger.Fatal("Missing --set argument") + cmd.UsageFunc()(cmd) + os.Exit(1) + } + + manifestFiles := manifest.ReadState().Files() + project := p.New(config.Get(), manifestFiles) + applier := apply.NewExecuteApplier([]string{}) + + apply.ForEachModule(project, true, func(module p.Module) { + utils.InDirectory(module.Path, func() { + applier.NpmVersionSet(module, version) + if shouldCommit { + applier.NpmVersionCommit(module, version) + } + }) + }) +} diff --git a/utils/utils.go b/utils/utils.go index d7b516d..79ac037 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,10 +3,12 @@ package utils import ( "bytes" "errors" + "fmt" "github.com/Graylog2/graylog-project-cli/logger" "io/ioutil" "os" "path/filepath" + "regexp" "strings" ) @@ -130,3 +132,66 @@ func FilesIdentical(file1, file2 string) bool { return bytes.Equal(buf1, buf2) } + +func SetPackageJsonVersion(filename, version string) error { + // Make sure to match the correct version string in package.json + re := regexp.MustCompile(`^\s{2}"version": "\d+\.\d+\.\d+-?.*?",`) + + err := ReplaceInFile(filename, re, fmt.Sprintf(` "version": "%s",`, version)) + if err != nil { + return err + } + + return nil +} + +func ReplaceInFile(filename string, re *regexp.Regexp, replacement string) error { + absFilename, err := filepath.Abs(filename) + if err != nil { + return err + } + + fileInfo, err := os.Stat(absFilename) + if err != nil { + return err + } + + buf, err := ioutil.ReadFile(absFilename) + if err != nil { + return err + } + + lines := make([]string, 0) + + for _, line := range strings.Split(string(buf), "\n") { + if re.MatchString(line) { + lines = append(lines, re.ReplaceAllString(line, replacement)) + } else { + lines = append(lines, line) + } + } + + f, err := ioutil.TempFile(filepath.Dir(absFilename), fileInfo.Name()) + if err != nil { + return err + } + + _, err = f.Write([]byte(strings.Join(lines, "\n"))) + if err != nil { + return nil + } + + if err := f.Close(); err != nil { + return err + } + + if err := os.Rename(f.Name(), absFilename); err != nil { + return err + } + + if err := os.Chmod(absFilename, fileInfo.Mode()); err != nil { + return err + } + + return nil +}