Update package.json files during release and add npm-version command

Closes #2
This commit is contained in:
Bernd Ahlers
2018-01-20 01:56:51 +01:00
parent a5071ca360
commit 97dab6aa97
6 changed files with 236 additions and 0 deletions

View File

@@ -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.

View File

@@ -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")
})
}

View File

@@ -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)
}

View File

@@ -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) {

57
cmd/npm_version.go Normal file
View File

@@ -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)
}
})
})
}

View File

@@ -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
}