Skip to content

Commit

Permalink
#154 Show project name in header of intellij (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-vcapgemini authored Apr 19, 2024
1 parent 118d9be commit eb9f9d0
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package com.devonfw.tools.ide.context;

import java.io.IOException;
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import com.devonfw.tools.ide.cli.CliAbortException;
import com.devonfw.tools.ide.cli.CliArgument;
import com.devonfw.tools.ide.cli.CliArguments;
Expand Down Expand Up @@ -48,6 +36,18 @@
import com.devonfw.tools.ide.step.StepImpl;
import com.devonfw.tools.ide.url.model.UrlMetadata;

import java.io.IOException;
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
* Abstract base implementation of {@link IdeContext}.
*/
Expand Down Expand Up @@ -131,11 +131,10 @@ public abstract class AbstractIdeContext implements IdeContext {
* @param minLogLevel the minimum {@link IdeLogLevel} to enable. Should be {@link IdeLogLevel#INFO} by default.
* @param factory the {@link Function} to create {@link IdeSubLogger} per {@link IdeLogLevel}.
* @param userDir the optional {@link Path} to current working directory.
* @param toolRepository @param toolRepository the {@link ToolRepository} of the context. If it is set to {@code null}
* {@link DefaultToolRepository} will be used.
* @param toolRepository @param toolRepository the {@link ToolRepository} of the context. If it is set to {@code null} {@link DefaultToolRepository} will be
* used.
*/
public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubLogger> factory, Path userDir,
ToolRepository toolRepository) {
public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubLogger> factory, Path userDir, ToolRepository toolRepository) {

super();
this.loggerFactory = factory;
Expand Down Expand Up @@ -186,8 +185,8 @@ public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubL
if (ideRootPath == null) {
ideRootPath = rootPath;
} else if (!ideRootPath.equals(rootPath)) {
warning("Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.",
rootPath, this.ideHome.getFileName(), ideRootPath);
warning("Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.", rootPath, this.ideHome.getFileName(),
ideRootPath);
}
}
}
Expand Down Expand Up @@ -272,8 +271,7 @@ private String getMessageIdeHomeNotFound() {
}

/**
* @return the status message about the {@link #getIdeHome() IDE_HOME} detection and environment variable
* initialization.
* @return the status message about the {@link #getIdeHome() IDE_HOME} detection and environment variable initialization.
*/
public String getMessageIdeHome() {

Expand Down Expand Up @@ -335,14 +333,12 @@ private EnvironmentVariables createVariables() {
AbstractEnvironmentVariables user = extendVariables(system, this.userHomeIde, EnvironmentVariablesType.USER);
AbstractEnvironmentVariables settings = extendVariables(user, this.settingsPath, EnvironmentVariablesType.SETTINGS);
// TODO should we keep this workspace properties? Was this feature ever used?
AbstractEnvironmentVariables workspace = extendVariables(settings, this.workspacePath,
EnvironmentVariablesType.WORKSPACE);
AbstractEnvironmentVariables workspace = extendVariables(settings, this.workspacePath, EnvironmentVariablesType.WORKSPACE);
AbstractEnvironmentVariables conf = extendVariables(workspace, this.confPath, EnvironmentVariablesType.CONF);
return conf.resolved();
}

private AbstractEnvironmentVariables extendVariables(AbstractEnvironmentVariables envVariables, Path propertiesPath,
EnvironmentVariablesType type) {
private AbstractEnvironmentVariables extendVariables(AbstractEnvironmentVariables envVariables, Path propertiesPath, EnvironmentVariablesType type) {

Path propertiesFile = null;
if (propertiesPath == null) {
Expand Down Expand Up @@ -398,6 +394,15 @@ public Path getIdeHome() {
return this.ideHome;
}

@Override
public String getProjectName() {

if (this.ideHome != null) {
return this.ideHome.getFileName().toString();
}
return "";
}

@Override
public Path getIdeRoot() {

Expand Down Expand Up @@ -611,8 +616,7 @@ public DirectoryMerger getWorkspaceMerger() {
}

/**
* @return the {@link #getDefaultExecutionDirectory() default execution directory} in which a command process is
* executed.
* @return the {@link #getDefaultExecutionDirectory() default execution directory} in which a command process is executed.
*/
public Path getDefaultExecutionDirectory() {

Expand Down Expand Up @@ -773,8 +777,7 @@ public void endStep(StepImpl step) {
}

/**
* Finds the matching {@link Commandlet} to run, applies {@link CliArguments} to its {@link Commandlet#getProperties()
* properties} and will execute it.
* Finds the matching {@link Commandlet} to run, applies {@link CliArguments} to its {@link Commandlet#getProperties() properties} and will execute it.
*
* @param arguments the {@link CliArgument}.
* @return the return code of the execution.
Expand Down Expand Up @@ -823,11 +826,10 @@ public int run(CliArguments arguments) {
}

/**
* @param cmd the potential {@link Commandlet} to
* {@link #apply(CliArguments, Commandlet, CompletionCandidateCollector) apply} and {@link Commandlet#run()
* run}.
* @return {@code true} if the given {@link Commandlet} matched and did {@link Commandlet#run() run} successfully,
* {@code false} otherwise (the {@link Commandlet} did not match and we have to try a different candidate).
* @param cmd the potential {@link Commandlet} to {@link #apply(CliArguments, Commandlet, CompletionCandidateCollector) apply} and
* {@link Commandlet#run() run}.
* @return {@code true} if the given {@link Commandlet} matched and did {@link Commandlet#run() run} successfully, {@code false} otherwise (the
* {@link Commandlet} did not match and we have to try a different candidate).
*/
private boolean applyAndRun(CliArguments arguments, Commandlet cmd) {

Expand Down Expand Up @@ -887,13 +889,12 @@ public List<CompletionCandidate> complete(CliArguments arguments, boolean includ
}

/**
* @param arguments the {@link CliArguments} to apply. Will be {@link CliArguments#next() consumed} as they are
* matched. Consider passing a {@link CliArguments#copy() copy} as needed.
* @param arguments the {@link CliArguments} to apply. Will be {@link CliArguments#next() consumed} as they are matched. Consider passing a
* {@link CliArguments#copy() copy} as needed.
* @param cmd the potential {@link Commandlet} to match.
* @param collector the {@link CompletionCandidateCollector}.
* @return {@code true} if the given {@link Commandlet} matches to the given {@link CliArgument}(s) and those have
* been applied (set in the {@link Commandlet} and {@link Commandlet#validate() validated}), {@code false}
* otherwise (the {@link Commandlet} did not match and we have to try a different candidate).
* @return {@code true} if the given {@link Commandlet} matches to the given {@link CliArgument}(s) and those have been applied (set in the {@link Commandlet}
* and {@link Commandlet#validate() validated}), {@code false} otherwise (the {@link Commandlet} did not match and we have to try a different candidate).
*/
public boolean apply(CliArguments arguments, Commandlet cmd, CompletionCandidateCollector collector) {

Expand Down
22 changes: 13 additions & 9 deletions cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ default void requireOnline(String purpose) {
*/
Path getIdeHome();

/**
* @return the name of the current project.
* @see com.devonfw.tools.ide.variable.IdeVariables#PROJECT_NAME
*/
String getProjectName();

/**
* @return the {@link Path} to the IDE installation root directory. This is the top-level folder where the {@link #getIdeHome() IDE instances} are located as
* sub-folder. There is a reserved ".ide" folder where central IDE data is stored such as the {@link #getUrlsPath() download metadata} and the central
Expand Down Expand Up @@ -411,9 +417,9 @@ default String getMavenArgs() {
}

/**
* Updates the current working directory (CWD) and configures the environment paths according to the specified parameters.
* This method is central to changing the IDE's notion of where it operates, affecting where configurations, workspaces,
* settings, and other resources are located or loaded from.
* Updates the current working directory (CWD) and configures the environment paths according to the specified parameters. This method is central to changing
* the IDE's notion of where it operates, affecting where configurations, workspaces, settings, and other resources are located or loaded from.
*
* @return the current {@link Step} of processing.
*/
Step getCurrentStep();
Expand Down Expand Up @@ -446,9 +452,8 @@ default Step newStep(String name, Object... parameters) {
Step newStep(boolean silent, String name, Object... parameters);

/**
* Updates the current working directory (CWD) and configures the environment paths according to the specified
* parameters. This method is central to changing the IDE's notion of where it operates, affecting where
* configurations, workspaces, settings, and other resources are located or loaded from.
* Updates the current working directory (CWD) and configures the environment paths according to the specified parameters. This method is central to changing
* the IDE's notion of where it operates, affecting where configurations, workspaces, settings, and other resources are located or loaded from.
*
* @param ideHome The path to the IDE home directory.
*/
Expand All @@ -458,9 +463,8 @@ default void setIdeHome(Path ideHome) {
}

/**
* Updates the current working directory (CWD) and configures the environment paths according to the specified
* parameters. This method is central to changing the IDE's notion of where it operates, affecting where
* configurations, workspaces, settings, and other resources are located or loaded from.
* Updates the current working directory (CWD) and configures the environment paths according to the specified parameters. This method is central to changing
* the IDE's notion of where it operates, affecting where configurations, workspaces, settings, and other resources are located or loaded from.
*
* @param userDir The path to set as the current working directory.
* @param workspace The name of the workspace within the IDE's environment.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.devonfw.tools.ide.merge;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.util.FilenameUtil;
import org.jline.utils.Log;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -9,12 +14,6 @@
import java.util.Map;
import java.util.Set;

import org.jline.utils.Log;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.util.FilenameUtil;

/**
* Implementation of {@link WorkspaceMerger} that does the whole thing:
* <ul>
Expand Down Expand Up @@ -47,6 +46,8 @@ public DirectoryMerger(IdeContext context) {
this.extension2mergerMap.put("launch", xmlMerger); // Eclipse specific
JsonMerger jsonMerger = new JsonMerger(context);
this.extension2mergerMap.put("json", jsonMerger);
TextMerger textMerger = new TextMerger(context);
this.extension2mergerMap.put("name", textMerger); // intellij specific
this.fallbackMerger = new FallbackMerger(context);
}

Expand Down
23 changes: 5 additions & 18 deletions cli/src/main/java/com/devonfw/tools/ide/merge/FallbackMerger.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.devonfw.tools.ide.merge;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

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

import java.nio.file.Files;
import java.nio.file.Path;

/**
* Implementation of {@link FileMerger} to use as fallback. It can not actually merge but will simply overwrite the
* files.
* Implementation of {@link FileMerger} to use as fallback. It can not actually merge but will simply overwrite the files.
*
* @since 3.0.0
*/
Expand All @@ -36,20 +34,9 @@ public void merge(Path setup, Path update, EnvironmentVariables variables, Path
}

@Override
public void inverseMerge(Path workspaceFile, EnvironmentVariables resolver, boolean addNewProperties,
Path updateFile) {
public void inverseMerge(Path workspaceFile, EnvironmentVariables resolver, boolean addNewProperties, Path updateFile) {

// nothing by default, we could copy the workspace file back to the update file if it exists...
}

private void copy(Path sourceFile, Path targetFile) {

ensureParentDirectoryExists(targetFile);
try {
Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new IllegalStateException("Failed to copy file " + sourceFile + " to " + targetFile, e);
}
}

}
18 changes: 18 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/merge/FileMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

/**
* {@link WorkspaceMerger} responsible for a single type of file.
*/
Expand All @@ -17,4 +21,18 @@ public FileMerger(IdeContext context) {
super(context);
}

/**
* @param sourceFile Path to source file.
* @param targetFile Path to target file.
*/
protected void copy(Path sourceFile, Path targetFile) {

ensureParentDirectoryExists(targetFile);
try {
Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new IllegalStateException("Failed to copy file " + sourceFile + " to " + targetFile, e);
}
}

}
61 changes: 61 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/merge/TextMerger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.devonfw.tools.ide.merge;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Implementation of {@link FileMerger} for Text files.
*/
public class TextMerger extends FileMerger {

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

super(context);
}

@Override
public void merge(Path setup, Path update, EnvironmentVariables variables, Path workspace) {

Path template = update;
if (!Files.exists(template)) {
template = setup;
assert Files.exists(template);
if (Files.exists(workspace)) {
return; // setup is only applied for initial setup if workspace file does not yet exist
}
}
StringBuilder inputBuffer = new StringBuilder();
try (BufferedReader reader = Files.newBufferedReader(template)) {
String line;
String resolvedValue;
while ((line = reader.readLine()) != null) {
resolvedValue = variables.resolve(line, workspace.getFileName());
inputBuffer.append(resolvedValue);
inputBuffer.append('\n');
}
} catch (IOException e) {
throw new IllegalStateException("Could not read text file: " + workspace, e);
}
try {
Files.write(workspace, inputBuffer.toString().getBytes());
} catch (IOException e) {
throw new IllegalStateException("Could not write to text file: " + workspace, e);
}

}

@Override
public void inverseMerge(Path workspace, EnvironmentVariables variables, boolean addNewProperties, Path update) {

}
}
Loading

0 comments on commit eb9f9d0

Please sign in to comment.