Skip to content

Commit

Permalink
feat: switch to new scaffolder API
Browse files Browse the repository at this point in the history
This resolves an issue where scaffolding into an existing directory
would evaluate all of the existing files too. It also fixes
use of --no-hermit.
  • Loading branch information
alecthomas committed Nov 14, 2023
1 parent 7aa1726 commit f92db20
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 57 deletions.
41 changes: 31 additions & 10 deletions cmd/ftl/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
},
}
8 changes: 2 additions & 6 deletions cmd/ftl/cmd_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions go.sum

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

30 changes: 0 additions & 30 deletions internal/scaffolder.go

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/integration-tests
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit f92db20

Please sign in to comment.