Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make ftl init create self contained kotlin modules #1005

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions 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 All @@ -61,7 +61,7 @@ func buildKotlin(ctx context.Context, sch *schema.Schema, module Module) error {
return nil
}

// SetPOMProperties updates the ftl.version and ftlEndpoint properties in the
// SetPOMProperties updates the ftl.version properties in the
// pom.xml file in the given base directory.
func SetPOMProperties(ctx context.Context, baseDir string) error {
logger := log.FromContext(ctx)
Expand All @@ -70,11 +70,6 @@ func SetPOMProperties(ctx context.Context, baseDir string) error {
ftlVersion = "1.0-SNAPSHOT"
}

ftlEndpoint := os.Getenv("FTL_ENDPOINT")
if ftlEndpoint == "" {
ftlEndpoint = "http://127.0.0.1:8892"
}

pomFile := filepath.Clean(filepath.Join(baseDir, "pom.xml"))

logger.Debugf("Setting ftl.version in %s to %s", pomFile, ftlVersion)
Expand All @@ -94,13 +89,6 @@ func SetPOMProperties(ctx context.Context, baseDir string) error {
}
version.SetText(ftlVersion)

endpoint := properties.SelectElement("ftlEndpoint")
if endpoint == nil {
logger.Warnf("unable to find <properties>/<ftlEndpoint> in %s", pomFile)
} else {
endpoint.SetText(ftlEndpoint)
}

return tree.WriteToFile(pomFile)
}

Expand Down
45 changes: 4 additions & 41 deletions cmd/ftl/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

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

"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/backend/schema/strcase"
Expand Down Expand Up @@ -72,52 +71,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 {
return err
}

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

func updatePom(pomFile, name string) error {
tree := etree.NewDocument()
err := tree.ReadFromFile(pomFile)
if err != nil {
if err := scaffold(parent.Hermit, kotlinruntime.Files(), i.Dir, i); 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
42 changes: 27 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,13 @@
<modelVersion>4.0.0</modelVersion>

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

<packaging>pom</packaging>

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

<properties>
<ftl.version>1.0-SNAPSHOT</ftl.version>
<ftlEndpoint>http://localhost: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 +29,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 +68,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 +111,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 +129,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 +161,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.

Loading