Skip to content

Commit

Permalink
chore: make ftl init create self contained kotlin modules
Browse files Browse the repository at this point in the history
fixes #933
  • Loading branch information
worstell committed Mar 2, 2024
1 parent 9a8df99 commit aa99ca4
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 193 deletions.
2 changes: 1 addition & 1 deletion buildengine/build_kotlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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, filepath.Join(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 Down
48 changes: 5 additions & 43 deletions cmd/ftl/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
"path/filepath"
"strings"

"github.com/TBD54566975/scaffolder"
"github.com/beevik/etree"

"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/backend/schema/strcase"
"github.com/TBD54566975/ftl/buildengine"
Expand All @@ -22,6 +19,7 @@ import (
"github.com/TBD54566975/ftl/internal/exec"
"github.com/TBD54566975/ftl/internal/log"
kotlinruntime "github.com/TBD54566975/ftl/kotlin-runtime"
"github.com/TBD54566975/scaffolder"
)

type initCmd struct {
Expand Down Expand Up @@ -72,52 +70,16 @@ func (i initKotlinCmd) Run(ctx context.Context, parent *initCmd) error {
return fmt.Errorf("module name %q is invalid", i.Name)
}

if _, err := os.Stat(filepath.Join(i.Dir, "ftl-module-"+i.Name)); err == nil {
moduleDir := filepath.Join(i.Dir, i.Name)
if _, err := os.Stat(moduleDir); err == nil {
return fmt.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 err
}
}

if err := scaffold(parent.Hermit, kotlinruntime.Files(), i.Dir, i, options...); err != nil {
if err := scaffold(parent.Hermit, kotlinruntime.Files(), i.Dir, i); err != nil {
return err
}

return buildengine.SetPOMProperties(ctx, i.Dir)
}

func updatePom(pomFile, name string) error {
tree := etree.NewDocument()
err := tree.ReadFromFile(pomFile)
if err != nil {
return err
}

// 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 err
}
return nil
return buildengine.SetPOMProperties(ctx, moduleDir)
}

func unzipToTmpDir(reader *zip.Reader) (string, error) {
Expand Down
File renamed without changes.
44 changes: 29 additions & 15 deletions examples/kotlin/pom.xml → examples/kotlin/echo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@
<modelVersion>4.0.0</modelVersion>

<groupId>ftl</groupId>
<artifactId>ftl</artifactId>
<artifactId>echo2</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>pom</packaging>

<modules>
<module>ftl-module-echo</module>
<module>ftl-module-time</module>
</modules>

<properties>
<ftlModuleName>echo</ftlModuleName>
<ftl.version>1.0-SNAPSHOT</ftl.version>
<ftlEndpoint>http://localhost:8892</ftlEndpoint>
<ftlEndpoint>http://127.0.0.1:8892</ftlEndpoint>
<java.version>1.8</java.version>
<kotlin.version>1.9.0</kotlin.version>
<kotlin.version>1.9.22</kotlin.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Expand All @@ -37,7 +31,7 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.1</version>
<version>42.7.2</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -76,7 +70,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<version>3.6.1</version>
<executions>
<!-- Copy all dependencies into the target directory -->
<execution>
Expand Down Expand Up @@ -119,7 +113,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<version>3.5.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
Expand All @@ -137,7 +131,7 @@
<plugin>
<groupId>com.github.ozsie</groupId>
<artifactId>detekt-maven-plugin</artifactId>
<version>1.23.3.1</version>
<version>1.23.5</version>
<configuration>
<disableDefaultRuleSets>true</disableDefaultRuleSets>
<classPath>${generated.classpath}</classPath>
Expand Down Expand Up @@ -169,5 +163,25 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<!-- Add generated sources to the classpath -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
<!-- Extract schema -->
<plugin>
<groupId>com.github.ozsie</groupId>
<artifactId>detekt-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
</project>
41 changes: 0 additions & 41 deletions examples/kotlin/ftl-module-echo/pom.xml

This file was deleted.

41 changes: 0 additions & 41 deletions examples/kotlin/ftl-module-time/pom.xml

This file was deleted.

File renamed without changes.
Loading

0 comments on commit aa99ca4

Please sign in to comment.