Skip to content

Commit

Permalink
- Fix issue project-flogo#117 Specify base go.mod for app creation
Browse files Browse the repository at this point in the history
- Fix build to handle go mod replace correctly & do not delete shim source code after build
  • Loading branch information
yxuco committed Dec 11, 2020
1 parent 5c3e1e3 commit c1be08a
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 25 deletions.
9 changes: 5 additions & 4 deletions api/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

var fileSampleEngineMain = filepath.Join("examples", "engine", "main.go")

func CreateProject(basePath, appName, appCfgPath, coreVersion string) (common.AppProject, error) {
func CreateProject(basePath, appName, appCfgPath, modFilePath, coreVersion string) (common.AppProject, error) {

var err error
var appJson string
Expand Down Expand Up @@ -58,7 +58,7 @@ func CreateProject(basePath, appName, appCfgPath, coreVersion string) (common.Ap
fmt.Printf("Setting up app directory: %s\n", appDir)
}

err = setupAppDirectory(dm, appDir, coreVersion)
err = setupAppDirectory(dm, appDir, modFilePath, coreVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func createAppDirectory(basePath, appName string) (string, error) {
}

//setupAppDirectory sets up the flogo app directory
func setupAppDirectory(dm util.DepManager, appPath, coreVersion string) error {
func setupAppDirectory(dm util.DepManager, appPath, modFilePath, coreVersion string) error {

err := os.Mkdir(filepath.Join(appPath, dirBin), os.ModePerm)
if err != nil {
Expand All @@ -140,7 +140,8 @@ func setupAppDirectory(dm util.DepManager, appPath, coreVersion string) error {
return err
}

err = dm.Init()
// create go.mod only if modFilePath is not specified or the specified file does not exist
err = dm.Init(modFilePath)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions api/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestCmdCreate_noflag(t *testing.T) {
defer testEnv.cleanup()

t.Logf("Current dir '%s'", testEnv.currentDir)
_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")
assert.Equal(t, nil, err)

_, err = os.Stat(filepath.Join(tempDir, "myApp", "src", "go.mod"))
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestCmdCreate_flag(t *testing.T) {
}
defer file.Close()
fmt.Fprintf(file, jsonString)
_, err = CreateProject(testEnv.currentDir, "flogo", "flogo.json", "")
_, err = CreateProject(testEnv.currentDir, "flogo", "flogo.json", "", "")
assert.Equal(t, nil, err)

_, err = os.Stat(filepath.Join(tempDir, "flogo", "src", "go.mod"))
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestCmdCreate_masterCore(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
os.Chdir(testEnv.currentDir)

_, err = CreateProject(testEnv.currentDir, "myApp", "", "master")
_, err = CreateProject(testEnv.currentDir, "myApp", "", "", "master")
assert.Equal(t, nil, err)
}

Expand Down
8 changes: 4 additions & 4 deletions api/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestInstallLegacyPkg(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
_ = os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "v0.9.2")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "v0.9.2")

assert.Nil(t, err)

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestInstallPkg(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
_ = os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")

assert.Nil(t, err)

Expand Down Expand Up @@ -168,7 +168,7 @@ func TestInstallPkgWithVersion(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
_ = os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")

assert.Nil(t, err)

Expand Down Expand Up @@ -198,7 +198,7 @@ func TestListPkg(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
_ = os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")

assert.Equal(t, nil, err)

Expand Down
4 changes: 2 additions & 2 deletions api/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestListAllContribs(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")

assert.Equal(t, nil, err)

Expand Down Expand Up @@ -50,7 +50,7 @@ func TestListWithLegacyPkg(t *testing.T) {
}
defer file.Close()
fmt.Fprintf(file, newJsonString)
_, err = CreateProject(testEnv.currentDir, "temp", "flogo.json", "")
_, err = CreateProject(testEnv.currentDir, "temp", "flogo.json", "", "")
assert.Equal(t, nil, err)

err = ListContribs(NewAppProject(filepath.Join(testEnv.currentDir, "temp")), true, "")
Expand Down
6 changes: 3 additions & 3 deletions api/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ var fileSampleShimSupport = filepath.Join("examples", "engine", "shim", fileShim

var flogoImportPattern = regexp.MustCompile(`^(([^ ]*)[ ]+)?([^@:]*)@?([^:]*)?:?(.*)?$`)


type ShimBuilder struct {
appBuilder common.Builder
shim string
shim string
}

func (sb *ShimBuilder) Build(project common.AppProject) error {
Expand All @@ -39,7 +38,8 @@ func (sb *ShimBuilder) Build(project common.AppProject) error {
return err
}

defer shimCleanup(project)
// do not clean up shim source code!
// defer shimCleanup(project)

err = createShimSupportGoFile(project)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var buildCmd = &cobra.Command{
}

api.SetVerbose(verbose)
tempProject, err := api.CreateProject(tempDir, "", flogoJsonFile, "latest")
tempProject, err := api.CreateProject(tempDir, "", flogoJsonFile, "", "latest")
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating temp project: %v\n", err)
os.Exit(1)
Expand Down
4 changes: 3 additions & 1 deletion commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

var flogoJsonPath string
var coreVersion string
var modFilePath string

func init() {
CreateCmd.Flags().StringVarP(&flogoJsonPath, "file", "f", "", "specify a flogo.json to create project from")
CreateCmd.Flags().StringVarP(&coreVersion, "cv", "", "", "specify core library version (ex. master)")
CreateCmd.Flags().StringVarP(&modFilePath, "mod", "m", "", "go.mod file to override default settings")
rootCmd.AddCommand(CreateCmd)
}

Expand All @@ -36,7 +38,7 @@ var CreateCmd = &cobra.Command{
fmt.Fprintf(os.Stderr, "Error determining working directory: %v\n", err)
os.Exit(1)
}
_, err = api.CreateProject(currentDir, appName, flogoJsonPath, coreVersion)
_, err = api.CreateProject(currentDir, appName, flogoJsonPath, modFilePath, coreVersion)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating project: %v\n", err)
os.Exit(1)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module github.com/project-flogo/cli

replace go.uber.org/multierr => go.uber.org/multierr v1.6.0

require (
github.com/coreos/go-semver v0.2.0
github.com/msoap/byline v1.1.1
github.com/pkg/errors v0.8.1 // indirect
github.com/project-flogo/core v0.9.5-beta.1
github.com/project-flogo/core v1.2.0
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
)

go 1.12
go 1.14
17 changes: 13 additions & 4 deletions util/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

type DepManager interface {
Init() error
Init(string) error
AddDependency(flogoImport Import) error
GetPath(flogoImport Import) (string, error)
AddReplacedContribForBuild() error
Expand All @@ -35,7 +35,16 @@ type ModDepManager struct {
localMods map[string]string
}

func (m *ModDepManager) Init() error {
func (m *ModDepManager) Init(modFilePath string) error {
if len(modFilePath) > 0 {
// try to copy specified go.mod file
err := CopyFile(modFilePath, filepath.Join(m.srcDir, "go.mod"))
if err != nil {
fmt.Printf("WARNING: failed to copy go.mod file %+v\n", err)
} else {
return nil
}
}

err := ExecCmd(exec.Command("go", "mod", "init", "main"), m.srcDir)
if err == nil {
Expand All @@ -55,7 +64,6 @@ func (m *ModDepManager) AddDependency(flogoImport Import) error {
return err
}


err = ExecCmd(exec.Command("go", "mod", "verify"), m.srcDir)
if err == nil {
err = ExecCmd(exec.Command("go", "mod", "download", flogoImport.ModulePath()), m.srcDir)
Expand Down Expand Up @@ -299,7 +307,8 @@ func (m *ModDepManager) AddReplacedContribForBuild() error {
localModules := strings.Split(data[index-1:], "\n")

for _, val := range localModules {
if val != "" {
// check lines starting with 'replace' only
if val != "" && strings.HasPrefix(val, "replace") {
mods := strings.Split(val, " ")
//If the length of mods is more than 4 it contains the versions of package
//so it is stating to use different version of pkg rather than
Expand Down

0 comments on commit c1be08a

Please sign in to comment.