Skip to content

Commit

Permalink
refactor: add ProjectConfig, ProjectKey
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Mar 20, 2024
1 parent 198e0e2 commit 35139bb
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 184 deletions.
4 changes: 2 additions & 2 deletions buildengine/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func Build(ctx context.Context, sch *schema.Schema, module Module) error {
logger := log.FromContext(ctx).Scope(module.Module)
ctx = log.ContextWithLogger(ctx, logger)
logger.Infof("Building module")
switch module.Language() {
switch module.Language {
case "go":
return buildGo(ctx, sch, module)

case "kotlin":
return buildKotlin(ctx, sch, module)

default:
return fmt.Errorf("unknown language %q", module.Language())
return fmt.Errorf("unknown language %q", module.Language)
}
}
4 changes: 2 additions & 2 deletions buildengine/build_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func buildGo(ctx context.Context, sch *schema.Schema, module Module) error {
if err := compile.Build(ctx, module.Dir(), sch); err != nil {
return fmt.Errorf("failed to build module %s: %w", module.Key(), err)
if err := compile.Build(ctx, module.Dir, sch); err != nil {
return fmt.Errorf("failed to build %q: %w", module, err)
}
return nil
}
9 changes: 5 additions & 4 deletions buildengine/build_kotlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (e externalModuleContext) ExternalModules() []*schema.Module {

func buildKotlin(ctx context.Context, sch *schema.Schema, module Module) error {
logger := log.FromContext(ctx)
if err := SetPOMProperties(ctx, module.Dir()); err != nil {
if err := SetPOMProperties(ctx, module.Dir); err != nil {
return fmt.Errorf("unable to update ftl.version in %s: %w", module.Dir, err)
}

Expand All @@ -57,7 +57,7 @@ func buildKotlin(ctx context.Context, sch *schema.Schema, module Module) error {
}

logger.Debugf("Using build command '%s'", module.Build)
err := exec.Command(ctx, log.Debug, module.Dir(), "bash", "-c", module.Build).RunBuffered(ctx)
err := exec.Command(ctx, log.Debug, module.Dir, "bash", "-c", module.Build).RunBuffered(ctx)
if err != nil {
return fmt.Errorf("failed to build module %s: %w", module.Module, err)
}
Expand Down Expand Up @@ -128,12 +128,13 @@ exec java -cp "classes:$(cat classpath.txt)" xyz.block.ftl.main.MainKt
func generateExternalModules(ctx context.Context, project Project, sch *schema.Schema) error {
logger := log.FromContext(ctx)
funcs := maps.Clone(scaffoldFuncs)
config := project.Config()

// Wipe the modules directory to ensure we don't have any stale modules.
_ = os.RemoveAll(filepath.Join(project.Dir(), "target", "generated-sources", "ftl"))
_ = os.RemoveAll(filepath.Join(config.Dir, "target", "generated-sources", "ftl"))

logger.Debugf("Generating external modules")
return internal.ScaffoldZip(kotlinruntime.ExternalModuleTemplates(), project.Dir(), externalModuleContext{
return internal.ScaffoldZip(kotlinruntime.ExternalModuleTemplates(), config.Dir, externalModuleContext{
project: project,
Schema: sch,
}, scaffolder.Exclude("^go.mod$"), scaffolder.Functions(funcs))
Expand Down
21 changes: 11 additions & 10 deletions buildengine/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// Project with those dependencies populated.
func UpdateDependencies(ctx context.Context, project Project) (Project, error) {
logger := log.FromContext(ctx)
logger.Debugf("Extracting dependencies for module %s", project.Key())
logger.Debugf("Extracting dependencies for %s", project)
dependencies, err := extractDependencies(project)
if err != nil {
return Project(&Module{}), err
Expand All @@ -43,24 +43,25 @@ func UpdateDependencies(ctx context.Context, project Project) (Project, error) {
return out, nil
}

func extractDependencies(project Project) ([]string, error) {
func extractDependencies(project Project) ([]ProjectKey, error) {
config := project.Config()
name := ""
if config, ok := project.(Module); ok {
name = config.Module
}
switch project.Language() {
switch config.Language {
case "go":
return extractGoFTLImports(name, project.Dir())
return extractGoFTLImports(name, config.Dir)

case "kotlin":
return extractKotlinFTLImports(name, project.Dir())
return extractKotlinFTLImports(name, config.Dir)

default:
return nil, fmt.Errorf("unsupported language: %s", project.Language())
return nil, fmt.Errorf("unsupported language: %s", config.Language)
}
}

func extractGoFTLImports(self, dir string) ([]string, error) {
func extractGoFTLImports(self, dir string) ([]ProjectKey, error) {
dependencies := map[string]bool{}
fset := token.NewFileSet()
err := WalkDir(dir, func(path string, d fs.DirEntry) error {
Expand Down Expand Up @@ -99,10 +100,10 @@ func extractGoFTLImports(self, dir string) ([]string, error) {
}
modules := maps.Keys(dependencies)
sort.Strings(modules)
return modules, nil
return ProjectKeysFromModuleNames(modules), nil
}

func extractKotlinFTLImports(self, dir string) ([]string, error) {
func extractKotlinFTLImports(self, dir string) ([]ProjectKey, error) {
dependencies := map[string]bool{}
kotlinImportRegex := regexp.MustCompile(`^import ftl\.([A-Za-z0-9_.]+)`)

Expand Down Expand Up @@ -138,5 +139,5 @@ func extractKotlinFTLImports(self, dir string) ([]string, error) {
}
modules := maps.Keys(dependencies)
sort.Strings(modules)
return modules, nil
return ProjectKeysFromModuleNames(modules), nil
}
2 changes: 1 addition & 1 deletion buildengine/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func discoverModules(ctx context.Context, dirs ...string) ([]Module, error) {
}
}
sort.Slice(out, func(i, j int) bool {
return out[i].Key() < out[j].Key()
return out[i].Config().Key < out[j].Config().Key
})
return out, nil
}
Loading

0 comments on commit 35139bb

Please sign in to comment.