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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,73 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.PathProperty;
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;

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

/**
* The path to build from.
*/
public PathProperty path;

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

super(context);
addKeyword(getName());
this.path = add(new PathProperty("", false, "path", true));
hohwille marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String getName() {

return "build";
}

@Override
public void run() {

Path buildPath = null;
if (this.path.getValue() != null) {
buildPath = this.path.getValue();
}
if (buildPath == null) {
buildPath = this.context.getCwd();
this.context.info("No path was provided, using current working directory {} as fallback.", buildPath);
}
if (buildPath != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if buildPath (aka cwd) would be null then we do nothing?
IMHO this will never happen but if we want to handle this explicitly, I would rather do something like this:

if (buildPath == null) {
  throw new CliException("Missing current working directory!");
}

ToolCommandlet commandlet = null;
if (Files.exists(buildPath.resolve("pom.xml"))) {
commandlet = this.context.getCommandletManager().getCommandlet(Mvn.class);
} else if (Files.exists(buildPath.resolve("build.gradle"))) {
commandlet = this.context.getCommandletManager().getCommandlet(Gradle.class);
} 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);
}
}
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved

if (commandlet != null) {
this.context.info("Building project at: {} with: {}", buildPath, commandlet.getName());
commandlet.runTool(null, buildPath.toString());
hohwille marked this conversation as resolved.
Show resolved Hide resolved
}
}

}
}
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
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.
cmd.build.val.path=Filepath to the project to build.
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=Starte build Prozess.
cmd.build.val.path=Dateipfad zu dem Projekt, das gebaut werden soll.
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,77 @@
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 org.junit.jupiter.api.Test;

import java.nio.file.Path;

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

private static final String PROJECT_BUILD = "build";

/**
* Tests if the current working directory is being used if no path was provided.
*/
@Test
public void testUseCurrentWorkingDirectoryIfNoPathWasProvidedMvnBuild() {

IdeTestContext context = newContext(PROJECT_BUILD);
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
buildCommandlet.path.setValue(null);
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.INFO, "No path was provided, using current working directory " + context.getCwd() + " as fallback.");
}

/**
* Tests a {@link com.devonfw.tools.ide.tool.mvn.Mvn} build on a provided path.
*/
@Test
public void testMvnBuildWithProvidedPath() {

IdeTestContext context = newContext(PROJECT_BUILD);
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
Path workspacePath = context.getWorkspacePath().resolve("mvn");
buildCommandlet.path.setValue(workspacePath);
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, "Building project at: " + workspacePath + " with: mvn");
}

/**
* Tests a {@link com.devonfw.tools.ide.tool.gradle.Gradle} build on a provided path.
*/
@Test
public void testGradleBuildWithProvidedPath() {

IdeTestContext context = newContext(PROJECT_BUILD);
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
Path workspacePath = context.getWorkspacePath().resolve("gradle");
buildCommandlet.path.setValue(workspacePath);
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, "Building project at: " + workspacePath + " with: gradle");
}

/**
* Tests a {@link com.devonfw.tools.ide.tool.npm.Npm} build on a provided path.
*/
@Test
public void testNpmBuildWithProvidedPath() {

IdeTestContext context = newContext(PROJECT_BUILD);
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
Path workspacePath = context.getWorkspacePath().resolve("npm");
buildCommandlet.path.setValue(workspacePath);
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, "Building project at: " + workspacePath + " with: npm");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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
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 $*"
Loading