Skip to content

Commit

Permalink
Default to using gradle0nexus in gen-sdk
Browse files Browse the repository at this point in the history
When using `pulumi-java-gen`, the user can bass a `—build` flag which the following options:

* `none`: no gradle file is generated (the default)
* `gradle`: a gradle file is generated -> BuildFiles: gradle
* `gradle-nexus`: a gradle file using the nexus publishing plugin is generated, using a default version of the plugin -> BuildFiles: gradle, GradleNexusPublishPluginVersion: 2.0.0
* `gradle-nexus:$VER`: a gradle file using the nexus publishing plugin is generated, using the version $VER for the plugin -> BuildFiles: gradle, GradleNexusPublishPluginVersion: $VER

Using `gen-sdk`, the options are managed via the schema, `buildFiles` can take the following options:
* `gradle-nexus`: the default if the buildFiles is not set, a gradle file using the nexus publishing plugin is generated, implies `gradleNexusPublishPluginVersion: 2.0.0` if `gradleNexusPublishPluginVersionA` is not set.
* `gradle`: a gradle file is generated, but without the nexus publishing plugin
* `none`: no gradle file is generated

Fixes #1593
  • Loading branch information
julienp committed Jan 30, 2025
1 parent 0ae92a2 commit 75bc047
Show file tree
Hide file tree
Showing 37 changed files with 1,499 additions and 22 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/pulumi-java-gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func generateJava(cfg generateJavaOptions) error {
extraFiles,
nil, /*localDependencies*/
cfg.Local,
true, /*legacyBuildFiles*/
)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/pulumi-language-java/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ func (host *javaLanguageHost) GeneratePackage(
req.ExtraFiles,
req.LocalDependencies,
req.Local,
false, /*legacyBuildFiles*/
)
if err != nil {
return nil, err
Expand Down
35 changes: 22 additions & 13 deletions pkg/codegen/java/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,7 @@ func GeneratePackage(
extraFiles map[string][]byte,
localDependencies map[string]string,
local bool,
legacyBuildFiles bool,
) (map[string][]byte, error) {
// Presently, Gradle is the primary build system we support for generated SDKs. Later on, when we validate the
// package in order to produce build system artifacts, we'll need a description and repository. To this end, we
Expand Down Expand Up @@ -2308,13 +2309,33 @@ func GeneratePackage(
}
}

// Unless we are generating a local package, we use gradle as the build
// system. Note that we do not support generating build files for any other
// system than gradle at this time.
//
// If the legacyBuildFiles is set to true, we will only use gradle if the
// BuildFiles field is explicitly set to `gradle`. This is to support the
// legacy behavior of `pulumi-java-gen`. Once `pulumi-java-gen` is
// deprecated, we can remove the legacyBuildFiles flag, and always use
// gradle if `local` is false.
useGradle := !local && // Only use gradle if we are not generating a local package.
// legacy behavior requires explicit gradle setting
(legacyBuildFiles && info.BuildFiles == "gradle") ||
// new behavior uses gradle by default, unless explicitly disabled
(!legacyBuildFiles && info.BuildFiles != "none")

// Currently, packages come bundled with a version.txt resource that is used by generated code to report a version.
// When a build tool is configured, we defer the generation of this file to the build process so that e.g. CI
// processes can set the version to be used when releasing or publishing a package, as opposed to when the code for
// that package is generated. In the case that we are generating a package without a build tool, or a local package
// to be incorporated into a program with an existing build process, we need to emit the version.txt file explicitly
// as part of code generation.
if info.BuildFiles == "" || local {
if useGradle {
if err := genGradleProject(pkg, info, files, legacyBuildFiles); err != nil {
return nil, err
}
return files, nil
} else {
pkgName := fmt.Sprintf("%s%s", info.BasePackageOrDefault(), pkg.Name)
pkgPath := strings.ReplaceAll(pkgName, ".", "/")

Expand All @@ -2328,18 +2349,6 @@ func GeneratePackage(
files.add("src/main/resources/"+pkgPath+"/version.txt", []byte(version))
return files, nil
}

// If we are emitting a publishable package with a configured build system, emit those files now.
switch info.BuildFiles {
case "gradle":
if err := genGradleProject(pkg, info, files); err != nil {
return nil, err
}
return files, nil
default:
return nil, fmt.Errorf("Only `gradle` value currently supported for the `buildFiles` setting, given `%s`",
info.BuildFiles)
}
}

func isInputType(t schema.Type) bool {
Expand Down
35 changes: 31 additions & 4 deletions pkg/codegen/java/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ func javaSpecificTests(keyDeps map[string]string) []generatePackageTestConfig {
Directory: "parameterized",
Description: "Tests for parameterized providers",
}),
newGeneratePackageTestConfig(&test.SDKTest{
Directory: "build-files/none",
Description: "Tests for build-files = none",
// We don't generate a gradle file, so we can't compile.
SkipCompileCheck: codegen.NewStringSet("java"),
}),
newGeneratePackageTestConfig(&test.SDKTest{
Directory: "build-files/gradle",
Description: "Tests for build-files = gradle",
}),
newGeneratePackageTestConfig(&test.SDKTest{
Directory: "build-files/gradle-nexus",
Description: "Tests for build-files = gradle-nexus",
}),
newGeneratePackageTestConfig(&test.SDKTest{
Directory: "build-files/unspecified",
Description: "Tests for build-files not set",
}),
}
}

Expand Down Expand Up @@ -224,10 +242,20 @@ func TestGeneratePackage(t *testing.T) {
) (map[string][]byte, error) {
pkg.Description = "test description"
pkg.Repository = "https://github.com/pulumi/pulumi-java"
pkg.Language = map[string]interface{}{
"java": testCase.packageInfo,

if pkg.Language == nil {
pkg.Language = map[string]interface{}{}
}
return GeneratePackage(tool, pkg, extraFiles, nil, false)
if pkg.Language["java"] == nil {
pkg.Language["java"] = testCase.packageInfo
} else {
if err := pkg.ImportLanguages(map[string]schema.Language{"java": Importer}); err != nil {
panic(err)
}
info := pkg.Language["java"].(PackageInfo)
pkg.Language["java"] = info.With(testCase.packageInfo)
}
return GeneratePackage(tool, pkg, extraFiles, nil, false /*local*/, false /*legacyBuildFiles*/)
},
Language: "java",
TestCases: []*test.SDKTest{testCase.sdkTest},
Expand All @@ -239,7 +267,6 @@ func TestGeneratePackage(t *testing.T) {
// Minimal test config that verifies code generation and compilation.
func newGeneratePackageTestConfig(test *test.SDKTest) generatePackageTestConfig {
packageInfo := PackageInfo{
BuildFiles: "gradle",
Dependencies: map[string]string{
"com.pulumi:pulumi": "0.0.1",
},
Expand Down
20 changes: 17 additions & 3 deletions pkg/codegen/java/templates_gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ import (
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)

const DefaultGradleNexusPublishPluginVersion = "2.0.0"

func genGradleProject(
pkg *schema.Package,
packageInfo *PackageInfo,
files fs,
legacyBuildFiles bool,
) error {
if err := gradleValidatePackage(pkg); err != nil {
return err
}

ctx := newGradleTemplateContext(pkg, packageInfo)
ctx := newGradleTemplateContext(pkg, packageInfo, legacyBuildFiles)
templates := map[string]string{
"build.gradle": buildGradleTemplate,
"settings.gradle": settingsGradleTemplate,
Expand Down Expand Up @@ -99,6 +102,7 @@ type gradleTemplateParameterization struct {
func newGradleTemplateContext(
pkg *schema.Package,
packageInfo *PackageInfo,
legacyBuildFiles bool,
) gradleTemplateContext {
ctx := gradleTemplateContext{
Name: pkg.Name,
Expand Down Expand Up @@ -126,9 +130,19 @@ func newGradleTemplateContext(
ctx.Version = pkg.Parameterization.BaseProvider.Version.String()
}

if packageInfo.GradleNexusPublishPluginVersion != "" {
if legacyBuildFiles {
// In legacy mode, we require the user to provide the Gradle Nexus Publish Plugin version.
if packageInfo.GradleNexusPublishPluginVersion != "" {
ctx.GradleNexusPublishPluginEnabled = true
ctx.GradleNexusPublishPluginVersion = packageInfo.GradleNexusPublishPluginVersion
}
} else if packageInfo.BuildFiles == "" || packageInfo.BuildFiles == "gradle-nexus" {
version := DefaultGradleNexusPublishPluginVersion
if packageInfo.GradleNexusPublishPluginVersion != "" {
version = packageInfo.GradleNexusPublishPluginVersion
}
ctx.GradleNexusPublishPluginEnabled = true
ctx.GradleNexusPublishPluginVersion = packageInfo.GradleNexusPublishPluginVersion
ctx.GradleNexusPublishPluginVersion = version
}

if packageInfo.Repositories != nil {
Expand Down
61 changes: 59 additions & 2 deletions pkg/codegen/java/templates_gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,30 @@ import (
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)

func TestNewGradleTemplateContextLegacy(t *testing.T) {
pkg, info := eksExample()
tctx := newGradleTemplateContext(pkg, info, true /*legacyBuildFiles*/)
assert.Equal(t, "0.37.1", tctx.Version)
assert.Equal(t, "com.pulumi", tctx.GroupID)
assert.Equal(t, "eks", tctx.Name)
assert.Equal(t, "https://github.com/pulumi/pulumi-eks", tctx.ProjectURL)
assert.Equal(t, "[email protected]/pulumi/pulumi-eks.git", tctx.ProjectGitURL)
assert.Equal(t, "Pulumi Amazon Web Services (AWS) EKS Components.", tctx.ProjectDescription)
assert.Equal(t, "2022", tctx.ProjectInceptionYear)
assert.Equal(t, "com.pulumi.eks", tctx.RootProjectName)
assert.Equal(t, "pulumi-eks", tctx.ProjectName)
assert.Equal(t, "pulumi", tctx.DeveloperID)
assert.Equal(t, "[email protected]", tctx.DeveloperEmail)
assert.Equal(t, "The Apache License, Version 2.0", tctx.LicenceName)
assert.Equal(t, "http://www.apache.org/licenses/LICENSE-2.0.txt", tctx.LicenceURL)
assert.Equal(t, info.Dependencies, tctx.Dependencies)
assert.Equal(t, "", tctx.GradleNexusPublishPluginVersion)
assert.Equal(t, false, tctx.GradleNexusPublishPluginEnabled)
}

func TestNewGradleTemplateContext(t *testing.T) {
pkg, info := eksExample()
tctx := newGradleTemplateContext(pkg, info)
tctx := newGradleTemplateContext(pkg, info, false /*legacyBuildFiles*/)
assert.Equal(t, "0.37.1", tctx.Version)
assert.Equal(t, "com.pulumi", tctx.GroupID)
assert.Equal(t, "eks", tctx.Name)
Expand All @@ -28,12 +49,48 @@ func TestNewGradleTemplateContext(t *testing.T) {
assert.Equal(t, "The Apache License, Version 2.0", tctx.LicenceName)
assert.Equal(t, "http://www.apache.org/licenses/LICENSE-2.0.txt", tctx.LicenceURL)
assert.Equal(t, info.Dependencies, tctx.Dependencies)
assert.Equal(t, "2.0.0", tctx.GradleNexusPublishPluginVersion)
assert.Equal(t, true, tctx.GradleNexusPublishPluginEnabled)
}

func TestNewGradleTemplateContextBuildFiles(t *testing.T) {
pkg, _ := eksExample()

// Legacy build files: false

// We default to the behavior of `gradle-nexus`.
info := &PackageInfo{BuildFiles: ""}
tctx := newGradleTemplateContext(pkg, info, false /*legacyBuildFiles*/)
assert.Equal(t, "2.0.0", tctx.GradleNexusPublishPluginVersion)
assert.Equal(t, true, tctx.GradleNexusPublishPluginEnabled)

info = &PackageInfo{BuildFiles: "gradle-nexus"}
tctx = newGradleTemplateContext(pkg, info, false /*legacyBuildFiles*/)
assert.Equal(t, "2.0.0", tctx.GradleNexusPublishPluginVersion)
assert.Equal(t, true, tctx.GradleNexusPublishPluginEnabled)

info = &PackageInfo{BuildFiles: "gradle"}
tctx = newGradleTemplateContext(pkg, info, false /*legacyBuildFiles*/)
assert.Equal(t, "", tctx.GradleNexusPublishPluginVersion)
assert.Equal(t, false, tctx.GradleNexusPublishPluginEnabled)

// Legacy build files: true

info = &PackageInfo{BuildFiles: "gradle"}
tctx = newGradleTemplateContext(pkg, info, true /*legacyBuildFiles*/)
assert.Equal(t, "", tctx.GradleNexusPublishPluginVersion)
assert.Equal(t, false, tctx.GradleNexusPublishPluginEnabled)

info = &PackageInfo{BuildFiles: "gradle", GradleNexusPublishPluginVersion: "1.2.3"}
tctx = newGradleTemplateContext(pkg, info, true /*legacyBuildFiles*/)
assert.Equal(t, "1.2.3", tctx.GradleNexusPublishPluginVersion)
assert.Equal(t, true, tctx.GradleNexusPublishPluginEnabled)
}

func TestGenGradleProject(t *testing.T) {
pkg, info := eksExample()
files := fs{}
err := genGradleProject(pkg, info, files)
err := genGradleProject(pkg, info, files, true /*legacyBuildFiles*/)
if err != nil {
t.Error(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test description
Loading

0 comments on commit 75bc047

Please sign in to comment.