Skip to content

Commit

Permalink
update default java package to io.dagger.modules.<usermodule>
Browse files Browse the repository at this point in the history
That way we avoid "samples" and there's no conflict with the default
package io.dagger.module

Signed-off-by: Yves Brissaud <gh@lgtd.io>
  • Loading branch information
eunomie committed Jan 30, 2025
1 parent caf4f0b commit fd9472a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions sdk/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ my-java-module
└── java
└── io
└── dagger
└── sample
└── module
└── modules
└── myjavamodule
├── MyJavaModule.java
└── package-info.java

Expand All @@ -56,7 +56,7 @@ grep-dir Returns lines that match a pattern in the files of the provided
```

```console
$ dagger call -q -m my-java-module container-echo "hello dagger" stdout
$ dagger call -q -m my-java-module container-echo --string-arg "hello dagger" stdout

hello dagger

Expand Down
25 changes: 16 additions & 9 deletions sdk/java/runtime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func (m *JavaSdk) addTemplate(
ctr *dagger.Container,
) (*dagger.Container, error) {
name := m.moduleConfig.name
snakeName := strcase.ToSnake(name)
pkgName := strings.ReplaceAll(strings.ReplaceAll(strings.ToLower(name), "-", ""), "_", "")
kebabName := strcase.ToKebab(name)
camelName := strcase.ToCamel(name)

// Check if there's a pom.xml inside the module path. If a file exist, no need to add the templates
Expand All @@ -160,32 +161,38 @@ func (m *JavaSdk) addTemplate(
return filepath.Join(append([]string{m.moduleConfig.modulePath()}, rel...)...)
}

changes := []repl{
{"dagger-module", kebabName},
{"daggermodule", pkgName},
}

// Edit template content so that they match the dagger module name
templateDir := dag.CurrentModule().Source().Directory("template")
pomXML, err := m.replace(ctx, templateDir,
"pom.xml",
repl{"dagger-module", snakeName})
"pom.xml", changes...)
if err != nil {
return ctr, fmt.Errorf("could not add template: %w", err)
}

changes = append(changes, repl{"DaggerModule", camelName})
daggerModuleJava, err := m.replace(ctx, templateDir,
filepath.Join("src", "main", "java", "io", "dagger", "sample", "module", "DaggerModule.java"),
repl{"DaggerModule", camelName}, repl{"dagger-module-name", name})
filepath.Join("src", "main", "java", "io", "dagger", "modules", "daggermodule", "DaggerModule.java"),
changes...)
if err != nil {
return ctr, fmt.Errorf("could not add template: %w", err)
}
packageInfoJava, err := m.replace(ctx, templateDir,
filepath.Join("src", "main", "java", "io", "dagger", "sample", "module", "package-info.java"),
repl{"DaggerModule", camelName}, repl{"dagger-module-name", name})
filepath.Join("src", "main", "java", "io", "dagger", "modules", "daggermodule", "package-info.java"),
changes...)
if err != nil {
return ctr, fmt.Errorf("could not add template: %w", err)
}

// And copy them to the container, renamed to match the dagger module name
ctr = ctr.
WithNewFile(absPath("pom.xml"), pomXML).
WithNewFile(absPath("src", "main", "java", "io", "dagger", "sample", "module", fmt.Sprintf("%s.java", camelName)), daggerModuleJava).
WithNewFile(absPath("src", "main", "java", "io", "dagger", "sample", "module", "package-info.java"), packageInfoJava)
WithNewFile(absPath("src", "main", "java", "io", "dagger", "modules", pkgName, fmt.Sprintf("%s.java", camelName)), daggerModuleJava).
WithNewFile(absPath("src", "main", "java", "io", "dagger", "modules", pkgName, "package-info.java"), packageInfoJava)

return ctr, nil
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/java/runtime/template/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.dagger.sample</groupId>
<groupId>io.dagger.modules.daggermodule</groupId>
<artifactId>dagger-module</artifactId>
<version>1.0-SNAPSHOT</version>
<name>dagger-module</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.dagger.sample.module;
package io.dagger.modules.daggermodule;

import io.dagger.client.Container;
import io.dagger.client.DaggerQueryException;
Expand All @@ -11,7 +11,7 @@
import java.util.concurrent.ExecutionException;

/** DaggerModule main object */
@Object(value = "dagger-module-name")
@Object
public class DaggerModule extends Base {
public DaggerModule() {
super();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** DaggerModule example */
@Module
package io.dagger.sample.module;
package io.dagger.modules.daggermodule;

import io.dagger.module.annotation.Module;

0 comments on commit fd9472a

Please sign in to comment.