Skip to content

Commit

Permalink
Angular: use single version in dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
murdos committed Sep 5, 2023
1 parent 8335458 commit 67d4f17
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<PackageName> 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<PackageName> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,7 +136,8 @@ private List<JsonEntry> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 67d4f17

Please sign in to comment.