Skip to content

Commit

Permalink
Update pom.xml when running ftl init kotlin on an existing directory
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Nov 14, 2023
1 parent 8bf4c9a commit e2f7df1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 11 deletions.
86 changes: 76 additions & 10 deletions cmd/ftl/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"reflect"
"strings"

"github.com/TBD54566975/scaffolder"
"github.com/alecthomas/errors"
"github.com/beevik/etree"
"github.com/iancoleman/strcase"

goruntime "github.com/TBD54566975/ftl/go-runtime"
"github.com/TBD54566975/scaffolder"

"github.com/TBD54566975/ftl/internal"
kotlinruntime "github.com/TBD54566975/ftl/kotlin-runtime"
)
Expand All @@ -32,7 +33,13 @@ func (i initGoCmd) Run(parent *initCmd) error {
if i.Name == "" {
i.Name = filepath.Base(i.Dir)
}
return errors.WithStack(scaffold(goruntime.Files, parent.Hermit, i.Dir, i))
tmpDir, err := unzipToTmpDir(kotlinruntime.Files)
if err != nil {
return errors.Wrap(err, "failed to unzip kotlin runtime")
}
defer os.RemoveAll(tmpDir)

return errors.WithStack(scaffold(parent.Hermit, tmpDir, i.Dir, i))
}

type initKotlinCmd struct {
Expand All @@ -42,28 +49,87 @@ type initKotlinCmd struct {
Name string `arg:"" help:"Name of the FTL module to create underneath the base directory."`
}

func (i *initKotlinCmd) Run(parent *initCmd) error {
func (i initKotlinCmd) Run(parent *initCmd) error {
if i.Name == "" {
i.Name = filepath.Base(i.Dir)
}
return errors.WithStack(scaffold(kotlinruntime.Files, parent.Hermit, i.Dir, i))

if _, err := os.Stat(filepath.Join(i.Dir, i.Name)); err == nil {
return errors.Errorf("module directory %s already exists", filepath.Join(i.Dir, i.Name))
}

options := []scaffolder.Option{}

// Update root POM if it already exists.
pomFile := filepath.Join(i.Dir, "pom.xml")
if _, err := os.Stat(pomFile); err == nil {
options = append(options, scaffolder.Exclude("pom.xml"))
if err := updatePom(pomFile, i.Name); err != nil {
return errors.WithStack(err)
}
}

tmpDir, err := unzipToTmpDir(kotlinruntime.Files)
if err != nil {
return errors.Wrap(err, "failed to unzip kotlin runtime")
}
defer os.RemoveAll(tmpDir)

return errors.WithStack(scaffold(parent.Hermit, tmpDir, i.Dir, initKotlinContext{initKotlinCmd: i}, options...))
}

func scaffold(reader *zip.Reader, hermit bool, dir string, ctx any) error {
tmpDir, err := os.MkdirTemp("", "ftl-init-*")
func updatePom(pomFile, name string) error {
tree := etree.NewDocument()
err := tree.ReadFromFile(pomFile)
if err != nil {
return errors.WithStack(err)
}
defer os.RemoveAll(tmpDir)
err = internal.UnzipDir(reader, tmpDir)

// Add new module entry to root of XML file
root := tree.Root()
modules := root.SelectElement("modules")
if modules == nil {
modules = root.CreateElement("modules")
}
modules.CreateText(" ")
module := modules.CreateElement("module")
module.SetText("ftl-module-" + name)
modules.CreateText("\n ")

// Write updated XML file back to disk
err = tree.WriteToFile(pomFile)
if err != nil {
return errors.WithStack(err)
}
return nil
}

type initKotlinContext struct {
initKotlinCmd
Modules []string
Prelude string
Postlude string
}

func unzipToTmpDir(reader *zip.Reader) (string, error) {
tmpDir, err := os.MkdirTemp("", "ftl-init-*")
if err != nil {
return "", errors.WithStack(err)
}
err = internal.UnzipDir(reader, tmpDir)
if err != nil {
return "", errors.WithStack(err)
}
return tmpDir, nil
}

func scaffold(hermit bool, source string, destination string, ctx any, options ...scaffolder.Option) error {
opts := []scaffolder.Option{scaffolder.Functions(scaffoldFuncs), scaffolder.Exclude("go.mod")}
if !hermit {
opts = append(opts, scaffolder.Exclude("bin"))
}
if err := scaffolder.Scaffold(tmpDir, dir, ctx, opts...); err != nil {
opts = append(opts, options...)
if err := scaffolder.Scaffold(source, destination, ctx, opts...); err != nil {
return errors.Wrap(err, "failed to scaffold")
}
return nil
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ require (
github.com/alecthomas/errors v0.4.0
github.com/alecthomas/participle/v2 v2.0.0
github.com/alecthomas/types v0.7.1
github.com/beevik/etree v1.2.0
github.com/deckarep/golang-set/v2 v2.3.0
github.com/gofrs/flock v0.8.1
github.com/iancoleman/strcase v0.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<version>1.0-SNAPSHOT</version>
</parent>

<properties>
<ftlModuleName>{{ .Name | lower }}</ftlModuleName>
</properties>

<build>
<plugins>
<plugin>
Expand Down
2 changes: 1 addition & 1 deletion kotlin-runtime/scaffolding/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<argument>target/dependency/ftl-generator.jar</argument>
<argument>--endpoint=http://127.0.0.1:8892</argument>
<argument>--dest=${project.build.directory}</argument>
<argument>--module={{ .Name | camel | lower }}</argument>
<argument>--module=${ftlModuleName}</argument>
<argument>--module-client-suffix=ModuleClient</argument>
</arguments>
</configuration>
Expand Down

0 comments on commit e2f7df1

Please sign in to comment.