From b3cf0d17a7e4d6b40bdbaabe93b915c7e58e5bf6 Mon Sep 17 00:00:00 2001 From: Xieshen Date: Tue, 12 Sep 2023 00:17:39 -0400 Subject: [PATCH] feature: configure settings to exhort api --- .../tools/intellij/exhort/ApiService.java | 3 + .../settings/ApiSettingsComponent.java | 152 ++++++++++++++++++ .../settings/ApiSettingsConfigurable.java | 74 +++++++++ .../intellij/settings/ApiSettingsState.java | 77 +++++++++ src/main/resources/META-INF/plugin.xml | 6 + 5 files changed, 312 insertions(+) create mode 100644 src/main/java/org/jboss/tools/intellij/settings/ApiSettingsComponent.java create mode 100644 src/main/java/org/jboss/tools/intellij/settings/ApiSettingsConfigurable.java create mode 100644 src/main/java/org/jboss/tools/intellij/settings/ApiSettingsState.java diff --git a/src/main/java/org/jboss/tools/intellij/exhort/ApiService.java b/src/main/java/org/jboss/tools/intellij/exhort/ApiService.java index 2aea69f..d8a4b97 100644 --- a/src/main/java/org/jboss/tools/intellij/exhort/ApiService.java +++ b/src/main/java/org/jboss/tools/intellij/exhort/ApiService.java @@ -16,6 +16,7 @@ import com.redhat.exhort.Api; import com.redhat.exhort.api.AnalysisReport; import com.redhat.exhort.impl.ExhortApi; +import org.jboss.tools.intellij.settings.ApiSettingsState; import java.io.IOException; import java.nio.file.Files; @@ -56,6 +57,7 @@ public Path getStackAnalysis(final String packageManager, final String manifestN telemetryMsg.property(TelemetryKeys.MANIFEST.toString(), manifestName); try { + ApiSettingsState.getInstance().setApiOptions(); var htmlContent = exhortApi.stackAnalysisHtml(manifestPath); var tmpFile = Files.createTempFile("exhort_", ".html"); Files.write(tmpFile, htmlContent.get()); @@ -78,6 +80,7 @@ public AnalysisReport getComponentAnalysis(final String packageManager, final St try { var manifestContent = Files.readAllBytes(Paths.get(manifestPath)); + ApiSettingsState.getInstance().setApiOptions(); CompletableFuture componentReport = exhortApi.componentAnalysis(manifestPath); AnalysisReport report = componentReport.get(); telemetryMsg.send(); diff --git a/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsComponent.java b/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsComponent.java new file mode 100644 index 0000000..53eb2de --- /dev/null +++ b/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsComponent.java @@ -0,0 +1,152 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ + +package org.jboss.tools.intellij.settings; + +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; +import com.intellij.openapi.ui.TextComponentAccessor; +import com.intellij.openapi.ui.TextFieldWithBrowseButton; +import com.intellij.ui.components.JBLabel; +import com.intellij.ui.components.JBTextField; +import com.intellij.util.ui.FormBuilder; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; + +public class ApiSettingsComponent { + + private final static String mvnPathLabel = "Maven > Executable: Path" + + "
Specifies absolute path of mvn executable."; + private final static String javaPathLabel = "Maven > JAVA_HOME: Path" + + "
Specifies absolute path of Java installation directory."; + private final static String npmPathLabel = "Npm > Executable: Path" + + "
Specifies absolute path of npm executable."; + private final static String nodePathLabel = "Node > Executable: Path" + + "
Specifies absolute path of the directory containing node executable."; + private final static String snykTokenLabel = "Red Hat Dependency Analytics: Exhort Snyk Token" + + "
Red Hat Dependency Analytics sever authentication token for Snky."; + + private final JPanel mainPanel; + + private final TextFieldWithBrowseButton mvnPathText; + private final TextFieldWithBrowseButton javaPathText; + private final TextFieldWithBrowseButton npmPathText; + private final TextFieldWithBrowseButton nodePathText; + private final JBTextField snykTokenText; + + public ApiSettingsComponent() { + mvnPathText = new TextFieldWithBrowseButton(); + mvnPathText.addBrowseFolderListener( + null, + null, + null, + FileChooserDescriptorFactory.createSingleFileDescriptor(), + TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT + ); + + javaPathText= new TextFieldWithBrowseButton(); + javaPathText.addBrowseFolderListener( + null, + null, + null, + FileChooserDescriptorFactory.createSingleFolderDescriptor(), + TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT + ); + + npmPathText = new TextFieldWithBrowseButton(); + npmPathText.addBrowseFolderListener( + null, + null, + null, + FileChooserDescriptorFactory.createSingleFileDescriptor(), + TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT + ); + + nodePathText = new TextFieldWithBrowseButton(); + nodePathText.addBrowseFolderListener( + null, + null, + null, + FileChooserDescriptorFactory.createSingleFolderDescriptor(), + TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT + ); + + snykTokenText = new JBTextField(); + + mainPanel = FormBuilder.createFormBuilder() + .addLabeledComponent(new JBLabel(mvnPathLabel), mvnPathText, 1, true) + .addVerticalGap(10) + .addLabeledComponent(new JBLabel(javaPathLabel), javaPathText, 1, true) + .addSeparator(10) + .addVerticalGap(10) + .addLabeledComponent(new JBLabel(npmPathLabel), npmPathText, 1, true) + .addVerticalGap(10) + .addLabeledComponent(new JBLabel(nodePathLabel), nodePathText, 1, true) + .addSeparator(10) + .addVerticalGap(10) + .addLabeledComponent(new JBLabel(snykTokenLabel), snykTokenText, 1, true) + .addComponentFillVertically(new JPanel(), 0) + .getPanel(); + } + + public JPanel getPanel() { + return mainPanel; + } + + public JComponent getPreferredFocusedComponent() { + return mvnPathText; + } + + @NotNull + public String getMvnPathText() { + return mvnPathText.getText(); + } + + public void setMvnPathText(@NotNull String text) { + mvnPathText.setText(text); + } + + @NotNull + public String getJavaPathText() { + return javaPathText.getText(); + } + + public void setJavaPathText(@NotNull String text) { + javaPathText.setText(text); + } + + @NotNull + public String getNpmPathText() { + return npmPathText.getText(); + } + + public void setNpmPathText(@NotNull String text) { + npmPathText.setText(text); + } + + @NotNull + public String getNodePathText() { + return nodePathText.getText(); + } + + public void setNodePathText(@NotNull String text) { + nodePathText.setText(text); + } + + @NotNull + public String getSnykTokenText() { + return snykTokenText.getText(); + } + + public void setSnykTokenText(@NotNull String text) { + snykTokenText.setText(text); + } +} diff --git a/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsConfigurable.java b/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsConfigurable.java new file mode 100644 index 0000000..a9fdadd --- /dev/null +++ b/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsConfigurable.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ + +package org.jboss.tools.intellij.settings; + +import com.intellij.openapi.util.NlsContexts; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +public class ApiSettingsConfigurable implements com.intellij.openapi.options.Configurable { + + private ApiSettingsComponent settingsComponent; + + @Override + public @NlsContexts.ConfigurableName String getDisplayName() { + return "Red Hat Dependency Analytics"; + } + + @Override + public @Nullable JComponent createComponent() { + settingsComponent = new ApiSettingsComponent(); + return settingsComponent.getPanel(); + } + + @Override + public JComponent getPreferredFocusedComponent() { + return settingsComponent.getPreferredFocusedComponent(); + } + + @Override + public boolean isModified() { + ApiSettingsState settings = ApiSettingsState.getInstance(); + boolean modified = !settingsComponent.getMvnPathText().equals(settings.mvnPath); + modified |= !settingsComponent.getJavaPathText().equals(settings.javaPath); + modified |= !settingsComponent.getNpmPathText().equals(settings.npmPath); + modified |= !settingsComponent.getNodePathText().equals(settings.nodePath); + modified |= !settingsComponent.getSnykTokenText().equals(settings.snykToken); + return modified; + } + + @Override + public void apply() { + ApiSettingsState settings = ApiSettingsState.getInstance(); + settings.mvnPath = settingsComponent.getMvnPathText(); + settings.javaPath = settingsComponent.getJavaPathText(); + settings.npmPath = settingsComponent.getNpmPathText(); + settings.nodePath = settingsComponent.getNodePathText(); + settings.snykToken = settingsComponent.getSnykTokenText(); + } + + @Override + public void reset() { + ApiSettingsState settings = ApiSettingsState.getInstance(); + settingsComponent.setMvnPathText(settings.mvnPath); + settingsComponent.setJavaPathText(settings.javaPath); + settingsComponent.setNpmPathText(settings.npmPath); + settingsComponent.setNodePathText(settings.nodePath); + settingsComponent.setSnykTokenText(settings.snykToken); + } + + @Override + public void disposeUIResources() { + settingsComponent = null; + } +} diff --git a/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsState.java b/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsState.java new file mode 100644 index 0000000..5b84e8c --- /dev/null +++ b/src/main/java/org/jboss/tools/intellij/settings/ApiSettingsState.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ + +package org.jboss.tools.intellij.settings; + +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.*; +import com.intellij.util.xmlb.XmlSerializerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@State( + name = "org.jboss.tools.intellij.settings.ApiSettingsState", + storages = @Storage( + value = "rhda.exhort.xml", + roamingType = RoamingType.DISABLED + ) +) +@Service(Service.Level.APP) +public final class ApiSettingsState implements PersistentStateComponent { + + public static ApiSettingsState getInstance() { + return ApplicationManager.getApplication().getService(ApiSettingsState.class); + } + + public String mvnPath; + public String javaPath; + public String npmPath; + public String nodePath; + public String snykToken; + + @Override + public @Nullable ApiSettingsState getState() { + return this; + } + + @Override + public void loadState(@NotNull ApiSettingsState state) { + XmlSerializerUtil.copyBean(state, this); + } + + public void setApiOptions() { + if (mvnPath != null && !mvnPath.isBlank()) { + System.setProperty("EXHORT_MVN_PATH", mvnPath); + } else { + System.clearProperty("EXHORT_MVN_PATH"); + } + if (javaPath != null && !javaPath.isBlank()) { + System.setProperty("JAVA_HOME", javaPath); + } else { + System.clearProperty("JAVA_HOME"); + } + if (npmPath != null && !npmPath.isBlank()) { + System.setProperty("EXHORT_NPM_PATH", npmPath); + } else { + System.clearProperty("EXHORT_NPM_PATH"); + } + if (nodePath != null && !nodePath.isBlank()) { + System.setProperty("NODE_HOME", nodePath); + } else { + System.clearProperty("NODE_HOME"); + } + if (snykToken != null && !snykToken.isBlank()) { + System.setProperty("EXHORT_SNYK_TOKEN", snykToken); + } else { + System.clearProperty("EXHORT_SNYK_TOKEN"); + } + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index a9905ad..9a15b7d 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -148,6 +148,12 @@ + +