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

#317: added BuildCommandlet #340

Merged
merged 27 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ebc970a
#317: added BuildCommandlet
jan-vcapgemini May 17, 2024
c5a3df1
Merge branch 'main' into feature/317-build-commandlet
jan-vcapgemini May 17, 2024
99eb686
#317: fixed npm test
jan-vcapgemini May 21, 2024
fdf83cb
Merge branch 'main' into feature/317-build-commandlet
jan-vcapgemini May 21, 2024
ea88658
#317: fixed nls format
jan-vcapgemini May 21, 2024
efc9081
#317: fixed tests
jan-vcapgemini May 21, 2024
fbfec39
Merge branch 'main' into feature/317-build-commandlet
jan-vcapgemini May 23, 2024
66dcb1a
Merge branch 'main' into feature/317-build-commandlet
jan-vcapgemini May 29, 2024
823b241
#317: implemented requested changes
jan-vcapgemini May 29, 2024
ed33422
#317: fixed npm test
jan-vcapgemini May 30, 2024
f9cdca7
#317: adjusted help
jan-vcapgemini May 30, 2024
976abb3
#317: added documentation files
jan-vcapgemini May 30, 2024
72075c9
#317: adjusted documentation
jan-vcapgemini May 30, 2024
c129a41
#12: implemented requested changes
jan-vcapgemini May 30, 2024
1299a62
Merge branch 'main' into feature/317-build-commandlet
jan-vcapgemini Jun 19, 2024
6afebdb
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/BuildComman…
jan-vcapgemini Jun 19, 2024
61a4c9f
#317: added missing import
jan-vcapgemini Jun 19, 2024
61e05f3
#317: added defaults
jan-vcapgemini Jul 1, 2024
ad07894
Merge branch 'main' into feature/317-build-commandlet
jan-vcapgemini Jul 3, 2024
e406a04
#317: adjusted help
jan-vcapgemini Jul 3, 2024
b5db8ba
#317: implemented requested changes
jan-vcapgemini Jul 3, 2024
a6b1cee
#317: implemented requested change
jan-vcapgemini Jul 3, 2024
a04ece4
#317: fixed help test
jan-vcapgemini Jul 3, 2024
513749c
Merge branch 'main' into feature/317-build-commandlet
jan-vcapgemini Jul 4, 2024
80bb4af
Merge branch 'main' into feature/317-build-commandlet
jan-vcapgemini Jul 4, 2024
9ecc8fc
Merge branch 'main' into feature/317-build-commandlet
hohwille Jul 5, 2024
ba10427
#317: implemented requested change
jan-vcapgemini Jul 5, 2024
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
@@ -0,0 +1,86 @@
package com.devonfw.tools.ide.commandlet;

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) {
hohwille marked this conversation as resolved.
Show resolved Hide resolved
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());
}
}
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -76,6 +76,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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ 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 -> "");
hohwille marked this conversation as resolved.
Show resolved Hide resolved

/** {@link VariableDefinition} for default build options of npm */
VariableDefinitionString NPM_BUILD_OPTS = new VariableDefinitionString("NPM_BUILD_OPTS", null, c -> "");
hohwille marked this conversation as resolved.
Show resolved Hide resolved

/** {@link VariableDefinition} for default build options of gradle */
VariableDefinitionString GRADLE_BUILD_OPTS = new VariableDefinitionString("GRADLE_BUILD_OPTS", null, c -> "");
hohwille marked this conversation as resolved.
Show resolved Hide resolved

/** {@link VariableDefinition} for default build options of yarn */
VariableDefinitionString YARN_BUILD_OPTS = new VariableDefinitionString("YARN_BUILD_OPTS", null, c -> "");
hohwille marked this conversation as resolved.
Show resolved Hide resolved

/** {@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 @@ -58,7 +70,7 @@ public interface IdeVariables {

/** 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, DOCKER_EDITION, JASYPT_OPTS, MAVEN_ARGS, PROJECT_NAME);
IDE_MIN_VERSION, MVN_VERSION, DOCKER_EDITION, MVN_BUILD_OPTS, NPM_BUILD_OPTS, GRADLE_BUILD_OPTS, YARN_BUILD_OPTS, JASYPT_OPTS, MAVEN_ARGS, PROJECT_NAME);

/**
* @param name the name of the requested {@link VariableDefinition}.
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main/resources/nls/Help.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmd.--version=Print the version of IDEasy.
cmd.aws=Tool commandlet for AWS CLI.
cmd.az=Tool commandlet for Azure CLI.
cmd.build=Runs a build job with given arguments.
cmd.build.val.args=Build arguments to be used when running a build job.
cmd.cobigen=Tool commandlet for Cobigen (code-generator).
cmd.complete=Internal commandlet for bash auto-completion.
cmd.create=Create a new IDEasy project.
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main/resources/nls/Help_de.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmd.--version=Gibt die Version von IDEasy aus.
cmd.aws=Werkzeug Kommando für AWS Kommandoschnittstelle.
cmd.az=Werkzeug Kommando für Azure Kommandoschnittstelle.
cmd.build=Startet einen build Prozess mit übergebenen Argumenten.
cmd.build.val.args=Argumente die bei dem build Prozess genutzt werden sollen.
cmd.cobigen=Werkzeug Kommando für Cobigen.
cmd.complete=Internes Werkzeug für bash Autovervollständigung.
cmd.create=Erstellt ein neues IDEasy Projekt.
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 install");
}

/**
* 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 install
hohwille marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "gradle $*"
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 $*"
12 changes: 12 additions & 0 deletions documentation/build.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:toc:
hohwille marked this conversation as resolved.
Show resolved Hide resolved
toc::[]

= build
The `build` commandlet is an abstraction of build systems like link:mvn.asciidoc[maven], link:gradle.asciidoc[gradle], link:npm.asciidoc[yarn], link:npm.asciidoc[npm], etc.
It 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.

So `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:

`ide build`

You 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»`).
26 changes: 26 additions & 0 deletions documentation/gradle.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:toc:
toc::[]

= gradle

The `gradle` commandlet allows to install, configure, and launch https://gradle.org/[gradle]. It is similar to https://docs.gradle.org/5.3.1/userguide/gradle_wrapper.html[gradle-wrapper]. So calling `ide gradle «args»` is more or less the same as calling `gradle «args»` but with the benefit that the version of gradle preferred by your project is used (and will be installed if not yet available).

The arguments (`ide gradle «args»`) are explained by the following table:

.Usage of `ide gradle`
[options="header"]
|=======================
|*Argument(s)* |*Meaning*
|`«args»` |run gradle with the given arguments (`«args»`)
|=======================

There are link:variables.asciidoc[variables] that can be used for gradle.
These are explained by the following table:

.Variables of IDEasy for gradle
[options="header"]
|=======================
|*Variable*|*Meaning*
|*`GRADLE_VERSION`* |The version of the tool gradle to install and use.
|*`GRADLE_BUILD_OPTS`* |The default arguments to use for the gradle build process e.g. `project1`.
|=======================
27 changes: 27 additions & 0 deletions documentation/mvn.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
:toc:
toc::[]

= mvn

The `mvn` commandlet allows to install, configure, and launch https://maven.apache.org/[maven]. It is similar to https://github.com/takari/maven-wrapper[maven-wrapper] and https://github.com/dansomething/mdub[mdub]. So calling `ide mvn «args»` is more or less the same as calling `mvn «args»` but with the benefit that the version of maven preferred by your project is used (and will be installed if not yet available).

The arguments (`ide mvn «args»`) are explained by the following table:

.Usage of `ide mvn`
[options="header"]
|=======================
|*Argument(s)* |*Meaning*
| |run default build, link:configuration.asciidoc[configurable] via `MVN_BUILD_OPTS`
|`«args»` |run maven with the given arguments (`«args»`)
|=======================

There are link:variables.asciidoc[variables] that can be used for mvn.
These are explained by the following table:

.Variables of IDEasy for mvn
[options="header"]
|=======================
|*Variable*|*Meaning*
|*`MVN_VERSION`* |The version of the tool mvn to install and use.
|*`MVN_BUILD_OPTS`* |The default options to use for the mvn build process e.g. `clean install`.
|=======================
27 changes: 27 additions & 0 deletions documentation/npm.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
:toc:
toc::[]

= npm

The `npm` commandlet allows to install, configure, and launch https://www.npmjs.com/[npm]. Calling `ide npm «args»` is more or less the same as calling `npm «args»` but with the benefit that the version of npm preferred by your project is used (and will be installed if not yet available).

The arguments (`ide npm «args»`) are explained by the following table:

.Usage of `ide npm`
[options="header"]
|=======================
|*Argument(s)* |*Meaning*
| |run default build, link:configuration.asciidoc[configurable] via `NPM_BUILD_OPTS`
|`«args»` |run NPM with the given arguments (`«args»`)
|=======================

There are link:variables.asciidoc[variables] that can be used for npm.
These are explained by the following table:

.Variables of IDEasy for npm
[options="header"]
|=======================
|*Variable*|*Meaning*
|*`NPM_VERSION`* |The version of the tool npm to install and use.
|*`NPM_BUILD_OPTS`* |The default options to use for the npm build process e.g. `run start`.
|=======================
26 changes: 26 additions & 0 deletions documentation/yarn.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:toc:
toc::[]

= yarn

The `yarn` commandlet allows to install, configure, and launch https://www.npmjs.com/[npm]. Calling `ide yarn «args»` is more or less the same as calling `yarn «args»` but with the benefit that the version of npm preferred by your project is used (and will be installed if not yet available).

The arguments (`ide yarn «args»`) are explained by the following table:

.Usage of `ide yarn`
[options="header"]
|=======================
|*Argument(s)* |*Meaning*
|`«args»` |run yarn with the given arguments (`«args»`)
|=======================

There are link:variables.asciidoc[variables] that can be used for yarn.
These are explained by the following table:

.Variables of IDEasy for yarn
[options="header"]
|=======================
|*Variable*|*Meaning*
|*`YARN_VERSION`* |The version of the tool yarn to install and use.
|*`YARN_BUILD_OPTS`* |The default options to use for the yarn build process e.g. `run start`.
|=======================
Loading