Skip to content

Commit

Permalink
feature: configure settings to exhort api
Browse files Browse the repository at this point in the history
  • Loading branch information
xieshenzh committed Sep 13, 2023
1 parent 7d37273 commit b3cf0d1
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/jboss/tools/intellij/exhort/ApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -78,6 +80,7 @@ public AnalysisReport getComponentAnalysis(final String packageManager, final St

try {
var manifestContent = Files.readAllBytes(Paths.get(manifestPath));
ApiSettingsState.getInstance().setApiOptions();
CompletableFuture<AnalysisReport> componentReport = exhortApi.componentAnalysis(manifestPath);
AnalysisReport report = componentReport.get();
telemetryMsg.send();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "<html>Maven > Executable: <b>Path</b>"
+ "<br>Specifies absolute path of <b>mvn</b> executable.</html>";
private final static String javaPathLabel = "<html>Maven > JAVA_HOME: <b>Path</b>"
+ "<br>Specifies absolute path of Java installation directory.</html>";
private final static String npmPathLabel = "<html>Npm > Executable: <b>Path</b>"
+ "<br>Specifies absolute path of <b>npm</b> executable.</html>";
private final static String nodePathLabel = "<html>Node > Executable: <b>Path</b>"
+ "<br>Specifies absolute path of the <i>directory</i> containing <b>node</b> executable.</html>";
private final static String snykTokenLabel = "<html>Red Hat Dependency Analytics: <b>Exhort Snyk Token</b>"
+ "<br>Red Hat Dependency Analytics sever authentication token for Snky.</html>";

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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ApiSettingsState> {

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");
}
}
}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
<fileEditorProvider implementation="org.jboss.tools.intellij.stackanalysis.SaReportEditorProvider"/>
<editorTabTitleProvider implementation="org.jboss.tools.intellij.stackanalysis.SaEditorTabTitleProvider"
order="first"/>

<applicationConfigurable
parentId="tools"
instance="org.jboss.tools.intellij.settings.ApiSettingsConfigurable"
id="org.jboss.tools.intellij.settings.ApiSettingsConfigurable"
displayName="Red Hat Dependency Analytics"/>
</extensions>

<!-- Action for running SA added in Editor window, Navigation Bar and Project View-->
Expand Down

0 comments on commit b3cf0d1

Please sign in to comment.