diff --git a/cmd/ftl/cmd_init.go b/cmd/ftl/cmd_init.go index e48001c077..72a2fbdfdb 100644 --- a/cmd/ftl/cmd_init.go +++ b/cmd/ftl/cmd_init.go @@ -2,14 +2,19 @@ package main import ( "archive/zip" + "html/template" "os" "path/filepath" + "reflect" + "strings" "github.com/alecthomas/errors" + "github.com/iancoleman/strcase" goruntime "github.com/TBD54566975/ftl/go-runtime" "github.com/TBD54566975/ftl/internal" kotlinruntime "github.com/TBD54566975/ftl/kotlin-runtime" + "github.com/TBD54566975/scaffolder" ) type initCmd struct { @@ -33,8 +38,8 @@ func (i initGoCmd) Run(parent *initCmd) error { type initKotlinCmd struct { GroupID string `short:"g" help:"Base Maven group ID (defaults to \"ftl\")." default:"ftl"` ArtifactID string `short:"a" help:"Base Maven artifact ID (defaults to \"ftl\")." default:"ftl"` - Name string `short:"n" help:"Name of the FTL module (defaults to name of directory)."` - Dir string `arg:"" default:"." help:"Directory to initialize the module in."` + Dir string `arg:"" help:"Directory to initialize the module in."` + Name string `arg:"" help:"Name of the FTL module to create underneath the base directory."` } func (i *initKotlinCmd) Run(parent *initCmd) error { @@ -45,20 +50,36 @@ func (i *initKotlinCmd) Run(parent *initCmd) error { } func scaffold(reader *zip.Reader, hermit bool, dir string, ctx any) error { - err := internal.UnzipDir(reader, dir) + tmpDir, err := os.MkdirTemp("", "ftl-init-*") if err != nil { return errors.WithStack(err) } - if err := os.Remove(filepath.Join(dir, "go.mod")); err != nil { - return errors.WithStack(err) - } - if err := internal.Scaffold(dir, ctx); err != nil { + defer os.RemoveAll(tmpDir) + err = internal.UnzipDir(reader, tmpDir) + if err != nil { return errors.WithStack(err) } + opts := []scaffolder.Option{scaffolder.Functions(scaffoldFuncs), scaffolder.Exclude("go.mod")} if !hermit { - if err := os.RemoveAll(filepath.Join(dir, "bin")); err != nil { - return errors.WithStack(err) - } + opts = append(opts, scaffolder.Exclude("bin")) + } + if err := scaffolder.Scaffold(tmpDir, dir, ctx, opts...); err != nil { + return errors.Wrap(err, "failed to scaffold") } return nil } + +var scaffoldFuncs = template.FuncMap{ + "snake": strcase.ToSnake, + "screamingSnake": strcase.ToScreamingSnake, + "camel": strcase.ToCamel, + "lowerCamel": strcase.ToLowerCamel, + "kebab": strcase.ToKebab, + "screamingKebab": strcase.ToScreamingKebab, + "upper": strings.ToUpper, + "lower": strings.ToLower, + "title": strings.Title, + "typename": func(v any) string { + return reflect.Indirect(reflect.ValueOf(v)).Type().Name() + }, +} diff --git a/cmd/ftl/cmd_schema.go b/cmd/ftl/cmd_schema.go index 15469dc3ce..1fa4096e2c 100644 --- a/cmd/ftl/cmd_schema.go +++ b/cmd/ftl/cmd_schema.go @@ -11,17 +11,16 @@ import ( "connectrpc.com/connect" "github.com/alecthomas/errors" "github.com/golang/protobuf/proto" - "github.com/otiai10/copy" "github.com/radovskyb/watcher" "golang.org/x/exp/maps" "golang.org/x/sync/errgroup" "github.com/TBD54566975/ftl/backend/common/log" "github.com/TBD54566975/ftl/backend/schema" - "github.com/TBD54566975/ftl/internal" ftlv1 "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1" "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/ftlv1connect" schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema" + "github.com/TBD54566975/scaffolder" ) type schemaCmd struct { @@ -177,10 +176,7 @@ func (s *schemaGenerateCmd) regenerateModules(logger *log.Logger, modules []*sch return errors.WithStack(err) } for _, module := range modules { - if err := copy.Copy(s.Template, s.Dest); err != nil { - return errors.WithStack(err) - } - if err := internal.Scaffold(s.Dest, module); err != nil { + if err := scaffolder.Scaffold(s.Template, s.Dest, module, scaffolder.Functions(scaffoldFuncs)); err != nil { return errors.WithStack(err) } } diff --git a/go.mod b/go.mod index 8542fc5ffb..59ced25aee 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( ) require ( - github.com/alecthomas/repr v0.2.0 // indirect + github.com/alecthomas/repr v0.3.0 // indirect github.com/alessio/shellescape v1.4.1 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/danieljoos/wincred v1.1.0 // indirect @@ -61,8 +61,8 @@ require ( require ( connectrpc.com/grpcreflect v1.2.0 connectrpc.com/otelconnect v0.5.0 - github.com/TBD54566975/scaffolder v0.0.0-20231026011825-304b47f65b46 - github.com/alecthomas/assert/v2 v2.3.0 + github.com/TBD54566975/scaffolder v0.3.0 + github.com/alecthomas/assert/v2 v2.4.0 github.com/alecthomas/atomic v0.1.0-alpha2 github.com/alecthomas/concurrency v0.0.2 github.com/alecthomas/errors v0.4.0 diff --git a/go.sum b/go.sum index 977581be8f..ccf692f908 100644 --- a/go.sum +++ b/go.sum @@ -6,10 +6,10 @@ connectrpc.com/otelconnect v0.5.0 h1:K7xQKFbgeaHx563B+IIbd1EJe856AanueIYtGEtdnH8 connectrpc.com/otelconnect v0.5.0/go.mod h1:cjBMmtJmTokg4/k/3iDjLOjfNVM4qSVfIWz/qWQ8FNw= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= -github.com/TBD54566975/scaffolder v0.0.0-20231026011825-304b47f65b46 h1:dy1l8sMkoRjVzKvBvozujrL9VNYw3G8H+Tb+EB/p+qs= -github.com/TBD54566975/scaffolder v0.0.0-20231026011825-304b47f65b46/go.mod h1:qQeIiuDDE901L32T7X/S36aIPdf4Qkxfm9qmF7+I2Wg= -github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0= -github.com/alecthomas/assert/v2 v2.3.0/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= +github.com/TBD54566975/scaffolder v0.3.0 h1:3DEUeyIVl/RwFznKXmWxr/Wk9OnSne77Ze/VXStf2Ro= +github.com/TBD54566975/scaffolder v0.3.0/go.mod h1:e8zjmmKaRNRah9TR+Td0EmyT1tsSfZezPWdSEbK94qk= +github.com/alecthomas/assert/v2 v2.4.0 h1:/ZiZ0NnriAWPYYO+4eOjgzNELrFQLaHNr92mHSHFj9U= +github.com/alecthomas/assert/v2 v2.4.0/go.mod h1:fw5suVxB+wfYJ3291t0hRTqtGzFYdSwstnRQdaQx2DM= github.com/alecthomas/atomic v0.1.0-alpha2 h1:dqwXmax66gXvHhsOS4pGPZKqYOlTkapELkLb3MNdlH8= github.com/alecthomas/atomic v0.1.0-alpha2/go.mod h1:zD6QGEyw49HIq19caJDc2NMXAy8rNi9ROrxtMXATfyI= github.com/alecthomas/concurrency v0.0.2 h1:Q3kGPtLbleMbH9lHX5OBFvJygfyFw29bXZKBg+IEVuo= @@ -22,8 +22,8 @@ github.com/alecthomas/kong-toml v0.0.1 h1:6E+1OdpSavFzA4IO/aczkaG9IK7fZ0Ce1yobHt github.com/alecthomas/kong-toml v0.0.1/go.mod h1:d0aHX/kbCSdgQSg7ZUQzSTbBCh02sjSu8RT3YF2TKzA= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= github.com/alecthomas/participle/v2 v2.0.0/go.mod h1:rAKZdJldHu8084ojcWevWAL8KmEU+AT+Olodb+WoN2Y= -github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk= -github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= +github.com/alecthomas/repr v0.3.0 h1:NeYzUPfjjlqHY4KtzgKJiWd6sVq2eNUPTi34PiFGjY8= +github.com/alecthomas/repr v0.3.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/alecthomas/types v0.7.1 h1:zU49e5jp0MS4sVDq0yEX4uX/jX7aVpvwd+Rm632ZpEQ= github.com/alecthomas/types v0.7.1/go.mod h1:t7PnU03TVweFpbPVKaeLtFykjJD8rqiBJ7gfkp6UvLQ= github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0= diff --git a/internal/scaffolder.go b/internal/scaffolder.go deleted file mode 100644 index a541b5e17a..0000000000 --- a/internal/scaffolder.go +++ /dev/null @@ -1,30 +0,0 @@ -package internal - -import ( - "reflect" - "strings" - "text/template" - - "github.com/TBD54566975/scaffolder" - "github.com/alecthomas/errors" - "github.com/iancoleman/strcase" -) - -// Scaffold evaluates the scaffolding files at the given destination against -// ctx. -func Scaffold(destination string, ctx any) error { - return errors.WithStack(scaffolder.Scaffold(destination, ctx, scaffolder.Functions(template.FuncMap{ - "snake": strcase.ToSnake, - "screamingSnake": strcase.ToScreamingSnake, - "camel": strcase.ToCamel, - "lowerCamel": strcase.ToLowerCamel, - "kebab": strcase.ToKebab, - "screamingKebab": strcase.ToScreamingKebab, - "upper": strings.ToUpper, - "lower": strings.ToLower, - "title": strings.Title, - "typename": func(v any) string { - return reflect.Indirect(reflect.ValueOf(v)).Type().Name() - }, - }))) -} diff --git a/scripts/integration-tests b/scripts/integration-tests index 9b67aeaeef..1aa2c3a683 100755 --- a/scripts/integration-tests +++ b/scripts/integration-tests @@ -60,8 +60,8 @@ deploy_echo_kotlin() ( deploy_fresh_kotlin() ( info "Deploying newly initialised Kotlin module" rm -rf build/echo2 - ftl init kotlin -n echo2 build/echo2 - cd build/echo2/ftl-module-echo2 + ftl init kotlin build/modules echo2 + cd build/modules/ftl-module-echo2 mvn compile ftl deploy target )