Skip to content

Commit

Permalink
Merge 9ecc8fc into bd17cb9
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-vcapgemini authored Jul 5, 2024
2 parents bd17cb9 + 9ecc8fc commit daa3c32
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.StringProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.tool.gradle.Gradle;
import com.devonfw.tools.ide.tool.mvn.Mvn;
import com.devonfw.tools.ide.tool.npm.Npm;

import java.nio.file.Files;
import java.nio.file.Path;

import static com.devonfw.tools.ide.variable.IdeVariables.GRADLE_BUILD_OPTS;
import static com.devonfw.tools.ide.variable.IdeVariables.MVN_BUILD_OPTS;
import static com.devonfw.tools.ide.variable.IdeVariables.NPM_BUILD_OPTS;

/**
* Build tool {@link Commandlet} for automatically detecting build configuration files and running the respective tool.
*/
public class BuildCommandlet extends Commandlet {

/**
* The arguments to build with.
*/
public StringProperty arguments;

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

super(context);
addKeyword(getName());
this.arguments = add(new StringProperty("", false, true, "args"));
}

@Override
public String getName() {

return "build";
}

@Override
public void run() {

Path buildPath = this.context.getCwd();
String[] defaultToolOptions = new String[0];

if (buildPath != null) {
ToolCommandlet commandlet = null;
if (Files.exists(buildPath.resolve("pom.xml"))) {
commandlet = this.context.getCommandletManager().getCommandlet(Mvn.class);
defaultToolOptions = getDefaultToolOptions(MVN_BUILD_OPTS.getName());
} else if (Files.exists(buildPath.resolve("build.gradle"))) {
commandlet = this.context.getCommandletManager().getCommandlet(Gradle.class);
defaultToolOptions = getDefaultToolOptions(GRADLE_BUILD_OPTS.getName());
} else if (Files.exists(buildPath.resolve("package.json"))) {
if (Files.exists(buildPath.resolve("yarn.lock"))) {
// TODO: add yarn here
} else {
commandlet = this.context.getCommandletManager().getCommandlet(Npm.class);

defaultToolOptions = getDefaultToolOptions(NPM_BUILD_OPTS.getName());
}
} else {
throw new CliException("Could not find build descriptor - no pom.xml, build.gradle, or package.json found!");
}

if (this.arguments.asArray().length != 0) {
defaultToolOptions = this.arguments.asArray();
}

if (commandlet != null) {
commandlet.runTool(null, defaultToolOptions);
}
}

}

private String[] getDefaultToolOptions(String buildOptionName) {

String[] defaultToolOptions;
defaultToolOptions = this.context.getVariables().get(buildOptionName).split(" ");
return defaultToolOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public CommandletManagerImpl(IdeContext context) {
add(new UninstallCommandlet(context));
add(new UpdateCommandlet(context));
add(new CreateCommandlet(context));
add(new BuildCommandlet(context));
add(new Gh(context));
add(new Helm(context));
add(new Java(context));
Expand Down
20 changes: 18 additions & 2 deletions cli/src/main/java/com/devonfw/tools/ide/variable/IdeVariables.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public interface IdeVariables {

/** {@link VariableDefinition} for {@link com.devonfw.tools.ide.context.IdeContext#getWorkspaceName() WORKSPACE}. */

/** {@link VariableDefinition} for default build options of mvn */
VariableDefinitionString MVN_BUILD_OPTS = new VariableDefinitionString("MVN_BUILD_OPTS", null, c -> "clean install");

/** {@link VariableDefinition} for default build options of npm */
// TODO: add default build options, see: https://github.com/devonfw/IDEasy/issues/441
VariableDefinitionString NPM_BUILD_OPTS = new VariableDefinitionString("NPM_BUILD_OPTS", null, c -> "");

/** {@link VariableDefinition} for default build options of gradle */
VariableDefinitionString GRADLE_BUILD_OPTS = new VariableDefinitionString("GRADLE_BUILD_OPTS", null, c -> "clean dist");

/** {@link VariableDefinition} for default build options of yarn */
// TODO: add default build options, see: https://github.com/devonfw/IDEasy/issues/441
VariableDefinitionString YARN_BUILD_OPTS = new VariableDefinitionString("YARN_BUILD_OPTS", null, c -> "");

/** {@link VariableDefinition} for options of jasypt */
VariableDefinitionString JASYPT_OPTS = new VariableDefinitionString("JASYPT_OPTS", null,
c -> "algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator");
Expand All @@ -63,11 +77,13 @@ public interface IdeVariables {
* {@link VariableDefinition} for support of legacy variable syntax when
* {@link com.devonfw.tools.ide.environment.EnvironmentVariables#resolve(String, Object, boolean) resolving variables} in configuration templates.
*/
VariableDefinitionBoolean IDE_VARIABLE_SYNTAX_LEGACY_SUPPORT_ENABLED = new VariableDefinitionBoolean("IDE_VARIABLE_SYNTAX_LEGACY_SUPPORT_ENABLED", null, c -> Boolean.TRUE);
VariableDefinitionBoolean IDE_VARIABLE_SYNTAX_LEGACY_SUPPORT_ENABLED = new VariableDefinitionBoolean("IDE_VARIABLE_SYNTAX_LEGACY_SUPPORT_ENABLED", null,
c -> Boolean.TRUE);

/** A {@link Collection} with all pre-defined {@link VariableDefinition}s. */
Collection<VariableDefinition<?>> VARIABLES = List.of(PATH, HOME, WORKSPACE_PATH, IDE_HOME, IDE_ROOT, WORKSPACE, IDE_TOOLS, CREATE_START_SCRIPTS,
IDE_MIN_VERSION, MVN_VERSION, M2_REPO, DOCKER_EDITION, JASYPT_OPTS, MAVEN_ARGS, PROJECT_NAME, IDE_VARIABLE_SYNTAX_LEGACY_SUPPORT_ENABLED);
IDE_MIN_VERSION, MVN_VERSION, M2_REPO, DOCKER_EDITION, MVN_BUILD_OPTS, NPM_BUILD_OPTS, GRADLE_BUILD_OPTS, YARN_BUILD_OPTS, JASYPT_OPTS, MAVEN_ARGS,
PROJECT_NAME, IDE_VARIABLE_SYNTAX_LEGACY_SUPPORT_ENABLED);

/**
* @param name the name of the requested {@link VariableDefinition}.
Expand Down
3 changes: 3 additions & 0 deletions cli/src/main/resources/nls/Help.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ cmd.aws=Tool commandlet for AWS CLI.
cmd.aws.detail=The AWS Command Line Interface (AWS CLI) is an open source tool for managing AWS resources. Detailed documentation can be found at https://docs.aws.amazon.com/cli/
cmd.az=Tool commandlet for Azure CLI.
cmd.az.detail=The Azure Command-Line Interface (CLI) is a tool for creating and managing Azure resources. Detailed documentation can be found at https://learn.microsoft.com/en-us/cli/azure/
cmd.build=Runs a build job with given arguments.
cmd.build.detail=The `build` commandlet is an abstraction of build systems like https://maven.apache.org/, https://gradle.org/, https://yarnpkg.com/, https://www.npmjs.com/, etc.\nIt will auto-detect your build-system (via existence of files like `pom.xml`, `package.json`, etc.). According to this detection, it will simply delegate to the according commandlet of the specific build system. If that build-system is not yet available it will be downloaded and installed automatically.\nSo `ide build` allows users to build any project without bothering about the build-system. Further specific build options can be configured per project. This makes `ide build` a universal part of every _definition of done_. Before pushing your changes, please always run the following command to verify the build:\n\n 'ide build'\n\nYou may also supply additional arguments as `ide build «args»`. This will simply delegate these arguments to the detected build command (e.g. call `mvn «args»`).
cmd.build.val.args=Build arguments to be used when running a build job.
cmd.cobigen=Tool commandlet for Cobigen (code-generator).
cmd.cobigen.detail=TODO cobigen
cmd.complete=Internal commandlet for bash auto-completion.
Expand Down
3 changes: 3 additions & 0 deletions cli/src/main/resources/nls/Help_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ cmd.aws=Werkzeug Kommando für AWS Kommandoschnittstelle.
cmd.aws.detail=Das AWS Command Line Interface (AWS CLI) ist ein Open-Source-Werkzeug zur Verwaltung von AWS Ressourcen. Detaillierte Dokumentation ist zu finden unter https://docs.aws.amazon.com/cli/
cmd.az=Werkzeug Kommando für Azure Kommandoschnittstelle.
cmd.az.detail=Das Azure Command-Line Interface (CLI) ist ein Werkzeug zur Erstellung und Verwaltung von Azure Ressourcen. Detaillierte Dokumentation ist zu finden unter https://learn.microsoft.com/en-us/cli/azure/
cmd.build=Startet einen build Prozess mit übergebenen Argumenten.
cmd.build.detail=Der `build`-Befehl ist eine Abstraktion von Build-Systemen wie https://maven.apache.org/, https://gradle.org/, https://yarnpkg.com/, https://www.npmjs.com/ usw.\nEr erkennt automatisch Ihr Build-System (anhand von Dateien wie `pom.xml`, `package.json` usw.). Entsprechend dieser Erkennung delegiert er einfach an den entsprechenden Befehl des spezifischen Build-Systems. Wenn dieses Build-System noch nicht verfügbar ist, wird es automatisch heruntergeladen und installiert.\nSo ermöglicht `ide build` Benutzern, jedes Projekt zu erstellen, ohne sich um das Build-System kümmern zu müssen. Weitere spezifische Build-Optionen können pro Projekt konfiguriert werden. Dies macht `ide build` zu einem universellen Bestandteil jeder _Definition of Done_. Bevor Sie Ihre Änderungen übertragen, führen Sie bitte immer den folgenden Befehl aus, um den Build zu überprüfen:\n\n`ide build`\n\nSie können auch zusätzliche Argumente wie `ide build «args»` angeben. Diese werden einfach an den erkannten Build-Befehl weitergeleitet (z. B. Aufruf von `mvn «args»`).
cmd.build.val.args=Argumente die bei dem build Prozess genutzt werden sollen.
cmd.cobigen=Werkzeug Kommando für Cobigen.
cmd.cobigen.detail=TODO DE cobigen
cmd.complete=Internes Werkzeug für bash Autovervollständigung.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.os.SystemInfoMock;
import org.junit.jupiter.api.Test;

/**
* Test of {@link BuildCommandlet}.
*/
public class BuildCommandletTest extends AbstractIdeContextTest {

private static final String PROJECT_BUILD = "build";

/**
* Tests a {@link com.devonfw.tools.ide.tool.mvn.Mvn} build without arguments and expects defaults to be taken from ide.properties.
*/
@Test
public void testMvnBuildWithoutProvidedArgumentsUsesDefaultOptions() {

IdeTestContext context = newContext(PROJECT_BUILD);
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
context.setCwd(context.getWorkspacePath().resolve("mvn"), context.getWorkspacePath().toString(), context.getIdeHome());
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed java in version 17.0.10_7");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed mvn in version 3.9.6");
assertLogMessage(context, IdeLogLevel.INFO, "mvn clean compile");
}

/**
* Tests a {@link com.devonfw.tools.ide.tool.mvn.Mvn} build with provided arguments.
*/
@Test
public void testMvnBuildWithProvidedArguments() {

IdeTestContext context = newContext(PROJECT_BUILD);
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
context.setCwd(context.getWorkspacePath().resolve("mvn"), context.getWorkspacePath().toString(), context.getIdeHome());
buildCommandlet.arguments.addValue("clean");
buildCommandlet.arguments.addValue("install");
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed java in version 17.0.10_7");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed mvn in version 3.9.6");
assertLogMessage(context, IdeLogLevel.INFO, "mvn clean install");
}

/**
* Tests a {@link com.devonfw.tools.ide.tool.gradle.Gradle} build with provided arguments.
*/
@Test
public void testGradleBuildWithProvidedArguments() {

IdeTestContext context = newContext(PROJECT_BUILD);
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
context.setCwd(context.getWorkspacePath().resolve("gradle"), context.getWorkspacePath().toString(), context.getIdeHome());
buildCommandlet.arguments.addValue("task1");
buildCommandlet.arguments.addValue("task2");
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed java in version 17.0.10_7");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed gradle in version 8.7");
assertLogMessage(context, IdeLogLevel.INFO, "gradle task1 task2");
}

/**
* Tests a {@link com.devonfw.tools.ide.tool.npm.Npm} build with provided arguments.
*/
@Test
public void testNpmBuildWithProvidedArguments() {

SystemInfo systemInfo = SystemInfoMock.of("linux");
IdeTestContext context = newContext(PROJECT_BUILD);
context.setSystemInfo(systemInfo);
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
context.setCwd(context.getWorkspacePath().resolve("npm"), context.getWorkspacePath().toString(), context.getIdeHome());
buildCommandlet.arguments.addValue("start");
buildCommandlet.arguments.addValue("test");
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed node in version v18.19.1");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed npm in version 9.9.2");
assertLogMessage(context, IdeLogLevel.INFO, "npm start test");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
JAVA_VERSION=17.0.10_7
MAVEN_VERSION=3.9.6
NODE_VERSION=v18.19.1
NPM_VERSION=9.9.2
GRADLE_VERSION=8.7
MVN_BUILD_OPTS=clean compile
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "gradle $*"
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "mvn $*"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "npm $*"

0 comments on commit daa3c32

Please sign in to comment.