Skip to content

Commit

Permalink
feat:(gradle) allow jx create spring to create gradle projects
Browse files Browse the repository at this point in the history
can either use:
```
jx create spring -x
```
to get prompted for the project types or add
```
jx create spring --type gradle-project
```

see jenkins-x#462
requires this PR to also merge: jenkins-x/draft-packs#9
  • Loading branch information
jstrachan committed Mar 21, 2018
1 parent 97d3743 commit 2667978
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
7 changes: 7 additions & 0 deletions pkg/jx/cmd/create_spring.go
Expand Up @@ -29,6 +29,12 @@ var (
# Creates a Spring Boot application passing in the required dependencies
jx create spring -d web -d actuator
# To pick the advanced options (such as what package type maven-project/gradle-project) etc then use
jx create spring -x
# To create a gradle project use:
jx create spring --type gradle-project
`)
)

Expand Down Expand Up @@ -78,6 +84,7 @@ func NewCmdCreateSpring(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cob
cmd.Flags().StringVarP(&options.SpringForm.BootVersion, spring.OptionBootVersion, "t", "", "Spring Boot version")
cmd.Flags().StringVarP(&options.SpringForm.JavaVersion, spring.OptionJavaVersion, "j", "", "Java version")
cmd.Flags().StringVarP(&options.SpringForm.Packaging, spring.OptionPackaging, "p", "", "Packaging")
cmd.Flags().StringVarP(&options.SpringForm.Type, spring.OptionType, "", "", "Project Type (such as maven-project or gradle-project)")

return cmd
}
Expand Down
53 changes: 16 additions & 37 deletions pkg/jx/cmd/import.go
Expand Up @@ -338,7 +338,12 @@ func (o *ImportOptions) DraftCreate() error {
}
lpack := ""
if exists {
lpack = filepath.Join(draftHome.Packs(), "github.com/jenkins-x/draft-packs/packs/java")
lpack = filepath.Join(draftHome.Packs(), "github.com/jenkins-x/draft-packs/packs/maven")

exists, _ = util.FileExists(lpack)
if !exists {
lpack = filepath.Join(draftHome.Packs(), "github.com/jenkins-x/draft-packs/packs/java")
}
} else {
// pack detection time
lpack, err = jxdraft.DoPackDetection(draftHome, o.Out, dir)
Expand Down Expand Up @@ -710,47 +715,21 @@ func (o *ImportOptions) checkChartmuseumCredentialExists() error {
func (o *ImportOptions) renameChartToMatchAppName() error {
var oldChartsDir string
dir := o.Dir
if err := filepath.Walk(dir, func(f string, fi os.FileInfo, err error) error {
if fi.Name() == ".git" {
return filepath.SkipDir
}
chartsDir := filepath.Join(dir, "charts")
files, err := ioutil.ReadDir(chartsDir)
if err != nil {
return fmt.Errorf("error matching a jenkins x draft pack name with chart folder %v", err)
}
for _, fi := range files {
if fi.IsDir() {
switch fi.Name() {
case "csharp":
oldChartsDir = filepath.Join(dir, "charts", "csharp")
break
case "go":
oldChartsDir = filepath.Join(dir, "charts", "go")
break
case "gradle":
oldChartsDir = filepath.Join(dir, "charts", "gradle")
break
case "java":
oldChartsDir = filepath.Join(dir, "charts", "java")
break
case "javascript":
oldChartsDir = filepath.Join(dir, "charts", "javascript")
break
case "php":
oldChartsDir = filepath.Join(dir, "charts", "php")
break
case "python":
oldChartsDir = filepath.Join(dir, "charts", "python")
break
case "ruby":
oldChartsDir = filepath.Join(dir, "charts", "ruby")
break
case "swift":
oldChartsDir = filepath.Join(dir, "charts", "swift")
name := fi.Name()
// TODO we maybe need to try check if the sub dir named after the build pack matches first?
if name != "preview" && name != ".git" {
oldChartsDir = filepath.Join(chartsDir, name)
break
}
}
return nil

}); err != nil {
return fmt.Errorf("error matching a jenkins x draft pack name with chart folder %v", err)
}

if oldChartsDir != "" {
// chart expects folder name to be the same as app name
newChartsDir := filepath.Join(dir, "charts", o.AppName)
Expand Down
25 changes: 25 additions & 0 deletions pkg/spring/model.go
Expand Up @@ -24,6 +24,7 @@ const (
OptionPackaging = "packaging"
OptionDependency = "dep"
OptionDependencyKind = "kind"
OptionType = "type"

startSpringURL = "http://start.spring.io"
)
Expand Down Expand Up @@ -65,6 +66,7 @@ type SpringBootModel struct {
Language SpringOptions
JavaVersion SpringOptions
BootVersion SpringOptions
Type SpringOptions
GroupId SpringValue
ArtifactId SpringValue
Version SpringValue
Expand All @@ -86,6 +88,7 @@ type SpringBootForm struct {
PackageName string
Dependencies []string
DependencyKinds []string
Type string
}

func LoadSpringBoot(cacheDir string) (*SpringBootModel, error) {
Expand Down Expand Up @@ -118,6 +121,24 @@ func LoadSpringBoot(cacheDir string) (*SpringBootModel, error) {
if err != nil {
return nil, err
}
// default the build tool
if model.Type.Default == "" {
model.Type.Default = "maven"
}
if len(model.Type.Values) == 0 {
model.Type.Values = []SpringOption{
{
ID: "gradle",
Name: "Gradle",
Description: "Build with the gradle build tool",
},
{
ID: "maven",
Name: "Maven",
Description: "Build with the maven build tool",
},
}
}
return &model, nil
}

Expand Down Expand Up @@ -159,6 +180,9 @@ func (model *SpringBootModel) CreateSurvey(data *SpringBootForm, advanced bool,
if data.Packaging == "" && advanced {
qs = append(qs, CreateValueSelect("Packaging", "packaging", &model.Packaging, data))
}
if data.Type == "" && advanced {
qs = append(qs, CreateValueSelect("Build Tool", "type", &model.Type, data))
}
if data.GroupId == "" {
qs = append(qs, CreateValueInput("Group", "groupId", &model.GroupId, data))
}
Expand Down Expand Up @@ -343,6 +367,7 @@ func (data *SpringBootForm) AddFormValues(form *url.Values) {
AddFormValue(form, "artifactId", data.ArtifactId)
AddFormValue(form, "version", data.Version)
AddFormValue(form, "name", data.Name)
AddFormValue(form, "type", data.Type)
AddFormValues(form, "dependencies", data.Dependencies)
}

Expand Down

0 comments on commit 2667978

Please sign in to comment.