-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
cli/src/main/java/com/devonfw/tools/ide/commandlet/BuildCommandlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
cli/src/test/java/com/devonfw/tools/ide/commandlet/BuildCommandletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
cli/src/test/resources/ide-projects/build/project/settings/ide.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
2 changes: 2 additions & 0 deletions
2
cli/src/test/resources/ide-projects/build/repository/gradle/gradle/default/bin/gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
echo "gradle $*" |
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions
2
cli/src/test/resources/ide-projects/build/repository/mvn/mvn/default/bin/mvn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
echo "mvn $*" |
2 changes: 2 additions & 0 deletions
2
...c/test/resources/ide-projects/build/repository/node/node/default/node_modules/npm/bin/npm
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
...st/resources/ide-projects/build/repository/node/node/default/node_modules/npm/bin/npm.cmd
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
...c/test/resources/ide-projects/build/repository/node/node/default/node_modules/npm/bin/npx
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
...st/resources/ide-projects/build/repository/node/node/default/node_modules/npm/bin/npx.cmd
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
cli/src/test/resources/ide-projects/build/repository/npm/npm/default/bin/npm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
echo "npm $*" |