Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
clone and install deps concurrently, set current directory as target …
Browse files Browse the repository at this point in the history
…directory, added —no-deps option for develop setup
  • Loading branch information
flipace committed Apr 9, 2017
1 parent 57fb470 commit a2a0e81
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 55 deletions.
Binary file modified bin/hantera
Binary file not shown.
45 changes: 28 additions & 17 deletions commands/develop/installDependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,56 @@ import (
"os"
"os/exec"
"path"
"sync"

"github.com/flipace/hantera/lib"
"github.com/urfave/cli"
)

func check(err error) {
if err != nil {
panic(err)
}
}

// InstallDependencies : installs project dependencies (tries to figure out package manager e.g. npm)
func InstallDependencies(c *cli.Context) {
configFile := c.String("config")

config := lib.GetProductConfig(configFile)

target := c.String("target")
if target == "" {
target = path.Join("./", config.Name)
}

lib.Catchy("Installing dependencies for \"%s\" v%s...\n", config.Name, config.Version)

// create a semaphore with l
var wg sync.WaitGroup

for key := range config.Dependencies {
targetDir := path.Join(target, key)
wg.Add(1)

if _, err := os.Stat(path.Join(targetDir, "package.json")); err == nil {
lib.Notice("|-- Found package.json for %s - running 'yarn'\n", key)
go func(target string, key string) {

cmd := exec.Command("yarn", "install")
cmd.Dir = targetDir
cmd.Stdout = os.Stdout
targetDir := path.Join(target, key)

err = cmd.Start()
if _, err := os.Stat(path.Join(targetDir, "package.json")); err == nil {
lib.Notice("|-- Found package.json for %s - running 'yarn'\n", key)

if err != nil {
panic(err)
}
cmd := exec.Command("yarn", "install")
cmd.Dir = targetDir
cmd.Stdout = os.Stdout

err = cmd.Wait()
err = cmd.Start()
check(err)

if err != nil {
panic(err)
err = cmd.Wait()
check(err)
}
}

wg.Done()
}(target, key)

}

wg.Wait()
}
65 changes: 35 additions & 30 deletions commands/develop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ package develop

import "github.com/urfave/cli"

// default flags for various develop commands
var defaultFlags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "load manifest from `FILE.yml`",
Value: "./hantera.yml",
},
cli.StringFlag{
Name: "target, t",
Usage: "setup project in `PATH`",
Value: "./",
},
}

// Commands : describes all commands for "develop" environments
var Commands = []cli.Command{
{
Expand All @@ -16,26 +30,24 @@ var Commands = []cli.Command{
UsageText: "hantera develop setup --config ./hantera.yml --target ./project",
Description: "sets up a project for development",
Action: Setup,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "load manifest from `FILE.yml`",
Value: "./hantera.yml",
},
cli.StringFlag{
Name: "target, t",
Usage: "setup project in `PATH`",
Flags: append(
[]cli.Flag{
cli.StringFlag{
Name: "branch, b",
Usage: "branch to checkout",
Value: "refs/heads/develop",
},
cli.BoolFlag{
Name: "progress, p",
Usage: "show clone progress",
},
cli.BoolFlag{
Name: "no-deps, no-dep",
Usage: "don't install dependencies (e.g. npm i)",
},
},
cli.StringFlag{
Name: "branch, b",
Usage: "branch to checkout",
Value: "refs/heads/develop",
},
cli.BoolFlag{
Name: "progress, p",
Usage: "show clone progress",
},
},
defaultFlags...,
),
},
{
Name: "install-dependencies",
Expand All @@ -44,17 +56,10 @@ var Commands = []cli.Command{
UsageText: "hantera develop install-deps --config ./hantera.yml --target ./project",
Description: "installs dependencies for a project",
Action: InstallDependencies,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "load manifest from `FILE.yml`",
Value: "./hantera.yml",
},
cli.StringFlag{
Name: "target, t",
Usage: "setup project in `PATH`",
},
},
Flags: append(
[]cli.Flag{},
defaultFlags...,
),
},
},
},
Expand Down
42 changes: 34 additions & 8 deletions commands/develop/setup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package develop

import (
"os"
"path"
"sync"

"github.com/flipace/hantera/lib"
"github.com/urfave/cli"
Expand All @@ -14,24 +16,48 @@ func Setup(c *cli.Context) {
config := lib.GetProductConfig(configFile)

target := c.String("target")
if target == "" {
target = path.Join("./", config.Name)
}

refName := c.String("branch")
progress := c.Bool("progress")
nodeps := c.Bool("no-deps")

lib.Catchy("Setup \"%s\" v%s\n", config.Name, config.Version)

// the ref to checkout
refName := c.String("branch")
println(nodeps)
// create and open .gitignore in target path - we put all dependencies into it
gitignore, err := os.Create(path.Join(target, ".gitignore"))
if err != nil {
panic(err)
}
defer gitignore.Close()

var wg sync.WaitGroup

for key, value := range config.Dependencies {
wg.Add(1)

targetDir := path.Join(target, key)

lib.Notice("--| Cloning %s to %s\n", key, targetDir)

lib.Clone(value.Repository, targetDir, refName, progress)
go func(
name string,
repository string,
target string,
refName string,
progress bool,
) {
lib.Clone(repository, target, refName, progress)

gitignore.WriteString(name + "\n")
gitignore.Sync()

wg.Done()
}(key, value.Repository, targetDir, refName, progress)
}

InstallDependencies(c)
wg.Wait()

if nodeps == false {
InstallDependencies(c)
}
}

0 comments on commit a2a0e81

Please sign in to comment.