Skip to content

Commit

Permalink
Merge 1c0b034 into 661ea6a
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-vcapgemini authored Apr 15, 2024
2 parents 661ea6a + 1c0b034 commit 37d0acd
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 32 deletions.
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);
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.
*/
public 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);
}
}

}
59 changes: 59 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,59 @@
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) {

if (Files.exists(update)) {
copy(update, workspace);
} else if (Files.exists(setup) && !Files.exists(workspace)) {
copy(setup, workspace);
}

StringBuilder inputBuffer = new StringBuilder();
try (BufferedReader reader = Files.newBufferedReader(update)) {
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) {

}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.devonfw.tools.ide.merge;

import java.nio.file.Path;
import java.util.Map.Entry;
import java.util.Properties;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map.Entry;
import java.util.Properties;

/**
* Test of {@link DirectoryMerger}.
*/
public class DirectoryMergerTest extends AbstractIdeContextTest {

private static final String IDE_HOME = TEST_PROJECTS.resolve(PROJECT_BASIC).resolve("project").toAbsolutePath()
.toString().replace('\\', '/');
private static final String IDE_HOME = TEST_PROJECTS.resolve(PROJECT_BASIC).resolve("project").toAbsolutePath().toString().replace('\\', '/');

private static final Prop JAVA_VERSION = new Prop("java.version", "1.11");

Expand Down Expand Up @@ -54,6 +53,9 @@ public void testConfigurator(@TempDir Path workspaceDir) throws Exception {
Path templates = Path.of("src/test/resources/templates");
Path setup = templates.resolve(IdeContext.FOLDER_SETUP);
Path update = templates.resolve(IdeContext.FOLDER_UPDATE);
Path namePath = workspaceDir.resolve(".name");
// to check overwrite for Text files
Files.createFile(namePath);
merger.merge(setup, update, context.getVariables(), workspaceDir);

// assert
Expand Down Expand Up @@ -114,6 +116,8 @@ public void testConfigurator(@TempDir Path workspaceDir) throws Exception {
// assert
mainPrefs = PropertiesMerger.load(mainPrefsFile);
assertThat(mainPrefs).containsOnly(JAVA_VERSION, JAVA_HOME, THEME_HACKED, UI_HACKED, EDITOR, INDENTATION_HACKED);

assertThat(namePath).hasContent(IDE_HOME + " - main\ntest");
}

private static class Prop implements Entry<String, String> {
Expand Down
2 changes: 2 additions & 0 deletions cli/src/test/resources/templates/setup/.name
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
${IDE_HOME} - main
test
2 changes: 2 additions & 0 deletions cli/src/test/resources/templates/update/.name
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
${IDE_HOME} - main
test

0 comments on commit 37d0acd

Please sign in to comment.