Skip to content

Commit

Permalink
fix: separate core and commercial components version files (#14014)
Browse files Browse the repository at this point in the history
* fix: separate core and commercial components version files

Fixes: #13749

Previously, the vaadin_versions.json contained
both core and commercial components listed
which led to including them all in package.json
even if the user only used vaadin-core. Now
platform generates vaadin-core-version.json in
vaadin-core and vaadin-version.json to include
commercial components in vaadin artifact.
This commit will adopt having separate files
while generating package.json and pinning the
platform dependencies.
  • Loading branch information
taefi committed Jun 23, 2022
1 parent 60a8728 commit 23f5496
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 37 deletions.
Expand Up @@ -461,9 +461,14 @@ public final class Constants implements Serializable {
public static final String STATISTIC_ROUTING_HYBRID = "routing/hybrid";

/**
* The name of platform versions file.
* The name of platform core components and tools versions file.
*/
public static final String VAADIN_VERSIONS_JSON = "vaadin_versions.json";
public static final String VAADIN_CORE_VERSIONS_JSON = "vaadin-core-versions.json";

/**
* The name of platform commercial components and tools versions file.
*/
public static final String VAADIN_VERSIONS_JSON = "vaadin-versions.json";

/**
* @deprecated Use
Expand Down
Expand Up @@ -39,7 +39,7 @@ public class Platform implements Serializable {
*/
public static Optional<String> getVaadinVersion() {
try (InputStream vaadinVersionsStream = Platform.class.getClassLoader()
.getResourceAsStream(Constants.VAADIN_VERSIONS_JSON)) {
.getResourceAsStream(Constants.VAADIN_CORE_VERSIONS_JSON)) {
if (vaadinVersionsStream != null) {
ObjectMapper m = new ObjectMapper();
JsonNode vaadinVersions = m.readTree(vaadinVersionsStream);
Expand Down
Expand Up @@ -193,21 +193,45 @@ protected File getPackageLockFile() {
* when versions file could not be read
*/
JsonObject getPlatformPinnedDependencies() throws IOException {
URL resource = finder.getResource(Constants.VAADIN_VERSIONS_JSON);
if (resource == null) {
log().info("Couldn't find {} file to pin dependency versions."
+ " Transitive dependencies won't be pinned for pnpm.",
Constants.VAADIN_VERSIONS_JSON);
URL coreVersionsResource = finder
.getResource(Constants.VAADIN_CORE_VERSIONS_JSON);
if (coreVersionsResource == null) {
log().info(
"Couldn't find {} file to pin dependency versions for core components."
+ " Transitive dependencies won't be pinned for npm/pnpm.",
Constants.VAADIN_CORE_VERSIONS_JSON);
return Json.createObject();
}

JsonObject versionsJson = getFilteredVersionsFromResource(
coreVersionsResource, Constants.VAADIN_CORE_VERSIONS_JSON);

URL vaadinVersionsResource = finder
.getResource(Constants.VAADIN_VERSIONS_JSON);
if (vaadinVersionsResource == null) {
// vaadin is not on the classpath, only vaadin-core is present.
return versionsJson;
}

JsonObject vaadinVersionsJson = getFilteredVersionsFromResource(
vaadinVersionsResource, Constants.VAADIN_VERSIONS_JSON);
for (String key : vaadinVersionsJson.keys()) {
versionsJson.put(key, vaadinVersionsJson.getString(key));
}

return versionsJson;
}

private JsonObject getFilteredVersionsFromResource(URL versionsResource,
String versionsOrigin) throws IOException {
JsonObject versionsJson;
try (InputStream content = resource.openStream()) {
try (InputStream content = versionsResource.openStream()) {
VersionsJsonConverter convert = new VersionsJsonConverter(Json
.parse(IOUtils.toString(content, StandardCharsets.UTF_8)));
versionsJson = convert.getConvertedJson();
versionsJson = new VersionsJsonFilter(getPackageJson(),
DEPENDENCIES).getFilteredVersions(versionsJson);
DEPENDENCIES)
.getFilteredVersions(versionsJson, versionsOrigin);
}
return versionsJson;
}
Expand Down
Expand Up @@ -51,14 +51,15 @@ class VersionsJsonFilter {
*
* @param versions
* to be filtered for user managed ones
* @param versionOrigin
* origin of the version (like a file), used in error message
* @return filtered versions json
*/
JsonObject getFilteredVersions(JsonObject versions) {
JsonObject getFilteredVersions(JsonObject versions, String versionOrigin) {
JsonObject json = Json.createObject();
for (String key : versions.keys()) {
final FrontendVersion version = FrontendUtils
.getPackageVersionFromJson(versions, key,
"vaadin_version.json");
.getPackageVersionFromJson(versions, key, versionOrigin);
if (version == null) {
continue;
}
Expand Down
Expand Up @@ -103,7 +103,8 @@ public void setup() throws Exception {
classFinder = Mockito.spy(getClassFinder());
File versions = temporaryFolder.newFile();
FileUtils.write(versions, "{}", StandardCharsets.UTF_8);
Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(
classFinder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versions.toURI().toURL());

packageUpdater = new TaskUpdatePackages(classFinder,
Expand Down
Expand Up @@ -89,7 +89,8 @@ public void setup() throws Exception {
StandardCharsets.UTF_8);
// @formatter:on

Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(
classFinder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versions.toURI().toURL());
}

Expand Down
Expand Up @@ -163,7 +163,8 @@ public void runPnpmInstall_versionsJsonIsFound_pnpmHookFileIsGenerated()
ClassFinder classFinder = getClassFinder();
File versions = temporaryFolder.newFile();
FileUtils.write(versions, "{}", StandardCharsets.UTF_8);
Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(
classFinder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versions.toURI().toURL());

TaskRunNpmInstall task = createTask();
Expand Down Expand Up @@ -503,11 +504,12 @@ public void runPnpmInstall_userVersionNewerThanPinned_installedOverlayVersionIsN
final VersionsJsonFilter versionsJsonFilter = new VersionsJsonFilter(
Json.parse(packageJsonContent), NodeUpdater.DEPENDENCIES);
// Platform defines a pinned version
TaskRunNpmInstall task = createTask(
versionsJsonFilter.getFilteredVersions(
TaskRunNpmInstall task = createTask(versionsJsonFilter
.getFilteredVersions(
Json.parse("{ \"@vaadin/vaadin-overlay\":\""
+ PINNED_VERSION + "\"}"))
.toJson());
+ PINNED_VERSION + "\"}"),
"test-versions.json")
.toJson());
task.execute();

File overlayPackageJson = new File(getNodeUpdater().nodeModulesFolder,
Expand Down Expand Up @@ -550,11 +552,12 @@ public void runPnpmInstall_frameworkCollectedVersionNewerThanPinned_installedOve
final VersionsJsonFilter versionsJsonFilter = new VersionsJsonFilter(
Json.parse(packageJsonContent), NodeUpdater.DEPENDENCIES);
// Platform defines a pinned version
TaskRunNpmInstall task = createTask(
versionsJsonFilter.getFilteredVersions(
TaskRunNpmInstall task = createTask(versionsJsonFilter
.getFilteredVersions(
Json.parse("{ \"@vaadin/vaadin-overlay\":\""
+ PINNED_VERSION + "\"}"))
.toJson());
+ PINNED_VERSION + "\"}"),
"test-versions.json")
.toJson());
task.execute();

File overlayPackageJson = new File(getNodeUpdater().nodeModulesFolder,
Expand Down Expand Up @@ -656,7 +659,8 @@ protected TaskRunNpmInstall createTask(String versionsContent) {
private JsonObject getGeneratedVersionsContent(File versions)
throws IOException {
ClassFinder classFinder = getClassFinder();
Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(
classFinder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versions.toURI().toURL());

String path = getNodeUpdater().generateVersionsJson();
Expand Down
Expand Up @@ -93,7 +93,7 @@ public void setUp() throws IOException {
generatedPath.mkdir();
versionJsonFile = new File(npmFolder, "versions.json");
finder = Mockito.mock(ClassFinder.class);
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versionJsonFile.toURI().toURL());

packageJson = new File(npmFolder, PACKAGE_JSON);
Expand Down Expand Up @@ -185,14 +185,14 @@ public void npmIsInUse_platformVersionIsBumped_versionsAreUpdated()
@Test
public void npmIsInUse_noPlatformVersionJsonPresent_noFailure()
throws IOException {
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(null);
final TaskUpdatePackages task = createTask(
createApplicationDependencies());
task.execute();
Assert.assertTrue("Updates not picked", task.modified);

Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versionJsonFile.toURI().toURL());
JsonObject dependencies = getOrCreatePackageJson()
.getObject(DEPENDENCIES);
Expand All @@ -203,11 +203,11 @@ public void npmIsInUse_noPlatformVersionJsonPresent_noFailure()
@Test
public void npmIsInUse_platformVersionsJsonAdded_versionsPinned()
throws IOException {
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(null);
createTask(createApplicationDependencies()).execute();

Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versionJsonFile.toURI().toURL());
final String newVersion = "20.0.0";
createVaadinVersionsJson(newVersion, newVersion, newVersion);
Expand Down Expand Up @@ -660,7 +660,7 @@ private void verifyVersionLockingWithNpmOverrides(boolean hasDialogLocking,

private void verifyPlatformDependenciesAreAdded(boolean enablePnpm)
throws IOException {
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versionJsonFile.toURI().toURL());
final String newVersion = "20.0.0";
createVaadinVersionsJson(newVersion, newVersion, newVersion);
Expand Down
Expand Up @@ -83,8 +83,8 @@ private void assertMissingVaadinDependencies_allDependenciesSholdBeUserHandled(

VersionsJsonFilter filter = new VersionsJsonFilter(Json.parse(pkgJson),
depKey);
JsonObject filteredJson = filter
.getFilteredVersions(Json.parse(versions));
JsonObject filteredJson = filter.getFilteredVersions(
Json.parse(versions), "versions/versions.json");
Assert.assertTrue(filteredJson.hasKey("@vaadin/vaadin-progress-bar"));
Assert.assertTrue(filteredJson.hasKey("@vaadin/vaadin-upload"));
Assert.assertTrue(filteredJson.hasKey("@polymer/iron-list"));
Expand All @@ -108,8 +108,8 @@ private void assertFilterPlatformVersions_multipleUserChanged_correctlyIgnored(

VersionsJsonFilter filter = new VersionsJsonFilter(Json.parse(pkgJson),
depKey);
JsonObject filteredJson = filter
.getFilteredVersions(Json.parse(versions));
JsonObject filteredJson = filter.getFilteredVersions(
Json.parse(versions), "versions/user_versions.json");
List<String> expectedKeys = Arrays.asList("@vaadin/vaadin-notification",
"@vaadin/vaadin-overlay", "@vaadin/vaadin-select",
"@vaadin/vaadin-split-layout", "@vaadin/vaadin-tabs");
Expand Down Expand Up @@ -156,8 +156,8 @@ private void assertFilterPlatformVersions(String depKey)

VersionsJsonFilter filter = new VersionsJsonFilter(Json.parse(pkgJson),
depKey);
JsonObject filteredJson = filter
.getFilteredVersions(Json.parse(versions));
JsonObject filteredJson = filter.getFilteredVersions(
Json.parse(versions), "versions/versions.json");
Assert.assertTrue(filteredJson.hasKey("@vaadin/vaadin-progress-bar"));
Assert.assertTrue(filteredJson.hasKey("@vaadin/vaadin-upload"));
Assert.assertTrue(filteredJson.hasKey("@polymer/iron-list"));
Expand Down

0 comments on commit 23f5496

Please sign in to comment.