Skip to content

Commit

Permalink
Added Windows end line support in migration tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Jan 25, 2024
1 parent 49202e6 commit bd6082a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Obeo.
* Copyright (c) 2017, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -87,14 +87,15 @@ public ModuleMigrator(IModuleResolver moduleResolver, Path targetFolderPath) {
* the emtl file
* @param originMTLFile
* optional, allow to retrieve any comments above the module declaration
* @param newLine
* the new line {@link String}
* @return the AST module
* @throws IOException
*/
public Module migrate(File emtlFile, File originMTLFile) throws IOException {
public Module migrate(File emtlFile, File originMTLFile, String newLine) throws IOException {
org.eclipse.acceleo.model.mtl.Module legacyModule = (org.eclipse.acceleo.model.mtl.Module)ModelUtils
.load(emtlFile, resourceSet);

ModuleConverter moduleConverter = new ModuleConverter(moduleResolver, targetFolderPath);
ModuleConverter moduleConverter = new ModuleConverter(moduleResolver, targetFolderPath, newLine);
Module convertedModule = (Module)moduleConverter.convert(legacyModule);
if (originMTLFile != null) {
parseModuleDocumentation(convertedModule, originMTLFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ public String getImplicitSelfName() {

}

/**
* The new line string.
*/
private static final String NEW_LINE = "\n";

/**
* A converter dedicated to expressions.
*/
Expand All @@ -156,6 +151,11 @@ public String getImplicitSelfName() {
*/
private final Path targetFolderPath;

/**
* The new line {@link String}.
*/
private final String newLine;

/**
* The index of the current for implicit iterator.
*/
Expand All @@ -173,11 +173,14 @@ public String getImplicitSelfName() {
* the module resolver
* @param targetFolderPath
* the target folder {@link Path}
* @param newLine
* the new line {@link String}
*/
public ModuleConverter(IModuleResolver moduleResolver, Path targetFolderPath) {
public ModuleConverter(IModuleResolver moduleResolver, Path targetFolderPath, String newLine) {
this.moduleResolver = moduleResolver;
this.targetFolderPath = targetFolderPath;
expressionConverter = new ExpressionConverter(targetFolderPath);
this.newLine = newLine;
}

/**
Expand Down Expand Up @@ -380,7 +383,7 @@ private org.eclipse.acceleo.Expression extractPrefix(final TextStatement textSta
final org.eclipse.acceleo.Expression res;

final String text = textStatement.getValue();
int index = Math.max(text.lastIndexOf(NEW_LINE), 0);
int index = Math.max(text.lastIndexOf(newLine), 0);
while (index < text.length() && Character.isWhitespace(text.charAt(index))) {
index++;
}
Expand Down Expand Up @@ -826,24 +829,25 @@ private Object caseText(StringLiteralExp input) {
final List<TextStatement> outputs = new ArrayList<TextStatement>();

final String text = input.getStringSymbol().replace("[", "['['/]");
final int textLength = text.length();
int startOfText = 0;
int endOfText;
do {
endOfText = text.indexOf(NEW_LINE, startOfText);
endOfText = AcceleoParser.nextNewLineIndex(text, textLength, startOfText);
if (endOfText < 0) {
endOfText = text.length();
final TextStatement output = AcceleoFactory.eINSTANCE.createTextStatement();
output.setValue(text.substring(startOfText, endOfText));
output.setValue(text.substring(startOfText, endOfText).replaceAll("(\\r\\n)|\\n", newLine));
output.setNewLineNeeded(false);
outputs.add(output);
} else {
final TextStatement output = AcceleoFactory.eINSTANCE.createTextStatement();
output.setValue(text.substring(startOfText, endOfText));
output.setValue(text.substring(startOfText, endOfText).replaceAll("(\\r\\n)|\\n", newLine));
output.setNewLineNeeded(true);
outputs.add(output);
}
startOfText = endOfText + NEW_LINE.length();
} while (endOfText < text.length() && startOfText < text.length());
startOfText = endOfText + AcceleoParser.newLineAt(text, textLength, endOfText);
} while (endOfText < textLength && startOfText < textLength);
return outputs;
}

Expand Down Expand Up @@ -875,10 +879,10 @@ private Object caseDocumentation(org.eclipse.acceleo.model.mtl.Documentation inp
Documentation output = AcceleoFactory.eINSTANCE.createModuleElementDocumentation();
CommentBody body = AcceleoFactory.eINSTANCE.createCommentBody();
StringBuilder formatValue = new StringBuilder();
formatValue.append(NEW_LINE);
for (String line : input.getBody().getValue().split(NEW_LINE)) {
formatValue.append(newLine);
for (String line : input.getBody().getValue().split("(\\r\\n)|\\n")) {
if (!line.trim().isEmpty()) {
formatValue.append(" * " + line + NEW_LINE);
formatValue.append(" * " + line + newLine);
}
}
formatValue.append("*");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2020 Obeo.
* Copyright (c) 2016, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -30,11 +30,6 @@
*/
public class DocumentationParser {

/**
* New line.
*/
public static final String NEW_LINE = "\n";

/**
* In line end delimiter.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,13 @@ public void migrate(Path mtlFile) throws IOException {
EMTL_FILE_EXTENSION)).toFile();
if (emtlFile.exists()) {
try {
final String newLine = System.lineSeparator();
// migrate AST
org.eclipse.acceleo.Module module = new ModuleMigrator(new StandaloneModuleResolver(
binFolderPath), targetFolderPath).migrate(emtlFile, mtlFile.toFile());
binFolderPath), targetFolderPath).migrate(emtlFile, mtlFile.toFile(), newLine);

// serialize
String a4Content = new AcceleoAstSerializer(System.lineSeparator()).serialize(module);
String a4Content = new AcceleoAstSerializer(newLine).serialize(module);

// write result
Path targetMtlFile = targetFolderPath.resolve(relativePath).getParent().resolve(module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ public class AcceleoParser {
*/
public static final String MODULE_FILE_EXTENSION = "mtl";

/**
* New line.
*/
public static final String NEW_LINE = "\n";

/**
* New line.
*/
public static final String WINDOWS_NEW_LINE = "\r\n";

/**
* In line end delimiter.
*/
Expand Down Expand Up @@ -526,7 +516,7 @@ private void computeLinesAndColumns(String str, int textLength) {
* @return the length of the new line if the given text has a new line at the given index, <code>0</code>
* otherwise
*/
private int newLineAt(String text, int textLength, int index) {
public static int newLineAt(String text, int textLength, int index) {
final int res;

if (index < textLength) {
Expand Down Expand Up @@ -555,7 +545,7 @@ private int newLineAt(String text, int textLength, int index) {
* the start position
* @return the next new line index in the given text starting at the given position
*/
private int nextNewLineIndex(String text, int textLength, int start) {
public static int nextNewLineIndex(String text, int textLength, int start) {
int res = -1;

for (int i = start; i < textLength; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ public String getQualifiedName(org.eclipse.acceleo.model.mtl.Module module,
*/
public AbstractMigrationTestSuite(String testFolderPath) throws IOException {
testFolder = new File(testFolderPath);
module = MODULE_MIGRATOR.migrate(getFile(".emtl"), getFile("-origin.mtl"));
final String newLine = "\n";
module = MODULE_MIGRATOR.migrate(getFile(".emtl"), getFile("-origin.mtl"), newLine);
try {
moduleContent = new AcceleoAstSerializer("\n").serialize(module);
// TODO fix eol issues
moduleContent = moduleContent.replaceAll("\r\n", "\n");
moduleContent = new AcceleoAstSerializer(newLine).serialize(module);
} catch (Exception e) {
System.err.println("Serialization issue:");
e.printStackTrace();
Expand Down

0 comments on commit bd6082a

Please sign in to comment.