Skip to content

UCT custom coming versions #1251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package com.magento.idea.magento2uct.execution.configurations;

import static com.magento.idea.magento2uct.execution.configurations.UctSettingsEditor.MAGENTO_VERSION_PATTERN;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.Executor;
import com.intellij.execution.RunManager;
Expand Down Expand Up @@ -32,6 +34,7 @@
import com.magento.idea.magento2uct.settings.UctSettingsService;
import com.magento.idea.magento2uct.util.UctExecutableValidatorUtil;
import com.magento.idea.magento2uct.util.module.UctModulePathValidatorUtil;
import java.util.regex.Matcher;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -241,6 +244,11 @@ public boolean isNewlyCreated() {
throw new ExecutionException("The coming/target version is not specified");
}

final Matcher matcher = MAGENTO_VERSION_PATTERN.matcher(getComingVersion());
if (!matcher.find()) {
throw new ExecutionException("The coming/target version is not correct");
}

if (getProjectRoot().isEmpty()) {
throw new ExecutionException("The project root is not specified");
}
Expand All @@ -255,14 +263,12 @@ public boolean isNewlyCreated() {
commandSettingsBuilder.addArgument("--coming-version=" + getComingVersion());
}

commandSettingsBuilder.addArgument(getProjectRoot());

final GeneralCommandLine commandLine =
commandSettingsBuilder.createGeneralCommandLine();

if (!getModulePath().isEmpty()) {
if (UctModulePathValidatorUtil.validate(getModulePath())) {
commandLine.addParameter("--module-path=".concat(getModulePath()));
commandLine.addParameter(getModulePath());
} else {
throw new ExecutionException("The path to analyse is not valid");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<properties>
<editable value="true"/>
</properties>
</component>
<component id="f6c56" class="javax.swing.JLabel" binding="comingVersionLabel">
<constraints>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.awt.Container;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
Expand All @@ -45,6 +48,10 @@ public class UctSettingsEditor extends SettingsEditor<UctRunConfiguration> {
private static final String LEARN_MORE_URI =
"https://docs.magento.com/user-guide/getting-started.html#product-editions";
private static final String LEARN_MORE_TEXT = "Learn more. ";
@SuppressWarnings("checkstyle:LineLength")
private static final String MAGENTO_VERSION_REGEX = "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$";
public static final Pattern MAGENTO_VERSION_PATTERN
= Pattern.compile(MAGENTO_VERSION_REGEX);

private final Project project;
private String uctExecutablePath;
Expand Down Expand Up @@ -126,6 +133,14 @@ protected void resetEditorFrom(final @NotNull UctRunConfiguration uctRunConfigur
if (!uctRunConfiguration.getComingVersion().isEmpty()) {
final String storedComingVersion = uctRunConfiguration.getComingVersion();
setSelectedValueByItsKey(comingVersion, storedComingVersion);

if (!Objects.requireNonNull(
comingVersion.getSelectedItem()).toString().equals(storedComingVersion)) {
final ComboBoxItemData customVersion
= new ComboBoxItemData(storedComingVersion, storedComingVersion);
comingVersion.addItem(customVersion);
comingVersion.setSelectedItem(customVersion);
}
}

if (uctRunConfiguration.getMinIssueLevel() > 0) {
Expand All @@ -148,8 +163,8 @@ protected void applyEditorTo(final @NotNull UctRunConfiguration uctRunConfigurat
uctRunConfiguration.setProjectRoot(projectRoot.getComponent().getText());
uctRunConfiguration.setModulePath(modulePath.getComponent().getText());

final ComboBoxItemData selectedComingVersion =
(ComboBoxItemData) comingVersion.getSelectedItem();
final ComboBoxItemData selectedComingVersion
= getCorrectedSelectedItem(comingVersion.getSelectedItem());

if (selectedComingVersion == null) {
uctRunConfiguration.setComingVersion("");
Expand Down Expand Up @@ -299,7 +314,7 @@ private String getStoredUctExecutablePath(
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private void initializeComboboxSources() {
comingVersion.addItem(new ComboBoxItemData("", "Choose a target version"));
comingVersion.setToolTipText("Choose a target version");

for (final String version : SupportedVersion.getSupportedVersions()) {
comingVersion.addItem(new ComboBoxItemData(version, version));
Expand Down Expand Up @@ -339,7 +354,7 @@ protected void textChanged(final @NotNull DocumentEvent event) {
});

comingVersion.addItemListener(event -> {
final ComboBoxItemData selectedItem = (ComboBoxItemData) event.getItem();
final ComboBoxItemData selectedItem = getCorrectedSelectedItem(event.getItem());

validateComingVersionField(selectedItem);
});
Expand All @@ -366,13 +381,37 @@ private void validateExecutablePathField() {
* @param selectedItem ComboBoxItemData
*/
private void validateComingVersionField(final ComboBoxItemData selectedItem) {
final Matcher matcher = MAGENTO_VERSION_PATTERN.matcher(selectedItem.getText());

if (selectedItem != null && selectedItem.getKey().isEmpty()) {
comingVersionError.setText("Please, specify target version");
} else if (!matcher.find()) { // NOPMD
comingVersionError.setText("Please, correct target version");
} else {
comingVersionError.setText("");
}
}

/**
* Get existing item or select and convert custom version.
*
* @param selectedItem String|ComboBoxItemData
* @return ComboBoxItemData
*/
private ComboBoxItemData getCorrectedSelectedItem(final Object selectedItem) {
ComboBoxItemData selectedComingVersion;

if (selectedItem instanceof ComboBoxItemData) {
selectedComingVersion = (ComboBoxItemData) selectedItem;
} else {
final String customSelectedVersion = selectedItem.toString();
selectedComingVersion
= new ComboBoxItemData(customSelectedVersion, customSelectedVersion);
}

return selectedComingVersion;
}

/**
* Set selected combobox item by key.
*
Expand Down