From 67d4f179614080457d91a6ef15b5e8fd78aba2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Wed, 6 Sep 2023 00:03:22 +0200 Subject: [PATCH] Angular: use single version in dependencies Fixes #3836 --- .../core/domain/AngularModuleFactory.java | 20 ++-- .../JHipsterModulePackageJson.java | 32 +++++- .../packagejson/PackageJsonDependency.java | 101 +++++++++++++++++- .../FileSystemPackageJsonHandler.java | 4 +- .../dependencies/angular/package.json | 7 -- .../module/domain/JHipsterModulesFixture.java | 2 +- .../FileSystemPackageJsonHandlerTest.java | 42 ++++++++ 7 files changed, 182 insertions(+), 26 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java index 3723f0678b3..2222dddbe7b 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java @@ -7,6 +7,7 @@ import tech.jhipster.lite.module.domain.Indentation; import tech.jhipster.lite.module.domain.JHipsterModule; import tech.jhipster.lite.module.domain.file.JHipsterSource; +import tech.jhipster.lite.module.domain.packagejson.PackageName; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; public class AngularModuleFactory { @@ -16,21 +17,22 @@ public class AngularModuleFactory { private static final JHipsterSource COMMON_ESLINT = from("client/common/eslint"); private static final String CACHE_NEEDLE = " \"cacheDirectories\":"; + private static final PackageName ANGULAR_CORE_PACKAGE = packageName("@angular/core"); public JHipsterModule buildModule(JHipsterModuleProperties properties) { //@formatter:off return ClientsModulesFactory.clientModuleBuilder(properties) .packageJson() - .addDependency(packageName("@angular/animations"), ANGULAR) + .addDependency(packageName("@angular/animations"), ANGULAR, ANGULAR_CORE_PACKAGE) .addDependency(packageName("@angular/cdk"), ANGULAR) - .addDependency(packageName("@angular/common"), ANGULAR) - .addDependency(packageName("@angular/compiler"), ANGULAR) - .addDependency(packageName("@angular/core"), ANGULAR) + .addDependency(packageName("@angular/common"), ANGULAR, ANGULAR_CORE_PACKAGE) + .addDependency(packageName("@angular/compiler"), ANGULAR, ANGULAR_CORE_PACKAGE) + .addDependency(ANGULAR_CORE_PACKAGE, ANGULAR) .addDependency(packageName("@angular/material"), ANGULAR) - .addDependency(packageName("@angular/forms"), ANGULAR) - .addDependency(packageName("@angular/platform-browser"), ANGULAR) - .addDependency(packageName("@angular/platform-browser-dynamic"), ANGULAR) - .addDependency(packageName("@angular/router"), ANGULAR) + .addDependency(packageName("@angular/forms"), ANGULAR, ANGULAR_CORE_PACKAGE) + .addDependency(packageName("@angular/platform-browser"), ANGULAR, ANGULAR_CORE_PACKAGE) + .addDependency(packageName("@angular/platform-browser-dynamic"), ANGULAR, ANGULAR_CORE_PACKAGE) + .addDependency(packageName("@angular/router"), ANGULAR, ANGULAR_CORE_PACKAGE) .addDependency(packageName("rxjs"), ANGULAR) .addDependency(packageName("tslib"), ANGULAR) .addDependency(packageName("zone.js"), ANGULAR) @@ -45,7 +47,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { .addDevDependency(packageName("@angular-builders/jest"), ANGULAR) .addDevDependency(packageName("@angular-devkit/build-angular"), ANGULAR) .addDevDependency(packageName("@angular/cli"), ANGULAR) - .addDevDependency(packageName("@angular/compiler-cli"), ANGULAR) + .addDevDependency(packageName("@angular/compiler-cli"), ANGULAR, ANGULAR_CORE_PACKAGE) .addDevDependency(packageName("@types/node"), ANGULAR) .addDevDependency(packageName("@types/jest"), ANGULAR) .addDevDependency(packageName("jest"), ANGULAR) diff --git a/src/main/java/tech/jhipster/lite/module/domain/packagejson/JHipsterModulePackageJson.java b/src/main/java/tech/jhipster/lite/module/domain/packagejson/JHipsterModulePackageJson.java index cf0affa0c9d..f8b32e3b5d7 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/packagejson/JHipsterModulePackageJson.java +++ b/src/main/java/tech/jhipster/lite/module/domain/packagejson/JHipsterModulePackageJson.java @@ -84,24 +84,48 @@ public JHipsterModulePackageJsonBuilder addScript(ScriptKey key, ScriptCommand c } public JHipsterModulePackageJsonBuilder addDependency(PackageName packageName, VersionSource versionSource) { - dependencies.add(new PackageJsonDependency(packageName, versionSource)); + dependencies.add(PackageJsonDependency.builder().packageName(packageName).versionSource(versionSource).build()); + + return this; + } + + public JHipsterModulePackageJsonBuilder addDependency( + PackageName packageName, + VersionSource versionSource, + PackageName versionPackageName + ) { + dependencies.add( + PackageJsonDependency.builder().packageName(packageName).versionSource(versionSource).versionPackageName(versionPackageName).build() + ); return this; } public JHipsterModulePackageJsonBuilder removeDependency(PackageName packageName, VersionSource versionSource) { - dependenciesToRemove.add(new PackageJsonDependency(packageName, versionSource)); + dependenciesToRemove.add(PackageJsonDependency.builder().packageName(packageName).versionSource(versionSource).build()); return this; } public JHipsterModulePackageJsonBuilder addDevDependency(PackageName packageName, VersionSource versionSource) { - devDependencies.add(new PackageJsonDependency(packageName, versionSource)); + devDependencies.add(PackageJsonDependency.builder().packageName(packageName).versionSource(versionSource).build()); + + return this; + } + + public JHipsterModulePackageJsonBuilder addDevDependency( + PackageName packageName, + VersionSource versionSource, + PackageName versionPackageName + ) { + devDependencies.add( + PackageJsonDependency.builder().packageName(packageName).versionSource(versionSource).versionPackageName(versionPackageName).build() + ); return this; } public JHipsterModulePackageJsonBuilder removeDevDependency(PackageName packageName, VersionSource versionSource) { - devDependenciesToRemove.add(new PackageJsonDependency(packageName, versionSource)); + devDependenciesToRemove.add(PackageJsonDependency.builder().packageName(packageName).versionSource(versionSource).build()); return this; } diff --git a/src/main/java/tech/jhipster/lite/module/domain/packagejson/PackageJsonDependency.java b/src/main/java/tech/jhipster/lite/module/domain/packagejson/PackageJsonDependency.java index 84ec80bb498..afa9253d326 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/packagejson/PackageJsonDependency.java +++ b/src/main/java/tech/jhipster/lite/module/domain/packagejson/PackageJsonDependency.java @@ -1,10 +1,103 @@ package tech.jhipster.lite.module.domain.packagejson; +import java.util.Objects; +import java.util.Optional; import tech.jhipster.lite.shared.error.domain.Assert; -public record PackageJsonDependency(PackageName packageName, VersionSource versionSource) { - public PackageJsonDependency { - Assert.notNull("packageName", packageName); - Assert.notNull("versionSource", versionSource); +public final class PackageJsonDependency { + + private final PackageName packageName; + private final VersionSource versionSource; + private final Optional versionPackageName; + + private PackageJsonDependency(PackageJsonDependencyBuilder builder) { + Assert.notNull("packageName", builder.packageName); + Assert.notNull("versionSource", builder.versionSource); + this.packageName = builder.packageName; + this.versionSource = builder.versionSource; + this.versionPackageName = Optional.ofNullable(builder.versionPackageName); + } + + public PackageName packageName() { + return packageName; + } + + public VersionSource versionSource() { + return versionSource; + } + + public Optional versionPackageName() { + return versionPackageName; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (PackageJsonDependency) obj; + return Objects.equals(this.packageName, that.packageName) && Objects.equals(this.versionSource, that.versionSource); + } + + @Override + public int hashCode() { + return Objects.hash(packageName, versionSource); + } + + @Override + public String toString() { + return "PackageJsonDependency[" + "packageName=" + packageName + ", " + "versionSource=" + versionSource + ']'; + } + + public static PackageJsonDependencyBuilder builder() { + return new PackageJsonDependencyBuilder(); + } + + public static class PackageJsonDependencyBuilder + implements PackageJsonDependencyPackageNameBuilder, PackageJsonDependencyVersionSourceBuilder, PackageJsonDependencyOptionalBuilder { + + private PackageName packageName; + private VersionSource versionSource; + private PackageName versionPackageName; + + @Override + public PackageJsonDependencyVersionSourceBuilder packageName(PackageName packageName) { + this.packageName = packageName; + return this; + } + + @Override + public PackageJsonDependencyOptionalBuilder versionSource(VersionSource versionSource) { + this.versionSource = versionSource; + return this; + } + + @Override + public PackageJsonDependencyOptionalBuilder versionPackageName(PackageName versionPackageName) { + this.versionPackageName = versionPackageName; + return this; + } + + @Override + public PackageJsonDependency build() { + return new PackageJsonDependency(this); + } + } + + public interface PackageJsonDependencyPackageNameBuilder { + PackageJsonDependencyVersionSourceBuilder packageName(PackageName packageName); + } + + public interface PackageJsonDependencyVersionSourceBuilder { + PackageJsonDependencyOptionalBuilder versionSource(VersionSource versionSource); + } + + public interface PackageJsonDependencyOptionalBuilder { + PackageJsonDependencyOptionalBuilder versionPackageName(PackageName versionPackageName); + + PackageJsonDependency build(); } } diff --git a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemPackageJsonHandler.java b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemPackageJsonHandler.java index 4ad604055c3..056a9989b67 100644 --- a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemPackageJsonHandler.java +++ b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemPackageJsonHandler.java @@ -20,6 +20,7 @@ import tech.jhipster.lite.module.domain.packagejson.PackageJsonDependencies; import tech.jhipster.lite.module.domain.packagejson.PackageJsonDependency; import tech.jhipster.lite.module.domain.packagejson.PackageJsonType; +import tech.jhipster.lite.module.domain.packagejson.PackageName; import tech.jhipster.lite.module.domain.packagejson.Scripts; import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder; import tech.jhipster.lite.shared.enumeration.domain.Enums; @@ -135,7 +136,8 @@ private List dependenciesEntries(PackageJsonDependencies devDependenc } private String getNpmVersion(PackageJsonDependency dependency) { - return npmVersions.get(dependency.packageName().get(), Enums.map(dependency.versionSource(), NpmVersionSource.class)).get(); + PackageName packageName = dependency.versionPackageName().orElse(dependency.packageName()); + return npmVersions.get(packageName.get(), Enums.map(dependency.versionSource(), NpmVersionSource.class)).get(); } @ExcludeFromGeneratedCodeCoverage(reason = "The error handling is an hard to test implementation detail") diff --git a/src/main/resources/generator/dependencies/angular/package.json b/src/main/resources/generator/dependencies/angular/package.json index 4848fd3a7ab..02cd086404a 100644 --- a/src/main/resources/generator/dependencies/angular/package.json +++ b/src/main/resources/generator/dependencies/angular/package.json @@ -1,15 +1,8 @@ { "dependencies": { - "@angular/animations": "16.2.3", "@angular/cdk": "16.2.2", - "@angular/common": "16.2.3", - "@angular/compiler": "16.2.3", "@angular/core": "16.2.3", - "@angular/forms": "16.2.3", "@angular/material": "16.2.2", - "@angular/platform-browser": "16.2.3", - "@angular/platform-browser-dynamic": "16.2.3", - "@angular/router": "16.2.3", "keycloak-js": "22.0.1", "rxjs": "7.8.1", "tslib": "2.6.2", diff --git a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java index d6775d33461..429df0c9d0c 100644 --- a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java +++ b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java @@ -88,7 +88,7 @@ public static JHipsterModule module() { .and() .packageJson() .addScript(scriptKey("serve"), scriptCommand("tikui-core serve")) - .addDependency(packageName("@angular/animations"), VersionSource.ANGULAR) + .addDependency(packageName("@angular/animations"), VersionSource.ANGULAR, packageName("@angular/core")) .addDevDependency(packageName("@playwright/test"), VersionSource.COMMON) .and() .preActions() diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemPackageJsonHandlerTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemPackageJsonHandlerTest.java index 1128b9cb3ec..068b8b6a1b6 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemPackageJsonHandlerTest.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemPackageJsonHandlerTest.java @@ -240,6 +240,27 @@ void shouldAddDevDependencyToPackageJsonWithDevDependencySection() { ); } + @Test + void shouldAddDevDependencyToPackageJsonUsingVersionSourcePackage() { + when(npmVersions.get("@angular/core", NpmVersionSource.ANGULAR)).thenReturn(new NpmPackageVersion("1.1.1")); + + JHipsterProjectFolder folder = projectWithPackageJson("src/test/resources/projects/node/package.json"); + + packageJson.handle( + Indentation.DEFAULT, + folder, + emptyBuilder().addDevDependency(packageName("@angular/animations"), VersionSource.ANGULAR, packageName("@angular/core")).build() + ); + + assertPackageJsonContent( + folder, + """ + "devDependencies": { + "@angular/animations": "1.1.1", + """ + ); + } + @Test void shouldReplaceExistingDevDependency() { mockDevVersion(); @@ -336,6 +357,27 @@ void shouldAddDependencyToPackageJsonWithDependencySection() { ); } + @Test + void shouldAddDependencyToPackageJsonUsingVersionSourcePackage() { + when(npmVersions.get("@angular/core", NpmVersionSource.ANGULAR)).thenReturn(new NpmPackageVersion("1.1.1")); + + JHipsterProjectFolder folder = projectWithPackageJson("src/test/resources/projects/node/package.json"); + + packageJson.handle( + Indentation.DEFAULT, + folder, + emptyBuilder().addDependency(packageName("@angular/animations"), VersionSource.ANGULAR, packageName("@angular/core")).build() + ); + + assertPackageJsonContent( + folder, + """ + "dependencies": { + "@angular/animations": "1.1.1", + """ + ); + } + @Test void shouldReplaceExistingDependency() { mockVersion();