diff --git a/cli/bnd.bnd b/cli/bnd.bnd index cb130d5ee..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,\ @@ -323,6 +329,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..27f6dc244 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) @@ -76,8 +76,9 @@ 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.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 +103,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" @@ -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" @@ -139,8 +140,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 +149,7 @@ downloadProductInfo { jar { dependsOn("unzipPortal") archiveFileName.set("blade.jar") - from createToolingZip, createWrapperZip, downloadProductInfo + from createToolingZip, createWrapperZip, downloadReleasesInfo } processResources { @@ -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 3c2be9d3a..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,8 +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.ProductInfo; +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; @@ -99,7 +100,7 @@ public static Map> getCommandMapByClassL boolean profileNameIsPresent = false; - if ((profileName != null) && (profileName.length() > 0)) { + if ((profileName != null) && !profileName.isEmpty()) { profileNameIsPresent = true; } @@ -862,16 +863,21 @@ private Map _buildMavenPossibleValuesMap( Iterator it = options.iterator(); - Map productInfos = BladeUtil.getProductInfos(true, error()); + Map releaseKeyInfos = BladeUtil.getReleaseKeyInfos(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)releaseKeyInfos.get(option)); - optionsMap.put(String.valueOf(x), productInfo.getTargetPlatformVersion()); + ReleaseInfo releaseInfo = new ReleaseInfo( + productKeyInfo, + BladeUtil.getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())); + + optionsMap.put(String.valueOf(x), releaseInfo.getTargetPlatformVersion()); } return optionsMap; @@ -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/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/ConvertCommand.java b/cli/src/main/java/com/liferay/blade/cli/command/ConvertCommand.java index 00f63c769..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; @@ -1211,14 +1211,12 @@ 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(); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(productKey); - ProductInfo productInfo = new ProductInfo((Map)productInfos.get(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 ce24ea41b..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 @@ -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; @@ -179,10 +180,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 +346,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 +354,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 +363,26 @@ protected ProjectTemplatesArgs getProjectTemplateArgs( projectTemplatesArgs.setTemplate(template); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(liferayVersion.get()); + + if (releaseInfo.getProductKey( + ).isQuarterly() && 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 +494,16 @@ private String _checkTemplateVersionRange(File templateFile, ProjectTemplatesArg try (InputStream fileInputStream = Files.newInputStream(templateFile.toPath(), StandardOpenOption.READ); JarInputStream in = new JarInputStream(fileInputStream)) { + String versionString = projectTemplatesArgs.getLiferayVersion(); + + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(versionString); + + if (releaseInfo.getProductKey( + ).isPromoted()) { + + return ""; + } + Manifest manifest = in.getManifest(); Attributes attributes = manifest.getMainAttributes(); @@ -483,17 +512,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 +671,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..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 @@ -12,15 +12,14 @@ 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; +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; @@ -49,7 +48,6 @@ public InitCommand() { } @Override - @SuppressWarnings("unchecked") public void execute() throws Exception { BladeCLI bladeCLI = getBladeCLI(); @@ -132,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(); @@ -191,7 +189,7 @@ public void execute() throws Exception { String liferayVersion; String workspaceProductKey; - Map productInfos = BladeUtil.getProductInfos(initArgs.isTrace(), bladeCLI.error()); + Map releasesInfos = BladeUtil.getReleaseKeyInfos(initArgs.isTrace(), bladeCLI.error()); if (!mavenBuild) { workspaceProductKey = _getDefaultProductKey(initArgs); @@ -204,7 +202,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,26 +210,41 @@ public void execute() throws Exception { return; } - ProductInfo productInfo = new ProductInfo((Map)productInfoObject); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(workspaceProductKey); - Version targetPlatformVersion = _makeCompatibleVersion(productInfo.getTargetPlatformVersion()); + if (releaseInfo.getProductKey( + ).isQuarterly()) { - liferayVersion = new String( - targetPlatformVersion.getMajor() + "." + targetPlatformVersion.getMinor() + "." + - targetPlatformVersion.getMicro()); + liferayVersion = "7.4"; + } + else { + Version normalTargetPlatformVersion = _makeCompatibleVersion(releaseInfo.getTargetPlatformVersion()); + + liferayVersion = + normalTargetPlatformVersion.getMajor() + "." + normalTargetPlatformVersion.getMinor() + "." + + normalTargetPlatformVersion.getMicro(); + } } else { - workspaceProductKey = _setProductAndVersionForMaven(productInfos, initArgs); + 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 = productInfos.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")) { @@ -309,6 +322,8 @@ public void execute() throws Exception { settings.setProfileName(profileName); settings.save(); + + commandPostAction(); } @Override @@ -320,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") || @@ -336,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) { @@ -348,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")) { @@ -368,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"); @@ -383,15 +390,6 @@ private boolean _isPluginsSDK70(File dir) { } catch (Exception exception) { } - finally { - if (in != null) { - try { - in.close(); - } - catch (Exception exception) { - } - } - } return false; } @@ -399,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)); @@ -480,40 +478,34 @@ 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") || possibleProductKey.startsWith("commerce")) { - Object productInfoObject = productInfos.get(possibleProductKey); - - if (Objects.nonNull(productInfoObject)) { - ProductInfo productInfo = new ProductInfo((Map)productInfoObject); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(possibleProductKey); - initArgs.setLiferayVersion(productInfo.getTargetPlatformVersion()); + initArgs.setLiferayVersion(releaseInfo.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 : releaseInfos.entrySet()) { + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(entryKey.getKey()); - String[] productKeyValues = possibleProductKey.split("-"); + if (Objects.equals(possibleProductKey, releaseInfo.getTargetPlatformVersion())) { + possibleProductKey = entryKey.getKey(); - initArgs.setLiferayProduct(productKeyValues[0]); + initArgs.setLiferayProduct( + releaseInfo.getProductKey( + ).getProduct()); - 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..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 @@ -12,7 +12,8 @@ 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; @@ -75,17 +76,13 @@ public String getLiferayVersion(File workspaceDir) { if (!baseLiferayVersion.isPresent()) { String productKey = gradleProperties.getProperty(WorkspaceConstants.DEFAULT_WORKSPACE_PRODUCT_PROPERTY); - Map productInfoMap = BladeUtil.getProductInfos(); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(productKey); - ProductInfo productInfo = new ProductInfo((Map)productInfoMap.get(productKey)); - - if (productInfo != null) { - baseLiferayVersion = Optional.ofNullable( - productInfo.getTargetPlatformVersion() - ).filter( - BladeUtil::isNotEmpty - ); - } + baseLiferayVersion = Optional.ofNullable( + releaseInfo.getTargetPlatformVersion() + ).filter( + BladeUtil::isNotEmpty + ); } if (!baseLiferayVersion.isPresent()) { @@ -144,6 +141,15 @@ else if (dockerImageProperty.contains("dxp")) { } } else { + Map workspaceProductTargetPlatformVersions = + BladeUtil.getWorkspaceProductTargetPlatformVersions(false); + + ProductKeyInfo productKeyInfo = workspaceProductTargetPlatformVersions.get(targetPlatformVersion); + + if (productKeyInfo.isQuarterly()) { + 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..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; @@ -37,6 +35,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 +49,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,53 +317,57 @@ public static String getManifestProperty(Path pathToJar, String propertyName) th } } - public static Map getProductInfos() { - return getProductInfos(false, null); + 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; } @SuppressWarnings("unchecked") - public static synchronized Map getProductInfos(boolean trace, PrintStream printStream) { - if (!_productInfoMap.isEmpty()) { - return _productInfoMap; - } + public static ReleaseInfo getReleaseInfo(String productKey) { + Map releasesInfos = getReleaseKeyInfos(); - JsonSlurper jsonSlurper = new JsonSlurper(); + ProductKeyInfo productKeyInfo = new ProductKeyInfo( + productKey, (Map)releasesInfos.get(productKey)); - try { - Path productInfoPath = downloadFile(_PRODUCT_INFO_URL, _workspaceCacheDir.toPath(), ".product_info.json"); + return new ReleaseInfo( + productKeyInfo, getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())); + } - try (BufferedReader reader = Files.newBufferedReader(productInfoPath)) { - _productInfoMap = (Map)jsonSlurper.parse(reader); - } - } - catch (Exception exception1) { - if (trace && (printStream != null)) { - exception1.printStackTrace(printStream); - } + public static Map getReleaseKeyInfos() { + return getReleaseKeyInfos(false, null); + } - try (InputStream resourceAsStream = BladeUtil.class.getResourceAsStream("/.product_info.json")) { - _productInfoMap = (Map)jsonSlurper.parse(resourceAsStream); - } - catch (Exception exception2) { - if (trace && (printStream != null)) { - exception2.printStackTrace(printStream); - } - } + @SuppressWarnings("unchecked") + public static synchronized Map getReleaseKeyInfos(boolean trace, PrintStream printStream) { + if (!_releaseKeyInfoMap.isEmpty()) { + return _releaseKeyInfoMap; } - return _productInfoMap; + _releaseKeyInfoMap = (Map)ResourceUtil.readJson( + Map.class, ResourceUtil.getURLResolver(_workspaceCacheDir, _RELEASE_INFO_URL, "releases.json"), + ResourceUtil.getClassLoaderResolver("/releases.json")); + + return _releaseKeyInfoMap; } - public static Properties getProperties(File file) { - Properties properties = new Properties(); + public static Properties getReleaseProperties(String product, String productKey) { + File productDest = new File(_workspaceCacheDir, product); - try (InputStream inputStream = Files.newInputStream(file.toPath())) { - properties.load(inputStream); - } - catch (Exception exception) { - } + File releasePropertiesDest = new File(productDest, productKey); - return properties; + String actualReleasePropertiesUrl = MessageFormat.format(_RELEASE_PROPERTIES_URL, product, productKey); + + 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 { @@ -391,25 +394,29 @@ public static Map getTemplates(BladeCLI bladeCLI) throws Excepti @SuppressWarnings("unchecked") public static List getWorkspaceProductKeys(boolean promoted) { - Map productInfos = getProductInfos(); + Map releasesInfos = getReleaseKeyInfos(); - 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 +426,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 = getReleaseKeyInfos(); - 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 ReleaseInfo( + productKeyInfo, getReleaseProperties(productKeyInfo.getProduct(), productKeyInfo.getProductKey())) + ).filter( + releaseInfo -> Objects.nonNull(releaseInfo.getTargetPlatformVersion()) ).collect( - Collectors.toSet() + Collectors.toMap(ReleaseInfo::getTargetPlatformVersion, ReleaseInfo::getProductKey) ); } @@ -796,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(); @@ -857,10 +867,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 _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/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/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..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 @@ -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.getReleaseKeyInfos(); - 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/main/java/com/liferay/blade/cli/util/ProductInfo.java b/cli/src/main/java/com/liferay/blade/cli/util/ReleaseInfo.java similarity index 51% 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 ba99a8818..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 @@ -5,23 +5,23 @@ package com.liferay.blade.cli.util; -import java.util.Map; -import java.util.Optional; +import java.util.Properties; /** * @author Simon Jiang * @author Gregory Amerson */ -public class ProductInfo { +public class ReleaseInfo { - 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 ReleaseInfo(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/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/BladeTest.java b/cli/src/test/java/com/liferay/blade/cli/BladeTest.java index 19f6c2514..7b7fbff63 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.4"; + 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..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 @@ -121,6 +121,19 @@ public void testBladeInitEmptyDirectoryHandleTwoDots() throws Exception { Assert.assertNotNull(bladeTest.getWorkspaceProvider(emptyDir)); } + @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 +360,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 +681,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/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 698e747dd..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 @@ -9,10 +9,12 @@ 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; /** @@ -35,6 +37,15 @@ public String getProduct(File workspaceDir) { return "portal"; } + Map workspaceProductTargetPlatformVersions = + BladeUtil.getWorkspaceProductTargetPlatformVersions(false); + + ProductKeyInfo productKeyInfo = workspaceProductTargetPlatformVersions.get(targetPlatformVersion); + + if (productKeyInfo.isQuarterly()) { + 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..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; @@ -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,14 +131,12 @@ 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)); + ReleaseInfo releaseInfo = BladeUtil.getReleaseInfo(productKey); - return productInfo.getTargetPlatformVersion(); + return releaseInfo.getTargetPlatformVersion(); } JSONObject jsonObject = new JSONObject(new String(Files.readAllBytes(productInfoPath.normalize()))); @@ -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");