From e2f7df1fbaed3006380e6af885c9c0df1ebb4108 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Tue, 14 Nov 2023 16:06:20 +1100 Subject: [PATCH] Update pom.xml when running `ftl init kotlin` on an existing directory --- cmd/ftl/cmd_init.go | 86 ++++++++++++++++--- go.mod | 1 + go.sum | 2 + .../ftl-module-{{ .Name | lower }}/pom.xml | 4 + kotlin-runtime/scaffolding/pom.xml | 2 +- 5 files changed, 84 insertions(+), 11 deletions(-) diff --git a/cmd/ftl/cmd_init.go b/cmd/ftl/cmd_init.go index 2ce75f2c08..a97aecd8ea 100644 --- a/cmd/ftl/cmd_init.go +++ b/cmd/ftl/cmd_init.go @@ -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" ) @@ -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 { @@ -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 diff --git a/go.mod b/go.mod index 59ced25aee..86fa90f425 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ccf692f908..838a981a51 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,8 @@ github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVK github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= github.com/amacneil/dbmate/v2 v2.7.0 h1:aaTyfVPxf01KyIx5E0i/OATygrit2hjivyKSX0YSJxI= github.com/amacneil/dbmate/v2 v2.7.0/go.mod h1:I13evRylGros6OXuJij+oskvr+YMaPvSD9Z7PNucXWA= +github.com/beevik/etree v1.2.0 h1:l7WETslUG/T+xOPs47dtd6jov2Ii/8/OjCldk5fYfQw= +github.com/beevik/etree v1.2.0/go.mod h1:aiPf89g/1k3AShMVAzriilpcE4R/Vuor90y83zVZWFc= github.com/bool64/dev v0.2.31 h1:OS57EqYaYe2M/2bw9uhDCIFiZZwywKFS/4qMLN6JUmQ= github.com/bool64/dev v0.2.31/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E= diff --git a/kotlin-runtime/scaffolding/ftl-module-{{ .Name | lower }}/pom.xml b/kotlin-runtime/scaffolding/ftl-module-{{ .Name | lower }}/pom.xml index 76bbd57d2c..5b6c7cafee 100644 --- a/kotlin-runtime/scaffolding/ftl-module-{{ .Name | lower }}/pom.xml +++ b/kotlin-runtime/scaffolding/ftl-module-{{ .Name | lower }}/pom.xml @@ -12,6 +12,10 @@ 1.0-SNAPSHOT + + {{ .Name | lower }} + + diff --git a/kotlin-runtime/scaffolding/pom.xml b/kotlin-runtime/scaffolding/pom.xml index f7dfe6ddf9..48c7f36176 100644 --- a/kotlin-runtime/scaffolding/pom.xml +++ b/kotlin-runtime/scaffolding/pom.xml @@ -136,7 +136,7 @@ target/dependency/ftl-generator.jar --endpoint=http://127.0.0.1:8892 --dest=${project.build.directory} - --module={{ .Name | camel | lower }} + --module=${ftlModuleName} --module-client-suffix=ModuleClient