Skip to content

Commit

Permalink
no-dependency option (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jney authored Feb 5, 2021
1 parent 47414cd commit 5aa9ee0
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 102 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.41
0.0.42
42 changes: 22 additions & 20 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,38 @@ import (
)

var buildCmd = &cobra.Command{
Use: "build",
Short: "build your script for updated files",
Use: "build",
Aliases: []string{"b"},
Short: "build your script for updated files",
Run: func(cmd *cobra.Command, args []string) {
handleVerboseFlag()
var p *string = nil
if projectName != "" {
p = &projectName
}
if tag == "" {
jww.INFO.Printf("building no tag projects")
if err := project.BuildNoTag(p, rootDir); err != nil {
jww.ERROR.Fatalf("could not build no-tag projects: %v", err)
var p *string = nil
if projectName != "" {
p = &projectName
}
if tag != "" {
if err := project.BuildTag(p, tag, rootDir); err != nil {
jww.ERROR.Fatalf("could not build properly project: %v", err)
}
if force {
jww.INFO.Printf("building projects with tag in force mode")
if err := project.BuildForceWithTag(p, rootDir); err != nil {
jww.ERROR.Fatalf("could not build tag projects: %v", err)
}
}
return
}
if err := project.BuildTag(p, tag, rootDir); err != nil {
jww.ERROR.Fatalf("could not build properly project: %v", err)
jww.INFO.Println("building no tag projects")
if err := project.BuildWithoutTag(p, rootDir, noDependency); err != nil {
jww.ERROR.Fatalf("could not build no-tag projects: %v", err)
}
if force {
jww.INFO.Println("building projects with tag in force mode")
if err := project.BuildForceWithoutTag(p, rootDir, noDependency); err != nil {
jww.ERROR.Fatalf("could not build tag projects: %v", err)
}
}
},
}

func init() {
addTagFlag(buildCmd)
addProjectFlag(buildCmd)
addForceFlag(buildCmd)
addNoDependencyFlag(buildCmd)
addProjectFlag(buildCmd)
addTagFlag(buildCmd)
rootCmd.AddCommand(buildCmd)
}
27 changes: 16 additions & 11 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ import (
)

var (
projectName string
rootDir string
tag string
updated bool
verbose bool
force bool
force bool
noDependency bool
projectName string
rootDir string
tag string
updated bool
verbose bool
)

func addForceFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&force, "force", "f", false, "force the command")
}

func addNoDependencyFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&noDependency, "no-dependency", "nd", false, "skip dependency")
}

func addProjectFlag(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&projectName, "project", "p", "", "project name")
}
Expand All @@ -29,18 +38,14 @@ func addTagFlag(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&tag, "tag", "t", "", "build tag")
}

func addupdatedFlag(cmd *cobra.Command) {
func addUpdatedFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&updated, "updated", "u", false, "updated projects")
}

func addVerboseFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "log level info")
}

func addForceFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&force, "force", "f", false, "force the command")
}

func handleVerboseFlag() {
if verbose {
jww.SetLogThreshold(jww.LevelTrace)
Expand Down
49 changes: 24 additions & 25 deletions commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,42 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
)

var initCmd = &cobra.Command{
Use: "init",
Short: "create a manifest in the local directory",
Use: "init",
Aliases: []string{"i"},
Short: "create a manifest in the local directory",
Run: func(cmd *cobra.Command, args []string) {
path, _ := filepath.Abs(".")
_, name := filepath.Split(path)
out := fmt.Sprintf("name: %s\n", name) +
"scripts:\n" +
fmt.Sprintf(" test: echo \"testing %s 🤞!\"\n", name) +
"strategies:\n" +
" compare-to:\n" +
" branch: master\n" +
" previous-commit:\n" +
" only: master\n" +
fmt.Sprintf("build: echo \"building %s 💖!\"\n", name) +
"version: git log -1 --format='%%h' .\n" +
"tag: false\n" +
"env:\n" +
" - name: NAMESPACE\n" +
" value: staging\n"
if _, err := os.Stat(path + string(filepath.Separator) + ".gally.yml"); os.IsNotExist(err) {
err := ioutil.WriteFile(path+string(filepath.Separator)+".gally.yml", []byte(out), 0644)
if err != nil {
panic(err)
}
fmt.Println("File .gally.yml created. Feel free to edit it!")
} else {
cwd, _ := filepath.Abs(".")
if projectName == "" {
_, projectName = filepath.Split(cwd)
}
f := path.Join(cwd, ".gally.yml")
if _, err := os.Stat(f); !os.IsNotExist(err) && !force {
fmt.Println("File .gally.yml already exists.")
return
}
config := fmt.Sprintf(`
name: %s
scripts:
test: echo "no test yet"
build: echo "building %s 💖!"
`, projectName, projectName)
if err := ioutil.WriteFile(f, []byte(config), 0644); err != nil {
jww.ERROR.Fatalf("cannot create config file: %v", err)
}
fmt.Println("File .gally.yml created. Feel free to edit it!")
},
}

func init() {
addForceFlag(initCmd)
addProjectFlag(initCmd)
rootCmd.AddCommand(initCmd)
}
10 changes: 6 additions & 4 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
const padding = 1

var listCmd = &cobra.Command{
Use: "list",
Short: "display all handled projects",
Use: "list",
Aliases: []string{"l", "ls"},
Short: "display all handled projects",
Run: func(cmd *cobra.Command, args []string) {
handleVerboseFlag()
projects := project.Projects{}
Expand All @@ -24,7 +25,7 @@ var listCmd = &cobra.Command{
}
projects[projectName] = project.FindByName(rootDir, projectName)
} else if updated {
projects = project.FindAllUpdated(rootDir)
projects = project.FindAllUpdated(rootDir, noDependency)
} else {
projects = project.FindAll(rootDir)
}
Expand All @@ -34,7 +35,8 @@ var listCmd = &cobra.Command{
}

func init() {
addNoDependencyFlag(listCmd)
addProjectFlag(listCmd)
addupdatedFlag(listCmd)
addUpdatedFlag(listCmd)
rootCmd.AddCommand(listCmd)
}
10 changes: 6 additions & 4 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
)

var runCmd = &cobra.Command{
Use: "run [script]",
Short: "run your script on projects having updated files",
Use: "run [script]",
Aliases: []string{"exec"},
Short: "run your script on projects having updated files",
Run: func(cmd *cobra.Command, args []string) {
handleVerboseFlag()
if len(args) == 0 {
Expand All @@ -24,7 +25,7 @@ var runCmd = &cobra.Command{
} else if force {
projects = project.FindAll(rootDir)
} else {
projects = project.FindAllUpdated(rootDir)
projects = project.FindAllUpdated(rootDir, noDependency)
}
script := args[0]
for _, p := range projects {
Expand All @@ -36,7 +37,8 @@ var runCmd = &cobra.Command{
}

func init() {
addProjectFlag(runCmd)
addForceFlag(runCmd)
addNoDependencyFlag(runCmd)
addProjectFlag(runCmd)
rootCmd.AddCommand(runCmd)
}
17 changes: 12 additions & 5 deletions commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,35 @@ import (
"github.com/missena-corp/gally/helpers"
"github.com/missena-corp/gally/project"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Gally version's number",
Use: "version",
Aliases: []string{"v"},
Short: "Gally version's number",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
if len(args) == 0 && projectName == "" {
fmt.Println(gallyVersionString())
return
}
p := project.Find(args[0], rootDir)
if projectName == "" {
jww.WARN.Println("using 'gally version <project>' is deprecated now use 'gally version -p <project>'")
projectName = args[0]
}
p := project.Find(projectName, rootDir)
fmt.Println(p.Version())
},
}

func init() {
addProjectFlag(versionCmd)
rootCmd.AddCommand(versionCmd)
}

func gallyVersionString() string {
program := "Gally, the monorepo slayer"
version := helpers.Version
osArch := runtime.GOOS + "/" + runtime.GOARCH
osArch := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
return fmt.Sprintf("%s %s %s BuildDate: %s", program, version, osArch, helpers.BuildDate)
}
47 changes: 15 additions & 32 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func BuildTag(name *string, tag string, rootDir string) error {
return p.runBuild(version)
}

func BuildForceWithTag(name *string, rootDir string) error {
func BuildForceWithoutTag(name *string, rootDir string, noDep bool) error {
projects := Projects{}
if name == nil {
allProjects := FindAllUpdated(rootDir)
for _, v := range allProjects {
if v.Tag {
projects[v.Name] = v
allProjects := FindAllUpdated(rootDir, noDep)
for _, p := range allProjects {
if p.Tag {
projects[p.Name] = p
}
}
} else {
Expand All @@ -100,10 +100,10 @@ func BuildForceWithTag(name *string, rootDir string) error {
return nil
}

func BuildNoTag(name *string, rootDir string) error {
func BuildWithoutTag(name *string, rootDir string, noDep bool) error {
projects := Projects{}
if name == nil {
projects = FindAllUpdated(rootDir)
projects = FindAllUpdated(rootDir, noDep)
} else {
p := Find(*name, rootDir)
if p == nil {
Expand Down Expand Up @@ -173,10 +173,10 @@ func FindAll(rootDir string) Projects {
return projects
}

func FindAllUpdated(rootDir string) Projects {
func FindAllUpdated(rootDir string, noDep bool) Projects {
projects := make(Projects)
for name, project := range FindAll(rootDir) {
if project.WasUpdated() {
if project.WasUpdated(noDep) {
projects[name] = project
}
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func (projs Projects) ToSlice() []map[string]interface{} {
"directory": p.BaseDir,
"environment": p.env(generateVersion()),
"name": p.Name,
"update": p.WasUpdated(),
"update": *p.updated,
"version": p.Version(),
}
if len(p.DependsOn) != 0 {
Expand All @@ -338,27 +338,7 @@ func (p *Project) Version() string {
return version
}

func (p *Project) WasBumped() (bumped bool) {
if p.bumped != nil {
return *p.bumped
}
p.bumped = newFalse()
if !p.WasUpdated() {
return false
}
currentVersion := p.Version()
commit := "master"
if repo.IsOnBranch("master") {
commit = "HEAD^1"
}
repo.Checkout(commit, func() { bumped = p.Version() != currentVersion })
if bumped {
p.bumped = newTrue()
}
return bumped
}

func (p *Project) WasUpdated() bool {
func (p *Project) WasUpdated(noDep bool) bool {
// cache response
if p.updated != nil {
return *p.updated
Expand All @@ -368,8 +348,11 @@ func (p *Project) WasUpdated() bool {
p.updated = newTrue()
return true
}
if noDep {
continue
}
for _, dep := range p.Dependencies {
if dep.WasUpdated() {
if dep.WasUpdated(noDep) {
p.updated = newTrue()
return true
}
Expand Down

0 comments on commit 5aa9ee0

Please sign in to comment.