Skip to content
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

#34: Implemented ToolCommandlet for SonarQube #216

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -25,6 +25,7 @@
import com.devonfw.tools.ide.tool.node.Node;
import com.devonfw.tools.ide.tool.oc.Oc;
import com.devonfw.tools.ide.tool.quarkus.Quarkus;
import com.devonfw.tools.ide.tool.sonar.Sonar;
import com.devonfw.tools.ide.tool.terraform.Terraform;
import com.devonfw.tools.ide.tool.vscode.Vscode;

Expand Down Expand Up @@ -83,6 +84,7 @@ public CommandletManagerImpl(IdeContext context) {
add(new Aws(context));
add(new Cobigen(context));
add(new Jmc(context));
add(new Sonar(context));
}

private void add(Commandlet commandlet) {
Expand Down
59 changes: 59 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/property/EnumProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.devonfw.tools.ide.property;

import java.util.Locale;

import com.devonfw.tools.ide.commandlet.Commandlet;
import com.devonfw.tools.ide.completion.CompletionCandidateCollector;
import com.devonfw.tools.ide.context.IdeContext;

/**
* {@link Property} with {@link #getValueType() value type} {@link Boolean}.
*/
public class EnumProperty<V extends Enum<V>> extends Property<V> {

private final Class<V> valueType;

/**
* The constructor.
*
* @param name the {@link #getName() property name}.
* @param required the {@link #isRequired() required flag}.
* @param alias the {@link #getAlias() property alias}.
*/
public EnumProperty(String name, boolean required, String alias, Class<V> valueType) {

super(name, required, alias, null);
this.valueType = valueType;
}

@Override
public Class<V> getValueType() {

return this.valueType;
}

@Override
public V parse(String valueAsString, IdeContext context) {

for (V enumConstant : this.valueType.getEnumConstants()) {
String name = enumConstant.name().toLowerCase(Locale.ROOT);
if (name.equals(valueAsString)) {
return enumConstant;
}
}

throw new IllegalArgumentException(String.format("Invalid Enum option: %s", valueAsString));
}

@Override
protected void completeValue(String arg, IdeContext context, Commandlet commandlet,
CompletionCandidateCollector collector) {

for (V enumConstant : this.valueType.getEnumConstants()) {
String name = enumConstant.name().toLowerCase(Locale.ROOT);
if (name.startsWith(arg)) {
collector.add(name, null, this, commandlet);
}
}
}
}
11 changes: 10 additions & 1 deletion cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ public ToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
this.tool = tool;
this.tags = tags;
addKeyword(tool);
this.arguments = add(new StringListProperty("", false, "args"));
this.arguments = new StringListProperty("", false, "args");
initProperties();
}

/**
* Add initial Properties to the tool
*/
protected void initProperties() {

add(this.arguments);
}

/**
Expand Down
100 changes: 100 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/tool/sonar/Sonar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.devonfw.tools.ide.tool.sonar;

import java.nio.file.Path;
import java.util.Properties;
import java.util.Set;

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.EnumProperty;
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
import com.devonfw.tools.ide.tool.java.Java;
import com.devonfw.tools.ide.tool.mvn.Mvn;
import com.devonfw.tools.ide.util.PropertiesFileUtil;

public class Sonar extends LocalToolCommandlet {

public final EnumProperty<SonarCommand> command;

/**
* The constructor.
*
* @param context the {@link IdeContext}. method.
*/
public Sonar(IdeContext context) {

super(context, "sonar", Set.of(Tag.CODE_QA));

this.command = add(new EnumProperty<>("", true, "command", SonarCommand.class));
add(this.arguments);
}

@Override
protected void initProperties() {

// Empty on purpose
}

@Override
public boolean install(boolean silent) {

getCommandlet(Java.class).install();
return super.install(silent);
}

@Override
public void run() {

SonarCommand command = this.command.getValue();

switch (command) {
case ANALYZE:
getCommandlet(Mvn.class).runTool(null, "sonar:sonar");
break;
case START:
printSonarWebPort();
arguments.setValueAsString("start", context);
super.run();
break;
case STOP:
arguments.setValueAsString("stop", context);
super.run();
break;
default:
}
}

@Override
protected String getBinaryName() {

SonarCommand command = this.command.getValue();

Path toolBinPath = getToolBinPath();
String sonarLocation = null;

if (this.context.getSystemInfo().isWindows()) {
if (command.equals(SonarCommand.START)) {
sonarLocation = "windows-x86-64/StartSonar.bat";
} else if (command.equals(SonarCommand.STOP)) {
sonarLocation = "windows-x86-64/SonarService.bat";
}
} else if (this.context.getSystemInfo().isMac()) {
sonarLocation = "macosx-universal-64/sonar.sh";
} else {
sonarLocation = "linux-x86-64/sonar.sh";
}
return toolBinPath.resolve(sonarLocation).toString();
}

private void printSonarWebPort() {

this.context.info("SonarQube is running at localhost on the following port (default 9000):");
Path sonarPropertiesPath = getToolPath().resolve("conf/sonar.properties");

Properties sonarProperties = PropertiesFileUtil.loadProperties(sonarPropertiesPath);
String sonarWebPort = sonarProperties.getProperty("sonar.web.port");
if (sonarWebPort != null) {
this.context.info(sonarWebPort);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.devonfw.tools.ide.tool.sonar;

/**
* Represents commands for controlling a sonar operation in The{@link Sonar} Tool.
*/
public enum SonarCommand {
START, STOP, ANALYZE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.devonfw.tools.ide.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

public class PropertiesFileUtil {

/**
* Loads properties from a file at the given path.
*
* @param path the path to the properties file
* @return a Properties object loaded with properties from the file
*/
public static Properties loadProperties(Path path) {

Properties properties = new Properties();

try (var inputStream = Files.newInputStream(path)) {
properties.load(inputStream);
} catch (IOException e) {
throw new IllegalArgumentException(String.format("Cannot read Properties File at %s", path.toString()), e);
}
return properties;
}
}
2 changes: 2 additions & 0 deletions cli/src/main/resources/nls/Ide.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ cmd-shell=Commandlet to start built-in shell with advanced auto-completion.
cmd-terraform=Tool commandlet for Terraform
cmd-vscode=Tool commandlet for Visual Studio Code (IDE)
cmd-cobigen=Tool commandlet for Cobigen
cmd-sonar=Tool commandlet for SonarQube
val-args=The commandline arguments to pass to the tool.
val-edition=The tool edition.
val-sonar-command=START|STOP|ANALYZE
val-tool=The tool commandlet to select.
val-version=The tool version
val-set-version-version=The tool version to set.
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main/resources/nls/Ide_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ cmd-set-version=Setzt die Version des selektierten Werkzeugs.
cmd-terraform=Werkzeug Kommando für Terraform.
cmd-vscode=Werkzeug Kommando für Visual Studio Code (IDE)
cmd-cobigen=Werkzeug Kommando für Cobigen.
cmd-sonar=Werkzeug Kommando für SonarQube.
val-sonar-command=START|STOP|ANALYZE
val-args=Die Kommandozeilen-Argumente zur Übergabe an das Werkzeug.
val-edition=Die Werkzeug Edition.
val-tool=Das zu selektierende Werkzeug Kommando.
Expand Down
1 change: 1 addition & 0 deletions documentation/IDEasy-usage.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include::cli.asciidoc[leveloffset=2]

include::docker-desktop-alternative.asciidoc[leveloffset=3]
include::jmc.asciidoc[leveloffset=3]
include::sonar.asciidoc[leveloffset=3]

<<<<

Expand Down
1 change: 1 addition & 0 deletions documentation/cli.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ The following commandlets are currently available:
* link:tomcat.asciidoc[tomcat]
* link:vscode.asciidoc[vscode]
* link:yarn.asciidoc[yarn]
* link:sonar.asciidoc[sonar]
8 changes: 5 additions & 3 deletions documentation/jmc.asciidoc
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
:toc:
toc::[]

# Java Mission Control
== Java Mission Control

The `jmc` commandlet allows to install and setup https://www.oracle.com/java/technologies/jdk-mission-control.html[Java Mission Control]. To learn more about Java Mission Control, please go https://docs.oracle.com/en/java/java-components/jdk-mission-control/index.html[here].
The `jmc` commandlet allows to install and setup https://www.oracle.com/java/technologies/jdk-mission-control.html[Java Mission Control].
To learn more about Java Mission Control, please go https://docs.oracle.com/en/java/java-components/jdk-mission-control/index.html[here].

The arguments (`devon jmc «args»`) are explained by the following table:

[options="header"]
|=======================
|*Command* |*Meaning*
|`install jmc` |install Java Mission Control (or update and verify)
|`jmc «args»` |run Java Mission Control with the given `«args»`
|`jmc «args»` |run Java Mission Control with the given `«args»`
|=======================
18 changes: 18 additions & 0 deletions documentation/sonar.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
:toc:
toc::[]

== Sonar(SonarQube)

The `sonar` commandlet allows to Set up and run a local https://www.sonarsource.com/products/sonarqube/[SonarQube] Server.
A project can be analyzed with this commandlet.
The SonarQube server starts on port 9000 by default, but can be changed in the SonarQube Properties.

The arguments (`devon sonar «args»`) are explained by the following table:

[options="header"]
|=======================
|*Arguments* |*Meaning*
|`start` |start local SonarQube server
|`stop` |stop local SonarQube server
|`analyze` |analyze current project with SonarQube
|=======================
Loading