Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions api/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func BuildProject(project common.AppProject, options BuildOptions) error {
fmt.Println("Path to exe is ", exePath)
}
if _, err := os.Stat(exePath); err == nil {
finalExePath := project.Executable()
os.MkdirAll(filepath.Dir(finalExePath), os.ModePerm)
err = os.Rename(exePath, project.Executable())

if err != nil {
Expand Down Expand Up @@ -125,7 +127,12 @@ func prepareShim(project common.AppProject, shim string) (bool, error) {
}
}

path, err := project.GetPath(ref)
refImport, err := util.NewFlogoImportFromPath(ref)
if err != nil {
return false, err
}

path, err := project.GetPath(refImport)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -205,7 +212,12 @@ func createShimSupportGoFile(project common.AppProject, create bool) error {
return nil
}

corePath, err := project.GetPath(flogoCoreRepo)
flogoCoreImport, err := util.NewFlogoImportFromPath(flogoCoreRepo)
if err != nil {
return err
}

corePath, err := project.GetPath(flogoCoreImport)
if err != nil {
return err
}
Expand Down
21 changes: 16 additions & 5 deletions api/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ func setupAppDirectory(dm util.DepManager, appPath, coreVersion string) error {

fmt.Println("installing core")

// add & fetch the core library
flogoCoreImport := util.NewFlogoImport(flogoCoreRepo, "", coreVersion, "")

dm.AddDependency(flogoCoreRepo, coreVersion)
// add & fetch the core library
dm.AddDependency(flogoCoreImport)

return nil
}
Expand Down Expand Up @@ -193,7 +194,7 @@ func importDependencies(project common.AppProject) error {
fmt.Printf("%-20s %s\n", instStr, imp)
}

legacy, err := IsLegacySupportRequired(desc, path, imp, true)
legacy, err := IsLegacySupportRequired(desc, path, imp.GoImportPath(), true)
if err != nil {
return err
}
Expand All @@ -211,7 +212,12 @@ func importDependencies(project common.AppProject) error {

func createMain(dm util.DepManager, appDir string) error {

corePath, err := dm.GetPath(flogoCoreRepo)
flogoCoreImport, err := util.NewFlogoImportFromPath(flogoCoreRepo)
if err != nil {
return err
}

corePath, err := dm.GetPath(flogoCoreImport)
if err != nil {
return err
}
Expand All @@ -234,7 +240,12 @@ func getAndUpdateAppJson(dm util.DepManager, appName, appJson string) (string, e
if len(appJson) == 0 {

// appJson wasn't provided, so lets grab the example
corePath, err := dm.GetPath(flogoCoreRepo)
flogoCoreImport, err := util.NewFlogoImportFromPath(flogoCoreRepo)
if err != nil {
return "", err
}

corePath, err := dm.GetPath(flogoCoreImport)
if err != nil {
return "", err
}
Expand Down
7 changes: 6 additions & 1 deletion api/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ func registerImport(project common.AppProject, anImport string) error {

func getContribType(project common.AppProject, ref string) (string, error) {

path, err := project.GetPath(ref)
refAsFlogoImport, err := util.NewFlogoImportFromPath(ref)
if err != nil {
return "", err
}

path, err := project.GetPath(refAsFlogoImport)
if err != nil {
return "", err
}
Expand Down
33 changes: 15 additions & 18 deletions api/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ import (

func InstallPackage(project common.AppProject, pkg string) error {

err := project.AddImports(false, pkg)
flogoImport, err := util.ParseImport(pkg)
if err != nil {
return err
}

path, err := project.GetPath(pkg)
err = project.AddImports(false, flogoImport)
if err != nil {
return err
}

path, err := project.GetPath(flogoImport)
if Verbose() {
fmt.Println("Installed path", path)
}
Expand Down Expand Up @@ -72,6 +77,7 @@ func InstallPalette(project common.AppProject, path string) error {
return nil

}

func ListPackages(project common.AppProject, format bool, all bool) error {

err := util.ExecCmd(exec.Command("go", "mod", "tidy"), project.SrcDir())
Expand All @@ -80,21 +86,21 @@ func ListPackages(project common.AppProject, format bool, all bool) error {
return err
}

var contribs []string
var contribs util.Imports

if all {
contribs, _ = util.GetAllImports(filepath.Join(project.SrcDir(), fileImportsGo)) // Get Imports from imports.go

imports, _ := util.GetAllImports(filepath.Join(project.SrcDir(), fileImportsGo)) // Get Imports from imports.go
for _, i := range imports {
flogoImport, _ := util.ParseImport(i)
contribs = append(contribs, flogoImport)
}
} else {
contribs, _ = util.GetImports(filepath.Join(project.Dir(), fileFlogoJson)) // Get Imports from flogo.json

}

var result []interface{}

for _, contrib := range contribs {
contrib = clearVersion(contrib)

path, err := project.GetPath(contrib)
if Verbose() {
fmt.Println("Path of contrib", path, "for contrib", contrib)
Expand Down Expand Up @@ -127,7 +133,7 @@ func ListPackages(project common.AppProject, format bool, all bool) error {
desc.Type,
desc.Description,
desc.Homepage,
contrib,
contrib.ModulePath(),
getDescriptorFile(path),
}

Expand Down Expand Up @@ -157,12 +163,3 @@ func getDescriptorFile(path string) string {
}
return ""
}
func clearVersion(pkg string) string {

if strings.Contains(pkg, "@") {

return strings.Split(pkg, "@")[0]

}
return pkg
}
6 changes: 5 additions & 1 deletion api/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func IsLegacySupportRequired(desc *util.FlogoContribDescriptor, path, pkg string
}

func InstallLegacySupport(project common.AppProject) error {
err := project.AddImports(false, pkgLegacySupport)
pkgLegacySupportImport, err := util.NewFlogoImportFromPath(pkgLegacySupport)
if err != nil {
return err
}
err = project.AddImports(false, pkgLegacySupportImport)
if err == nil {
fmt.Println("Installed Legacy Support")
}
Expand Down
77 changes: 67 additions & 10 deletions api/project.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package api

import (
"encoding/json"
"fmt"
"github.com/project-flogo/core/app" // dependency to core ensures the CLI always uses an up-to-date struct for JSON manipulation (this dependency already exists implicitly in the "flogo create" command)
"go/parser"
"go/printer"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -99,13 +102,11 @@ func (p *appProjectImpl) Executable() string {
return execPath
}

func (p *appProjectImpl) GetPath(pkg string) (string, error) {

return p.dm.GetPath(pkg)
func (p *appProjectImpl) GetPath(flogoImport util.Import) (string, error) {
return p.dm.GetPath(flogoImport)
}

func (p *appProjectImpl) AddImports(ignoreError bool, imports ...string) error {

func (p *appProjectImpl) addImportsInGo(ignoreError bool, imports ...util.Import) error {
importsFile := filepath.Join(p.SrcDir(), fileImportsGo)

fset := token.NewFileSet()
Expand All @@ -114,17 +115,19 @@ func (p *appProjectImpl) AddImports(ignoreError bool, imports ...string) error {
return err
}

for _, impPath := range imports {
path, version := util.ParseImportPath(impPath)
err := p.DepManager().AddDependency(path, version)
for _, i := range imports {
err := p.DepManager().AddDependency(i)
if err != nil {
if ignoreError {
fmt.Printf("Warning: unable to install %s\n", impPath)
fmt.Printf("Warning: unable to install '%s'\n", i)
continue
}

fmt.Errorf("Error in installing '%s'\n", i)

return err
}
util.AddImport(fset, file, path)
util.AddImport(fset, file, i.GoImportPath())
}

f, err := os.Create(importsFile)
Expand All @@ -138,6 +141,60 @@ func (p *appProjectImpl) AddImports(ignoreError bool, imports ...string) error {
return nil
}

func (p *appProjectImpl) addImportsInJson(ignoreError bool, imports ...util.Import) error {
appDescriptorFile := filepath.Join(p.appDir, fileFlogoJson)
appDescriptorJsonFile, err := os.Open(appDescriptorFile)
if err != nil {
return err
}
defer appDescriptorJsonFile.Close()

appDescriptorData, err := ioutil.ReadAll(appDescriptorJsonFile)
if err != nil {
return err
}

var appDescriptor app.Config
json.Unmarshal([]byte(appDescriptorData), &appDescriptor)

// list existing imports in JSON to avoid duplicates
existingImports := make(map[string]bool)
jsonImports, _ := util.ParseImports(appDescriptor.Imports)
for _, e := range jsonImports {
existingImports[e.CanonicalImport()] = true
}

for _, i := range imports {
if _, ok := existingImports[i.CanonicalImport()]; !ok {
appDescriptor.Imports = append(appDescriptor.Imports, i.CanonicalImport())
}
}

appDescriptorUpdated, err := json.MarshalIndent(appDescriptor, "", " ")
if err != nil {
return err
}

appDescriptorUpdatedJson := string(appDescriptorUpdated)

err = ioutil.WriteFile(appDescriptorFile, []byte(appDescriptorUpdatedJson), 0644)
if err != nil {
return err
}

return nil
}

func (p *appProjectImpl) AddImports(ignoreError bool, imports ...util.Import) error {
err := p.addImportsInGo(ignoreError, imports...) // begin with Go imports as they are more likely to fail
if err != nil {
return err
}
err = p.addImportsInJson(ignoreError, imports...) // adding imports in JSON after Go imports ensure the flogo.json is self-sufficient

return err
}

func (p *appProjectImpl) RemoveImports(imports ...string) error {

importsFile := filepath.Join(p.SrcDir(), fileImportsGo)
Expand Down
4 changes: 2 additions & 2 deletions common/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type AppProject interface {
BinDir() string
SrcDir() string
Executable() string
AddImports(ignoreError bool, imports ...string) error
AddImports(ignoreError bool, imports ...util.Import) error
RemoveImports(imports ...string) error
GetPath(pkg string) (string, error)
GetPath(flogoImport util.Import) (string, error)
DepManager() util.DepManager
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/project-flogo/cli

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/msoap/byline v1.1.1
github.com/project-flogo/core v0.9.0-alpha.4.0.20190225212330-f76237454d56
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/testify v1.2.2
Expand Down
13 changes: 11 additions & 2 deletions util/flogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func GetContribDescriptor(path string) (*FlogoContribDescriptor, error) {
}

// ParseAppDescriptor parse the application descriptor
func GetImports(appJsonPath string) ([]string, error) {
func GetImports(appJsonPath string) (Imports, error) {

importSet := make(map[string]struct{})

Expand Down Expand Up @@ -116,7 +116,16 @@ func GetImports(appJsonPath string) ([]string, error) {
allImports = append(allImports, key)
}

return allImports, nil
var result Imports
for _, i := range allImports {
parsedImport, err := ParseImport(i)
if err != nil {
return nil, err
}
result = append(result, parsedImport)
}

return result, nil
}

func getImports(appJsonPath string) ([]string, error) {
Expand Down
Loading