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

#100: implement repository commandlet #143

Merged
merged 81 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
e9d574f
added class RepositoryCommandlet and its help function
salimbouch Nov 8, 2023
76eba65
implemented run method when project is given as argument
salimbouch Nov 9, 2023
0e16de6
implemented loadProperties
salimbouch Nov 10, 2023
69835e3
added record RepositoryConfig, implemented run method, implemented lo…
salimbouch Nov 11, 2023
90145a6
implemented build functionality, import missing
salimbouch Nov 13, 2023
c738a0e
minor improvements
salimbouch Nov 13, 2023
c3de09e
javadoc
salimbouch Nov 14, 2023
c0fbd26
fixed bug: tool installed but not found
salimbouch Nov 16, 2023
89061ae
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Nov 16, 2023
1c729ae
removed useless improt
salimbouch Nov 28, 2023
01cb2b9
minor change
salimbouch Nov 28, 2023
db8008a
Update RepositoryCommandlet.java
salimbouch Nov 28, 2023
82cc617
changes after review
salimbouch Nov 30, 2023
dbc3bd3
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Dec 13, 2023
a582601
Update cli/src/main/resources/nls/Ide_de.properties
salimbouch Dec 13, 2023
7e3bdd9
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Dec 13, 2023
d71f68e
Update cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java
salimbouch Dec 13, 2023
1e5aba8
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Dec 13, 2023
c8cf771
Update cli/src/main/resources/nls/Ide.properties
salimbouch Dec 13, 2023
4b60135
Update cli/src/main/resources/nls/Ide.properties
salimbouch Dec 13, 2023
061cfa5
Update cli/src/main/resources/nls/Ide_de.properties
salimbouch Dec 13, 2023
c314c08
reviewed changes
salimbouch Dec 13, 2023
e7a8323
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Dec 13, 2023
07d4a34
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Dec 13, 2023
37da950
updates
salimbouch Dec 13, 2023
45e16e8
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Dec 13, 2023
05bcde1
reviewed changes
salimbouch Dec 13, 2023
e055a6a
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Dec 13, 2023
f2ee210
reviewed changes
salimbouch Dec 13, 2023
268a067
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Dec 13, 2023
5773121
implemented repositoryProperty
salimbouch Dec 15, 2023
d047d62
removed useless comment
salimbouch Dec 19, 2023
48db7c3
added null check
salimbouch Jan 2, 2024
dcecb50
Update RepositoryProperty.java
salimbouch Jan 2, 2024
4880d12
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Jan 7, 2024
de061fe
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Jan 7, 2024
e62ea03
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Jan 7, 2024
8a35bbd
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Jan 7, 2024
e8ba8f7
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Jan 7, 2024
21f14df
reviewed changes
salimbouch Jan 8, 2024
b030586
Merge branch 'main' into 100-implement-repository-commandlet
hohwille Jan 8, 2024
f847820
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Jan 11, 2024
bb2beb8
Merge branch 'main' of https://github.com/devonfw/IDEasy into 100-imp…
salimbouch Jan 18, 2024
ff0d192
fixed conflicts
salimbouch Jan 23, 2024
bc7b48a
wrote test for repositoryCom
salimbouch Jan 25, 2024
e21f222
changed repositoryProperty to FileProperty inheritance
salimbouch Jan 26, 2024
d06a970
Merge branch 'main' of https://github.com/devonfw/IDEasy into 100-imp…
salimbouch Feb 2, 2024
df05c7c
Update Ide_de.properties
salimbouch Feb 2, 2024
3c0584b
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Feb 15, 2024
a218748
Merge branch 'main' into 100-implement-repository-commandlet
hohwille Feb 19, 2024
e0a2b50
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Feb 20, 2024
7b119df
Update cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java
salimbouch Feb 21, 2024
a60cf62
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Feb 21, 2024
128dbcf
minor change
salimbouch Feb 27, 2024
216260c
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Feb 27, 2024
7a3c3c2
added tests for repo commandlet
salimbouch Feb 29, 2024
e5b794a
removed my stupid fix
salimbouch Feb 29, 2024
9254184
removed * import
salimbouch Mar 4, 2024
b490f16
adjusted tests
salimbouch Mar 6, 2024
424887f
Merge branch 'main' into 100-implement-repository-commandlet
hohwille Mar 12, 2024
2b99a21
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Mar 13, 2024
6129a5a
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Mar 13, 2024
4b5b005
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Mar 13, 2024
5dd5d49
Merge branch 'main' of https://github.com/devonfw/ideasy into 100-imp…
salimbouch Mar 13, 2024
d079c78
reviewed changes
salimbouch Mar 13, 2024
34408ea
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Mar 13, 2024
7a9bb0b
minor change
salimbouch Mar 13, 2024
4236eae
crlf diff
salimbouch Mar 13, 2024
862eabc
diff war
salimbouch Mar 13, 2024
de2c3f9
solved minor bug
salimbouch Mar 13, 2024
6a1eefc
cleanup
salimbouch Mar 13, 2024
62fe8f2
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Mar 13, 2024
8a06af5
again crlf switching ..
salimbouch Mar 13, 2024
1752f0e
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Mar 13, 2024
b7e76d0
Update AbstractIdeContext.java
salimbouch Mar 13, 2024
6765d89
Update GitContextImpl.java
salimbouch Mar 13, 2024
6a8bf0e
Update GitContextImpl.java
salimbouch Mar 13, 2024
7b4bc57
Update GitContextImpl.java
salimbouch Mar 13, 2024
b73e83d
small git api improvements
salimbouch Mar 15, 2024
f513dfa
Update GitContextImpl.java
salimbouch Mar 15, 2024
5854687
Merge branch 'main' into 100-implement-repository-commandlet
hohwille Mar 19, 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
Expand Up @@ -65,6 +65,7 @@ public CommandletManagerImpl(IdeContext context) {
add(new EditionSetCommandlet(context));
add(new EditionListCommandlet(context));
add(new VersionCommandlet(context));
add(new RepositoryCommandlet(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
@@ -0,0 +1,130 @@
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.property.RepositoryProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import static com.devonfw.tools.ide.commandlet.RepositoryConfig.loadProperties;

/**
* {@link Commandlet} to setup one or multiple GIT repositories for development.
*/
public class RepositoryCommandlet extends Commandlet {

/** the repository to setup. */
public final RepositoryProperty repository;

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

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

@Override
public String getName() {

return "repository";
}

@Override
public void run() {

Path repositoryFile = repository.getValue();

if (repositoryFile != null) {
// Handle the case when a specific repository is provided
doImportRepository(repositoryFile, true);
} else {
// If no specific repository is provided, check for repositories folder
Path repositoriesPath = this.context.getSettingsPath().resolve(IdeContext.FOLDER_REPOSITORIES);
Path legacyRepositoriesPath = this.context.getSettingsPath().resolve(IdeContext.FOLDER_LEGACY_REPOSITORIES);
Path repositories;
if (Files.exists(repositoriesPath)) {
repositories = repositoriesPath;
} else if (Files.exists(legacyRepositoriesPath)) {
repositories = legacyRepositoriesPath;
} else {
this.context.warning("Cannot find repositories folder nor projects folder.");
return;
}

List <Path> propertiesFiles = this.context.getFileAccess().getChildrenInDir(repositories,
path -> path.getFileName().toString().endsWith(".properties"));

boolean forceMode = this.context.isForceMode();
for (Path propertiesFile : propertiesFiles) {
doImportRepository(propertiesFile, forceMode);
}
}
}

private void doImportRepository(Path repositoryFile, boolean forceMode) {

this.context.info("Importing repository from {} ...", repositoryFile.getFileName().toString());
RepositoryConfig repositoryConfig = loadProperties(repositoryFile);

if (!repositoryConfig.active()) {
this.context.info("Repository is not active by default.");
if (forceMode) {
this.context.info("Repository setup is forced, hence proceeding ...");
} else {
this.context.info("Skipping repository - use force (-f) to setup all repositories ...");
return;
}
}

String repository = repositoryConfig.path();
String gitUrl = repositoryConfig.gitUrl();
if (repository == null || "".equals(repository) || gitUrl == null || "".equals(gitUrl)) {
this.context.warning("Invalid repository configuration {} - both 'path' and 'git-url' have to be defined."
, repositoryFile);
return;
}

this.context.debug(repositoryConfig.toString());

String workspace = repositoryConfig.workspace() != null ? repositoryConfig.workspace() : "main";
Path workspacePath = this.context.getIdeHome().resolve("workspaces").resolve(workspace);
this.context.getFileAccess().mkdirs(workspacePath);

if (repositoryConfig.gitBranch() != null && !repositoryConfig.gitBranch().isEmpty()) {
gitUrl = gitUrl + "#" + repositoryConfig.gitBranch();
}
hohwille marked this conversation as resolved.
Show resolved Hide resolved

Path repositoryPath = workspacePath.resolve(repository);
this.context.gitPullOrClone(repositoryPath, gitUrl);

String buildCmd = repositoryConfig.buildCmd();
this.context.debug("Building repository with ide command: {}", buildCmd);
if (buildCmd != null && !buildCmd.isEmpty()) {
String[] command = buildCmd.split("\\s+");
ToolCommandlet commandlet = this.context.getCommandletManager().getToolCommandlet(command[0]);
List<String> args = new ArrayList<>(command.length - 1);
for (int i = 1; i < command.length; i++) {
args.add(command[i]);
}
commandlet.arguments.setValue(args);
commandlet.run();
} else {
this.context.info("Build command not set. Skipping build for repository.");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.devonfw.tools.ide.commandlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Properties;
import java.util.Set;

/**
* Represents the configuration of a repository to be used by the {@link RepositoryCommandlet}.
*
* @param path Path into which the project is cloned. This path is relative to the workspace.
* @param workingSets The working sets associated with the repository.
* @param workspace Workspace to use for checkout and import. Default is main.
* @param gitUrl Git URL to use for cloning the project.
* @param gitBranch Git branch to checkout. Git default branch is default.
* @param buildPath The build path for the repository.
* @param buildCmd The command to invoke to build the repository after clone or pull. If omitted no build is triggered.
* @param imports list of IDEs where the repository will be imported to.
* @param active {@code true} to setup the repository during setup, {@code false} to skip.
*/
public record RepositoryConfig(
String path,
String workingSets,
String workspace,
String gitUrl,
String gitBranch,
String buildPath,
String buildCmd,
Set<String> imports,
boolean active) {
public static RepositoryConfig loadProperties(Path filePath) {

Properties properties = new Properties();
try (InputStream input = new FileInputStream(filePath.toString())) {
properties.load(input);
} catch (IOException e) {
throw new IllegalStateException("Failed to read file: " + filePath, e);
}

Set<String> importsSet = getImports(properties);

return new RepositoryConfig(properties.getProperty("path"), properties.getProperty("workingsets"),
properties.getProperty("workspace"), properties.getProperty("git_url"), properties.getProperty("git_branch"),
properties.getProperty(("build_path")), properties.getProperty("build_cmd"), importsSet,
Boolean.parseBoolean(properties.getProperty("active").trim()));
}

private static Set<String> getImports(Properties properties) {

String importProperty = properties.getProperty("import");
if (importProperty != null && !importProperty.isEmpty()) {
return Set.of(importProperty.split("\\s*,\\s*"));
}

String legacyImportProperty = properties.getProperty("eclipse");
if ("import".equals(legacyImportProperty)) {
return Set.of("eclipse");
} else {
return Collections.emptySet();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public Path getPath(String tool) {
*/
public void setPath(String tool, Path path) {

this.paths.add(path);
hohwille marked this conversation as resolved.
Show resolved Hide resolved
this.tool2pathMap.put(tool, path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ public void gitPullOrClone(Path target, String gitRepoUrl) {
if (!gitRepoUrl.startsWith("http")) {
throw new IllegalArgumentException("Invalid git URL '" + gitRepoUrl + "'!");
}
debug("Pull or clone git repository {} ...", gitRepoUrl);
ProcessContext pc = newProcess().directory(target).executable("git").withEnvVar("GIT_TERMINAL_PROMPT", "0");
if (Files.isDirectory(target.resolve(".git"))) {
ProcessResult result = pc.addArg("remote").run(true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public interface IdeContext extends IdeLogger {
/** The name of the bin folder where executable files are found by default. */
String FOLDER_BIN = "bin";

/** The name of the repositories folder where properties files are stores for each repository */
String FOLDER_REPOSITORIES = "repositories";

/** The name of the repositories folder where properties files are stores for each repository */
String FOLDER_LEGACY_REPOSITORIES = "projects";

/** The name of the Contents folder inside a MacOS app. */
String FOLDER_CONTENTS = "Contents";

Expand Down
18 changes: 18 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devonfw.tools.ide.io;

import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -149,4 +150,21 @@ default void copy(Path source, Path target) {
*/
Path findFirst(Path dir, Predicate<Path> filter, boolean recursive);

/**
* Retrieves a list of file paths from the specified directory that satisfy the given predicate.
* @param dir the folder to iterate through
* @param filter the {@link Predicate} that determines whether a file should be included in the list.
* @return a list of paths that satisfy the provided {@link Predicate}. Will be {@link List#isEmpty() empty} if no match was found but is never {@code null}.
*/
List<Path> getChildrenInDir(Path dir, Predicate<Path> filter);

/**
* Finds the existing file with the specified name in the given list of directories.
*
* @param fileName The name of the file to find.
* @param searchDirs The list of directories to search for the file.
* @return The {@code Path} of the existing file, or {@code null} if the file is not found.
*/
Path findExistingFile(String fileName, List<Path> searchDirs);

}
33 changes: 33 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,37 @@ private Path findFirstRecursive(Path dir, Predicate<Path> filter, boolean recurs
return null;
}

@Override
public List<Path> getChildrenInDir(Path dir, Predicate<Path> filter) {
List<Path> files = new ArrayList<>();
try (Stream<Path> childStream = Files.list(dir)) {
Iterator<Path> iterator = childStream.iterator();
while (iterator.hasNext()) {
Path child = iterator.next();
if (filter.test(child)) {
files.add(child);
}
}
} catch (IOException e) {
throw new IllegalStateException("Failed to iterate through " + dir, e);
}
return files;
}

@Override
public Path findExistingFile(String fileName, List<Path> searchDirs) {

for (Path dir : searchDirs) {
Path filePath = dir.resolve(fileName);
try {
if (Files.exists(filePath)) {
return filePath;
}
} catch (SecurityException e) {
throw new IllegalStateException("SecurityException while checking file existence.");
salimbouch marked this conversation as resolved.
Show resolved Hide resolved
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.devonfw.tools.ide.property;

import com.devonfw.tools.ide.context.IdeContext;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.function.Consumer;

public class RepositoryProperty extends FileProperty {

/**
* The constructor.
*
* @param name the {@link #getName() property name}.
* @param required the {@link #isRequired() required flag}.
* @param alias the {@link #getAlias() property alias}.
* @param mustExist the {@link #isPathRequiredToExist() required to exist flag}.
*/
public RepositoryProperty(String name, boolean required, String alias, boolean mustExist) {

super(name, required, alias, mustExist);
}
salimbouch marked this conversation as resolved.
Show resolved Hide resolved

/**
* The constructor.
*
* @param name the {@link #getName() property name}.
* @param required the {@link #isRequired() required flag}.
* @param alias the {@link #getAlias() property alias}.
* @param mustExist the {@link #isPathRequiredToExist() required to exist flag}.
* @param validator the {@link Consumer} used to {@link #validate() validate} the {@link #getValue() value}.
*/
public RepositoryProperty(String name, boolean required, String alias, boolean mustExist, Consumer<Path> validator) {

super(name, required, alias, mustExist, validator);
}
salimbouch marked this conversation as resolved.
Show resolved Hide resolved

public Path parse(String valueAsString, IdeContext context) {

if (valueAsString == null) {
return null;
}

Path repositoryFile = Path.of(valueAsString);
if (!Files.exists(repositoryFile)) {
Path repositoriesPath = context.getSettingsPath().resolve(IdeContext.FOLDER_REPOSITORIES);
Path legacyRepositoriesPath = context.getSettingsPath().resolve(IdeContext.FOLDER_LEGACY_REPOSITORIES);
String propertiesFileName = valueAsString;
if (!valueAsString.endsWith(".properties")) {
propertiesFileName += ".properties";
}
repositoryFile = context.getFileAccess().findExistingFile(propertiesFileName,
Arrays.asList(repositoriesPath, legacyRepositoriesPath));
}
if (repositoryFile == null) {
throw new IllegalStateException("Could not find properties file: " + valueAsString);
}
return repositoryFile;
}

}
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 @@ -11,6 +11,7 @@ cmd-env=Print the environment variables to set and export.
cmd-get-edition=Get the edition of the selected tool.
cmd-get-version=Get the version of the selected tool.
cmd-gh=Tool commandlet for Github CLI.
cmd-repository=setup the pre-configured git repository.
cmd-gradle=Tool commandlet for Gradle (Build-Tool)
cmd-helm=Tool commandlet for Helm (Kubernetes Package Manager)
cmd-help=Prints this help.
Expand All @@ -37,6 +38,7 @@ val-edition=The tool edition.
val-tool=The tool commandlet to select.
val-version=The tool version
val-set-version-version=The tool version to set.
val-repository-repository=The name of the properties file of the pre-configured git repository to setup, omit to setup all active repositories.
version-banner=Current version of IDE is {}
opt--batch=enable batch mode (non-interactive)
opt--debug=enable debug logging
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 @@ -10,6 +10,7 @@ cmd-env=Gibt die zu setztenden und exportierenden Umgebungsvariablen aus.
cmd-get-edition=Zeigt die Edition des selektierten Werkzeugs an.
cmd-get-version=Zeigt die Version des selektierten Werkzeugs an.
cmd-gh=Werkzeug Kommando für die Github Kommandoschnittstelle.
cmd-repository=Richtet das vorkonfigurierte Git Repository ein.
cmd-helm=Werkzeug Kommando für Helm (Kubernetes Package Manager)
cmd-help=Zeigt diese Hilfe an.
cmd-install=Installiert das selektierte Werkzeug.
Expand All @@ -34,6 +35,7 @@ val-edition=Die Werkzeug Edition.
val-tool=Das zu selektierende Werkzeug Kommando.
val-version=Die Werkzeug Version.
val-set-version-version=Die zu setztende Werkzeug Version.
val-repository-repository=Der Name der Properties-Datei des vorkonfigurierten Git Repositories zum Einrichten. Falls nicht angegeben, werden alle aktiven Projekte eingerichtet.
version-banner=Die aktuelle Version der IDE ist {}
opt--batch=Aktiviert den Batch-Modus (nicht-interaktive Stapelverarbeitung)
opt--debug=Aktiviert Debug-Ausgaben (Fehleranalyse)
Expand Down
Loading