Skip to content

Commit 2faaa97

Browse files
committed
feat(dist): auto-update version numbers in more files
Teach the version auto-update function to ignore lines not matching a regexp. Use this feature to change only the main "version" value in package.json files, only the "PackageVersion" value in winget YAML files, etc.
1 parent ff9668a commit 2faaa97

File tree

2 files changed

+116
-51
lines changed

2 files changed

+116
-51
lines changed

dist/release.go

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,28 @@ var Steps []Step = []Step{
5858
Step{
5959
Title: "Update version number in files",
6060
Run: func() {
61-
UpdateReleaseVersionsInFiles([]string{
62-
"Formula/quick-lint-js.rb",
63-
"dist/arch/PKGBUILD-dev",
64-
"dist/arch/PKGBUILD-git",
65-
"dist/arch/PKGBUILD-release",
66-
"dist/chocolatey/quick-lint-js.nuspec",
67-
"dist/chocolatey/tools/VERIFICATION.txt",
68-
"dist/debian/README.md",
69-
"dist/npm/BUILDING.md",
70-
"dist/npm/package.json",
71-
"dist/scoop/quick-lint-js.template.json",
72-
"dist/sign-release.go",
73-
"plugin/vscode-lsp/README.md",
74-
"plugin/vscode/BUILDING.md",
61+
UpdateReleaseVersionsInFiles(map[string]string{
62+
"Formula/quick-lint-js.rb": "",
63+
"dist/arch/PKGBUILD-dev": "",
64+
"dist/arch/PKGBUILD-git": "",
65+
"dist/arch/PKGBUILD-release": "",
66+
"dist/chocolatey/quick-lint-js.nuspec": "",
67+
"dist/chocolatey/tools/VERIFICATION.txt": "",
68+
"dist/debian/README.md": "",
69+
"dist/npm/BUILDING.md": "",
70+
"dist/npm/package.json": "",
71+
"dist/scoop/quick-lint-js.template.json": "",
72+
"dist/sign-release.go": "",
73+
"plugin/vscode-lsp/README.md": "",
74+
"plugin/vscode/BUILDING.md": "",
75+
76+
"dist/msix/AppxManifest.xml": "\\bVersion=",
77+
"dist/winget/quick-lint.quick-lint-js.installer.template.yaml": "PackageVersion:",
78+
"dist/winget/quick-lint.quick-lint-js.locale.en-US.template.yaml": "PackageVersion:",
79+
"dist/winget/quick-lint.quick-lint-js.template.yaml": "PackageVersion:",
80+
"plugin/vim/quick-lint-js.vim/doc/quick-lint-js.txt": "This plugin version is designed for quick-lint-js version",
81+
"plugin/vscode-lsp/package.json": "\"version\":",
82+
"plugin/vscode/package.json": "\"version\":",
7583
})
7684
},
7785
},
@@ -105,21 +113,6 @@ var Steps []Step = []Step{
105113
},
106114
},
107115

108-
Step{
109-
Title: "Manually update version number and release date",
110-
Run: func() {
111-
fmt.Printf("Change these files containing version numbers:\n")
112-
fmt.Printf("* dist/msix/AppxManifest.xml\n")
113-
fmt.Printf("* dist/winget/quick-lint.quick-lint-js.installer.template.yaml\n")
114-
fmt.Printf("* dist/winget/quick-lint.quick-lint-js.locale.en-US.template.yaml\n")
115-
fmt.Printf("* dist/winget/quick-lint.quick-lint-js.template.yaml\n")
116-
fmt.Printf("* plugin/vim/quick-lint-js.vim/doc/quick-lint-js.txt\n")
117-
fmt.Printf("* plugin/vscode-lsp/package.json\n")
118-
fmt.Printf("* plugin/vscode/package.json\n")
119-
WaitForDone()
120-
},
121-
},
122-
123116
Step{
124117
Title: "Re-generate man pages",
125118
Run: func() {
@@ -507,10 +500,12 @@ func Stop() {
507500
os.Exit(0)
508501
}
509502

510-
func UpdateReleaseVersionsInFiles(paths []string) {
503+
// Key: file path
504+
// Value: UpdateReleaseVersionsOptions.LineMatchRegexp
505+
func UpdateReleaseVersionsInFiles(pathToLineMatchRegexp map[string]string) {
511506
fileContents := make(map[string][]byte)
512507

513-
for _, path := range paths {
508+
for path, _ := range pathToLineMatchRegexp {
514509
data, err := os.ReadFile(path)
515510
if err != nil {
516511
Stopf("failed to read file: %v", err)
@@ -519,7 +514,17 @@ func UpdateReleaseVersionsInFiles(paths []string) {
519514
}
520515

521516
for path, data := range fileContents {
522-
fileContents[path] = UpdateReleaseVersions(data, path)
517+
var err error
518+
fileContents[path], err = UpdateReleaseVersions(UpdateReleaseVersionsOptions{
519+
FileContent: data,
520+
PathForDebugging: path,
521+
OldReleaseVersion: OldReleaseVersion,
522+
NewReleaseVersion: ReleaseVersion,
523+
LineMatchRegexp: pathToLineMatchRegexp[path],
524+
})
525+
if err != nil {
526+
Stopf("failed to update version numbers in %s: %v", path, err)
527+
}
523528
}
524529

525530
for path, data := range fileContents {
@@ -530,30 +535,39 @@ func UpdateReleaseVersionsInFiles(paths []string) {
530535
}
531536
}
532537

533-
func UpdateReleaseVersions(fileContent []byte, pathForDebugging string) []byte {
534-
oldVersion := []byte(OldReleaseVersion)
535-
newVersion := []byte(ReleaseVersion)
538+
type UpdateReleaseVersionsOptions struct {
539+
FileContent []byte
540+
PathForDebugging string
541+
OldReleaseVersion string
542+
NewReleaseVersion string
543+
LineMatchRegexp string
544+
}
545+
546+
func UpdateReleaseVersions(options UpdateReleaseVersionsOptions) ([]byte, error) {
547+
oldVersion := []byte(options.OldReleaseVersion)
548+
newVersion := []byte(options.NewReleaseVersion)
536549
foundOldVersion := false
537-
foundUnexpectedVersion := false
538-
fileContent = ThreePartVersionRegexp.ReplaceAllFunc(fileContent, func(match []byte) []byte {
539-
if bytes.Equal(match, oldVersion) {
540-
foundOldVersion = true
541-
return newVersion
542-
} else {
543-
foundUnexpectedVersion = true
544-
log.Printf("error: found unexpected version number in %s: %s\n", pathForDebugging, string(match))
545-
return match
546-
}
550+
var foundUnexpectedVersion error = nil
551+
fullLineMatchRegexp := regexp.MustCompile("(?m:^.*" + options.LineMatchRegexp + ".*$)")
552+
newFileContent := fullLineMatchRegexp.ReplaceAllFunc(options.FileContent, func(lineMatch []byte) []byte {
553+
return ThreePartVersionRegexp.ReplaceAllFunc(lineMatch, func(versionMatch []byte) []byte {
554+
if bytes.Equal(versionMatch, oldVersion) {
555+
foundOldVersion = true
556+
return newVersion
557+
} else {
558+
foundUnexpectedVersion = fmt.Errorf("found unexpected version number in %s: %s", options.PathForDebugging, string(versionMatch))
559+
return versionMatch
560+
}
561+
})
547562
})
548-
if !foundOldVersion {
549-
log.Printf("error: failed to find old version number %s in %s\n", OldReleaseVersion, pathForDebugging)
550-
os.Exit(1)
563+
if foundUnexpectedVersion != nil {
564+
return nil, foundUnexpectedVersion
551565
}
552-
if foundUnexpectedVersion {
553-
os.Exit(1)
566+
if !foundOldVersion {
567+
return nil, fmt.Errorf("failed to find old version number %s in %s", options.OldReleaseVersion, options.PathForDebugging)
554568
}
555569

556-
return fileContent
570+
return newFileContent, nil
557571
}
558572

559573
func GetCurrentCommitHash() string {

dist/release_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,57 @@ func TestUpdateDebianChangelog(t *testing.T) {
102102
})
103103
}
104104

105+
func TestUpdateReleaseVersions(t *testing.T) {
106+
t.Run("only version number", func(t *testing.T) {
107+
updated, err := UpdateReleaseVersions(UpdateReleaseVersionsOptions{
108+
FileContent: []byte("hello, this is version 0.2.0 (beta)\n"),
109+
PathForDebugging: "file.txt",
110+
OldReleaseVersion: "0.2.0",
111+
NewReleaseVersion: "0.3.0",
112+
})
113+
Check(t, err)
114+
AssertStringsEqual(t, string(updated), "hello, this is version 0.3.0 (beta)\n")
115+
})
116+
117+
t.Run("missing version number", func(t *testing.T) {
118+
_, err := UpdateReleaseVersions(UpdateReleaseVersionsOptions{
119+
FileContent: []byte("hello, world\n"),
120+
PathForDebugging: "file.txt",
121+
OldReleaseVersion: "0.2.0",
122+
NewReleaseVersion: "0.3.0",
123+
})
124+
if err == nil {
125+
t.Fatalf("expected error")
126+
}
127+
AssertStringsEqual(t, err.Error(), "failed to find old version number 0.2.0 in file.txt")
128+
})
129+
130+
t.Run("wrong version number", func(t *testing.T) {
131+
_, err := UpdateReleaseVersions(UpdateReleaseVersionsOptions{
132+
FileContent: []byte("this feature was introduced in version 0.1.0\n"),
133+
PathForDebugging: "file.txt",
134+
OldReleaseVersion: "0.2.0",
135+
NewReleaseVersion: "0.3.0",
136+
})
137+
if err == nil {
138+
t.Fatalf("expected error")
139+
}
140+
AssertStringsEqual(t, err.Error(), "found unexpected version number in file.txt: 0.1.0")
141+
})
142+
143+
t.Run("ignores versions not matching line regexp", func(t *testing.T) {
144+
updated, err := UpdateReleaseVersions(UpdateReleaseVersionsOptions{
145+
FileContent: []byte("0.1.0\nversion 0.2.0\n0.2.0\n0.2.0 version\n0.3.0\n"),
146+
PathForDebugging: "file.txt",
147+
OldReleaseVersion: "0.2.0",
148+
NewReleaseVersion: "0.3.0",
149+
LineMatchRegexp: "version",
150+
})
151+
Check(t, err)
152+
AssertStringsEqual(t, string(updated), "0.1.0\nversion 0.3.0\n0.2.0\n0.3.0 version\n0.3.0\n")
153+
})
154+
}
155+
105156
// quick-lint-js finds bugs in JavaScript programs.
106157
// Copyright (C) 2020 Matthew "strager" Glazar
107158
//

0 commit comments

Comments
 (0)