diff --git a/cli/bnd.bnd b/cli/bnd.bnd index a59c83ba6..90eca2af7 100644 --- a/cli/bnd.bnd +++ b/cli/bnd.bnd @@ -21,6 +21,10 @@ Private-Package:\ com.fasterxml.jackson.*,\ \ com.liferay.blade.gradle.tooling,\ + com.liferay.release.util,\ + com.liferay.release.util.internal,\ + com.liferay.release.util.internal.util,\ + com.liferay.release.util.internal.constants,\ \ groovy.json,\ \ diff --git a/cli/build.gradle b/cli/build.gradle index 9f8589047..9c8b987aa 100644 --- a/cli/build.gradle +++ b/cli/build.gradle @@ -80,7 +80,8 @@ dependencies { api group: "biz.aQute.bnd", name: "biz.aQute.bndlib", version: "5.3.0" api group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.16.1" api group: "com.liferay", name: "com.liferay.gogo.shell.client", version: "1.0.0" - api group: "com.liferay", name: "com.liferay.project.templates", version: "5.0.295" + api group: "com.liferay", name: "com.liferay.project.templates", version: "5.0.296" + api group: "com.liferay", name: "com.liferay.release.util", version: "1.0.0" api group: "commons-io", name: "commons-io", version: "2.7" api group: "commons-lang", name: "commons-lang", version: "2.6" api group: "org.apache.ant", name: "ant", version: "1.10.11" diff --git a/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java b/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java index e93af1aba..dce920796 100644 --- a/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java +++ b/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java @@ -32,7 +32,7 @@ import com.liferay.blade.cli.util.ProcessesUtil; import com.liferay.blade.cli.util.Prompter; import com.liferay.blade.cli.util.ReleaseUtil; -import com.liferay.blade.cli.util.ResourceUtil; +import com.liferay.release.util.ReleaseEntry; import java.io.BufferedReader; import java.io.File; @@ -408,13 +408,15 @@ public void printUsage(String command, String message) { public void run(String[] args) throws Exception { try { if (ArrayUtil.contains(args, "--trace")) { - ResourceUtil.setTrace(true); + + // TODO restore ability to get more logging + } if (ArrayUtil.contains(args, "--refresh-releases")) { System.out.println("Checking for new releases..."); - ReleaseUtil.populateReleases(0); + ReleaseUtil.initialize(0); } _removeOutDatedTempDir(); @@ -880,7 +882,7 @@ private Map _buildMavenPossibleValuesMap( for (int x = 1; it.hasNext(); x++) { String option = it.next(); - ReleaseUtil.ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(option); + ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(option); optionsMap.put(String.valueOf(x), releaseEntry.getTargetPlatformVersion()); } diff --git a/cli/src/main/java/com/liferay/blade/cli/command/ConvertCommand.java b/cli/src/main/java/com/liferay/blade/cli/command/ConvertCommand.java index 4da66ff87..1e4c55f4d 100644 --- a/cli/src/main/java/com/liferay/blade/cli/command/ConvertCommand.java +++ b/cli/src/main/java/com/liferay/blade/cli/command/ConvertCommand.java @@ -19,6 +19,7 @@ import com.liferay.project.templates.extensions.ProjectTemplatesArgs; import com.liferay.project.templates.extensions.util.Validator; import com.liferay.project.templates.extensions.util.VersionUtil; +import com.liferay.release.util.ReleaseEntry; import java.io.BufferedReader; import java.io.File; @@ -1158,8 +1159,8 @@ private List _getReleaseApirtifactIds() { String productKey = productKeyOpt.get(); - String targetPlatformVersion = ReleaseUtil.withReleaseEntry( - productKey, ReleaseUtil.ReleaseEntry::getTargetPlatformVersion); + String targetPlatformVersion = ReleaseUtil.getFromReleaseEntry( + productKey, ReleaseEntry::getTargetPlatformVersion); if (targetPlatformVersion == null) { return Collections.emptyList(); diff --git a/cli/src/main/java/com/liferay/blade/cli/command/CreateCommand.java b/cli/src/main/java/com/liferay/blade/cli/command/CreateCommand.java index a027ef0bc..14c6901d9 100644 --- a/cli/src/main/java/com/liferay/blade/cli/command/CreateCommand.java +++ b/cli/src/main/java/com/liferay/blade/cli/command/CreateCommand.java @@ -17,12 +17,13 @@ import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.gradle.GradleWorkspaceProvider; import com.liferay.blade.cli.util.BladeUtil; +import com.liferay.blade.cli.util.ReleaseUtil; import com.liferay.blade.cli.util.StringUtil; import com.liferay.project.templates.ProjectTemplates; import com.liferay.project.templates.extensions.ProjectTemplatesArgs; import com.liferay.project.templates.extensions.ProjectTemplatesArgsExt; import com.liferay.project.templates.extensions.util.ProjectTemplatesUtil; -import com.liferay.project.templates.extensions.util.VersionUtil; +import com.liferay.release.util.ReleaseEntry; import java.io.File; import java.io.IOException; @@ -353,7 +354,9 @@ protected ProjectTemplatesArgs getProjectTemplateArgs( throw new IOException("Cannot determine Liferay Version. Please enter a valid value for Liferay Version."); } - projectTemplatesArgs.setLiferayVersion(liferayVersion.get()); + ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(liferayVersion.get()); + + projectTemplatesArgs.setLiferayVersion(releaseEntry.getTargetPlatformVersion()); projectTemplatesArgs.setName(name); projectTemplatesArgs.setPackageName(createArgs.getPackageName()); @@ -472,10 +475,6 @@ private boolean _checkDir(File file) { private String _checkTemplateVersionRange(File templateFile, ProjectTemplatesArgs projectTemplatesArgs) { String versionString = projectTemplatesArgs.getLiferayVersion(); - if (VersionUtil.isLiferayQuarterlyVersion(versionString)) { - return ""; - } - try (InputStream fileInputStream = Files.newInputStream(templateFile.toPath(), StandardOpenOption.READ); JarInputStream in = new JarInputStream(fileInputStream)) { @@ -487,14 +486,18 @@ private String _checkTemplateVersionRange(File templateFile, ProjectTemplatesArg VersionRange versionRange = new VersionRange(versionRangeValue); - String liferayVersionString = String.format( - "%s.%s", VersionUtil.getMajorVersion(versionString), VersionUtil.getMinorVersion(versionString)); + if (!versionRange.includes( + Version.parseVersion( + versionString.replaceAll( + "[a-z]", "" + ).replaceAll( + "-", "." + )))) { - if (!versionRange.includes(Version.parseVersion(liferayVersionString))) { return String.format( "Error: The %s project can only be created in liferay version range: %s, current liferay version " + "is %s.", - projectTemplatesArgs.getTemplate(), versionRange, liferayVersionString); + projectTemplatesArgs.getTemplate(), versionRange, versionString); } } catch (Exception exception) { diff --git a/cli/src/main/java/com/liferay/blade/cli/command/InitCommand.java b/cli/src/main/java/com/liferay/blade/cli/command/InitCommand.java index dfa9f99f2..666f68580 100644 --- a/cli/src/main/java/com/liferay/blade/cli/command/InitCommand.java +++ b/cli/src/main/java/com/liferay/blade/cli/command/InitCommand.java @@ -14,6 +14,7 @@ import com.liferay.project.templates.ProjectTemplates; import com.liferay.project.templates.extensions.ProjectTemplatesArgs; import com.liferay.project.templates.extensions.util.FileUtil; +import com.liferay.release.util.ReleaseEntry; import java.io.File; import java.io.FileInputStream; @@ -31,7 +32,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.Properties; import java.util.Set; @@ -54,11 +54,11 @@ public void execute() throws Exception { InitArgs initArgs = getArgs(); if (initArgs.isList()) { - ReleaseUtil.releaseEntriesStream( + ReleaseUtil.getReleaseEntryStream( ).filter( releaseEntry -> initArgs.isAll() || releaseEntry.isPromoted() ).map( - ReleaseUtil.ReleaseEntry::getReleaseKey + ReleaseEntry::getReleaseKey ).forEach( bladeCLI::out ); @@ -191,17 +191,15 @@ public void execute() throws Exception { projectTemplatesArgs.setGradle(!mavenBuild); - Optional releaseEntryOptional = _getDefaultReleaseEntry( + ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry( initArgs.getLiferayProduct(), initArgs.getLiferayVersion()); - if (!releaseEntryOptional.isPresent()) { + if (releaseEntry == null) { _addError("Unable to get product info for selected version " + initArgs.getLiferayVersion()); return; } - ReleaseUtil.ReleaseEntry releaseEntry = releaseEntryOptional.get(); - String workspaceProductKey = releaseEntry.getReleaseKey(); if (!mavenBuild && _legacyProductKeys.contains(workspaceProductKey)) { @@ -290,39 +288,6 @@ private void _addError(String msg) { getBladeCLI().addErrors("init", Collections.singleton(msg)); } - private Optional _getDefaultReleaseEntry(String liferayProduct, String liferayVersion) { - ReleaseUtil.ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(liferayVersion); - - if (releaseEntry.getReleaseKey() != null) { - return Optional.of(releaseEntry); - } - - Optional defaultVersion = ReleaseUtil.withReleaseEntriesStream( - stream -> stream.filter( - releaseEntry1 -> Objects.equals(releaseEntry1.getProduct(), liferayProduct) - ).filter( - releaseEntry1 -> Objects.equals(releaseEntry1.getTargetPlatformVersion(), liferayVersion) - ).findFirst()); - - if (!defaultVersion.isPresent()) { - defaultVersion = ReleaseUtil.withReleaseEntriesStream( - stream -> stream.filter( - releaseEntry1 -> Objects.equals(releaseEntry1.getProduct(), liferayProduct) - ).filter( - releaseEntry1 -> Objects.equals(releaseEntry1.getProductGroupVersion(), liferayVersion) - ).findFirst()); - } - - if (!defaultVersion.isPresent()) { - defaultVersion = ReleaseUtil.withReleaseEntriesStream( - stream -> stream.filter( - releaseEntry1 -> Objects.equals(releaseEntry1.getTargetPlatformVersion(), liferayVersion) - ).findFirst()); - } - - return defaultVersion; - } - private boolean _isPluginsSDK(File dir) { if ((dir == null) || !dir.exists() || !dir.isDirectory()) { return false; diff --git a/cli/src/main/java/com/liferay/blade/cli/command/validator/LiferayDefaultVersionValidator.java b/cli/src/main/java/com/liferay/blade/cli/command/validator/LiferayDefaultVersionValidator.java index 2aacf8e1a..c878e1673 100644 --- a/cli/src/main/java/com/liferay/blade/cli/command/validator/LiferayDefaultVersionValidator.java +++ b/cli/src/main/java/com/liferay/blade/cli/command/validator/LiferayDefaultVersionValidator.java @@ -6,6 +6,7 @@ package com.liferay.blade.cli.command.validator; import com.liferay.blade.cli.util.ReleaseUtil; +import com.liferay.release.util.ReleaseEntry; import java.util.List; import java.util.stream.Collectors; @@ -19,14 +20,14 @@ public class LiferayDefaultVersionValidator extends LiferayMoreVersionValidator @Override public List get() { - return ReleaseUtil.withReleaseEntriesStream( - stream -> stream.filter( - ReleaseUtil.ReleaseEntry::isPromoted - ).map( - ReleaseUtil.ReleaseEntry::getReleaseKey - ).collect( - Collectors.toList() - )); + return ReleaseUtil.getReleaseEntryStream( + ).filter( + ReleaseEntry::isPromoted + ).map( + ReleaseEntry::getReleaseKey + ).collect( + Collectors.toList() + ); } } \ No newline at end of file diff --git a/cli/src/main/java/com/liferay/blade/cli/command/validator/LiferayMoreVersionValidator.java b/cli/src/main/java/com/liferay/blade/cli/command/validator/LiferayMoreVersionValidator.java index a640c2485..c44ac69c0 100644 --- a/cli/src/main/java/com/liferay/blade/cli/command/validator/LiferayMoreVersionValidator.java +++ b/cli/src/main/java/com/liferay/blade/cli/command/validator/LiferayMoreVersionValidator.java @@ -8,6 +8,7 @@ import com.beust.jcommander.ParameterException; import com.liferay.blade.cli.util.ReleaseUtil; +import com.liferay.release.util.ReleaseEntry; import java.util.List; import java.util.stream.Collectors; @@ -19,17 +20,17 @@ public class LiferayMoreVersionValidator implements ValidatorSupplier { @Override public List get() { - return ReleaseUtil.withReleaseEntriesStream( - stream -> stream.map( - ReleaseUtil.ReleaseEntry::getReleaseKey - ).collect( - Collectors.toList() - )); + return ReleaseUtil.getReleaseEntryStream( + ).map( + ReleaseEntry::getReleaseKey + ).collect( + Collectors.toList() + ); } @Override public void validate(String name, String value) throws ParameterException { - ReleaseUtil.ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(value); + ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(value); if (releaseEntry == null) { throw new ParameterException(value + " is not a valid value."); diff --git a/cli/src/main/java/com/liferay/blade/cli/gradle/GradleWorkspaceProvider.java b/cli/src/main/java/com/liferay/blade/cli/gradle/GradleWorkspaceProvider.java index 8803b2158..d0d0bfbe5 100644 --- a/cli/src/main/java/com/liferay/blade/cli/gradle/GradleWorkspaceProvider.java +++ b/cli/src/main/java/com/liferay/blade/cli/gradle/GradleWorkspaceProvider.java @@ -13,6 +13,7 @@ import com.liferay.blade.cli.command.BaseArgs; import com.liferay.blade.cli.util.BladeUtil; import com.liferay.blade.cli.util.ReleaseUtil; +import com.liferay.release.util.ReleaseEntry; import java.io.File; import java.io.FilenameFilter; @@ -62,104 +63,74 @@ public File getGradlePropertiesFile(File dir) { @Override @SuppressWarnings("unchecked") public String getLiferayVersion(File workspaceDir) { - try { - Properties gradleProperties = getGradleProperties(workspaceDir); + Properties gradleProperties = getGradleProperties(workspaceDir); + + Optional baseLiferayVersion = Optional.ofNullable( + gradleProperties.getProperty(WorkspaceConstants.DEFAULT_TARGET_PLATFORM_VERSION_PROPERTY) + ).filter( + BladeUtil::isNotEmpty + ); + + if (!baseLiferayVersion.isPresent()) { + String productKey = gradleProperties.getProperty(WorkspaceConstants.DEFAULT_WORKSPACE_PRODUCT_PROPERTY); + + String targetPlatformVersion = ReleaseUtil.getFromReleaseEntry( + productKey, ReleaseEntry::getTargetPlatformVersion); - Optional baseLiferayVersion = Optional.ofNullable( - gradleProperties.getProperty(WorkspaceConstants.DEFAULT_TARGET_PLATFORM_VERSION_PROPERTY) + baseLiferayVersion = Optional.ofNullable( + targetPlatformVersion ).filter( BladeUtil::isNotEmpty ); + } - if (!baseLiferayVersion.isPresent()) { - String productKey = gradleProperties.getProperty(WorkspaceConstants.DEFAULT_WORKSPACE_PRODUCT_PROPERTY); - - String targetPlatformVersion = ReleaseUtil.withReleaseEntry( - productKey, ReleaseUtil.ReleaseEntry::getTargetPlatformVersion); + if (!baseLiferayVersion.isPresent()) { + String dockerImageProperty = gradleProperties.getProperty( + WorkspaceConstants.DEFAULT_LIFERAY_DOCKER_IMAGE_PROPERTY); - baseLiferayVersion = Optional.ofNullable( - targetPlatformVersion - ).filter( - BladeUtil::isNotEmpty - ); + if (BladeUtil.isEmpty(dockerImageProperty)) { + return null; } - if (!baseLiferayVersion.isPresent()) { - String dockerImageProperty = gradleProperties.getProperty( - WorkspaceConstants.DEFAULT_LIFERAY_DOCKER_IMAGE_PROPERTY); + Matcher matcher = patternDockerImageLiferayVersion.matcher(dockerImageProperty); - if (BladeUtil.isEmpty(dockerImageProperty)) { - return null; - } - - Matcher matcher = patternDockerImageLiferayVersion.matcher(dockerImageProperty); - - if (matcher.find()) { - baseLiferayVersion = Optional.of(matcher.group(1)); - - if (dockerImageProperty.contains("dxp")) { - baseLiferayVersion = Optional.of(baseLiferayVersion.get() + ".10"); - } - else { - baseLiferayVersion = Optional.of(baseLiferayVersion.get() + ".0"); - } - } - } - - if (baseLiferayVersion.isPresent()) { - return baseLiferayVersion.get(); + if (matcher.find()) { + baseLiferayVersion = ReleaseUtil.getReleaseEntryStream( + ).filter( + releaseEntry -> dockerImageProperty.startsWith(releaseEntry.getLiferayDockerImage()) + ).findFirst( + ).map( + ReleaseEntry::getTargetPlatformVersion + ); } } - catch (Exception exception) { - BladeCLI.instance.error(exception); - } - return null; + return baseLiferayVersion.orElse(null); } @Override public String getProduct(File workspaceDir) { - try { - Properties gradleProperties = getGradleProperties(workspaceDir); + Properties gradleProperties = getGradleProperties(workspaceDir); - String productKey = gradleProperties.getProperty(WorkspaceConstants.DEFAULT_WORKSPACE_PRODUCT_PROPERTY); - - if (productKey == null) { - String targetPlatformVersion = gradleProperties.getProperty( - WorkspaceConstants.DEFAULT_TARGET_PLATFORM_VERSION_PROPERTY); + String versionKey = gradleProperties.getProperty(WorkspaceConstants.DEFAULT_WORKSPACE_PRODUCT_PROPERTY); - if (targetPlatformVersion == null) { - String dockerImageProperty = gradleProperties.getProperty( - WorkspaceConstants.DEFAULT_LIFERAY_DOCKER_IMAGE_PROPERTY); - - if (dockerImageProperty == null) { - return "portal"; - } - else if (dockerImageProperty.contains("dxp")) { - return "dxp"; - } - } - else { - Version version = Version.parseVersion(targetPlatformVersion.replaceAll("-", ".")); + if (versionKey == null) { + versionKey = gradleProperties.getProperty(WorkspaceConstants.DEFAULT_TARGET_PLATFORM_VERSION_PROPERTY); + } - int microVersion = version.getMicro(); + if (versionKey != null) { + ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(versionKey); - if (microVersion >= 10) { - return "dxp"; - } - } + if (releaseEntry != null) { + return releaseEntry.getProduct(); } - else { - String product = productKey.substring(0, productKey.indexOf("-")); + } - if (Objects.equals(product, "commerce")) { - product = "dxp"; - } + String dockerImageProperty = gradleProperties.getProperty( + WorkspaceConstants.DEFAULT_LIFERAY_DOCKER_IMAGE_PROPERTY); - return product; - } - } - catch (Exception exception) { + if ((dockerImageProperty != null) && dockerImageProperty.contains("dxp")) { + return "dxp"; } return "portal"; diff --git a/cli/src/main/java/com/liferay/blade/cli/util/ReleaseUtil.java b/cli/src/main/java/com/liferay/blade/cli/util/ReleaseUtil.java index 271ba6bfa..3199b0be7 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/ReleaseUtil.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/ReleaseUtil.java @@ -5,140 +5,54 @@ package com.liferay.blade.cli.util; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; - -import java.io.File; - -import java.time.temporal.ChronoUnit; +import com.liferay.release.util.ReleaseEntry; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Stream; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Predicate; /** * @author Drew Brokke */ -public class ReleaseUtil { - - public static ReleaseEntry getReleaseEntry(String releaseKey) { - if (_releaseUtil == null) { - populateReleases(_DEFAULT_MAX_AGE); - } - - return _releaseUtil._releaseEntryMap.getOrDefault(releaseKey, _EMPTY_RELEASE_ENTRY); - } - - public static void populateReleases(int maxAge) { - _releaseUtil = new ReleaseUtil(maxAge); - } - - public static Stream releaseEntriesStream() { - if (_releaseUtil == null) { - populateReleases(_DEFAULT_MAX_AGE); - } +public class ReleaseUtil extends com.liferay.release.util.ReleaseUtil { - return _releaseUtil._releaseEntries.stream(); + public static ReleaseEntry getReleaseEntry(String version) { + return getReleaseEntry(null, version); } - public static T withReleaseEntriesStream(Function, T> function) { - return function.apply(releaseEntriesStream()); - } - - public static T withReleaseEntry(String releaseKey, Function function) { - return function.apply(getReleaseEntry(releaseKey)); - } - - @JsonIgnoreProperties(ignoreUnknown = true) - public static class ReleaseEntry { - - public String getProduct() { - return _product; - } - - public String getProductGroupVersion() { - return _productGroupVersion; - } - - public String getProductVersion() { - return _productVersion; - } - - public String getReleaseKey() { - return _releaseKey; - } - - public String getTargetPlatformVersion() { - return _targetPlatformVersion; - } - - public String getUrl() { - return _url; - } - - public boolean isPromoted() { - return _promoted; - } - - @JsonProperty("product") - private String _product; - - @JsonProperty("productGroupVersion") - private String _productGroupVersion; + public static ReleaseEntry getReleaseEntry(String product, String version) { + Predicate productGroupVersionPredicate = releaseEntry -> Objects.equals( + releaseEntry.getProductGroupVersion(), version); + Predicate productPredicate = releaseEntry -> Objects.equals(releaseEntry.getProduct(), product); + Predicate targetPlatformVersionPredicate = releaseEntry -> Objects.equals( + releaseEntry.getTargetPlatformVersion(), version); - @JsonProperty("productVersion") - private String _productVersion; + List> predicates = new ArrayList<>(); - @JsonProperty("promoted") - private boolean _promoted; + predicates.add(releaseEntry -> Objects.equals(releaseEntry.getReleaseKey(), version)); - @JsonProperty("releaseKey") - private String _releaseKey; + predicates.add(productPredicate.and(targetPlatformVersionPredicate)); - @JsonProperty("targetPlatformVersion") - private String _targetPlatformVersion; + predicates.add(productPredicate.and(productGroupVersionPredicate)); - @JsonProperty("url") - private String _url; + predicates.add(targetPlatformVersionPredicate); - } - - private ReleaseUtil(int maxAge) { - File releasesJsonFile = new File(_workspaceCacheDir, "releases.json"); - - _releaseEntries = ResourceUtil.readJson( - ReleaseEntries.class, ResourceUtil.getLocalFileResolver(System.getenv("BLADE_LOCAL_RELEASES_JSON_FILE")), - ResourceUtil.getLocalFileResolver(releasesJsonFile, maxAge, ChronoUnit.DAYS), - ResourceUtil.getURLResolver( - _workspaceCacheDir, "https://releases-cdn.liferay.com/releases.json", "releases.json"), - ResourceUtil.getURLResolver( - _workspaceCacheDir, "https://releases.liferay.com/releases.json", "releases.json"), - ResourceUtil.getClassLoaderResolver("/releases.json")); - - if (_releaseEntries == null) { - throw new RuntimeException("Could not find releases.json"); - } + predicates.add(productGroupVersionPredicate); - _releaseEntryMap.clear(); + for (Predicate predicate : predicates) { + Optional first = getReleaseEntryStream( + ).filter( + predicate + ).findFirst(); - for (ReleaseEntry releaseEntry : _releaseEntries) { - _releaseEntryMap.put(releaseEntry.getReleaseKey(), releaseEntry); + if (first.isPresent()) { + return first.get(); + } } - } - - private static final int _DEFAULT_MAX_AGE = 7; - - private static final ReleaseEntry _EMPTY_RELEASE_ENTRY = new ReleaseEntry(); - - private static ReleaseUtil _releaseUtil; - - private final ReleaseEntries _releaseEntries; - private final Map _releaseEntryMap = new HashMap<>(); - private final File _workspaceCacheDir = new File(System.getProperty("user.home"), ".liferay/workspace"); - private static class ReleaseEntries extends ArrayList { + return null; } } \ No newline at end of file diff --git a/cli/src/main/java/com/liferay/blade/cli/util/ResourceUtil.java b/cli/src/main/java/com/liferay/blade/cli/util/ResourceUtil.java deleted file mode 100644 index 56b413b22..000000000 --- a/cli/src/main/java/com/liferay/blade/cli/util/ResourceUtil.java +++ /dev/null @@ -1,344 +0,0 @@ -/** - * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com - * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 - */ - -package com.liferay.blade.cli.util; - -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -import java.net.URI; -import java.net.URL; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.attribute.BasicFileAttributes; -import java.nio.file.attribute.FileTime; - -import java.time.Duration; -import java.time.Instant; -import java.time.temporal.TemporalUnit; - -import java.util.Date; -import java.util.Objects; - -import org.apache.hc.client5.http.auth.AuthScope; -import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; -import org.apache.hc.client5.http.classic.methods.HttpGet; -import org.apache.hc.client5.http.classic.methods.HttpHead; -import org.apache.hc.client5.http.config.RequestConfig; -import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; -import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; -import org.apache.hc.client5.http.impl.classic.HttpClients; -import org.apache.hc.client5.http.protocol.HttpClientContext; -import org.apache.hc.client5.http.protocol.RedirectLocations; -import org.apache.hc.client5.http.utils.DateUtils; -import org.apache.hc.core5.http.Header; -import org.apache.hc.core5.http.HttpEntity; -import org.apache.hc.core5.http.HttpHeaders; -import org.apache.hc.core5.http.HttpHost; -import org.apache.hc.core5.http.HttpResponse; -import org.apache.hc.core5.http.HttpStatus; -import org.apache.hc.core5.http.protocol.BasicHttpContext; -import org.apache.hc.core5.http.protocol.HttpContext; - -/** - * @author Drew Brokke - */ -public class ResourceUtil { - - public static Resolver getClassLoaderResolver(String resourcePath) { - return () -> { - _print("Trying to get resource from class path: %s", resourcePath); - - return Objects.requireNonNull( - ResourceUtil.class.getResourceAsStream(resourcePath), - "Unable to get resource from class path: " + resourcePath); - }; - } - - public static Resolver getLocalFileResolver(File file) { - return () -> { - _print("Trying to get resource from local file: %s", file.getAbsolutePath()); - - _checkFileExists(file); - - return Files.newInputStream(file.toPath()); - }; - } - - public static Resolver getLocalFileResolver(File file, long maxAge, TemporalUnit temporalUnit) { - return () -> { - _print( - "Trying to get resource from local file with max age of %s %s: %s", maxAge, temporalUnit, - file.getAbsolutePath()); - - _checkFileExists(file); - - BasicFileAttributes basicFileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class); - - FileTime fileTime = basicFileAttributes.lastModifiedTime(); - - Duration age = Duration.between(fileTime.toInstant(), Instant.now()); - - if (age.compareTo(Duration.of(maxAge, temporalUnit)) > 0) { - throw new Exception( - String.format("Cached file %s is older than max age of %s %s", file, maxAge, temporalUnit)); - } - - return Files.newInputStream(file.toPath()); - }; - } - - public static Resolver getLocalFileResolver(String file) { - if (StringUtil.isNullOrEmpty(file)) { - return () -> null; - } - - return getLocalFileResolver(new File(file)); - } - - public static Resolver getURIResolver(File cacheDir, URI uri, String targetFileName) { - return () -> { - _print("Trying to get resource from URL %s", uri); - - URL url = uri.toURL(); - - try { - Path path = _downloadFile(url.toString(), cacheDir.toPath(), targetFileName); - - Files.setLastModifiedTime(path, FileTime.from(Instant.now())); - - return Files.newInputStream(path); - } - catch (Exception exception) { - throw new Exception( - String.format("Unable to get resource from URL %s: %s", url, exception.getMessage()), exception); - } - }; - } - - public static Resolver getURLResolver(File cacheDir, String url, String targetFileName) { - return getURIResolver(cacheDir, URI.create(url), targetFileName); - } - - public static T readJson(Class clazz, Resolver... resolvers) { - return _withInputStream(inputStream -> _objectMapper.readValue(inputStream, clazz), resolvers); - } - - public static void setTrace(boolean trace) { - ResourceUtil._trace = trace; - } - - @FunctionalInterface - public interface Resolver { - - public InputStream resolve() throws Exception; - - } - - @FunctionalInterface - public interface Transformer { - - public T transform(InputStream inputStream) throws Exception; - - } - - private static void _checkFileExists(File file) throws Exception { - if (!file.exists()) { - throw new FileNotFoundException("Unable to get resource from local file: " + file.getAbsolutePath()); - } - } - - private static void _checkResponseStatus(HttpResponse httpResponse) throws IOException { - if (httpResponse.getCode() != HttpStatus.SC_OK) { - throw new IOException(httpResponse.getReasonPhrase()); - } - } - - private static Path _downloadFile( - CloseableHttpClient closeableHttpClient, URI uri, Path cacheDirPath, String targetFileName) - throws Exception { - - HttpHead httpHead = new HttpHead(uri); - - HttpContext httpContext = new BasicHttpContext(); - - Date lastModifiedDate; - - try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpHead, httpContext)) { - _checkResponseStatus(closeableHttpResponse); - - Header dispositionHeader = closeableHttpResponse.getFirstHeader("Content-Disposition"); - - if (dispositionHeader == null) { - RedirectLocations redirectLocations = (RedirectLocations)httpContext.getAttribute( - HttpClientContext.REDIRECT_LOCATIONS); - - if ((redirectLocations != null) && (redirectLocations.size() > 0)) { - uri = redirectLocations.get(redirectLocations.size() - 1); - } - } - - Header lastModifiedHeader = closeableHttpResponse.getFirstHeader(HttpHeaders.LAST_MODIFIED); - - if (lastModifiedHeader != null) { - lastModifiedDate = DateUtils.parseDate(lastModifiedHeader.getValue()); - } - else { - lastModifiedDate = new Date(); - } - } - - Files.createDirectories(cacheDirPath); - - Path targetPath = cacheDirPath.resolve(targetFileName); - - if (Files.exists(targetPath)) { - FileTime fileTime = Files.getLastModifiedTime(targetPath); - - if (fileTime.toMillis() == lastModifiedDate.getTime()) { - return targetPath; - } - - Files.delete(targetPath); - } - - HttpGet httpGet = new HttpGet(uri); - - try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpGet)) { - _checkResponseStatus(closeableHttpResponse); - - HttpEntity httpEntity = closeableHttpResponse.getEntity(); - - try (InputStream inputStream = httpEntity.getContent(); - OutputStream outputStream = Files.newOutputStream(targetPath)) { - - byte[] buffer = new byte[10 * 1024]; - int read = -1; - - while ((read = inputStream.read(buffer)) >= 0) { - outputStream.write(buffer, 0, read); - } - } - } - - Files.setLastModifiedTime(targetPath, FileTime.fromMillis(lastModifiedDate.getTime())); - - return targetPath; - } - - private static Path _downloadFile(String urlString, Path cacheDirPath, String targetFileName) throws Exception { - URL downladURL = new URL(urlString); - - URI downladURI = downladURL.toURI(); - - if (Objects.equals(downladURI.getScheme(), "file")) { - return Paths.get(downladURI); - } - - try (CloseableHttpClient closeableHttpClient = _getHttpClient(downladURL.toURI(), null, null)) { - return _downloadFile(closeableHttpClient, downladURI, cacheDirPath, targetFileName); - } - } - - private static CloseableHttpClient _getHttpClient(URI uri, String userName, String password) { - HttpClientBuilder httpClientBuilder = HttpClients.custom(); - - RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); - - requestConfigBuilder.setCookieSpec(RequestConfig.DEFAULT.getCookieSpec()); - requestConfigBuilder.setRedirectsEnabled(true); - - httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()); - - String scheme = uri.getScheme(); - - String proxyHost = System.getProperty(scheme + ".proxyHost"); - String proxyPort = System.getProperty(scheme + ".proxyPort"); - - String proxyUser = userName; - - if (Objects.isNull(proxyUser)) { - proxyUser = System.getProperty(scheme + ".proxyUser"); - } - - String proxyPassword = password; - - if (Objects.isNull(proxyPassword)) { - proxyPassword = System.getProperty(scheme + ".proxyPassword"); - } - - if ((proxyUser != null) && (proxyPassword != null)) { - BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - - if ((proxyHost != null) && (proxyPort != null)) { - credentialsProvider.setCredentials( - new AuthScope(proxyHost, Integer.parseInt(proxyPort)), - new UsernamePasswordCredentials(proxyUser, proxyPassword.toCharArray())); - } - - httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - } - else { - if ((proxyHost != null) && (proxyPort != null)) { - httpClientBuilder.setProxy(new HttpHost(proxyHost, Integer.parseInt(proxyPort))); - } - } - - httpClientBuilder.useSystemProperties(); - - return httpClientBuilder.build(); - } - - private static void _print(String message, Object... args) { - if (_trace) { - System.out.printf(message + "%n", args); - } - } - - private static T _withInputStream(Transformer transformer, Resolver... resolvers) { - InputStream inputStream1 = null; - - for (Resolver resolver : resolvers) { - try { - inputStream1 = resolver.resolve(); - } - catch (Exception exception) { - _print(exception.getMessage()); - } - - if (inputStream1 != null) { - break; - } - } - - if (inputStream1 == null) { - _print("Resource not found"); - - return null; - } - - try (InputStream inputStream2 = inputStream1) { - _print("Found resource"); - - return transformer.transform(inputStream2); - } - catch (Exception exception) { - throw new RuntimeException("Unable to read resource", exception); - } - } - - private static final ObjectMapper _objectMapper = new ObjectMapper(); - private static boolean _trace; - -} \ No newline at end of file diff --git a/cli/src/test/java/com/liferay/blade/cli/BladeTest.java b/cli/src/test/java/com/liferay/blade/cli/BladeTest.java index ca81b39de..5586d86c2 100644 --- a/cli/src/test/java/com/liferay/blade/cli/BladeTest.java +++ b/cli/src/test/java/com/liferay/blade/cli/BladeTest.java @@ -7,6 +7,7 @@ import com.liferay.blade.cli.command.BaseArgs; import com.liferay.blade.cli.util.ReleaseUtil; +import com.liferay.release.util.ReleaseEntry; import java.io.File; import java.io.IOException; @@ -19,7 +20,6 @@ import java.util.Objects; import java.util.Scanner; -import java.util.function.Predicate; import java.util.stream.Stream; /** @@ -37,49 +37,19 @@ public class BladeTest extends BladeCLI { public static final String LIFERAY_VERSION_PORTAL_7456 = "7.4.3.56"; - public static final String PRODUCT_VERSION_DXP_70 = getFirstProductKey( - _getProductPredicate( - "dxp" - ).and( - _getProductGroupVersionPredicate("7.0") - )); - - public static final String PRODUCT_VERSION_DXP_71 = getFirstProductKey( - _getProductPredicate( - "dxp" - ).and( - _getProductGroupVersionPredicate("7.1") - )); - - public static final String PRODUCT_VERSION_DXP_72 = getFirstProductKey( - _getProductPredicate( - "dxp" - ).and( - _getProductGroupVersionPredicate("7.2") - )); - - public static final String PRODUCT_VERSION_DXP_73 = getFirstProductKey( - _getProductPredicate( - "dxp" - ).and( - _getProductGroupVersionPredicate("7.3") - )); - - public static final String PRODUCT_VERSION_DXP_74 = getFirstProductKey( - _getProductPredicate( - "dxp" - ).and( - _getProductGroupVersionPredicate("7.4") - )); + public static final String PRODUCT_VERSION_DXP_70 = getFirstProductKey("dpx", "7.0"); + + public static final String PRODUCT_VERSION_DXP_71 = getFirstProductKey("dpx", "7.1"); + + public static final String PRODUCT_VERSION_DXP_72 = getFirstProductKey("dpx", "7.2"); + + public static final String PRODUCT_VERSION_DXP_73 = getFirstProductKey("dpx", "7.3"); + + public static final String PRODUCT_VERSION_DXP_74 = getFirstProductKey("dpx", "7.4"); public static final String PRODUCT_VERSION_DXP_74_U72 = "dxp-7.4-u72"; - public static final String PRODUCT_VERSION_PORTAL_73 = getFirstProductKey( - _getProductPredicate( - "portal" - ).and( - _getProductGroupVersionPredicate("7.3") - )); + public static final String PRODUCT_VERSION_PORTAL_73 = getFirstProductKey("portal", "7.3"); // Temporarily hard-coded due to an upstream issue with release metadata @@ -89,16 +59,10 @@ public static BladeTestBuilder builder() { return new BladeTestBuilder(); } - public static String getFirstProductKey(Predicate predicate) { - return ReleaseUtil.withReleaseEntriesStream( - releaseEntryStream -> releaseEntryStream.filter( - predicate - ).map( - ReleaseUtil.ReleaseEntry::getReleaseKey - ).findFirst( - ).orElse( - "" - )); + public static String getFirstProductKey(String product, String version) { + ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(product, version); + + return releaseEntry.getReleaseKey(); } @Override @@ -261,14 +225,6 @@ protected BladeTest(PrintStream out, PrintStream err, InputStream in) { super(out, err, in); } - private static Predicate _getProductGroupVersionPredicate(String productGroupVersion) { - return releaseEntry -> Objects.equals(releaseEntry.getProductGroupVersion(), productGroupVersion); - } - - private static Predicate _getProductPredicate(String product) { - return releaseEntry -> Objects.equals(releaseEntry.getProduct(), product); - } - private File _getSettingsBaseDir() { BaseArgs args = getArgs(); diff --git a/cli/src/test/java/com/liferay/blade/cli/command/InitCommandTest.java b/cli/src/test/java/com/liferay/blade/cli/command/InitCommandTest.java index 9da784f8a..cb291bb83 100644 --- a/cli/src/test/java/com/liferay/blade/cli/command/InitCommandTest.java +++ b/cli/src/test/java/com/liferay/blade/cli/command/InitCommandTest.java @@ -11,6 +11,7 @@ import com.liferay.blade.cli.TestUtil; import com.liferay.blade.cli.util.FileUtil; import com.liferay.blade.cli.util.ReleaseUtil; +import com.liferay.release.util.ReleaseEntry; import java.io.ByteArrayInputStream; import java.io.File; @@ -348,7 +349,11 @@ public void testInitCommandListPromoted() throws Exception { String firstLine = lines.get(0); - Assert.assertEquals(firstLine, BladeTest.getFirstProductKey(ReleaseUtil.ReleaseEntry::isPromoted)); + ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(firstLine); + + Assert.assertNotNull(releaseEntry); + + Assert.assertTrue(releaseEntry.isPromoted()); } @Test diff --git a/cli/src/test/java/com/liferay/blade/cli/command/validator/LiferayMoreVersionValidatorTest.java b/cli/src/test/java/com/liferay/blade/cli/command/validator/LiferayMoreVersionValidatorTest.java index 2c3f78d12..d8d49e523 100644 --- a/cli/src/test/java/com/liferay/blade/cli/command/validator/LiferayMoreVersionValidatorTest.java +++ b/cli/src/test/java/com/liferay/blade/cli/command/validator/LiferayMoreVersionValidatorTest.java @@ -6,6 +6,7 @@ package com.liferay.blade.cli.command.validator; import com.liferay.blade.cli.util.ReleaseUtil; +import com.liferay.release.util.ReleaseEntry; import java.util.List; import java.util.stream.Collectors; @@ -20,12 +21,12 @@ public class LiferayMoreVersionValidatorTest { @Test public void testGet() throws Exception { - List expectedReleaseKeys = ReleaseUtil.withReleaseEntriesStream( - releaseEntryStream -> releaseEntryStream.map( - ReleaseUtil.ReleaseEntry::getReleaseKey - ).collect( - Collectors.toList() - )); + List expectedReleaseKeys = ReleaseUtil.getReleaseEntryStream( + ).map( + ReleaseEntry::getReleaseKey + ).collect( + Collectors.toList() + ); LiferayMoreVersionValidator lmvv = new LiferayMoreVersionValidator(); diff --git a/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/MavenWorkspaceProvider.java b/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/MavenWorkspaceProvider.java index 698e747dd..0b88c17fb 100644 --- a/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/MavenWorkspaceProvider.java +++ b/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/MavenWorkspaceProvider.java @@ -5,11 +5,11 @@ package com.liferay.blade.extensions.maven.profile; -import aQute.bnd.version.Version; - import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.util.BladeUtil; +import com.liferay.blade.cli.util.ReleaseUtil; import com.liferay.blade.extensions.maven.profile.internal.MavenUtil; +import com.liferay.release.util.ReleaseEntry; import java.io.File; @@ -35,19 +35,13 @@ public String getProduct(File workspaceDir) { return "portal"; } - try { - Version version = Version.parseVersion(targetPlatformVersion.replaceAll("-", ".")); - - int microVersion = version.getMicro(); + ReleaseEntry releaseEntry = ReleaseUtil.getReleaseEntry(targetPlatformVersion); - if (microVersion >= 10) { - return "dxp"; - } - } - catch (Exception exception) { + if (releaseEntry == null) { + return "portal"; } - return "portal"; + return releaseEntry.getProduct(); } @Override diff --git a/extensions/project-templates-js-theme/bnd.bnd b/extensions/project-templates-js-theme/bnd.bnd index db79d2d50..9520dd1eb 100644 --- a/extensions/project-templates-js-theme/bnd.bnd +++ b/extensions/project-templates-js-theme/bnd.bnd @@ -2,7 +2,7 @@ Bundle-Description: Creates a JS Theme as a module project. Bundle-Name: Liferay Project Templates JS Theme Bundle-SymbolicName: com.liferay.project.templates.js.theme Bundle-Version: ${project.version} -Liferay-Versions: [7,8) +Liferay-Versions: [7,2025) -removeheaders:\ Import-Package,\ Private-Package,\ diff --git a/extensions/project-templates-js-widget/bnd.bnd b/extensions/project-templates-js-widget/bnd.bnd index 8071d761c..3afa8c9d9 100644 --- a/extensions/project-templates-js-widget/bnd.bnd +++ b/extensions/project-templates-js-widget/bnd.bnd @@ -2,7 +2,7 @@ Bundle-Description: Creates a JS Widget as a module project. Bundle-Name: Liferay Project Templates JS Widget Bundle-SymbolicName: com.liferay.project.templates.js.widget Bundle-Version: ${project.version} -Liferay-Versions: [7,8) +Liferay-Versions: [7,2025) -removeheaders:\ Import-Package,\ Private-Package,\ diff --git a/extensions/sample-template/bnd.bnd b/extensions/sample-template/bnd.bnd index ed314d80b..49f9616e9 100644 --- a/extensions/sample-template/bnd.bnd +++ b/extensions/sample-template/bnd.bnd @@ -2,7 +2,7 @@ Bundle-Description: Creates a Sample as a module project. Bundle-Name: Liferay Project Templates Sample Bundle-SymbolicName: com.liferay.project.templates.sample Bundle-Version: ${project.version} -Liferay-Versions: [7,8) +Liferay-Versions: [7,2025) -removeheaders:\ Import-Package,\ Private-Package,\ diff --git a/extensions/sample-workspace-template/bnd.bnd b/extensions/sample-workspace-template/bnd.bnd index 2040b9311..8d7c21e3b 100644 --- a/extensions/sample-workspace-template/bnd.bnd +++ b/extensions/sample-workspace-template/bnd.bnd @@ -2,7 +2,7 @@ Bundle-Description: Creates a sample workspace template Bundle-Name: Liferay Project Templates Workspace Sample Bundle-SymbolicName: com.liferay.project.templates.workspace.sample Bundle-Version: ${project.version} -Liferay-Versions: [7,8) +Liferay-Versions: [7,2025) -removeheaders:\ Import-Package,\ Private-Package,\