Skip to content

Commit fe7450a

Browse files
committed
Introduce a new Flogo import syntax
1 parent 1e044ca commit fe7450a

11 files changed

Lines changed: 238 additions & 91 deletions

File tree

api/build.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func BuildProject(project common.AppProject, options BuildOptions) error {
8181
fmt.Println("Path to exe is ", exePath)
8282
}
8383
if _, err := os.Stat(exePath); err == nil {
84+
finalExePath := project.Executable()
85+
os.MkdirAll(filepath.Dir(finalExePath), os.ModePerm)
8486
err = os.Rename(exePath, project.Executable())
8587

8688
if err != nil {
@@ -125,7 +127,12 @@ func prepareShim(project common.AppProject, shim string) (bool, error) {
125127
}
126128
}
127129

128-
path, err := project.GetPath(ref)
130+
refImport, err := util.NewFlogoImportFromPath(ref)
131+
if err != nil {
132+
return false, err
133+
}
134+
135+
path, err := project.GetPath(refImport)
129136
if err != nil {
130137
return false, err
131138
}
@@ -205,7 +212,12 @@ func createShimSupportGoFile(project common.AppProject, create bool) error {
205212
return nil
206213
}
207214

208-
corePath, err := project.GetPath(flogoCoreRepo)
215+
flogoCoreImport, err := util.NewFlogoImportFromPath(flogoCoreRepo)
216+
if err != nil {
217+
return err
218+
}
219+
220+
corePath, err := project.GetPath(flogoCoreImport)
209221
if err != nil {
210222
return err
211223
}

api/create.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ func setupAppDirectory(dm util.DepManager, appPath, coreVersion string) error {
146146

147147
fmt.Println("installing core")
148148

149+
flogoCoreImport := util.NewFlogoImport(flogoCoreRepo, "", coreVersion, "")
150+
149151
// add & fetch the core library
150-
dm.AddDependency(flogoCoreRepo, coreVersion, true)
152+
dm.AddDependency(flogoCoreImport, true)
151153

152154
return nil
153155
}
@@ -192,7 +194,7 @@ func importDependencies(project common.AppProject) error {
192194
fmt.Printf("%-20s %s\n", instStr, imp)
193195
}
194196

195-
legacy, err := IsLegacySupportRequired(desc, path, imp, true)
197+
legacy, err := IsLegacySupportRequired(desc, path, imp.ImportPath(), true)
196198
if err != nil {
197199
return err
198200
}
@@ -210,7 +212,12 @@ func importDependencies(project common.AppProject) error {
210212

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

213-
corePath, err := dm.GetPath(flogoCoreRepo)
215+
flogoCoreImport, err := util.NewFlogoImportFromPath(flogoCoreRepo)
216+
if err != nil {
217+
return err
218+
}
219+
220+
corePath, err := dm.GetPath(flogoCoreImport)
214221
if err != nil {
215222
return err
216223
}
@@ -233,7 +240,12 @@ func getAndUpdateAppJson(dm util.DepManager, appName, appJson string) (string, e
233240
if len(appJson) == 0 {
234241

235242
// appJson wasn't provided, so lets grab the example
236-
corePath, err := dm.GetPath(flogoCoreRepo)
243+
flogoCoreImport, err := util.NewFlogoImportFromPath(flogoCoreRepo)
244+
if err != nil {
245+
return "", err
246+
}
247+
248+
corePath, err := dm.GetPath(flogoCoreImport)
237249
if err != nil {
238250
return "", err
239251
}

api/imports.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ func registerImport(project common.AppProject, anImport string) error {
5959

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

62-
path, err := project.GetPath(ref)
62+
refAsFlogoImport, err := util.NewFlogoImportFromPath(ref)
63+
if err != nil {
64+
return "", err
65+
}
66+
67+
path, err := project.GetPath(refAsFlogoImport)
6368
if err != nil {
6469
return "", err
6570
}

api/install.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ import (
1515

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

18-
err := project.AddImports(false, pkg)
18+
flogoImport, err := util.ParseImport(pkg)
1919
if err != nil {
2020
return err
2121
}
2222

23-
path, err := project.GetPath(pkg)
23+
err = project.AddImports(false, flogoImport)
24+
if err != nil {
25+
return err
26+
}
27+
28+
path, err := project.GetPath(flogoImport)
2429
if Verbose() {
2530
fmt.Println("Installed path", path)
2631
}
@@ -72,6 +77,7 @@ func InstallPalette(project common.AppProject, path string) error {
7277
return nil
7378

7479
}
80+
7581
func ListPackages(project common.AppProject, format bool, all bool) error {
7682

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

83-
var contribs []string
89+
var contribs util.Imports
8490

8591
if all {
86-
contribs, _ = util.GetAllImports(filepath.Join(project.SrcDir(), fileImportsGo)) // Get Imports from imports.go
87-
92+
imports, _ := util.GetAllImports(filepath.Join(project.SrcDir(), fileImportsGo)) // Get Imports from imports.go
93+
for _, i := range imports {
94+
flogoImport, _ := util.ParseImport(i)
95+
contribs = append(contribs, flogoImport)
96+
}
8897
} else {
8998
contribs, _ = util.GetImports(filepath.Join(project.Dir(), fileFlogoJson)) // Get Imports from flogo.json
90-
9199
}
92100

93101
var result []interface{}
94102

95103
for _, contrib := range contribs {
96-
contrib = clearVersion(contrib)
97-
98104
path, err := project.GetPath(contrib)
99105
if Verbose() {
100106
fmt.Println("Path of contrib", path, "for contrib", contrib)
@@ -127,7 +133,7 @@ func ListPackages(project common.AppProject, format bool, all bool) error {
127133
desc.Type,
128134
desc.Description,
129135
desc.Homepage,
130-
contrib,
136+
contrib.ModulePath(),
131137
getDescriptorFile(path),
132138
}
133139

@@ -157,12 +163,3 @@ func getDescriptorFile(path string) string {
157163
}
158164
return ""
159165
}
160-
func clearVersion(pkg string) string {
161-
162-
if strings.Contains(pkg, "@") {
163-
164-
return strings.Split(pkg, "@")[0]
165-
166-
}
167-
return pkg
168-
}

api/legacy.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ func IsLegacySupportRequired(desc *util.FlogoContribDescriptor, path, pkg string
3535
}
3636

3737
func InstallLegacySupport(project common.AppProject) error {
38-
err := project.AddImports(false, pkgLegacySupport)
38+
pkgLegacySupportImport, err := util.NewFlogoImportFromPath(pkgLegacySupport)
39+
if err != nil {
40+
return err
41+
}
42+
err = project.AddImports(false, pkgLegacySupportImport)
3943
if err == nil {
4044
fmt.Println("Installed Legacy Support")
4145
}

api/project.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"os"
1212
"os/exec"
1313
"path/filepath"
14-
"regexp"
1514
"runtime"
1615

1716
"github.com/project-flogo/cli/common"
@@ -103,12 +102,11 @@ func (p *appProjectImpl) Executable() string {
103102
return execPath
104103
}
105104

106-
func (p *appProjectImpl) GetPath(pkg string) (string, error) {
107-
108-
return p.dm.GetPath(pkg)
105+
func (p *appProjectImpl) GetPath(flogoImport util.Import) (string, error) {
106+
return p.dm.GetPath(flogoImport)
109107
}
110108

111-
func (p *appProjectImpl) addImportsInGo(ignoreError bool, imports ...string) error {
109+
func (p *appProjectImpl) addImportsInGo(ignoreError bool, imports ...util.Import) error {
112110
importsFile := filepath.Join(p.SrcDir(), fileImportsGo)
113111

114112
fset := token.NewFileSet()
@@ -117,16 +115,16 @@ func (p *appProjectImpl) addImportsInGo(ignoreError bool, imports ...string) err
117115
return err
118116
}
119117

120-
for _, importPath := range imports {
121-
importPath, err := p.DepManager().AddDependency(importPath, "", true)
118+
for _, i := range imports {
119+
err := p.DepManager().AddDependency(i, true)
122120
if err != nil {
123121
if ignoreError {
124-
fmt.Printf("Warning: unable to install %s\n", importPath)
122+
fmt.Printf("Warning: unable to install %s\n", i)
125123
continue
126124
}
127125
return err
128126
}
129-
util.AddImport(fset, file, importPath)
127+
util.AddImport(fset, file, i.ImportPath())
130128
}
131129

132130
f, err := os.Create(importsFile)
@@ -146,7 +144,7 @@ func (p *appProjectImpl) addImportsInGo(ignoreError bool, imports ...string) err
146144
return nil
147145
}
148146

149-
func (p *appProjectImpl) addImportsInJson(ignoreError bool, imports ...string) error {
147+
func (p *appProjectImpl) addImportsInJson(ignoreError bool, imports ...util.Import) error {
150148
appDescriptorFile := filepath.Join(p.appDir, fileFlogoJson)
151149
appDescriptorJsonFile, err := os.Open(appDescriptorFile)
152150
if err != nil {
@@ -162,18 +160,16 @@ func (p *appProjectImpl) addImportsInJson(ignoreError bool, imports ...string) e
162160
var appDescriptor app.Config
163161
json.Unmarshal([]byte(appDescriptorData), &appDescriptor)
164162

165-
importPattern := regexp.MustCompile(`^([^ ]* )?([^@]*)@?(.*)?$`) // extract import path even if there is an alias and/or a version
166-
167163
// list existing imports in JSON to avoid duplicates
168164
existingImports := make(map[string]bool)
169-
for _, e := range appDescriptor.Imports {
170-
importPath := importPattern.FindStringSubmatch(e)[2]
171-
existingImports[importPath] = true
165+
jsonImports, _ := util.ParseImports(appDescriptor.Imports)
166+
for _, e := range jsonImports {
167+
existingImports[e.CanonicalImport()] = true
172168
}
173169

174170
for _, i := range imports {
175-
if _, ok := existingImports[i]; !ok {
176-
appDescriptor.Imports = append(appDescriptor.Imports, importPattern.FindStringSubmatch(i)[2])
171+
if _, ok := existingImports[i.CanonicalImport()]; !ok {
172+
appDescriptor.Imports = append(appDescriptor.Imports, i.CanonicalImport())
177173
}
178174
}
179175

@@ -192,7 +188,7 @@ func (p *appProjectImpl) addImportsInJson(ignoreError bool, imports ...string) e
192188
return nil
193189
}
194190

195-
func (p *appProjectImpl) AddImports(ignoreError bool, imports ...string) error {
191+
func (p *appProjectImpl) AddImports(ignoreError bool, imports ...util.Import) error {
196192
err := p.addImportsInGo(ignoreError, imports...) // begin with Go imports as they are more likely to fail
197193
if err != nil {
198194
return err

common/project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ type AppProject interface {
99
BinDir() string
1010
SrcDir() string
1111
Executable() string
12-
AddImports(ignoreError bool, imports ...string) error
12+
AddImports(ignoreError bool, imports ...util.Import) error
1313
RemoveImports(imports ...string) error
14-
GetPath(pkg string) (string, error)
14+
GetPath(flogoImport util.Import) (string, error)
1515
DepManager() util.DepManager
1616
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/project-flogo/cli
33
require (
44
github.com/davecgh/go-spew v1.1.1 // indirect
55
github.com/pmezard/go-difflib v1.0.0 // indirect
6-
github.com/project-flogo/core v0.9.0-alpha.4.0.20190220191401-07116138c345
6+
github.com/project-flogo/core v0.9.0-alpha.4.0.20190222151024-3eb86689b764
77
github.com/spf13/cobra v0.0.3
88
github.com/spf13/pflag v1.0.3 // indirect
99
github.com/stretchr/testify v1.2.2

util/flogo.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func GetContribDescriptor(path string) (*FlogoContribDescriptor, error) {
8787
}
8888

8989
// ParseAppDescriptor parse the application descriptor
90-
func GetImports(appJsonPath string) ([]string, error) {
90+
func GetImports(appJsonPath string) (Imports, error) {
9191

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

@@ -116,7 +116,16 @@ func GetImports(appJsonPath string) ([]string, error) {
116116
allImports = append(allImports, key)
117117
}
118118

119-
return allImports, nil
119+
var result Imports
120+
for _, i := range allImports {
121+
parsedImport, err := ParseImport(i)
122+
if err != nil {
123+
return nil, err
124+
}
125+
result = append(result, parsedImport)
126+
}
127+
128+
return result, nil
120129
}
121130

122131
func getImports(appJsonPath string) ([]string, error) {

0 commit comments

Comments
 (0)