From 3bf3fc9954660f540a88a198cf4d1f31633ac14a Mon Sep 17 00:00:00 2001 From: Simon Jiang Date: Tue, 26 Dec 2023 18:27:19 +0800 Subject: [PATCH 1/3] BLADE-742 replace product_info.json with releases.json --- cli/bnd.bnd | 2 +- cli/build.gradle | 12 +- .../java/com/liferay/blade/cli/BladeCLI.java | 14 +- .../com/liferay/blade/cli/Extensions.java | 5 +- .../liferay/blade/cli/WorkspaceConstants.java | 3 + .../blade/cli/command/ConvertCommand.java | 6 +- .../blade/cli/command/CreateCommand.java | 56 ++++++-- .../blade/cli/command/InitCommand.java | 61 ++++---- .../LiferayMoreVersionValidator.java | 22 ++- .../cli/gradle/GradleWorkspaceProvider.java | 24 ++-- .../com/liferay/blade/cli/util/BladeUtil.java | 130 ++++++++++++------ .../com/liferay/blade/cli/util/NodeUtil.java | 9 +- .../liferay/blade/cli/util/ProductInfo.java | 40 ++---- .../blade/cli/util/ProductKeyInfo.java | 104 +++++++++----- .../blade/cli/util/ProductKeyUtil.java | 108 +-------------- .../blade/cli/util/ProductKeyVersion.java | 11 -- .../java/com/liferay/blade/cli/BladeTest.java | 6 +- .../blade/cli/command/CreateCommandTest.java | 29 ++++ .../blade/cli/command/InitCommandTest.java | 19 ++- .../blade/cli/command/JavaProcesses.java | 5 +- .../cli/command/ServerStartCommandTest.java | 2 +- .../maven/profile/MavenWorkspaceProvider.java | 9 ++ ...entExtensionProjectTemplateCustomizer.java | 57 ++++---- .../client/extension/internal/LXCUtil.java | 5 +- 24 files changed, 396 insertions(+), 343 deletions(-) diff --git a/cli/bnd.bnd b/cli/bnd.bnd index cb130d5ee..483bb0b20 100644 --- a/cli/bnd.bnd +++ b/cli/bnd.bnd @@ -323,6 +323,6 @@ Private-Package:\ @jansi-*.jar,\ ${project.buildDir}/tooling.zip,\ ${project.buildDir}/wrapper.zip,\ - ${project.buildDir}/.product_info.json,\ + ${project.buildDir}/releases.json,\ mvnw.cmd -sources: false \ No newline at end of file diff --git a/cli/build.gradle b/cli/build.gradle index f57e46948..98deb8d44 100644 --- a/cli/build.gradle +++ b/cli/build.gradle @@ -31,7 +31,7 @@ tasks.register('copyMavenProfileJar', Copy) tasks.register('createToolingZip', Zip) tasks.register('createWrapperZip', Zip) tasks.register('downloadPortal', Download) -tasks.register('downloadProductInfo', Download) +tasks.register('downloadReleasesInfo', Download) tasks.register('unzipManifest', Copy) tasks.register('unzipPortal', Copy) @@ -77,7 +77,7 @@ createWrapperZip { dependencies { api group: "biz.aQute.bnd", name: "biz.aQute.bndlib", version: "5.3.0" 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.269" + api group: "com.liferay", name: "com.liferay.project.templates", version: "5.0.283" 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" @@ -102,7 +102,7 @@ dependencies { api group: "org.gradle", name: "gradle-base-services-groovy", version: "5.6.4" api group: "org.gradle", name: "gradle-core", version: "5.6.4" api group: "org.gradle", name: "gradle-tooling-api", version: "5.6.4" - api group: "org.json", name: "json", version: "20230227" + api group: "org.json", name: "json", version: "20231013" api group: "org.jsoup", name: "jsoup", version: "1.15.3" api group: "org.tukaani", name: "xz", version: "1.6" api name: "org.objectweb.asm-6.0.0" @@ -139,8 +139,8 @@ downloadPortal { onlyIfNewer true } -downloadProductInfo { - src "https://releases-cdn.liferay.com/tools/workspace/.product_info.json" +downloadReleasesInfo { + src "http://localhost:3000/releases.json" dest buildDir onlyIfNewer true } @@ -148,7 +148,7 @@ downloadProductInfo { jar { dependsOn("unzipPortal") archiveFileName.set("blade.jar") - from createToolingZip, createWrapperZip, downloadProductInfo + from createToolingZip, createWrapperZip, downloadReleasesInfo } processResources { 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 3c2be9d3a..624f48dbb 100644 --- a/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java +++ b/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java @@ -31,6 +31,7 @@ import com.liferay.blade.cli.util.Pair; import com.liferay.blade.cli.util.ProcessesUtil; import com.liferay.blade.cli.util.ProductInfo; +import com.liferay.blade.cli.util.ProductKeyInfo; import com.liferay.blade.cli.util.Prompter; import java.io.BufferedReader; @@ -862,14 +863,19 @@ private Map _buildMavenPossibleValuesMap( Iterator it = options.iterator(); - Map productInfos = BladeUtil.getProductInfos(true, error()); + Map releasesInfos = BladeUtil.getReleasesInfos(true, error()); Map optionsMap = new LinkedHashMap<>(); for (int x = 1; it.hasNext(); x++) { String option = it.next(); - ProductInfo productInfo = new ProductInfo((Map)productInfos.get(option)); + ProductKeyInfo productKeyInfo = new ProductKeyInfo( + option, (Map)releasesInfos.get(option)); + + ProductInfo productInfo = new ProductInfo( + productKeyInfo, + BladeUtil.getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())); optionsMap.put(String.valueOf(x), productInfo.getTargetPlatformVersion()); } @@ -982,9 +988,7 @@ private String _getCommandProfile(String[] args) throws MissingCommandException for (String arg : args) { String[] argSplit = arg.split(" "); - for (String argEach : argSplit) { - argsCollection.add(argEach); - } + Collections.addAll(argsCollection, argSplit); } String[] argsArray = argsCollection.toArray(new String[0]); diff --git a/cli/src/main/java/com/liferay/blade/cli/Extensions.java b/cli/src/main/java/com/liferay/blade/cli/Extensions.java index bbb3a2503..7b435951b 100644 --- a/cli/src/main/java/com/liferay/blade/cli/Extensions.java +++ b/cli/src/main/java/com/liferay/blade/cli/Extensions.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -296,9 +297,7 @@ private static Collection _getFlags(Class clazz, boo if ((withArguments && !type.equals(boolean.class)) || (!withArguments && type.equals(boolean.class))) { - for (String name : names) { - flags.add(name); - } + Collections.addAll(flags, names); } } } diff --git a/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java b/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java index a2b2a0c35..efa016188 100644 --- a/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java +++ b/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java @@ -7,6 +7,7 @@ import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; /** * @author David Truong @@ -50,6 +51,8 @@ public class WorkspaceConstants { public static final String DEFAULT_WORKSPACE_PRODUCT_PROPERTY = "liferay.workspace.product"; + public static final Pattern dxpQuarterReleaseVersionPattern = Pattern.compile( + "^(?dxp)-(?2\\d{3})\\.(?q[1234])\\.(?\\d+)$"); public static final List originalLiferayVersions = Arrays.asList("7.0", "7.1", "7.2", "7.3", "7.4"); } \ No newline at end of file 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 00f63c769..093669ccf 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 @@ -1211,12 +1211,10 @@ private Optional _getTargetPlatformVersionFromProduct(String productKey) Path userHomePath = userHomeDir.toPath(); - Path productInfoPath = userHomePath.resolve(".liferay/workspace/.product_info.json"); + Path productInfoPath = userHomePath.resolve(".liferay/workspace/releases.json"); if (!Files.exists(productInfoPath)) { - Map productInfos = BladeUtil.getProductInfos(); - - ProductInfo productInfo = new ProductInfo((Map)productInfos.get(productKey)); + ProductInfo productInfo = BladeUtil.getProductInfo(productKey); return Optional.of(productInfo.getTargetPlatformVersion()); } 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 ce24ea41b..d4d98c1b1 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 @@ -179,10 +179,10 @@ else if (defaultModulesDirSet) { return; } - String templateValidateStrig = _checkTemplateVersionRange(templateFile, projectTemplatesArgs); + String templateValidateString = _checkTemplateVersionRange(templateFile, projectTemplatesArgs); - if (!StringUtil.isNullOrEmpty(templateValidateStrig)) { - getBladeCLI().error(templateValidateStrig); + if (!StringUtil.isNullOrEmpty(templateValidateString)) { + getBladeCLI().error(templateValidateString); return; } @@ -345,7 +345,7 @@ protected ProjectTemplatesArgs getProjectTemplateArgs( WorkspaceProvider workspaceProvider = bladeCLI.getWorkspaceProvider(dir); projectTemplatesArgs.setDependencyManagementEnabled( - (workspaceProvider != null) ? workspaceProvider.isDependencyManagementEnabled(dir) : false); + (workspaceProvider != null) && workspaceProvider.isDependencyManagementEnabled(dir)); Optional liferayVersion = _getLiferayVersion(workspaceProvider, createArgs); @@ -353,8 +353,6 @@ protected ProjectTemplatesArgs getProjectTemplateArgs( throw new IOException("Cannot determine Liferay Version. Please enter a valid value for Liferay Version."); } - projectTemplatesArgs.setLiferayVersion(liferayVersion.get()); - projectTemplatesArgs.setName(name); projectTemplatesArgs.setPackageName(createArgs.getPackageName()); @@ -364,6 +362,25 @@ protected ProjectTemplatesArgs getProjectTemplateArgs( projectTemplatesArgs.setTemplate(template); + Matcher dxpQuarterlyVersionMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( + liferayVersion.get()); + + if (dxpQuarterlyVersionMatcher.matches() && product.isPresent() && Objects.equals(product.get(), "dxp")) { + String projectTemplate = projectTemplatesArgs.getTemplate(); + + switch (projectTemplate) { + case _TEMPLATE_PORTLET_PROVIDER_NAME: + projectTemplatesArgs.setLiferayVersion("7.4.13.u86"); + case _TEMPLATE_SIMULATION_PANEL_NAME: + projectTemplatesArgs.setLiferayVersion("7.4.13.u72"); + default: + projectTemplatesArgs.setLiferayVersion("7.4"); + } + } + else { + projectTemplatesArgs.setLiferayVersion(liferayVersion.get()); + } + return projectTemplatesArgs; } @@ -475,6 +492,15 @@ private String _checkTemplateVersionRange(File templateFile, ProjectTemplatesArg try (InputStream fileInputStream = Files.newInputStream(templateFile.toPath(), StandardOpenOption.READ); JarInputStream in = new JarInputStream(fileInputStream)) { + String versionString = projectTemplatesArgs.getLiferayVersion(); + + Matcher dxpQuarterlyVersionMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( + versionString); + + if (dxpQuarterlyVersionMatcher.matches()) { + return ""; + } + Manifest manifest = in.getManifest(); Attributes attributes = manifest.getMainAttributes(); @@ -483,17 +509,13 @@ private String _checkTemplateVersionRange(File templateFile, ProjectTemplatesArg VersionRange versionRange = new VersionRange(versionRangeValue); - String versionString = projectTemplatesArgs.getLiferayVersion(); - - String liferayVersionString = new String( - String.valueOf(VersionUtil.getMajorVersion(versionString)) + "." + - String.valueOf(VersionUtil.getMinorVersion(versionString))); + String liferayVersionString = + VersionUtil.getMajorVersion(versionString) + "." + VersionUtil.getMinorVersion(versionString); if (!versionRange.includes(Version.parseVersion(liferayVersionString))) { - return new String( - "Error: The " + projectTemplatesArgs.getTemplate() + - " project can only be created in liferay version range: " + versionRange + - ", current liferay version is " + liferayVersionString + "."); + return "Error: The " + projectTemplatesArgs.getTemplate() + + " project can only be created in liferay version range: " + versionRange + + ", current liferay version is " + liferayVersionString + "."; } } catch (Exception exception) { @@ -646,6 +668,10 @@ private boolean _isWorkspaceDir(File dir) { return bladeCLI.isWorkspaceDir(dir); } + private static final String _TEMPLATE_PORTLET_PROVIDER_NAME = "portlet-provider"; + + private static final String _TEMPLATE_SIMULATION_PANEL_NAME = "simulation-panel-entry"; + private Pattern _inValidNamePattern = Pattern.compile("((-)\\2+)"); } \ No newline at end of file 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 35f602619..744df54e6 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 @@ -9,6 +9,7 @@ import com.liferay.blade.cli.BladeCLI; import com.liferay.blade.cli.BladeSettings; +import com.liferay.blade.cli.WorkspaceConstants; import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.gradle.GradleExec; import com.liferay.blade.cli.util.BladeUtil; @@ -36,6 +37,7 @@ import java.util.Optional; import java.util.Properties; import java.util.Set; +import java.util.regex.Matcher; /** * @author Gregory Amerson @@ -191,7 +193,7 @@ public void execute() throws Exception { String liferayVersion; String workspaceProductKey; - Map productInfos = BladeUtil.getProductInfos(initArgs.isTrace(), bladeCLI.error()); + Map releasesInfos = BladeUtil.getReleasesInfos(initArgs.isTrace(), bladeCLI.error()); if (!mavenBuild) { workspaceProductKey = _getDefaultProductKey(initArgs); @@ -204,7 +206,7 @@ public void execute() throws Exception { return; } - Object productInfoObject = productInfos.get(workspaceProductKey); + Object productInfoObject = releasesInfos.get(workspaceProductKey); if (productInfoObject == null) { _addError("Unable to get product info for selected version " + workspaceProductKey); @@ -212,21 +214,31 @@ public void execute() throws Exception { return; } - ProductInfo productInfo = new ProductInfo((Map)productInfoObject); + ProductInfo productInfo = BladeUtil.getProductInfo(workspaceProductKey); - Version targetPlatformVersion = _makeCompatibleVersion(productInfo.getTargetPlatformVersion()); + String targetPlatformVersion = productInfo.getTargetPlatformVersion(); - liferayVersion = new String( - targetPlatformVersion.getMajor() + "." + targetPlatformVersion.getMinor() + "." + - targetPlatformVersion.getMicro()); + Matcher targetPlatformMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( + targetPlatformVersion); + + if (targetPlatformMatcher.matches()) { + liferayVersion = "7.4"; + } + else { + Version normalTargetPlatformVersion = _makeCompatibleVersion(targetPlatformVersion); + + liferayVersion = + normalTargetPlatformVersion.getMajor() + "." + normalTargetPlatformVersion.getMinor() + "." + + normalTargetPlatformVersion.getMicro(); + } } else { - workspaceProductKey = _setProductAndVersionForMaven(productInfos, initArgs); + workspaceProductKey = _setProductAndVersionForMaven(releasesInfos, initArgs); liferayVersion = initArgs.getLiferayVersion(); } - Object productInfoObject = productInfos.get(workspaceProductKey); + Object productInfoObject = releasesInfos.get(workspaceProductKey); if (productInfoObject == null) { _addError("Unable to get product info for selected version " + workspaceProductKey); @@ -487,33 +499,28 @@ private String _setProductAndVersionForMaven(Map productInfos, I if (possibleProductKey.startsWith("portal") || possibleProductKey.startsWith("dxp") || possibleProductKey.startsWith("commerce")) { - Object productInfoObject = productInfos.get(possibleProductKey); - - if (Objects.nonNull(productInfoObject)) { - ProductInfo productInfo = new ProductInfo((Map)productInfoObject); + ProductInfo productInfo = BladeUtil.getProductInfo(possibleProductKey); - initArgs.setLiferayVersion(productInfo.getTargetPlatformVersion()); + initArgs.setLiferayVersion(productInfo.getTargetPlatformVersion()); - String[] productKeyValues = possibleProductKey.split("-"); + String[] productKeyValues = possibleProductKey.split("-"); - initArgs.setLiferayProduct(productKeyValues[0]); + initArgs.setLiferayProduct(productKeyValues[0]); - return possibleProductKey; - } + return possibleProductKey; } - else { - for (Map.Entry entryKey : productInfos.entrySet()) { - ProductInfo productInfo = new ProductInfo((Map)entryKey.getValue()); - if (Objects.equals(possibleProductKey, productInfo.getTargetPlatformVersion())) { - possibleProductKey = entryKey.getKey(); + for (Map.Entry entryKey : productInfos.entrySet()) { + ProductInfo productInfo = BladeUtil.getProductInfo(entryKey.getKey()); + + if (Objects.equals(possibleProductKey, productInfo.getTargetPlatformVersion())) { + possibleProductKey = entryKey.getKey(); - String[] productKeyValues = possibleProductKey.split("-"); + String[] productKeyValues = possibleProductKey.split("-"); - initArgs.setLiferayProduct(productKeyValues[0]); + initArgs.setLiferayProduct(productKeyValues[0]); - return possibleProductKey; - } + return possibleProductKey; } } 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 b43158c1c..7f5c88bff 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 @@ -9,11 +9,12 @@ import com.liferay.blade.cli.WorkspaceConstants; import com.liferay.blade.cli.util.BladeUtil; -import com.liferay.blade.cli.util.ProductKeyUtil; -import com.liferay.project.templates.extensions.util.VersionUtil; +import com.liferay.blade.cli.util.ProductKeyInfo; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.Set; /** @@ -30,15 +31,22 @@ public List get() { public void validate(String name, String value) throws ParameterException { List possibleValues = new ArrayList<>(get()); - Set allTargetPlatformVersions = BladeUtil.getWorkspaceProductTargetPlatformVersions(false); + Map workspaceProductTargetPlatformVersions = + BladeUtil.getWorkspaceProductTargetPlatformVersions(false); possibleValues.addAll(WorkspaceConstants.originalLiferayVersions); - if ((!possibleValues.contains(value) && !allTargetPlatformVersions.contains(value)) || - (!ProductKeyUtil.verifyPortalDxpWorkspaceProduct(value) && !VersionUtil.isLiferayVersion(value) && - !ProductKeyUtil.verifyCommerceWorkspaceProduct(value))) { + Set allTargetPlatformVersions = workspaceProductTargetPlatformVersions.keySet(); - throw new ParameterException(value + " is not a valid value."); + if (!possibleValues.contains(value) && !allTargetPlatformVersions.contains(value)) { + ProductKeyInfo productKeyInfo = workspaceProductTargetPlatformVersions.get(value); + + if (!productKeyInfo.isQuarterly() && + !(Objects.equals(productKeyInfo.getProduct(), "dxp") || + Objects.equals(productKeyInfo.getProduct(), "portal"))) { + + 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 fd7b24ce3..18278e1c9 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 @@ -17,7 +17,6 @@ import java.io.File; import java.io.FilenameFilter; -import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Properties; @@ -75,17 +74,13 @@ public String getLiferayVersion(File workspaceDir) { if (!baseLiferayVersion.isPresent()) { String productKey = gradleProperties.getProperty(WorkspaceConstants.DEFAULT_WORKSPACE_PRODUCT_PROPERTY); - Map productInfoMap = BladeUtil.getProductInfos(); + ProductInfo productInfo = BladeUtil.getProductInfo(productKey); - ProductInfo productInfo = new ProductInfo((Map)productInfoMap.get(productKey)); - - if (productInfo != null) { - baseLiferayVersion = Optional.ofNullable( - productInfo.getTargetPlatformVersion() - ).filter( - BladeUtil::isNotEmpty - ); - } + baseLiferayVersion = Optional.ofNullable( + productInfo.getTargetPlatformVersion() + ).filter( + BladeUtil::isNotEmpty + ); } if (!baseLiferayVersion.isPresent()) { @@ -144,6 +139,13 @@ else if (dockerImageProperty.contains("dxp")) { } } else { + Matcher dxpQuarterlyVersionMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( + targetPlatformVersion); + + if (dxpQuarterlyVersionMatcher.matches()) { + return "dxp"; + } + Version version = Version.parseVersion(targetPlatformVersion.replaceAll("-", ".")); int microVersion = version.getMicro(); diff --git a/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java b/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java index a749b28d5..682d4123c 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java @@ -37,6 +37,8 @@ import java.security.CodeSource; import java.security.ProtectionDomain; +import java.text.MessageFormat; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -49,7 +51,6 @@ import java.util.Objects; import java.util.Properties; import java.util.Scanner; -import java.util.Set; import java.util.function.Predicate; import java.util.jar.Attributes; import java.util.jar.JarFile; @@ -318,23 +319,71 @@ public static String getManifestProperty(Path pathToJar, String propertyName) th } } - public static Map getProductInfos() { - return getProductInfos(false, null); + @SuppressWarnings("unchecked") + public static ProductInfo getProductInfo(String productKey) { + Map releasesInfos = getReleasesInfos(); + + ProductKeyInfo productKeyInfo = new ProductKeyInfo( + productKey, (Map)releasesInfos.get(productKey)); + + return new ProductInfo( + productKeyInfo, getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())); + } + + public static Properties getProperties(File file) { + Properties properties = new Properties(); + + try (InputStream inputStream = Files.newInputStream(file.toPath())) { + properties.load(inputStream); + } + catch (Exception exception) { + } + + return properties; + } + + public static Properties getReleaseProperties(String product, String productKey) { + Properties properties = new Properties(); + + File productDest = new File(_workspaceCacheDir, product); + + File releasePropertiesDest = new File(productDest, productKey); + + try { + String actualReleasePropertiesUrl = MessageFormat.format(_RELEASE_PROPERTIES_URL, product, productKey); + + Path releasesPropertiesPath = downloadFile( + actualReleasePropertiesUrl, releasePropertiesDest.toPath(), "release.properties"); + + if (Files.exists(releasesPropertiesPath)) { + try (InputStream input = Files.newInputStream(releasesPropertiesPath)) { + properties.load(input); + } + } + } + catch (Exception exception) { + } + + return properties; + } + + public static Map getReleasesInfos() { + return getReleasesInfos(false, null); } @SuppressWarnings("unchecked") - public static synchronized Map getProductInfos(boolean trace, PrintStream printStream) { - if (!_productInfoMap.isEmpty()) { - return _productInfoMap; + public static synchronized Map getReleasesInfos(boolean trace, PrintStream printStream) { + if (!_releasesInfoMap.isEmpty()) { + return _releasesInfoMap; } JsonSlurper jsonSlurper = new JsonSlurper(); try { - Path productInfoPath = downloadFile(_PRODUCT_INFO_URL, _workspaceCacheDir.toPath(), ".product_info.json"); + Path releasesInfoPath = downloadFile(_RELEASE_INFO_URL, _workspaceCacheDir.toPath(), "releases.json"); - try (BufferedReader reader = Files.newBufferedReader(productInfoPath)) { - _productInfoMap = (Map)jsonSlurper.parse(reader); + try (BufferedReader reader = Files.newBufferedReader(releasesInfoPath)) { + _releasesInfoMap = (Map)jsonSlurper.parse(reader); } } catch (Exception exception1) { @@ -342,8 +391,8 @@ public static synchronized Map getProductInfos(boolean trace, Pr exception1.printStackTrace(printStream); } - try (InputStream resourceAsStream = BladeUtil.class.getResourceAsStream("/.product_info.json")) { - _productInfoMap = (Map)jsonSlurper.parse(resourceAsStream); + try (InputStream resourceAsStream = BladeUtil.class.getResourceAsStream("/releases.json")) { + _releasesInfoMap = (Map)jsonSlurper.parse(resourceAsStream); } catch (Exception exception2) { if (trace && (printStream != null)) { @@ -352,19 +401,7 @@ public static synchronized Map getProductInfos(boolean trace, Pr } } - return _productInfoMap; - } - - public static Properties getProperties(File file) { - Properties properties = new Properties(); - - try (InputStream inputStream = Files.newInputStream(file.toPath())) { - properties.load(inputStream); - } - catch (Exception exception) { - } - - return properties; + return _releasesInfoMap; } public static Collection getTemplateNames(BladeCLI blade) throws Exception { @@ -391,25 +428,29 @@ public static Map getTemplates(BladeCLI bladeCLI) throws Excepti @SuppressWarnings("unchecked") public static List getWorkspaceProductKeys(boolean promoted) { - Map productInfos = getProductInfos(); + Map releasesInfos = getReleasesInfos(); - return productInfos.keySet( + return releasesInfos.keySet( ).stream( ).filter( - key -> Objects.nonNull(productInfos.get(key)) + key -> Objects.nonNull(releasesInfos.get(key)) ).filter( key -> { - ProductInfo productInfo = new ProductInfo((Map)productInfos.get(key)); + ProductKeyInfo productKeyInfo = new ProductKeyInfo(key, (Map)releasesInfos.get(key)); - if (productInfo.getTargetPlatformVersion() == null) { + if (productKeyInfo.getProduct() == null) { return false; } - if (promoted && !productInfo.isPromoted()) { + if (!promoted || productKeyInfo.isPromoted()) { + return true; + } + + if (productKeyInfo.getReleaseDate() == null) { return false; } - return true; + return productKeyInfo.getLiferayProductVersion() != null; } ).sorted( ProductKeyUtil.comparator @@ -419,21 +460,24 @@ public static List getWorkspaceProductKeys(boolean promoted) { } @SuppressWarnings("unchecked") - public static Set getWorkspaceProductTargetPlatformVersions(boolean promoted) { - Map productInfos = getProductInfos(); + public static Map getWorkspaceProductTargetPlatformVersions(boolean promoted) { + Map releasesInfos = getReleasesInfos(); - return productInfos.entrySet( + return releasesInfos.entrySet( ).stream( ).filter( - entry -> Objects.nonNull(productInfos.get(entry.getKey())) + entry -> Objects.nonNull(releasesInfos.get(entry.getKey())) ).map( - entry -> new ProductInfo((Map)entry.getValue()) + entry -> new ProductKeyInfo(entry.getKey(), (Map)entry.getValue()) ).filter( - product -> Objects.nonNull(product.getTargetPlatformVersion()) && (!promoted || product.isPromoted()) + productKeyInfo -> !promoted || productKeyInfo.isPromoted() ).map( - ProductInfo::getTargetPlatformVersion + productKeyInfo -> new ProductInfo( + productKeyInfo, getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())) + ).filter( + productInfo -> Objects.nonNull(productInfo.getTargetPlatformVersion()) ).collect( - Collectors.toSet() + Collectors.toMap(ProductInfo::getTargetPlatformVersion, ProductInfo::getProductKey) ); } @@ -857,10 +901,14 @@ private static Path _downloadFile( private static final String _GRADLEW_WINDOWS_FILE_NAME = "gradlew.bat"; - private static final String _PRODUCT_INFO_URL = "https://releases.liferay.com/tools/workspace/.product_info.json"; + //private static final String _PRODUCT_INFO_URL = "https://releases.liferay.com/tools/workspace/.product_info.json"; + + private static final String _RELEASE_INFO_URL = "http://localhost:3000/releases.json"; + + private static final String _RELEASE_PROPERTIES_URL = "http://localhost:3000/{0}/{1}/release.properties"; private static final Pattern _microPattern = Pattern.compile("((([efs])p)|(ga)|(u))([0-9]+)(-[0-9]+)?"); - private static Map _productInfoMap = Collections.emptyMap(); + private static Map _releasesInfoMap = Collections.emptyMap(); private static final File _workspaceCacheDir = new File( System.getProperty("user.home"), _DEFAULT_WORKSPACE_CACHE_DIR_NAME); diff --git a/cli/src/main/java/com/liferay/blade/cli/util/NodeUtil.java b/cli/src/main/java/com/liferay/blade/cli/util/NodeUtil.java index 86098f2cf..ac385d4a7 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/NodeUtil.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/NodeUtil.java @@ -16,6 +16,7 @@ import java.nio.file.attribute.PosixFilePermissions; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.stream.Stream; @@ -111,9 +112,7 @@ public static int runLiferayCli(String liferayVersion, File dir, String[] args, commands.add(nodePath.toString()); commands.add(liferayPath.toString()); - for (String arg : args) { - commands.add(arg); - } + Collections.addAll(commands, args); } else { Path nodePath = nodeDirPath.resolve("bin/node"); @@ -192,9 +191,7 @@ public static int runYo(String liferayVersion, File dir, String[] args, boolean commands.add(nodePath.toString()); commands.add(yoPath.toString()); - for (String arg : args) { - commands.add(arg); - } + Collections.addAll(commands, args); } else { Path nodePath = nodeDirPath.resolve("bin/node"); diff --git a/cli/src/main/java/com/liferay/blade/cli/util/ProductInfo.java b/cli/src/main/java/com/liferay/blade/cli/util/ProductInfo.java index ba99a8818..3502c2522 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/ProductInfo.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/ProductInfo.java @@ -5,8 +5,7 @@ package com.liferay.blade.cli.util; -import java.util.Map; -import java.util.Optional; +import java.util.Properties; /** * @author Simon Jiang @@ -14,14 +13,15 @@ */ public class ProductInfo { - public ProductInfo(Map productMap) { - _appServerTomcatVersion = _safeGet(productMap, "appServerTomcatVersion", ""); - _bundleUrl = _safeGet(productMap, "bundleUrl", ""); - _liferayDockerImage = _safeGet(productMap, "liferayDockerImage", ""); - _liferayProductVersion = _safeGet(productMap, "liferayProductVersion", ""); - _releaseDate = _safeGet(productMap, "releaseDate", ""); - _targetPlatformVersion = _safeGet(productMap, "targetPlatformVersion", ""); - _promoted = Boolean.parseBoolean(_safeGet(productMap, "promoted", "false")); + public ProductInfo(ProductKeyInfo productKeyInfo, Properties releaseProperties) { + _productKeyInfo = productKeyInfo; + + _appServerTomcatVersion = releaseProperties.getProperty("app.server.tomcat.version"); + _bundleUrl = releaseProperties.getProperty("bundle.url"); + _liferayDockerImage = releaseProperties.getProperty("liferay.docker.image"); + _liferayProductVersion = releaseProperties.getProperty("liferay.product.version"); + _releaseDate = releaseProperties.getProperty("release.date"); + _targetPlatformVersion = releaseProperties.getProperty("target.platform.version"); } public String getAppServerTomcatVersion() { @@ -40,6 +40,10 @@ public String getLiferayProductVersion() { return _liferayProductVersion; } + public ProductKeyInfo getProductKey() { + return _productKeyInfo; + } + public String getReleaseDate() { return _releaseDate; } @@ -48,25 +52,11 @@ public String getTargetPlatformVersion() { return _targetPlatformVersion; } - public boolean isPromoted() { - return _promoted; - } - - private String _safeGet(Map map, String key, String defVal) { - return Optional.ofNullable( - map - ).map( - m -> m.get(key) - ).orElse( - defVal - ); - } - private String _appServerTomcatVersion; private String _bundleUrl; private final String _liferayDockerImage; private final String _liferayProductVersion; - private Boolean _promoted = false; + private ProductKeyInfo _productKeyInfo; private final String _releaseDate; private String _targetPlatformVersion; diff --git a/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyInfo.java b/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyInfo.java index 15f13c4a9..944b1b93c 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyInfo.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyInfo.java @@ -5,84 +5,114 @@ package com.liferay.blade.cli.util; +import java.text.SimpleDateFormat; + import java.util.Comparator; +import java.util.Date; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; /** * @author Drew Brokke + * @author Simon Jiang */ public class ProductKeyInfo implements Comparable { + public ProductKeyInfo(String productKey, Map releaseMap) { + _productKey = productKey; + + _releaseDate = _safeGet(releaseMap, "releaseDate", ""); + _liferayProductVersion = _safeGet(releaseMap, "liferayProductVersion", ""); + _product = _safeGet(releaseMap, "product", ""); + + _promoted = Boolean.parseBoolean(_safeGet(releaseMap, "promoted", "false")); + _quarterly = Boolean.parseBoolean(_safeGet(releaseMap, "quarterly", "false")); + } + @Override - public int compareTo(final ProductKeyInfo keyInfo) { + public int compareTo(ProductKeyInfo keyInfo) { return Comparator.comparing( - ProductKeyInfo::getProductRank - ).thenComparing( - ProductKeyInfo::isQuarterly + (Function)productKeyInfo -> { + if (Objects.equals(productKeyInfo.getProduct(), "dxp")) { + return 1; + } + + return -1; + } ).thenComparing( - ProductKeyInfo::getMajorProductKeyVersion + productKeyInfo -> { + if (productKeyInfo.isQuarterly()) { + return 1; + } + + return -1; + } ).thenComparing( - ProductKeyInfo::getMinorProductKeyVersion + productKeyInfo -> ProductKeyUtil.createProductKeyVersion(productKeyInfo.getLiferayProductVersion()) ).thenComparing( - ProductKeyInfo::getMicroProductKeyVersion + ProductKeyInfo::getReleaseDate ).reversed( ).compare( this, keyInfo ); } - public ProductKeyVersion getMajorProductKeyVersion() { - return _majorProductKeyVersion; - } - - public ProductKeyVersion getMicroProductKeyVersion() { - return _microProductKeyVersion; - } - - public ProductKeyVersion getMinorProductKeyVersion() { - return _minorProductKeyVersion; + public String getLiferayProductVersion() { + return _liferayProductVersion; } public String getProduct() { return _product; } - public int getProductRank() { - return _productRank; + public String getProductKey() { + return _productKey; } - public boolean isQuarterly() { - return _quarterly; - } + public Date getReleaseDate() { + try { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - public void setMajorProductKeyVersion(ProductKeyVersion majorProductKeyVersion) { - _majorProductKeyVersion = majorProductKeyVersion; + return format.parse(_releaseDate); + } + catch (Exception exception) { + return null; + } } - public void setMicroProductKeyVersion(ProductKeyVersion microProductKeyVersion) { - _microProductKeyVersion = microProductKeyVersion; + public boolean isPromoted() { + return _promoted; } - public void setMinorProductKeyVersion(ProductKeyVersion minorProductKeyVersion) { - _minorProductKeyVersion = minorProductKeyVersion; + public boolean isQuarterly() { + return _quarterly; } public void setProduct(String product) { _product = product; } - public void setProductRank(int productRank) { - _productRank = productRank; - } - public void setQuarterly(boolean quarterly) { _quarterly = quarterly; } - private ProductKeyVersion _majorProductKeyVersion = ProductKeyVersion.BLANK; - private ProductKeyVersion _microProductKeyVersion = ProductKeyVersion.BLANK; - private ProductKeyVersion _minorProductKeyVersion = ProductKeyVersion.BLANK; + private String _safeGet(Map map, String key, String defVal) { + return Optional.ofNullable( + map + ).map( + m -> m.get(key) + ).orElse( + defVal + ); + } + + private String _liferayProductVersion; private String _product; - private int _productRank = -1; - private boolean _quarterly = false; + private String _productKey; + private Boolean _promoted = false; + private Boolean _quarterly = false; + private String _releaseDate; } \ No newline at end of file diff --git a/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyUtil.java b/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyUtil.java index 8011dbb6c..092abcf5d 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyUtil.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyUtil.java @@ -5,14 +5,8 @@ package com.liferay.blade.cli.util; -import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; -import java.util.List; -import java.util.Objects; -import java.util.function.Consumer; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.Map; /** * @author Drew Brokke @@ -20,14 +14,6 @@ public class ProductKeyUtil { public static final Comparator comparator = ProductKeyUtil::compare; - public static final Pattern productKeyCommercePattern = Pattern.compile( - "^(?commerce)-(?[1-9]\\.\\d\\.\\d)(?:-(?[1-9]\\.\\d))?$"); - public static final Pattern productKeyDXPNonquarterlyPattern = Pattern.compile( - "^(?dxp)-(?[1-9]\\.\\d)-(?(?:de|ep|fp|ga|sp|u)\\d+)$"); - public static final Pattern productKeyDXPQuarterlyPattern = Pattern.compile( - "^(?dxp)-(?2\\d{3})\\.(?q[1234])\\.(?\\d+)$"); - public static final Pattern productKeyPortalPattern = Pattern.compile( - "^(?portal)-(?[1-9]\\.\\d)-(?ga\\d+)$"); public static int compare(String productKey1, String productKey2) { ProductKeyInfo keyInfo1 = createProductKeyInfo(productKey1); @@ -35,115 +21,29 @@ public static int compare(String productKey1, String productKey2) { return keyInfo1.compareTo(createProductKeyInfo(productKey2)); } + @SuppressWarnings("unchecked") public static ProductKeyInfo createProductKeyInfo(String productKey) { - Matcher matcher = _getFirstMatchingMatcher( - productKey, productKeyDXPQuarterlyPattern, productKeyDXPNonquarterlyPattern, productKeyPortalPattern, - productKeyCommercePattern); + Map releasesInfos = BladeUtil.getReleasesInfos(); - if (matcher == null) { - throw new IllegalArgumentException(String.format("%s is not a valid Liferay product key\n", productKey)); - } - - ProductKeyInfo productKeyInfo = new ProductKeyInfo(); - - _withGroup( - matcher, "product", - group -> { - productKeyInfo.setProduct(group); - - productKeyInfo.setProductRank(_getProductRank(group)); - }); - _withGroup(matcher, "major", group -> productKeyInfo.setMajorProductKeyVersion(createProductKeyVersion(group))); - _withGroup( - matcher, "minor", - group -> { - ProductKeyVersion minorProductKeyVersion = createProductKeyVersion(group); - - productKeyInfo.setMinorProductKeyVersion(minorProductKeyVersion); - - if (Objects.equals(productKeyInfo.getProduct(), "dxp") && - Objects.equals(minorProductKeyVersion.getType(), "q")) { - - productKeyInfo.setQuarterly(true); - } - }); - _withGroup(matcher, "micro", group -> productKeyInfo.setMicroProductKeyVersion(createProductKeyVersion(group))); - - return productKeyInfo; + return new ProductKeyInfo(productKey, (Map)releasesInfos.get(productKey)); } public static ProductKeyVersion createProductKeyVersion(String versionString) { ProductKeyVersion productKeyVersion = new ProductKeyVersion(); StringBuilder numberStringBuilder = new StringBuilder(); - StringBuilder typeStringBuilder = new StringBuilder(); for (char c : versionString.toCharArray()) { if (Character.isDigit(c)) { numberStringBuilder.append(c); } - else if (Character.isAlphabetic(c)) { - typeStringBuilder.append(c); - } } if (numberStringBuilder.length() > 0) { productKeyVersion.setNumber(Integer.parseInt(numberStringBuilder.toString())); } - productKeyVersion.setType(typeStringBuilder.toString()); - return productKeyVersion; } - public static boolean verifyCommerceWorkspaceProduct(String product) { - return _matchesAny(product, productKeyCommercePattern); - } - - public static boolean verifyPortalDxpWorkspaceProduct(String product) { - return _matchesAny( - product, productKeyDXPQuarterlyPattern, productKeyDXPNonquarterlyPattern, productKeyPortalPattern); - } - - private static Matcher _getFirstMatchingMatcher(String s, Pattern... patterns) { - for (Pattern pattern : patterns) { - Matcher matcher = pattern.matcher(s); - - if (matcher.matches()) { - return matcher; - } - } - - return null; - } - - private static int _getProductRank(String name) { - return _orderedProducts.size() - _orderedProducts.indexOf(name); - } - - private static boolean _matchesAny(String s, Pattern... patterns) { - Matcher matcher = _getFirstMatchingMatcher(s, patterns); - - if (matcher != null) { - return true; - } - - return false; - } - - private static void _withGroup(Matcher matcher, String groupName, Consumer consumer) { - try { - String group = matcher.group(groupName); - - if (group != null) { - consumer.accept(group); - } - } - catch (Exception exception) { - } - } - - private static final List _orderedProducts = Collections.unmodifiableList( - Arrays.asList("dxp", "portal", "commerce")); - } \ No newline at end of file diff --git a/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyVersion.java b/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyVersion.java index 38b7f32ca..64a9f5d4b 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyVersion.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyVersion.java @@ -18,8 +18,6 @@ public class ProductKeyVersion implements Comparable { public int compareTo(final ProductKeyVersion version) { return Comparator.comparingInt( ProductKeyVersion::getNumber - ).thenComparing( - ProductKeyVersion::getType ).compare( this, version ); @@ -29,19 +27,10 @@ public int getNumber() { return _number; } - public String getType() { - return _type; - } - public void setNumber(int number) { _number = number; } - public void setType(String type) { - _type = type; - } - private int _number = 0; - private String _type; } \ 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 19f6c2514..5baeb2158 100644 --- a/cli/src/test/java/com/liferay/blade/cli/BladeTest.java +++ b/cli/src/test/java/com/liferay/blade/cli/BladeTest.java @@ -57,12 +57,12 @@ public class BladeTest extends BladeCLI { public static final String PRODUCT_VERSION_DXP_74_U72 = "dxp-7.4-u72"; - public static final String PRODUCT_VERSION_PORTAL_71 = "portal-7.1-ga4"; - public static final String PRODUCT_VERSION_PORTAL_73 = "portal-7.3-ga8"; public static final String PRODUCT_VERSION_PORTAL_74 = "portal-7.4-ga4"; + public static final String PRODUCT_VERSION_PORTAL_QUARTER_RELEASE = "dxp-2023.q4.2"; + public static BladeTestBuilder builder() { return new BladeTestBuilder(); } @@ -119,7 +119,7 @@ public void run(String[] args) throws Exception { while (scanner.hasNextLine() && !bridj) { String line = scanner.nextLine(); - if ((line != null) && (line.length() > 0)) { + if ((line != null) && !line.isEmpty()) { if (line.contains("org/bridj/Platform$DeleteFiles")) { bridj = true; } diff --git a/cli/src/test/java/com/liferay/blade/cli/command/CreateCommandTest.java b/cli/src/test/java/com/liferay/blade/cli/command/CreateCommandTest.java index d43acab3c..78cf63bcb 100644 --- a/cli/src/test/java/com/liferay/blade/cli/command/CreateCommandTest.java +++ b/cli/src/test/java/com/liferay/blade/cli/command/CreateCommandTest.java @@ -49,6 +49,7 @@ import org.junit.Assert; import org.junit.Assume; import org.junit.Before; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -457,6 +458,34 @@ public void testCreateMVCPortletInteractive() throws Exception { _checkGradleBuildFiles(projectDir.getAbsolutePath()); } + @Ignore + @Test + public void testCreateMVCPortletQuarterRelease() throws Exception { + File workspace = new File(_rootDir, "workspace"); + + _makeWorkspaceVersion(workspace, BladeTest.PRODUCT_VERSION_PORTAL_QUARTER_RELEASE); + + String[] gradleArgs = {"create", "--base", workspace.getAbsolutePath(), "-t", "mvc-portlet", "foo"}; + + File projectDir = new File(workspace, "modules/foo"); + + String projectPath = projectDir.getAbsolutePath(); + + TestUtil.runBlade(workspace, _extensionsDir, gradleArgs); + + _checkGradleBuildFiles(projectPath); + + _contains( + _checkFileExists(projectPath + "/src/main/java/foo/portlet/FooPortlet.java"), + ".*^public class FooPortlet extends MVCPortlet.*$"); + + _checkFileExists(projectPath + "/build.gradle"); + + _checkFileExists(projectPath + "/src/main/resources/META-INF/resources/view.jsp"); + + _checkFileExists(projectPath + "/src/main/resources/META-INF/resources/init.jsp"); + } + @Test public void testCreateNpmAngular71() throws Exception { File workspace = new File(_rootDir, "workspace"); 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 e6bcabf47..f8e5affd9 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 @@ -27,6 +27,7 @@ import org.junit.Assert; import org.junit.Before; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -121,6 +122,20 @@ public void testBladeInitEmptyDirectoryHandleTwoDots() throws Exception { Assert.assertNotNull(bladeTest.getWorkspaceProvider(emptyDir)); } + @Ignore + @Test + public void testBladeInitQuarterRelease() throws Exception { + File emptyDir = temporaryFolder.newFolder(); + + String[] args = {"--base", emptyDir.getPath(), "init", "-v", BladeTest.PRODUCT_VERSION_PORTAL_QUARTER_RELEASE}; + + BladeTest bladeTest = _getBladeTestCustomWorkspace(emptyDir); + + bladeTest.run(args); + + Assert.assertNotNull(bladeTest.getWorkspaceProvider(emptyDir)); + } + @Test public void testBladeInitUpgradePluginsSDKTo70() throws Exception { File testdir = new File(temporaryFolder.getRoot(), "build/testUpgradePluginsSDKTo70"); @@ -347,7 +362,7 @@ public void testInitCommandListPromoted() throws Exception { String firstLine = lines.get(0); - Assert.assertTrue(firstLine, firstLine.contains("dxp-7.4-")); + Assert.assertTrue(firstLine, firstLine.contains("dxp-")); } @Test @@ -668,7 +683,7 @@ private void _verifyGradleBuild() throws Exception { GradleRunnerUtil.verifyBuildOutput(projectPath.toString(), "foo-1.0.0.jar"); } - private static final String _GRADLE_PLUGINS_WORKSPACE_VERSION = "9.0.12"; + private static final String _GRADLE_PLUGINS_WORKSPACE_VERSION = "9.1.3"; private File _extensionsDir = null; private File _workspaceDir = null; diff --git a/cli/src/test/java/com/liferay/blade/cli/command/JavaProcesses.java b/cli/src/test/java/com/liferay/blade/cli/command/JavaProcesses.java index 2202b35ab..14a4feb10 100644 --- a/cli/src/test/java/com/liferay/blade/cli/command/JavaProcesses.java +++ b/cli/src/test/java/com/liferay/blade/cli/command/JavaProcesses.java @@ -179,10 +179,7 @@ public static void main(String[] args) { tomcatFilter ).findAny(); - System.out.println( - "tomcatProcess = " + - tomcatProcess.get( - ).getId()); + tomcatProcess.ifPresent(javaProcess -> System.out.println("tomcatProcess = " + javaProcess.getId())); } public static int maxProcessId() { diff --git a/cli/src/test/java/com/liferay/blade/cli/command/ServerStartCommandTest.java b/cli/src/test/java/com/liferay/blade/cli/command/ServerStartCommandTest.java index 91897a25f..d6f742a53 100644 --- a/cli/src/test/java/com/liferay/blade/cli/command/ServerStartCommandTest.java +++ b/cli/src/test/java/com/liferay/blade/cli/command/ServerStartCommandTest.java @@ -501,7 +501,7 @@ private String[] _getDebugArgs(String[] serverStartArgs) { private void _initBladeWorkspace() throws Exception { String[] initArgs = { - "--base", _testWorkspacePath.toString(), "init", "-v", BladeTest.PRODUCT_VERSION_PORTAL_71 + "--base", _testWorkspacePath.toString(), "init", "-v", BladeTest.PRODUCT_VERSION_PORTAL_74 }; TestUtil.runBlade(_testWorkspacePath, _extensionsPath, initArgs); 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..8cbabe215 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 @@ -7,6 +7,7 @@ import aQute.bnd.version.Version; +import com.liferay.blade.cli.WorkspaceConstants; import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.util.BladeUtil; import com.liferay.blade.extensions.maven.profile.internal.MavenUtil; @@ -14,6 +15,7 @@ import java.io.File; import java.util.Properties; +import java.util.regex.Matcher; /** * @author Christopher Bryan Boyd @@ -35,6 +37,13 @@ public String getProduct(File workspaceDir) { return "portal"; } + Matcher dxpQuarterlyVersionMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( + targetPlatformVersion); + + if (dxpQuarterlyVersionMatcher.matches()) { + return "dxp"; + } + try { Version version = Version.parseVersion(targetPlatformVersion.replaceAll("-", ".")); diff --git a/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/ClientExtensionProjectTemplateCustomizer.java b/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/ClientExtensionProjectTemplateCustomizer.java index f9b511998..b85d204bf 100644 --- a/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/ClientExtensionProjectTemplateCustomizer.java +++ b/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/ClientExtensionProjectTemplateCustomizer.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; import org.apache.maven.archetype.ArchetypeGenerationRequest; @@ -82,28 +81,7 @@ public void onBeforeGenerateProject( String projectName = projectTemplatesArgs.getName(); - List args = new ArrayList<>(); - - args.add("generate"); - args.add("-i"); - args.add(projectName); - - ClientExtensionProjectTemplatesArgsExt clientExtensionTemplateExt = - (ClientExtensionProjectTemplatesArgsExt)projectTemplatesArgs.getProjectTemplatesArgsExt(); - - String extensionName = clientExtensionTemplateExt.getExtensionName(); - - if (extensionName != null) { - args.add("-n"); - args.add(extensionName); - } - - String extensionType = clientExtensionTemplateExt.getExtensionType(); - - if (extensionType != null) { - args.add("-t"); - args.add(extensionType); - } + List args = _getProjectTemplatesArgs(projectTemplatesArgs, projectName); File destinationDir = projectTemplatesArgs.getDestinationDir(); @@ -153,12 +131,10 @@ private static String _getTargetPlatformVersionFromProduct(String productKey) { Path userHomePath = userHomeDir.toPath(); - Path productInfoPath = userHomePath.resolve(".liferay/workspace/.product_info.json"); + Path productInfoPath = userHomePath.resolve(".liferay/workspace/releases.json"); if (!Files.exists(productInfoPath)) { - Map productInfos = BladeUtil.getProductInfos(); - - ProductInfo productInfo = new ProductInfo((Map)productInfos.get(productKey)); + ProductInfo productInfo = BladeUtil.getProductInfo(productKey); return productInfo.getTargetPlatformVersion(); } @@ -183,6 +159,33 @@ private static String _getTargetPlatformVersionFromProduct(String productKey) { return null; } + private List _getProjectTemplatesArgs(ProjectTemplatesArgs projectTemplatesArgs, String projectName) { + List args = new ArrayList<>(); + + args.add("generate"); + args.add("-i"); + args.add(projectName); + + ClientExtensionProjectTemplatesArgsExt clientExtensionTemplateExt = + (ClientExtensionProjectTemplatesArgsExt)projectTemplatesArgs.getProjectTemplatesArgsExt(); + + String extensionName = clientExtensionTemplateExt.getExtensionName(); + + if (extensionName != null) { + args.add("-n"); + args.add(extensionName); + } + + String extensionType = clientExtensionTemplateExt.getExtensionType(); + + if (extensionType != null) { + args.add("-t"); + args.add(extensionType); + } + + return args; + } + private static final String _BASE_BOM_URL = "https://repository.liferay.com/nexus/content/groups/public/com/liferay/portal/"; diff --git a/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/LXCUtil.java b/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/LXCUtil.java index aa35dddba..c900a7b0c 100644 --- a/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/LXCUtil.java +++ b/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/LXCUtil.java @@ -25,6 +25,7 @@ import java.security.MessageDigest; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; @@ -126,9 +127,7 @@ public static int run(Path dir, String[] args, Map env, boolean commands.add("/c"); commands.add(lxcPath.toString()); - for (String arg : args) { - commands.add(arg); - } + Collections.addAll(commands, args); } else { commands.add("sh"); From 02c24bad1af8273bad433cc4ebc3ef255a0ba1b2 Mon Sep 17 00:00:00 2001 From: Simon Jiang Date: Sat, 10 Feb 2024 18:14:35 +0800 Subject: [PATCH 2/3] BLADE-742 replace productInfo with releaseInfo --- .../java/com/liferay/blade/cli/BladeCLI.java | 6 +++--- .../liferay/blade/cli/WorkspaceConstants.java | 2 -- .../blade/cli/command/ConvertCommand.java | 6 +++--- .../blade/cli/command/CreateCommand.java | 11 +++++------ .../blade/cli/command/InitCommand.java | 19 ++++++++----------- .../cli/gradle/GradleWorkspaceProvider.java | 15 +++++++++------ .../com/liferay/blade/cli/util/BladeUtil.java | 10 +++++----- .../{ProductInfo.java => ReleaseInfo.java} | 4 ++-- .../java/com/liferay/blade/cli/BladeTest.java | 2 +- .../blade/cli/command/InitCommandTest.java | 1 - .../maven/profile/MavenWorkspaceProvider.java | 9 ++++++--- ...entExtensionProjectTemplateCustomizer.java | 6 +++--- 12 files changed, 45 insertions(+), 46 deletions(-) rename cli/src/main/java/com/liferay/blade/cli/util/{ProductInfo.java => ReleaseInfo.java} (94%) 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 624f48dbb..973ec303c 100644 --- a/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java +++ b/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java @@ -30,7 +30,7 @@ import com.liferay.blade.cli.util.FileUtil; import com.liferay.blade.cli.util.Pair; import com.liferay.blade.cli.util.ProcessesUtil; -import com.liferay.blade.cli.util.ProductInfo; +import com.liferay.blade.cli.util.ReleaseInfo; import com.liferay.blade.cli.util.ProductKeyInfo; import com.liferay.blade.cli.util.Prompter; @@ -873,11 +873,11 @@ private Map _buildMavenPossibleValuesMap( ProductKeyInfo productKeyInfo = new ProductKeyInfo( option, (Map)releasesInfos.get(option)); - ProductInfo productInfo = new ProductInfo( + ReleaseInfo releaseInfo = new ReleaseInfo( productKeyInfo, BladeUtil.getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())); - optionsMap.put(String.valueOf(x), productInfo.getTargetPlatformVersion()); + optionsMap.put(String.valueOf(x), releaseInfo.getTargetPlatformVersion()); } return optionsMap; diff --git a/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java b/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java index efa016188..638b292f1 100644 --- a/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java +++ b/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java @@ -51,8 +51,6 @@ public class WorkspaceConstants { public static final String DEFAULT_WORKSPACE_PRODUCT_PROPERTY = "liferay.workspace.product"; - public static final Pattern dxpQuarterReleaseVersionPattern = Pattern.compile( - "^(?dxp)-(?2\\d{3})\\.(?q[1234])\\.(?\\d+)$"); public static final List originalLiferayVersions = Arrays.asList("7.0", "7.1", "7.2", "7.3", "7.4"); } \ No newline at end of file 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 093669ccf..02c24382e 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 @@ -13,7 +13,7 @@ import com.liferay.blade.cli.util.CopyDirVisitor; import com.liferay.blade.cli.util.FileUtil; import com.liferay.blade.cli.util.ListUtil; -import com.liferay.blade.cli.util.ProductInfo; +import com.liferay.blade.cli.util.ReleaseInfo; import com.liferay.blade.cli.util.StringUtil; import com.liferay.blade.gradle.model.GradleDependency; import com.liferay.project.templates.extensions.ProjectTemplatesArgs; @@ -1214,9 +1214,9 @@ private Optional _getTargetPlatformVersionFromProduct(String productKey) Path productInfoPath = userHomePath.resolve(".liferay/workspace/releases.json"); if (!Files.exists(productInfoPath)) { - ProductInfo productInfo = BladeUtil.getProductInfo(productKey); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(productKey); - return Optional.of(productInfo.getTargetPlatformVersion()); + return Optional.of(releaseInfo.getTargetPlatformVersion()); } JSONObject jsonObject = new JSONObject(new String(Files.readAllBytes(productInfoPath.normalize()))); 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 d4d98c1b1..c9f1cf627 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,6 +17,7 @@ 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.ReleaseInfo; import com.liferay.blade.cli.util.StringUtil; import com.liferay.project.templates.ProjectTemplates; import com.liferay.project.templates.extensions.ProjectTemplatesArgs; @@ -362,10 +363,9 @@ protected ProjectTemplatesArgs getProjectTemplateArgs( projectTemplatesArgs.setTemplate(template); - Matcher dxpQuarterlyVersionMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( - liferayVersion.get()); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(liferayVersion.get()); - if (dxpQuarterlyVersionMatcher.matches() && product.isPresent() && Objects.equals(product.get(), "dxp")) { + if (releaseInfo.getProductKey().isQuarterly() && product.isPresent() && Objects.equals(product.get(), "dxp")) { String projectTemplate = projectTemplatesArgs.getTemplate(); switch (projectTemplate) { @@ -494,10 +494,9 @@ private String _checkTemplateVersionRange(File templateFile, ProjectTemplatesArg String versionString = projectTemplatesArgs.getLiferayVersion(); - Matcher dxpQuarterlyVersionMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( - versionString); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(versionString); - if (dxpQuarterlyVersionMatcher.matches()) { + if (releaseInfo.getProductKey().isPromoted()) { return ""; } 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 744df54e6..0bf43a415 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 @@ -13,7 +13,7 @@ import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.gradle.GradleExec; import com.liferay.blade.cli.util.BladeUtil; -import com.liferay.blade.cli.util.ProductInfo; +import com.liferay.blade.cli.util.ReleaseInfo; import com.liferay.project.templates.ProjectTemplates; import com.liferay.project.templates.extensions.ProjectTemplatesArgs; import com.liferay.project.templates.extensions.util.FileUtil; @@ -214,14 +214,11 @@ public void execute() throws Exception { return; } - ProductInfo productInfo = BladeUtil.getProductInfo(workspaceProductKey); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(workspaceProductKey); - String targetPlatformVersion = productInfo.getTargetPlatformVersion(); + String targetPlatformVersion = releaseInfo.getTargetPlatformVersion(); - Matcher targetPlatformMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( - targetPlatformVersion); - - if (targetPlatformMatcher.matches()) { + if (releaseInfo.getProductKey().isQuarterly()) { liferayVersion = "7.4"; } else { @@ -499,9 +496,9 @@ private String _setProductAndVersionForMaven(Map productInfos, I if (possibleProductKey.startsWith("portal") || possibleProductKey.startsWith("dxp") || possibleProductKey.startsWith("commerce")) { - ProductInfo productInfo = BladeUtil.getProductInfo(possibleProductKey); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(possibleProductKey); - initArgs.setLiferayVersion(productInfo.getTargetPlatformVersion()); + initArgs.setLiferayVersion(releaseInfo.getTargetPlatformVersion()); String[] productKeyValues = possibleProductKey.split("-"); @@ -511,9 +508,9 @@ private String _setProductAndVersionForMaven(Map productInfos, I } for (Map.Entry entryKey : productInfos.entrySet()) { - ProductInfo productInfo = BladeUtil.getProductInfo(entryKey.getKey()); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(entryKey.getKey()); - if (Objects.equals(possibleProductKey, productInfo.getTargetPlatformVersion())) { + if (Objects.equals(possibleProductKey, releaseInfo.getTargetPlatformVersion())) { possibleProductKey = entryKey.getKey(); String[] productKeyValues = possibleProductKey.split("-"); 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 18278e1c9..1d9a03a3b 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 @@ -12,11 +12,13 @@ import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.command.BaseArgs; import com.liferay.blade.cli.util.BladeUtil; -import com.liferay.blade.cli.util.ProductInfo; +import com.liferay.blade.cli.util.ProductKeyInfo; +import com.liferay.blade.cli.util.ReleaseInfo; import java.io.File; import java.io.FilenameFilter; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Properties; @@ -74,10 +76,10 @@ public String getLiferayVersion(File workspaceDir) { if (!baseLiferayVersion.isPresent()) { String productKey = gradleProperties.getProperty(WorkspaceConstants.DEFAULT_WORKSPACE_PRODUCT_PROPERTY); - ProductInfo productInfo = BladeUtil.getProductInfo(productKey); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(productKey); baseLiferayVersion = Optional.ofNullable( - productInfo.getTargetPlatformVersion() + releaseInfo.getTargetPlatformVersion() ).filter( BladeUtil::isNotEmpty ); @@ -139,10 +141,11 @@ else if (dockerImageProperty.contains("dxp")) { } } else { - Matcher dxpQuarterlyVersionMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( - targetPlatformVersion); + Map workspaceProductTargetPlatformVersions = BladeUtil.getWorkspaceProductTargetPlatformVersions(false); - if (dxpQuarterlyVersionMatcher.matches()) { + ProductKeyInfo productKeyInfo = workspaceProductTargetPlatformVersions.get(targetPlatformVersion); + + if (productKeyInfo.isQuarterly()) { return "dxp"; } diff --git a/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java b/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java index 682d4123c..9a943d877 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java @@ -320,13 +320,13 @@ public static String getManifestProperty(Path pathToJar, String propertyName) th } @SuppressWarnings("unchecked") - public static ProductInfo getProductInfo(String productKey) { + public static ReleaseInfo getReleaseInfo(String productKey) { Map releasesInfos = getReleasesInfos(); ProductKeyInfo productKeyInfo = new ProductKeyInfo( productKey, (Map)releasesInfos.get(productKey)); - return new ProductInfo( + return new ReleaseInfo( productKeyInfo, getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())); } @@ -472,12 +472,12 @@ public static Map getWorkspaceProductTargetPlatformVersi ).filter( productKeyInfo -> !promoted || productKeyInfo.isPromoted() ).map( - productKeyInfo -> new ProductInfo( + productKeyInfo -> new ReleaseInfo( productKeyInfo, getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())) ).filter( - productInfo -> Objects.nonNull(productInfo.getTargetPlatformVersion()) + releaseInfo -> Objects.nonNull(releaseInfo.getTargetPlatformVersion()) ).collect( - Collectors.toMap(ProductInfo::getTargetPlatformVersion, ProductInfo::getProductKey) + Collectors.toMap(ReleaseInfo::getTargetPlatformVersion, ReleaseInfo::getProductKey) ); } diff --git a/cli/src/main/java/com/liferay/blade/cli/util/ProductInfo.java b/cli/src/main/java/com/liferay/blade/cli/util/ReleaseInfo.java similarity index 94% rename from cli/src/main/java/com/liferay/blade/cli/util/ProductInfo.java rename to cli/src/main/java/com/liferay/blade/cli/util/ReleaseInfo.java index 3502c2522..c12c3b00c 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/ProductInfo.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/ReleaseInfo.java @@ -11,9 +11,9 @@ * @author Simon Jiang * @author Gregory Amerson */ -public class ProductInfo { +public class ReleaseInfo { - public ProductInfo(ProductKeyInfo productKeyInfo, Properties releaseProperties) { + public ReleaseInfo(ProductKeyInfo productKeyInfo, Properties releaseProperties) { _productKeyInfo = productKeyInfo; _appServerTomcatVersion = releaseProperties.getProperty("app.server.tomcat.version"); 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 5baeb2158..7b7fbff63 100644 --- a/cli/src/test/java/com/liferay/blade/cli/BladeTest.java +++ b/cli/src/test/java/com/liferay/blade/cli/BladeTest.java @@ -61,7 +61,7 @@ public class BladeTest extends BladeCLI { public static final String PRODUCT_VERSION_PORTAL_74 = "portal-7.4-ga4"; - public static final String PRODUCT_VERSION_PORTAL_QUARTER_RELEASE = "dxp-2023.q4.2"; + public static final String PRODUCT_VERSION_PORTAL_QUARTER_RELEASE = "dxp-2023.q4.4"; public static BladeTestBuilder builder() { return new BladeTestBuilder(); 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 f8e5affd9..298692721 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 @@ -122,7 +122,6 @@ public void testBladeInitEmptyDirectoryHandleTwoDots() throws Exception { Assert.assertNotNull(bladeTest.getWorkspaceProvider(emptyDir)); } - @Ignore @Test public void testBladeInitQuarterRelease() throws Exception { File emptyDir = temporaryFolder.newFolder(); 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 8cbabe215..33ada84c4 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 @@ -10,10 +10,12 @@ import com.liferay.blade.cli.WorkspaceConstants; import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.util.BladeUtil; +import com.liferay.blade.cli.util.ProductKeyInfo; import com.liferay.blade.extensions.maven.profile.internal.MavenUtil; import java.io.File; +import java.util.Map; import java.util.Properties; import java.util.regex.Matcher; @@ -37,10 +39,11 @@ public String getProduct(File workspaceDir) { return "portal"; } - Matcher dxpQuarterlyVersionMatcher = WorkspaceConstants.dxpQuarterReleaseVersionPattern.matcher( - targetPlatformVersion); + Map workspaceProductTargetPlatformVersions = BladeUtil.getWorkspaceProductTargetPlatformVersions(false); - if (dxpQuarterlyVersionMatcher.matches()) { + ProductKeyInfo productKeyInfo = workspaceProductTargetPlatformVersions.get(targetPlatformVersion); + + if (productKeyInfo.isQuarterly()) { return "dxp"; } diff --git a/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/ClientExtensionProjectTemplateCustomizer.java b/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/ClientExtensionProjectTemplateCustomizer.java index b85d204bf..72529180b 100644 --- a/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/ClientExtensionProjectTemplateCustomizer.java +++ b/extensions/project-templates-client-extension/src/main/java/com/liferay/project/templates/client/extension/internal/ClientExtensionProjectTemplateCustomizer.java @@ -8,7 +8,7 @@ import com.liferay.blade.cli.WorkspaceConstants; import com.liferay.blade.cli.gradle.GradleWorkspaceProvider; import com.liferay.blade.cli.util.BladeUtil; -import com.liferay.blade.cli.util.ProductInfo; +import com.liferay.blade.cli.util.ReleaseInfo; import com.liferay.project.templates.extensions.ProjectTemplateCustomizer; import com.liferay.project.templates.extensions.ProjectTemplatesArgs; @@ -134,9 +134,9 @@ private static String _getTargetPlatformVersionFromProduct(String productKey) { Path productInfoPath = userHomePath.resolve(".liferay/workspace/releases.json"); if (!Files.exists(productInfoPath)) { - ProductInfo productInfo = BladeUtil.getProductInfo(productKey); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(productKey); - return productInfo.getTargetPlatformVersion(); + return releaseInfo.getTargetPlatformVersion(); } JSONObject jsonObject = new JSONObject(new String(Files.readAllBytes(productInfoPath.normalize()))); From 3fbf80e58f2b1f50fd0d2a360f4cc9fab396f2cb Mon Sep 17 00:00:00 2001 From: Simon Jiang Date: Sun, 11 Feb 2024 23:01:05 +0800 Subject: [PATCH 3/3] BLADE-742 fix maven workspace init error --- cli/bnd.bnd | 8 +- cli/build.gradle | 4 +- .../java/com/liferay/blade/cli/BladeCLI.java | 8 +- .../liferay/blade/cli/WorkspaceConstants.java | 1 - .../blade/cli/command/BaseCommand.java | 3 + .../blade/cli/command/CreateCommand.java | 8 +- .../blade/cli/command/InitCommand.java | 76 +++++------ .../cli/gradle/GradleWorkspaceProvider.java | 3 +- .../com/liferay/blade/cli/util/BladeUtil.java | 94 +++++--------- .../blade/cli/util/ProductKeyUtil.java | 2 +- .../liferay/blade/cli/util/ResourceUtil.java | 121 ++++++++++++++++++ .../blade/cli/command/InitCommandTest.java | 1 - extensions/maven-profile/build.gradle | 2 +- .../profile/BuildServiceCommandMaven.java | 10 +- .../maven/profile/InitCommandMaven.java | 61 ++++++++- .../maven/profile/MavenWorkspaceProvider.java | 5 +- 16 files changed, 277 insertions(+), 130 deletions(-) create mode 100644 cli/src/main/java/com/liferay/blade/cli/util/ResourceUtil.java diff --git a/cli/bnd.bnd b/cli/bnd.bnd index 483bb0b20..123b9bdfe 100644 --- a/cli/bnd.bnd +++ b/cli/bnd.bnd @@ -3,7 +3,6 @@ Bundle-Version: ${project.version}.${tstamp} JPM-Command: blade Main-Class: com.liferay.blade.cli.BladeCLI Private-Package:\ - aQute.bnd.header;-split-package:=merge-first,\ aQute.bnd.maven;-split-package:=merge-first,\ aQute.bnd.osgi;-split-package:=merge-first,\ aQute.bnd.service;-split-package:=merge-first,\ @@ -18,6 +17,13 @@ Private-Package:\ \ aQute.service.reporter;-split-package:=merge-first,\ \ + com.google.gson,\ + com.google.gson.stream,\ + com.google.gson.internal.bind,\ + com.google.gson.internal,\ + com.google.gson.reflect,\ + com.google.gson.internal.sql,\ + \ com.liferay.blade.gradle.tooling,\ \ groovy.json,\ diff --git a/cli/build.gradle b/cli/build.gradle index 98deb8d44..27f6dc244 100644 --- a/cli/build.gradle +++ b/cli/build.gradle @@ -76,6 +76,7 @@ createWrapperZip { dependencies { api group: "biz.aQute.bnd", name: "biz.aQute.bndlib", version: "5.3.0" + api group: "com.google.code.gson", name: "gson", version: "2.9.0" 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.283" api group: "commons-io", name: "commons-io", version: "2.7" @@ -111,7 +112,7 @@ dependencies { api name: "org.objectweb.asm.tree-6.0.0" api name: "org.objectweb.asm.util-6.0.0" - bladeExtensions group: "com.liferay.blade", name: "com.liferay.blade.extensions.maven.profile", version: "1.0.39-SNAPSHOT" + bladeExtensions group: "com.liferay.blade", name: "com.liferay.blade.extensions.maven.profile", version: "1.0.40-SNAPSHOT" bladeExtensions group: "com.liferay.blade", name: "com.liferay.project.templates.client.extension", version: "1.0.6-SNAPSHOT" bladeExtensions group: "com.liferay.blade", name: "com.liferay.project.templates.js.theme", version: "1.0.22-SNAPSHOT" bladeExtensions group: "com.liferay.blade", name: "com.liferay.project.templates.js.widget", version: "1.0.23-SNAPSHOT" @@ -213,6 +214,7 @@ if (project.hasProperty("release") || project.hasProperty("snapshots")) { } repositories { + mavenLocal() flatDir { dirs "${rootProject.projectDir}/libs" } 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 973ec303c..2b420b3fe 100644 --- a/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java +++ b/cli/src/main/java/com/liferay/blade/cli/BladeCLI.java @@ -30,9 +30,9 @@ import com.liferay.blade.cli.util.FileUtil; import com.liferay.blade.cli.util.Pair; import com.liferay.blade.cli.util.ProcessesUtil; -import com.liferay.blade.cli.util.ReleaseInfo; import com.liferay.blade.cli.util.ProductKeyInfo; import com.liferay.blade.cli.util.Prompter; +import com.liferay.blade.cli.util.ReleaseInfo; import java.io.BufferedReader; import java.io.File; @@ -100,7 +100,7 @@ public static Map> getCommandMapByClassL boolean profileNameIsPresent = false; - if ((profileName != null) && (profileName.length() > 0)) { + if ((profileName != null) && !profileName.isEmpty()) { profileNameIsPresent = true; } @@ -863,7 +863,7 @@ private Map _buildMavenPossibleValuesMap( Iterator it = options.iterator(); - Map releasesInfos = BladeUtil.getReleasesInfos(true, error()); + Map releaseKeyInfos = BladeUtil.getReleaseKeyInfos(true, error()); Map optionsMap = new LinkedHashMap<>(); @@ -871,7 +871,7 @@ private Map _buildMavenPossibleValuesMap( String option = it.next(); ProductKeyInfo productKeyInfo = new ProductKeyInfo( - option, (Map)releasesInfos.get(option)); + option, (Map)releaseKeyInfos.get(option)); ReleaseInfo releaseInfo = new ReleaseInfo( productKeyInfo, diff --git a/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java b/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java index 638b292f1..a2b2a0c35 100644 --- a/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java +++ b/cli/src/main/java/com/liferay/blade/cli/WorkspaceConstants.java @@ -7,7 +7,6 @@ import java.util.Arrays; import java.util.List; -import java.util.regex.Pattern; /** * @author David Truong diff --git a/cli/src/main/java/com/liferay/blade/cli/command/BaseCommand.java b/cli/src/main/java/com/liferay/blade/cli/command/BaseCommand.java index 3681e0984..c15a658f6 100644 --- a/cli/src/main/java/com/liferay/blade/cli/command/BaseCommand.java +++ b/cli/src/main/java/com/liferay/blade/cli/command/BaseCommand.java @@ -20,6 +20,9 @@ public BaseCommand(BladeCLI blade, T args) { _args = args; } + public void commandPostAction() throws Exception { + } + public abstract void execute() throws Exception; public T getArgs() { 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 c9f1cf627..1089026e7 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 @@ -365,7 +365,9 @@ protected ProjectTemplatesArgs getProjectTemplateArgs( ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(liferayVersion.get()); - if (releaseInfo.getProductKey().isQuarterly() && product.isPresent() && Objects.equals(product.get(), "dxp")) { + if (releaseInfo.getProductKey( + ).isQuarterly() && product.isPresent() && Objects.equals(product.get(), "dxp")) { + String projectTemplate = projectTemplatesArgs.getTemplate(); switch (projectTemplate) { @@ -496,7 +498,9 @@ private String _checkTemplateVersionRange(File templateFile, ProjectTemplatesArg ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(versionString); - if (releaseInfo.getProductKey().isPromoted()) { + if (releaseInfo.getProductKey( + ).isPromoted()) { + return ""; } 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 0bf43a415..6571c7c9f 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 @@ -9,7 +9,6 @@ import com.liferay.blade.cli.BladeCLI; import com.liferay.blade.cli.BladeSettings; -import com.liferay.blade.cli.WorkspaceConstants; import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.gradle.GradleExec; import com.liferay.blade.cli.util.BladeUtil; @@ -18,10 +17,9 @@ import com.liferay.project.templates.extensions.ProjectTemplatesArgs; import com.liferay.project.templates.extensions.util.FileUtil; +import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -37,7 +35,6 @@ import java.util.Optional; import java.util.Properties; import java.util.Set; -import java.util.regex.Matcher; /** * @author Gregory Amerson @@ -51,7 +48,6 @@ public InitCommand() { } @Override - @SuppressWarnings("unchecked") public void execute() throws Exception { BladeCLI bladeCLI = getBladeCLI(); @@ -134,7 +130,7 @@ public void execute() throws Exception { _trace("Found Plugins SDK, moving contents to new subdirectory and initing workspace."); - Path tempDir = Files.createTempDirectory("orignal-sdk"); + Path tempDir = Files.createTempDirectory("original-sdk"); temp = tempDir.toFile(); @@ -193,7 +189,7 @@ public void execute() throws Exception { String liferayVersion; String workspaceProductKey; - Map releasesInfos = BladeUtil.getReleasesInfos(initArgs.isTrace(), bladeCLI.error()); + Map releasesInfos = BladeUtil.getReleaseKeyInfos(initArgs.isTrace(), bladeCLI.error()); if (!mavenBuild) { workspaceProductKey = _getDefaultProductKey(initArgs); @@ -216,13 +212,13 @@ public void execute() throws Exception { ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(workspaceProductKey); - String targetPlatformVersion = releaseInfo.getTargetPlatformVersion(); + if (releaseInfo.getProductKey( + ).isQuarterly()) { - if (releaseInfo.getProductKey().isQuarterly()) { liferayVersion = "7.4"; } else { - Version normalTargetPlatformVersion = _makeCompatibleVersion(targetPlatformVersion); + Version normalTargetPlatformVersion = _makeCompatibleVersion(releaseInfo.getTargetPlatformVersion()); liferayVersion = normalTargetPlatformVersion.getMajor() + "." + normalTargetPlatformVersion.getMinor() + "." + @@ -232,15 +228,23 @@ public void execute() throws Exception { else { workspaceProductKey = _setProductAndVersionForMaven(releasesInfos, initArgs); + Object productInfoObject = releasesInfos.get(workspaceProductKey); + + if (productInfoObject == null) { + _addError("Unable to get product info for selected version " + workspaceProductKey); + + return; + } + liferayVersion = initArgs.getLiferayVersion(); - } - Object productInfoObject = releasesInfos.get(workspaceProductKey); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(workspaceProductKey); - if (productInfoObject == null) { - _addError("Unable to get product info for selected version " + workspaceProductKey); + if (releaseInfo.getProductKey( + ).isQuarterly()) { - return; + liferayVersion = "7.4"; + } } if (Objects.equals(initArgs.getLiferayProduct(), "commerce")) { @@ -318,6 +322,8 @@ public void execute() throws Exception { settings.setProfileName(profileName); settings.save(); + + commandPostAction(); } @Override @@ -329,7 +335,7 @@ private void _addError(String msg) { getBladeCLI().addErrors("init", Collections.singleton(msg)); } - private String _getDefaultProductKey(InitArgs initArgs) throws Exception { + private String _getDefaultProductKey(InitArgs initArgs) { String liferayVersion = initArgs.getLiferayVersion(); if (liferayVersion.startsWith("portal") || liferayVersion.startsWith("dxp") || @@ -345,11 +351,7 @@ private String _getDefaultProductKey(InitArgs initArgs) throws Exception { value -> value.startsWith(initArgs.getLiferayProduct() + "-" + initArgs.getLiferayVersion()) ).findFirst(); - if (!defaultVersion.isPresent()) { - return initArgs.getLiferayVersion(); - } - - return defaultVersion.get(); + return defaultVersion.orElseGet(initArgs::getLiferayVersion); } private boolean _isPluginsSDK(File dir) { @@ -357,9 +359,9 @@ private boolean _isPluginsSDK(File dir) { return false; } - List names = Arrays.asList(dir.list()); + List names = Arrays.asList(Objects.requireNonNull(dir.list())); - if ((names != null) && names.contains("portlets") && names.contains("hooks") && names.contains("layouttpl") && + if (names.contains("portlets") && names.contains("hooks") && names.contains("layouttpl") && names.contains("themes") && names.contains("build.properties") && names.contains("build.xml") && names.contains("build-common.xml") && names.contains("build-common-plugin.xml")) { @@ -377,11 +379,7 @@ private boolean _isPluginsSDK70(File dir) { File buildProperties = new File(dir, "build.properties"); Properties properties = new Properties(); - InputStream in = null; - - try { - in = new FileInputStream(buildProperties); - + try (BufferedReader in = Files.newBufferedReader(buildProperties.toPath())) { properties.load(in); String sdkVersionValue = (String)properties.get("lp.version"); @@ -392,15 +390,6 @@ private boolean _isPluginsSDK70(File dir) { } catch (Exception exception) { } - finally { - if (in != null) { - try { - in.close(); - } - catch (Exception exception) { - } - } - } return false; } @@ -408,7 +397,7 @@ private boolean _isPluginsSDK70(File dir) { private Version _makeCompatibleVersion(String targetPlatformVersion) { int dash = targetPlatformVersion.indexOf("-"); - Version productTargetPlatformVersion = null; + Version productTargetPlatformVersion; if (dash != -1) { productTargetPlatformVersion = Version.parseVersion(targetPlatformVersion.substring(0, dash)); @@ -489,8 +478,7 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttribu }); } - @SuppressWarnings("unchecked") - private String _setProductAndVersionForMaven(Map productInfos, InitArgs initArgs) throws Exception { + private String _setProductAndVersionForMaven(Map releaseInfos, InitArgs initArgs) throws Exception { String possibleProductKey = _getDefaultProductKey(initArgs); if (possibleProductKey.startsWith("portal") || possibleProductKey.startsWith("dxp") || @@ -507,15 +495,15 @@ private String _setProductAndVersionForMaven(Map productInfos, I return possibleProductKey; } - for (Map.Entry entryKey : productInfos.entrySet()) { + for (Map.Entry entryKey : releaseInfos.entrySet()) { ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(entryKey.getKey()); if (Objects.equals(possibleProductKey, releaseInfo.getTargetPlatformVersion())) { possibleProductKey = entryKey.getKey(); - String[] productKeyValues = possibleProductKey.split("-"); - - initArgs.setLiferayProduct(productKeyValues[0]); + initArgs.setLiferayProduct( + releaseInfo.getProductKey( + ).getProduct()); return possibleProductKey; } 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 1d9a03a3b..056d3e4c9 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 @@ -141,7 +141,8 @@ else if (dockerImageProperty.contains("dxp")) { } } else { - Map workspaceProductTargetPlatformVersions = BladeUtil.getWorkspaceProductTargetPlatformVersions(false); + Map workspaceProductTargetPlatformVersions = + BladeUtil.getWorkspaceProductTargetPlatformVersions(false); ProductKeyInfo productKeyInfo = workspaceProductTargetPlatformVersions.get(targetPlatformVersion); diff --git a/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java b/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java index 9a943d877..95b3c75c9 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/BladeUtil.java @@ -11,8 +11,6 @@ import com.liferay.project.templates.ProjectTemplates; import com.liferay.project.templates.extensions.util.ProjectTemplatesUtil; -import groovy.json.JsonSlurper; - import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -319,17 +317,6 @@ public static String getManifestProperty(Path pathToJar, String propertyName) th } } - @SuppressWarnings("unchecked") - public static ReleaseInfo getReleaseInfo(String productKey) { - Map releasesInfos = getReleasesInfos(); - - ProductKeyInfo productKeyInfo = new ProductKeyInfo( - productKey, (Map)releasesInfos.get(productKey)); - - return new ReleaseInfo( - productKeyInfo, getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())); - } - public static Properties getProperties(File file) { Properties properties = new Properties(); @@ -342,66 +329,45 @@ public static Properties getProperties(File file) { return properties; } - public static Properties getReleaseProperties(String product, String productKey) { - Properties properties = new Properties(); - - File productDest = new File(_workspaceCacheDir, product); - - File releasePropertiesDest = new File(productDest, productKey); - - try { - String actualReleasePropertiesUrl = MessageFormat.format(_RELEASE_PROPERTIES_URL, product, productKey); - - Path releasesPropertiesPath = downloadFile( - actualReleasePropertiesUrl, releasePropertiesDest.toPath(), "release.properties"); + @SuppressWarnings("unchecked") + public static ReleaseInfo getReleaseInfo(String productKey) { + Map releasesInfos = getReleaseKeyInfos(); - if (Files.exists(releasesPropertiesPath)) { - try (InputStream input = Files.newInputStream(releasesPropertiesPath)) { - properties.load(input); - } - } - } - catch (Exception exception) { - } + ProductKeyInfo productKeyInfo = new ProductKeyInfo( + productKey, (Map)releasesInfos.get(productKey)); - return properties; + return new ReleaseInfo( + productKeyInfo, getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())); } - public static Map getReleasesInfos() { - return getReleasesInfos(false, null); + public static Map getReleaseKeyInfos() { + return getReleaseKeyInfos(false, null); } @SuppressWarnings("unchecked") - public static synchronized Map getReleasesInfos(boolean trace, PrintStream printStream) { - if (!_releasesInfoMap.isEmpty()) { - return _releasesInfoMap; + public static synchronized Map getReleaseKeyInfos(boolean trace, PrintStream printStream) { + if (!_releaseKeyInfoMap.isEmpty()) { + return _releaseKeyInfoMap; } - JsonSlurper jsonSlurper = new JsonSlurper(); + _releaseKeyInfoMap = (Map)ResourceUtil.readJson( + Map.class, ResourceUtil.getURLResolver(_workspaceCacheDir, _RELEASE_INFO_URL, "releases.json"), + ResourceUtil.getClassLoaderResolver("/releases.json")); - try { - Path releasesInfoPath = downloadFile(_RELEASE_INFO_URL, _workspaceCacheDir.toPath(), "releases.json"); + return _releaseKeyInfoMap; + } - try (BufferedReader reader = Files.newBufferedReader(releasesInfoPath)) { - _releasesInfoMap = (Map)jsonSlurper.parse(reader); - } - } - catch (Exception exception1) { - if (trace && (printStream != null)) { - exception1.printStackTrace(printStream); - } + public static Properties getReleaseProperties(String product, String productKey) { + File productDest = new File(_workspaceCacheDir, product); - try (InputStream resourceAsStream = BladeUtil.class.getResourceAsStream("/releases.json")) { - _releasesInfoMap = (Map)jsonSlurper.parse(resourceAsStream); - } - catch (Exception exception2) { - if (trace && (printStream != null)) { - exception2.printStackTrace(printStream); - } - } - } + File releasePropertiesDest = new File(productDest, productKey); + + String actualReleasePropertiesUrl = MessageFormat.format(_RELEASE_PROPERTIES_URL, product, productKey); - return _releasesInfoMap; + return ResourceUtil.readProperties( + ResourceUtil.getLocalFileResolver(new File(releasePropertiesDest, "release.properties")), + ResourceUtil.getURLResolver(releasePropertiesDest, actualReleasePropertiesUrl, "release.properties"), + ResourceUtil.getURLResolver(releasePropertiesDest, actualReleasePropertiesUrl, "release.properties")); } public static Collection getTemplateNames(BladeCLI blade) throws Exception { @@ -428,7 +394,7 @@ public static Map getTemplates(BladeCLI bladeCLI) throws Excepti @SuppressWarnings("unchecked") public static List getWorkspaceProductKeys(boolean promoted) { - Map releasesInfos = getReleasesInfos(); + Map releasesInfos = getReleaseKeyInfos(); return releasesInfos.keySet( ).stream( @@ -461,7 +427,7 @@ public static List getWorkspaceProductKeys(boolean promoted) { @SuppressWarnings("unchecked") public static Map getWorkspaceProductTargetPlatformVersions(boolean promoted) { - Map releasesInfos = getReleasesInfos(); + Map releasesInfos = getReleaseKeyInfos(); return releasesInfos.entrySet( ).stream( @@ -840,7 +806,7 @@ private static Path _downloadFile( Header lastModifiedHeader = closeableHttpResponse.getFirstHeader(HttpHeaders.LAST_MODIFIED); if (lastModifiedHeader != null) { - lastModifiedDate = DateUtils.parseDate(lastModifiedHeader.getValue()); + lastModifiedDate = DateUtils.toDate(DateUtils.parseStandardDate(lastModifiedHeader.getValue())); } else { lastModifiedDate = new Date(); @@ -908,7 +874,7 @@ private static Path _downloadFile( private static final String _RELEASE_PROPERTIES_URL = "http://localhost:3000/{0}/{1}/release.properties"; private static final Pattern _microPattern = Pattern.compile("((([efs])p)|(ga)|(u))([0-9]+)(-[0-9]+)?"); - private static Map _releasesInfoMap = Collections.emptyMap(); + private static Map _releaseKeyInfoMap = Collections.emptyMap(); private static final File _workspaceCacheDir = new File( System.getProperty("user.home"), _DEFAULT_WORKSPACE_CACHE_DIR_NAME); diff --git a/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyUtil.java b/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyUtil.java index 092abcf5d..25b1a2e37 100644 --- a/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyUtil.java +++ b/cli/src/main/java/com/liferay/blade/cli/util/ProductKeyUtil.java @@ -23,7 +23,7 @@ public static int compare(String productKey1, String productKey2) { @SuppressWarnings("unchecked") public static ProductKeyInfo createProductKeyInfo(String productKey) { - Map releasesInfos = BladeUtil.getReleasesInfos(); + Map releasesInfos = BladeUtil.getReleaseKeyInfos(); return new ProductKeyInfo(productKey, (Map)releasesInfos.get(productKey)); } 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 new file mode 100644 index 000000000..df67930b1 --- /dev/null +++ b/cli/src/main/java/com/liferay/blade/cli/util/ResourceUtil.java @@ -0,0 +1,121 @@ +/** + * 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.google.gson.Gson; + +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; + +import java.nio.file.Files; +import java.nio.file.Path; + +import java.util.Objects; +import java.util.Properties; + +import org.gradle.api.GradleException; + +/** + * @author Drew Brokke + */ +public class ResourceUtil { + + public static Resolver getClassLoaderResolver(String resourcePath) { + return () -> Objects.requireNonNull( + ResourceUtil.class.getResourceAsStream(resourcePath), + "Unable to get resource from class path: " + resourcePath); + } + + public static Resolver getLocalFileResolver(File file) { + return () -> { + if (!file.exists()) { + throw new Exception("Unable to get resource from local file: " + file.getAbsolutePath()); + } + + return Files.newInputStream(file.toPath()); + }; + } + + public static Resolver getLocalFileResolver(String filePath) { + if (Objects.isNull(filePath)) { + return _nullResolver; + } + + return getLocalFileResolver(new File(filePath)); + } + + public static Resolver getURLResolver(File cacheDir, String url, String targetFileName) { + return () -> { + try { + return Files.newInputStream(BladeUtil.downloadFile(url, cacheDir.toPath(), targetFileName)); + } + catch (Exception exception) { + throw new Exception( + String.format("Unable to get resource from URL %s: %s", url, exception.getMessage()), exception); + } + }; + } + + public static File readFile(Path path, Resolver... resolvers) { + return _withInputStream( + inputStream -> { + Files.copy(inputStream, path); + + return path.toFile(); + }, + resolvers); + } + + public static T readJson(Class clazz, Resolver... resolvers) { + return _withInputStream(inputStream -> _gson.fromJson(new InputStreamReader(inputStream), clazz), resolvers); + } + + public static Properties readProperties(Resolver... resolvers) { + return _withInputStream( + inputStream -> { + Properties properties = new Properties(); + + properties.load(inputStream); + + return properties; + }, + resolvers); + } + + @FunctionalInterface + public interface Resolver { + + public InputStream resolve() throws Exception; + + } + + @FunctionalInterface + public interface Transformer { + + public T transform(InputStream inputStream) throws Exception; + + } + + private static T _withInputStream(Transformer transformer, Resolver... resolvers) { + for (Resolver resolver : resolvers) { + try (InputStream inputStream = resolver.resolve()) { + if (inputStream != null) { + return transformer.transform(inputStream); + } + } + catch (Exception exception) { + System.out.println(exception.getMessage()); + } + } + + throw new GradleException("Unable to get resource"); + } + + private static final Gson _gson = new Gson(); + private static final Resolver _nullResolver = () -> null; + +} \ No newline at end of file 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 298692721..3c8206b37 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 @@ -27,7 +27,6 @@ import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; diff --git a/extensions/maven-profile/build.gradle b/extensions/maven-profile/build.gradle index e7caba951..9922190bb 100644 --- a/extensions/maven-profile/build.gradle +++ b/extensions/maven-profile/build.gradle @@ -44,4 +44,4 @@ publishing { } } -version = "1.0.39-SNAPSHOT" \ No newline at end of file +version = "1.0.40-SNAPSHOT" \ No newline at end of file diff --git a/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/BuildServiceCommandMaven.java b/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/BuildServiceCommandMaven.java index e409c5ade..b24572b88 100644 --- a/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/BuildServiceCommandMaven.java +++ b/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/BuildServiceCommandMaven.java @@ -47,11 +47,11 @@ public void execute() throws Exception { try (Stream pathStream = Files.walk(baseDirPath)) { List paths = pathStream.filter( - path -> _pathHasFileName(path, "service.xml") + this::_pathHasFileName ).map( - path -> baseDirPath.relativize(path) + baseDirPath::relativize ).map( - path -> path.getParent() + Path::getParent ).collect( Collectors.toList() ); @@ -93,10 +93,10 @@ public Class getArgsClass() { return BuildServiceArgsMaven.class; } - private boolean _pathHasFileName(Path path, String expectedFileName) { + private boolean _pathHasFileName(Path path) { String fileNameString = String.valueOf(path.getFileName()); - return fileNameString.equals(expectedFileName); + return fileNameString.equals("service.xml"); } } \ No newline at end of file diff --git a/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/InitCommandMaven.java b/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/InitCommandMaven.java index aac38b372..abceaa214 100644 --- a/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/InitCommandMaven.java +++ b/extensions/maven-profile/src/main/java/com/liferay/blade/extensions/maven/profile/InitCommandMaven.java @@ -8,6 +8,23 @@ import com.liferay.blade.cli.command.BladeProfile; import com.liferay.blade.cli.command.InitArgs; import com.liferay.blade.cli.command.InitCommand; +import com.liferay.blade.cli.util.BladeUtil; +import com.liferay.blade.cli.util.ProductKeyInfo; +import com.liferay.blade.cli.util.ReleaseInfo; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; + +import java.nio.file.Files; + +import java.util.Map; +import java.util.Objects; +import java.util.Properties; + +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; /** * @author Gregory Amerson @@ -16,7 +33,41 @@ @BladeProfile("maven") public class InitCommandMaven extends InitCommand { - public InitCommandMaven() { + public static final String BUNDLE_URL_PROPERTY = "liferay.workspace.bundle.url"; + + public static final String LIFERAY_BOM_VERSION = "liferay.bom.version"; + + public void commandPostAction() throws Exception { + InitArgs initArgs = getArgs(); + + Map workspaceProductTargetPlatformVersions = + BladeUtil.getWorkspaceProductTargetPlatformVersions(false); + + ProductKeyInfo productKeyInfo = workspaceProductTargetPlatformVersions.get(initArgs.getLiferayVersion()); + + if (Objects.nonNull(productKeyInfo)) { + File initBaseDir = initArgs.getBase(); + + if (initBaseDir.exists()) { + File workspacePomFile = new File(initBaseDir, "pom.xml"); + + if (Files.exists(workspacePomFile.toPath())) { + MavenXpp3Reader xppReader = new MavenXpp3Reader(); + + Model mavenModel = xppReader.read(new FileReader(workspacePomFile)); + + Properties properties = mavenModel.getProperties(); + + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(productKeyInfo.getProductKey()); + + properties.setProperty(BUNDLE_URL_PROPERTY, releaseInfo.getBundleUrl()); + + properties.setProperty(LIFERAY_BOM_VERSION, releaseInfo.getTargetPlatformVersion()); + + _updateMavenPom(mavenModel, workspacePomFile); + } + } + } } @Override @@ -28,4 +79,12 @@ public void execute() throws Exception { super.execute(); } + private void _updateMavenPom(Model model, File file) throws Exception { + MavenXpp3Writer mavenWriter = new MavenXpp3Writer(); + + FileWriter fileWriter = new FileWriter(file); + + mavenWriter.write(fileWriter, model); + } + } \ No newline at end of file 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 33ada84c4..63ea10864 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 @@ -7,7 +7,6 @@ import aQute.bnd.version.Version; -import com.liferay.blade.cli.WorkspaceConstants; import com.liferay.blade.cli.WorkspaceProvider; import com.liferay.blade.cli.util.BladeUtil; import com.liferay.blade.cli.util.ProductKeyInfo; @@ -17,7 +16,6 @@ import java.util.Map; import java.util.Properties; -import java.util.regex.Matcher; /** * @author Christopher Bryan Boyd @@ -39,7 +37,8 @@ public String getProduct(File workspaceDir) { return "portal"; } - Map workspaceProductTargetPlatformVersions = BladeUtil.getWorkspaceProductTargetPlatformVersions(false); + Map workspaceProductTargetPlatformVersions = + BladeUtil.getWorkspaceProductTargetPlatformVersions(false); ProductKeyInfo productKeyInfo = workspaceProductTargetPlatformVersions.get(targetPlatformVersion);