Skip to content

Commit

Permalink
[sarlc] Path detector is now injectable.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jul 19, 2018
1 parent 7e50053 commit b31be87
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 151 deletions.
Expand Up @@ -21,20 +21,12 @@

package io.sarl.lang.sarlc.commands;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedList;

import com.google.common.collect.Iterables;
import com.google.inject.Provider;
import io.bootique.cli.Cli;
import io.bootique.command.CommandOutcome;
import io.bootique.command.CommandWithMetadata;
import io.bootique.meta.application.CommandMetadata;
import org.eclipse.xtext.util.Strings;

import io.sarl.lang.SARLConfig;
import io.sarl.lang.compiler.batch.SarlBatchCompiler;
import io.sarl.lang.sarlc.configs.SarlcConfig;
import io.sarl.maven.bqextension.Constants;
Expand All @@ -54,17 +46,22 @@ public class CompilerCommand extends CommandWithMetadata {

private final Provider<SarlcConfig> configuration;

private final Provider<PathDetector> pathDetector;

/** Constructor.
*
* @param compiler the SARL batch compiler.
* @param configuration the configuration of the tool.
* @param pathDetector the detector of path.
*/
public CompilerCommand(Provider<SarlBatchCompiler> compiler, Provider<SarlcConfig> configuration) {
public CompilerCommand(Provider<SarlBatchCompiler> compiler, Provider<SarlcConfig> configuration,
Provider<PathDetector> pathDetector) {
super(CommandMetadata
.builder(CompilerCommand.class)
.description(Messages.CompilerCommand_0));
this.compiler = compiler;
this.configuration = configuration;
this.pathDetector = pathDetector;
}

@Override
Expand All @@ -75,49 +72,17 @@ public CommandOutcome run(Cli cli) {
}

final SarlcConfig config = this.configuration.get();

File outputPath = config.getOutputPath();
File workingPath = config.getWorkingPath();
File classOutputPath = config.getClassOutputPath();

if (outputPath == null || workingPath == null || classOutputPath == null) {
final Iterable<File> cliFiles = Iterables.transform(
cli.standaloneArguments(),
it -> toFile(it));
File root = determineCommonRoot(Iterables.concat(
cliFiles,
Collections.singleton(outputPath),
Collections.singleton(workingPath),
Collections.singleton(classOutputPath)));
if (root != null) {
root = normalize(root);
if (outputPath == null) {
outputPath = toFile(root, SARLConfig.FOLDER_SOURCE_GENERATED);
}
if (workingPath == null) {
workingPath = toFile(root, SARLConfig.FOLDER_TMP);
}
if (classOutputPath == null) {
classOutputPath = toFile(root, SARLConfig.FOLDER_BIN);
}
}
}

if (outputPath == null) {
outputPath = toFile(cwd(), SARLConfig.FOLDER_SOURCE_GENERATED);
}
if (workingPath == null) {
workingPath = toFile(cwd(), SARLConfig.FOLDER_TMP);
}
if (classOutputPath == null) {
classOutputPath = toFile(cwd(), SARLConfig.FOLDER_BIN);
}
final PathDetector paths = this.pathDetector.get();
paths.setSarlOutputPath(config.getOutputPath());
paths.setClassOutputPath(config.getClassOutputPath());
paths.setWorkingPath(config.getWorkingPath());
paths.resolve(cli.standaloneArguments());

final SarlBatchCompiler comp = this.compiler.get();

comp.setOutputPath(outputPath);
comp.setClassOutputPath(classOutputPath);
comp.setTempDirectory(workingPath);
comp.setOutputPath(paths.getSarlOutputPath());
comp.setClassOutputPath(paths.getClassOutputPath());
comp.setTempDirectory(paths.getWorkingPath());

for (final String cliArg : cli.standaloneArguments()) {
comp.addSourcePath(cliArg);
Expand All @@ -129,104 +94,4 @@ public CommandOutcome run(Cli cli) {
return CommandOutcome.succeeded();
}

private static File normalize(File filename) {
final Path path1 = toFile(SARLConfig.FOLDER_SOURCE_SARL).toPath();
final Path path2 = toFile(SARLConfig.FOLDER_SOURCE_JAVA).toPath();
final Path path3 = toFile(SARLConfig.FOLDER_TEST_SOURCE_SARL).toPath();
final Path path = filename.toPath();
Path toRemove = null;
if (path.endsWith(path1)) {
toRemove = path1;
} else if (path.endsWith(path2)) {
toRemove = path2;
} else if (path.endsWith(path3)) {
toRemove = path3;
}
if (toRemove != null) {
final int nb = toRemove.getNameCount();
File res = filename;
for (int i = 0; i < nb; ++i) {
res = res.getParentFile();
}
return res;
}
return filename;
}

private static File cwd() {
return new File("").getAbsoluteFile(); //$NON-NLS-1$
}

private static File toFile(String filename) {
File result = null;
for (final String element : filename.split("\\/")) { //$NON-NLS-1$
if (result == null) {
result = new File(element);
} else {
result = new File(result, element);
}
}
return result;
}

private static File toFile(File root, String filename) {
File result = root;
for (final String element : filename.split("\\/")) { //$NON-NLS-1$
result = new File(result, element);
}
return result;
}

@SuppressWarnings("checkstyle:npathcomplexity")
private static File determineCommonRoot(Iterable<File> files) {
LinkedList<String> longuestPrefix = null;

for (final File file : files) {
if (file == null) {
continue;
}
final LinkedList<String> components = splitFile(file);
if (longuestPrefix == null) {
longuestPrefix = components;
} else {
int i = 0;
while (i < longuestPrefix.size() && i < components.size()
&& Strings.equal(longuestPrefix.get(i), components.get(i))) {
++i;
}
while (i < longuestPrefix.size()) {
longuestPrefix.removeLast();
}
if (longuestPrefix.isEmpty()) {
return null;
}
}
}

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

File prefix = null;
for (final String component : longuestPrefix) {
if (prefix == null) {
prefix = new File(component);
} else {
prefix = new File(prefix, component);
}
}

return prefix;
}

private static LinkedList<String> splitFile(File file) {
final LinkedList<String> elements = new LinkedList<>();
File current = file;
do {
elements.addFirst(current.getName());
current = current.getParentFile();
} while (current != null);
return elements;
}

}

0 comments on commit b31be87

Please sign in to comment.