Skip to content

Commit 65ee815

Browse files
feat(release): add automatic git tagging
Closes whyrusleeping#157
1 parent 855ebcf commit 65ee815

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ republish it, it would produce the *exact* same hash.
7373

7474
### package.json
7575

76-
It should be noted that gx is meant to *work with* existing `package.json` files. If you are adding a package to gx that already has a `package.json` file in its root, gx will try and work with it. Any shared fields will have the same types, and any fields unique to gx will kept separate.
76+
It should be noted that gx is meant to *work with* existing `package.json` files. If you are adding a package to gx that already has a `package.json` file in its root, gx will try and work with it. Any shared fields will have the same types, and any fields unique to gx will kept separate.
7777

7878
E.g. A single `package.json` file could be used to serve both gx and another packaging tool, such as npm. Since gx is **Alpha Quality** there may be some exceptions to the above statements, if you notice one, please file an issue.
7979

@@ -300,6 +300,9 @@ in your `package.json` as your `releaseCmd`. To get the above git commit flow,
300300
you can set it to: `git commit -a -m \"gx publish $VERSION\"` and gx will
301301
replace `$VERSION` with the newly changed version before executing the git
302302
commit.
303+
In addition to commiting, it will also publish a git tag, using the default command
304+
`git tag -a "v$VERSION" -m "gx release published as $HASH"`. To change the command you can set
305+
the `tagCmd` field to a command of your choosing.
303306

304307
### Ignoring files from a publish
305308
You can use a `.gxignore` file to make gx ignore certain files during a publish.

gxutil/pkgfile.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type PackageBase struct {
2121
Build string `json:"build,omitempty"`
2222
Test string `json:"test,omitempty"`
2323
ReleaseCmd string `json:"releaseCmd,omitempty"`
24+
TagCmd string `json:"tagCmd,omitempty"`
2425
SubtoolRequired bool `json:"subtoolRequired,omitempty"`
2526
Language string `json:"language,omitempty"`
2627
License string `json:"license"`

gxutil/pm.go

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const GxVersion = "0.12.1"
2323

2424
const PkgFileName = "package.json"
2525

26+
const DefaultTagCmd = "git tag -a \"v$VERSION\" -m \"gx release published as $HASH\""
27+
2628
var installPathsCache map[string]string
2729
var binarySuffix string
2830

@@ -208,6 +210,7 @@ func (pm *PM) InitPkg(dir, name, lang string, setup func(*Package)) error {
208210
Version: "0.0.0",
209211
GxVersion: GxVersion,
210212
ReleaseCmd: "git commit -a -m \"gx publish $VERSION\"",
213+
TagCmd: DefaultTagCmd,
211214
},
212215
}
213216

main.go

+46-15
Original file line numberDiff line numberDiff line change
@@ -173,34 +173,41 @@ number. This is a soft requirement and can be skipped by specifying the
173173
}
174174
}
175175

176-
return doPublish(pkg)
176+
if _, err := doPublish(pkg); err != nil {
177+
return err
178+
}
179+
180+
return nil
177181
},
178182
}
179183

180-
func doPublish(pkg *gx.Package) error {
184+
func doPublish(pkg *gx.Package) (string, error) {
181185
if !pm.ShellOnline() {
182-
return fmt.Errorf("ipfs daemon isn't running")
186+
return "", fmt.Errorf("ipfs daemon isn't running")
183187
}
184188

185189
err := gx.TryRunHook("pre-publish", pkg.Language, pkg.SubtoolRequired)
186190
if err != nil {
187-
return err
191+
return "", err
188192
}
189193

190194
hash, err := pm.PublishPackage(cwd, &pkg.PackageBase)
191195
if err != nil {
192-
return fmt.Errorf("publishing: %s", err)
196+
return "", fmt.Errorf("publishing: %s", err)
193197
}
194198
log.Log("package %s published with hash: %s", pkg.Name, hash)
195199

196200
// write out version hash
197201
err = writeLastPub(pkg.Version, hash)
198202
if err != nil {
199-
return err
203+
return "", err
204+
}
205+
206+
if err := gx.TryRunHook("post-publish", pkg.Language, pkg.SubtoolRequired, hash); err != nil {
207+
return "", err
200208
}
201209

202-
err = gx.TryRunHook("post-publish", pkg.Language, pkg.SubtoolRequired, hash)
203-
return err
210+
return hash, nil
204211
}
205212

206213
func writeLastPub(vers string, hash string) error {
@@ -1235,12 +1242,12 @@ var ReleaseCommand = cli.Command{
12351242
}
12361243

12371244
fmt.Printf("publishing package...\r")
1238-
err = doPublish(pkg)
1245+
hash, err := doPublish(pkg)
12391246
if err != nil {
12401247
return err
12411248
}
12421249

1243-
return runRelease(pkg)
1250+
return runRelease(pkg, hash)
12441251
},
12451252
}
12461253

@@ -1313,20 +1320,44 @@ func splitArgs(in string) []string {
13131320
return out
13141321
}
13151322

1316-
func escapeReleaseCmd(pkg *gx.Package, cmd string) string {
1317-
cmd = strings.Replace(cmd, "$VERSION", pkg.Version, -1)
1323+
func escapeReleaseCmd(cmd, version, hash string) string {
1324+
cmd = strings.Replace(cmd, "$VERSION", version, -1)
1325+
cmd = strings.Replace(cmd, "$HASH", hash, -1)
13181326

13191327
return cmd
13201328
}
13211329

1322-
func runRelease(pkg *gx.Package) error {
1330+
func runRelease(pkg *gx.Package, hash string) error {
13231331
if pkg.ReleaseCmd == "" {
1332+
fmt.Println("no release command defined, skipping")
13241333
return nil
13251334
}
13261335

1327-
replaced := escapeReleaseCmd(pkg, pkg.ReleaseCmd)
1336+
commitCmd := escapeReleaseCmd(pkg.ReleaseCmd, pkg.Version, hash)
1337+
1338+
if err := runCmd(commitCmd); err != nil {
1339+
return fmt.Errorf("failed to commit release: %s", err)
1340+
}
1341+
1342+
tagCmd := pkg.TagCmd
1343+
if tagCmd == "" {
1344+
// TODO: maybe don't do this? but otherwise we run into the issue that all existing
1345+
// gx configs don't have this defined, and would not start using tags :(
1346+
fmt.Println("no tag command defined, using default")
1347+
tagCmd = gx.DefaultTagCmd
1348+
}
1349+
1350+
tagCmd = escapeReleaseCmd(tagCmd, pkg.Version, hash)
1351+
if err := runCmd(tagCmd); err != nil {
1352+
return fmt.Errorf("failed to tag release: %s", err)
1353+
}
1354+
1355+
return nil
1356+
}
1357+
1358+
func runCmd(command string) error {
1359+
parts := splitArgs(command)
13281360

1329-
parts := splitArgs(replaced)
13301361
cmd := exec.Command(parts[0], parts[1:]...)
13311362
cmd.Stderr = os.Stderr
13321363
cmd.Stdout = os.Stdout

0 commit comments

Comments
 (0)