-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
810c616
commit e122f21
Showing
16 changed files
with
473 additions
and
52 deletions.
There are no files selected for viewing
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
126 changes: 126 additions & 0 deletions
126
cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryCommandlet.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,126 @@ | ||
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"); | ||
this.repository = add(new RepositoryProperty("", false, "repository")); | ||
} | ||
|
||
@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().listChildren(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.getFileName().toString()); | ||
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); | ||
|
||
Path repositoryPath = workspacePath.resolve(repository); | ||
this.context.getGitContext().pullOrClone(gitUrl, repositoryConfig.gitBranch(), repositoryPath); | ||
|
||
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."); | ||
} | ||
|
||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryConfig.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,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(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.