Skip to content

Commit

Permalink
feat: publish command
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Dec 31, 2024
1 parent 62cd831 commit 24baed4
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 118 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log (go-package-manager)

## 0.25.0

- feat: `publish` command
- fix: `push` command

## 0.24.0

- feat: `update` command which updates all dependencies of current project
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- [Open alias](#open-alias-)
- [Open project](#open-project-)
- [Pack project](#pack-project-)
- [Publish new version](#publish-new-version-)
- [Pull from Git remotes](#pull-from-git-remotes-)
- [Push to Git remotes](#push-to-git-remotes-)
- [Remove alias](#remove-alias-)
Expand Down Expand Up @@ -424,6 +425,18 @@ will open this URL usually in the browser.

![AI Chat Demo 1](./img/demos/pack-demo-1.gif)

#### Publish new version [<a href="#commands-">↑</a>]

Running

```bash
gpm publish
```

will bump the feature part of the project's current version represented by Git tags.

After this, the code will be pushed to all remotes, including tags as well.

#### Pull from Git remotes [<a href="#commands-">↑</a>]

The execution of
Expand Down
83 changes: 14 additions & 69 deletions commands/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ package commands

import (
"fmt"
"strings"

"github.com/hashicorp/go-version"
"github.com/mkloubert/go-package-manager/types"
"github.com/mkloubert/go-package-manager/utils"
"github.com/spf13/cobra"
Expand All @@ -48,78 +46,25 @@ func init_bump_version_command(parentCmd *cobra.Command, app *types.AppContext)
Short: "Bump version",
Long: `Bumps a version number.`,
Run: func(cmd *cobra.Command, args []string) {
latestVersion, err := app.GetLatestVersion()
utils.CheckForError(err)

if latestVersion == nil {
latestVersion, _ = version.NewVersion("0.0.0")
}

segments := latestVersion.Segments64()
currentMajor := segments[0]
currentMinor := segments[1]
currentPatch := segments[2]

newMajor := currentMajor
if major > -1 {
newMajor = major
}
newMinor := currentMinor
if minor > -1 {
newMinor = minor
}
newPatch := currentPatch
if patch > -1 {
newPatch = patch
pvm := app.NewVersionManager()

bumpOptions := types.BumpProjectVersionOptions{
Breaking: &breaking,
Feature: &feature,
Fix: &fix,
Force: &force,
Major: &major,
Message: &message,
Minor: &minor,
Patch: &patch,
}

if !breaking && !feature && !fix {
// default: 1.2.3 => 1.3.0

newMinor++
newPatch = 0
} else {
if breaking {
newMajor++ // by default e.g.: 1.2.3 => 2.0.0
if !feature {
newMinor = 0
}
if !fix {
newPatch = 0
}
}
if feature {
newMinor++ // by default e.g.: 1.2.3 => 1.3.0
if !fix {
newPatch = 0
}
}
if fix {
newPatch++ // e.g. 1.2.3 => 1.2.4
}
}

nextVersion, err := version.NewVersion(
fmt.Sprintf(
"%v.%v.%v",
newMajor, newMinor, newPatch,
),
)
newVersion, err := pvm.Bump(bumpOptions)
utils.CheckForError(err)

if !force && nextVersion.LessThanOrEqual(latestVersion) {
utils.CloseWithError(fmt.Errorf("new version is not greater than latest one"))
if newVersion != nil {
fmt.Printf("v%s%s", newVersion.String(), fmt.Sprintln())
}

gitMessage := strings.TrimSpace(message)
if gitMessage == "" {
gitMessage = fmt.Sprintf("version %v", nextVersion.String())
}

tagName := fmt.Sprintf("v%v", nextVersion.String())
fmt.Println(tagName)

app.RunShellCommandByArgs("git", "tag", "-a", tagName, "-m", gitMessage)
},
}

Expand Down
4 changes: 3 additions & 1 deletion commands/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func Init_Pack_Command(parentCmd *cobra.Command, app *types.AppContext) {
Short: "Pack project",
Long: `Packs and zips project files`,
Run: func(cmd *cobra.Command, args []string) {
pvm := app.NewVersionManager()

if !noPreScript {
_, ok := app.GpmFile.Scripts[constants.PrePackScriptName]
if ok {
Expand All @@ -76,7 +78,7 @@ func Init_Pack_Command(parentCmd *cobra.Command, app *types.AppContext) {
var latestVersion *ver.Version
var err error
if customVersion == "" {
latestVersion, err = app.GetLatestVersion()
latestVersion, err = pvm.GetLatestVersion()
utils.CheckForError(err)
} else {
latestVersion, err = ver.NewVersion(customVersion)
Expand Down
126 changes: 126 additions & 0 deletions commands/publish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// MIT License
//
// Copyright (c) 2024 Marcel Joachim Kloubert (https://marcel.coffee)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package commands

import (
"fmt"

"github.com/mkloubert/go-package-manager/types"
"github.com/mkloubert/go-package-manager/utils"
"github.com/spf13/cobra"
)

func Init_Publish_Command(parentCmd *cobra.Command, app *types.AppContext) {
var breaking bool
var defaultRemoteOnly bool
var feature bool
var fix bool
var force bool
var major int64
var minor int64
var message string
var noBump bool
var patch int64

var publishCmd = &cobra.Command{
Use: "publish [remotes]",
Aliases: []string{"pub"},
Short: "Publish version",
Long: `Bumps the version of the current project and pushes it to all remote repositories.`,
Run: func(cmd *cobra.Command, args []string) {
currentBranchName, _ := app.GetCurrentGitBranch()

if !noBump {
pvm := app.NewVersionManager()

bumpOptions := types.BumpProjectVersionOptions{
Breaking: &breaking,
Feature: &feature,
Fix: &fix,
Force: &force,
Major: &major,
Message: &message,
Minor: &minor,
Patch: &patch,
}

newVersion, err := pvm.Bump(bumpOptions)
utils.CheckForError(err)

if newVersion != nil {
fmt.Printf("v%s%s", newVersion.String(), fmt.Sprintln())
}
}

var remotes []string
if len(args) == 0 {
listOfRemotes, err := app.GetGitRemotes()
utils.CheckForError(err)

remotes = append(remotes, listOfRemotes...)
} else {
remotes = append(remotes, args...)
}

if len(remotes) == 0 {
utils.CloseWithError(fmt.Errorf("no remotes found"))
}

if defaultRemoteOnly {
// default only
remotes = []string{remotes[0]}
}

for _, r := range remotes {
// first push code
{
cmdArgs := []string{"git", "push", r, currentBranchName}

app.RunShellCommandByArgs(cmdArgs[0], cmdArgs[1:]...)
}

// then push tags
{
cmdArgs := []string{"git", "push", r, "--tags"}

app.RunShellCommandByArgs(cmdArgs[0], cmdArgs[1:]...)
}
}
},
}

publishCmd.Flags().BoolVarP(&breaking, "breaking", "", false, "increase major part by 1")
publishCmd.Flags().BoolVarP(&defaultRemoteOnly, "default", "d", false, "default / first remote only")
publishCmd.Flags().BoolVarP(&feature, "feature", "", false, "increase minor part by 1")
publishCmd.Flags().BoolVarP(&fix, "fix", "", false, "increase patch part by 1")
publishCmd.Flags().BoolVarP(&force, "force", "", false, "ignore value of previous version")
publishCmd.Flags().Int64VarP(&major, "major", "", -1, "set major part")
publishCmd.Flags().StringVarP(&message, "message", "", "", "custom git message")
publishCmd.Flags().Int64VarP(&minor, "minor", "", -1, "set minor part")
publishCmd.Flags().BoolVarP(&noBump, "no-bump", "", false, "do not bump version")
publishCmd.Flags().Int64VarP(&patch, "patch", "", -1, "set patch part")

parentCmd.AddCommand(
publishCmd,
)
}
2 changes: 1 addition & 1 deletion commands/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Init_Push_Command(parentCmd *cobra.Command, app *types.AppContext) {
}

// then push tags
{
if withTags {
cmdArgs := []string{"git", "push", r, "--tags"}

app.RunShellCommandByArgs(cmdArgs[0], cmdArgs[1:]...)
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func main() {
commands.Init_Open_Command(rootCmd, &app)
commands.Init_Pack_Command(rootCmd, &app)
commands.Init_Prompt_Command(rootCmd, &app)
commands.Init_Publish_Command(rootCmd, &app)
commands.Init_Pull_Command(rootCmd, &app)
commands.Init_Push_Command(rootCmd, &app)
commands.Init_Remove_Command(rootCmd, &app)
Expand Down
57 changes: 10 additions & 47 deletions types/app_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"strings"

"github.com/goccy/go-yaml"
"github.com/hashicorp/go-version"
"github.com/joho/godotenv"
"github.com/mkloubert/go-package-manager/utils"

Expand Down Expand Up @@ -675,32 +674,6 @@ func (app *AppContext) GetGpmFilePath() (string, error) {
return path.Join(app.Cwd, "gpm.yaml"), nil
}

// app.GetLatestVersion() - Returns the latest version based on the Git tags
// of the current repository or nil if not found.
func (app *AppContext) GetLatestVersion() (*version.Version, error) {
allVersions, err := app.GetVersions()
if err != nil {
return nil, err
}

var latestVersion *version.Version
for _, v := range allVersions {
updateVersion := func() {
latestVersion = v
}

if latestVersion != nil {
if latestVersion.LessThanOrEqual(v) {
updateVersion()
}
} else {
updateVersion()
}
}

return latestVersion, nil
}

// app.GetModuleUrls() - returns the list of module urls based on the
// information from gpm.y(a)ml file
func (app *AppContext) GetModuleUrls(moduleNameOrUrl string) []string {
Expand Down Expand Up @@ -763,26 +736,6 @@ func (app *AppContext) GetSystemAIPrompt(defaultPrompt string) string {
return prompt
}

// app.GetVersions() - Returns all versions represented by Git tags
// inside the current working directory.
func (app *AppContext) GetVersions() ([]*version.Version, error) {
var versions []*version.Version

tags, err := app.GetGitTags()
if err != nil {
return versions, err
}

for _, t := range tags {
v, err := version.NewVersion(t)
if err == nil {
versions = append(versions, v)
}
}

return versions, nil
}

// app.ListFiles() - Lists all files inside the current working directory
// based of the patterns from "files" section of gpm.yaml file.
func (app *AppContext) ListFiles() ([]string, error) {
Expand Down Expand Up @@ -962,6 +915,16 @@ func (app *AppContext) LoadProjectsFileIfExist() bool {
return true
}

// app.NewVersionManager() - creates a new `ProjectVersionManager` instance based on
// this application context
func (app *AppContext) NewVersionManager() *ProjectVersionManager {
pvm := &ProjectVersionManager{
app: app,
}

return pvm
}

// app.RunCurrentProject() - runs the current go project
func (app *AppContext) RunCurrentProject(additionalArgs ...string) {
p := utils.CreateShellCommandByArgs("go", "run", ".")
Expand Down
Loading

0 comments on commit 24baed4

Please sign in to comment.