From b31be87de369aa5ac51cfbe4b9291ccca14afb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Thu, 19 Jul 2018 17:32:52 +0200 Subject: [PATCH] [sarlc] Path detector is now injectable. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- .../lang/sarlc/commands/CompilerCommand.java | 163 ++----------- .../sarlc/commands/DefaultPathDetector.java | 218 ++++++++++++++++++ .../lang/sarlc/commands/PathDetector.java | 88 +++++++ .../lang/sarlc/modules/CompilerModule.java | 6 +- 4 files changed, 324 insertions(+), 151 deletions(-) create mode 100644 products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/DefaultPathDetector.java create mode 100644 products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/PathDetector.java diff --git a/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/CompilerCommand.java b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/CompilerCommand.java index 1e603f1c97..20f9cc6b4b 100644 --- a/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/CompilerCommand.java +++ b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/CompilerCommand.java @@ -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; @@ -54,17 +46,22 @@ public class CompilerCommand extends CommandWithMetadata { private final Provider configuration; + private final Provider pathDetector; + /** Constructor. * * @param compiler the SARL batch compiler. * @param configuration the configuration of the tool. + * @param pathDetector the detector of path. */ - public CompilerCommand(Provider compiler, Provider configuration) { + public CompilerCommand(Provider compiler, Provider configuration, + Provider pathDetector) { super(CommandMetadata .builder(CompilerCommand.class) .description(Messages.CompilerCommand_0)); this.compiler = compiler; this.configuration = configuration; + this.pathDetector = pathDetector; } @Override @@ -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 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); @@ -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 files) { - LinkedList longuestPrefix = null; - - for (final File file : files) { - if (file == null) { - continue; - } - final LinkedList 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 splitFile(File file) { - final LinkedList elements = new LinkedList<>(); - File current = file; - do { - elements.addFirst(current.getName()); - current = current.getParentFile(); - } while (current != null); - return elements; - } - } diff --git a/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/DefaultPathDetector.java b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/DefaultPathDetector.java new file mode 100644 index 0000000000..7b88cd9f77 --- /dev/null +++ b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/DefaultPathDetector.java @@ -0,0 +1,218 @@ +/* + * $Id$ + * + * SARL is an general-purpose agent programming language. + * More details on http://www.sarl.io + * + * Copyright (C) 2014-2018 the original authors or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sarl.lang.sarlc.commands; + +import java.io.File; +import java.nio.file.Path; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import com.google.common.collect.Iterables; +import org.eclipse.xtext.util.Strings; + +import io.sarl.lang.SARLConfig; + +/** + * Default implementation of a path detector. + * + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + * @since 0.8 + */ +public class DefaultPathDetector implements PathDetector { + + private File sarlOutputPath; + + private File classOutputPath; + + private File workingPath; + + @Override + public void setSarlOutputPath(File path) { + this.sarlOutputPath = path; + } + + @Override + public void setClassOutputPath(File path) { + this.classOutputPath = path; + } + + @Override + public void setWorkingPath(File path) { + this.workingPath = path; + } + + @Override + public File getSarlOutputPath() { + return this.sarlOutputPath; + } + + @Override + public File getClassOutputPath() { + return this.classOutputPath; + } + + @Override + public File getWorkingPath() { + return this.workingPath; + } + + @Override + public void resolve(List args) { + if (this.sarlOutputPath == null || this.workingPath == null || this.classOutputPath == null) { + final Iterable cliFiles = Iterables.transform( + args, + it -> toFile(it)); + File root = determineCommonRoot(Iterables.concat( + cliFiles, + Collections.singleton(this.sarlOutputPath), + Collections.singleton(this.workingPath), + Collections.singleton(this.classOutputPath))); + if (root != null) { + root = normalize(root); + if (this.sarlOutputPath == null) { + this.sarlOutputPath = toFile(root, SARLConfig.FOLDER_SOURCE_GENERATED); + } + if (this.workingPath == null) { + this.workingPath = toFile(root, SARLConfig.FOLDER_TMP); + } + if (this.classOutputPath == null) { + this.classOutputPath = toFile(root, SARLConfig.FOLDER_BIN); + } + } + } + + if (this.sarlOutputPath == null) { + this.sarlOutputPath = toFile(cwd(), SARLConfig.FOLDER_SOURCE_GENERATED); + } + if (this.workingPath == null) { + this.workingPath = toFile(cwd(), SARLConfig.FOLDER_TMP); + } + if (this.classOutputPath == null) { + this.classOutputPath = toFile(cwd(), SARLConfig.FOLDER_BIN); + } + } + + 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 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; + } + + private static File cwd() { + return new File("").getAbsoluteFile(); //$NON-NLS-1$ + } + + @SuppressWarnings("checkstyle:npathcomplexity") + private static File determineCommonRoot(Iterable files) { + LinkedList longuestPrefix = null; + + for (final File file : files) { + if (file == null) { + continue; + } + final LinkedList 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 splitFile(File file) { + final LinkedList elements = new LinkedList<>(); + File current = file; + do { + elements.addFirst(current.getName()); + current = current.getParentFile(); + } while (current != null); + return elements; + } + +} diff --git a/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/PathDetector.java b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/PathDetector.java new file mode 100644 index 0000000000..f2eaa81037 --- /dev/null +++ b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/PathDetector.java @@ -0,0 +1,88 @@ +/* + * $Id$ + * + * SARL is an general-purpose agent programming language. + * More details on http://www.sarl.io + * + * Copyright (C) 2014-2018 the original authors or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sarl.lang.sarlc.commands; + +import java.io.File; +import java.util.List; + +import com.google.inject.ImplementedBy; + +/** + * Detect the three base paths when they are missed. + * The paths are:
    + *
  • the SARL output path,
  • + *
  • the Java class output path, and
  • + *
  • the working path.
  • + *
+ * + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + * @since 0.8 + */ +@ImplementedBy(DefaultPathDetector.class) +public interface PathDetector { + + /** Change the SARL output path. + * + * @param path the path. + */ + void setSarlOutputPath(File path); + + /** Change the Java class output path. + * + * @param path the path. + */ + void setClassOutputPath(File path); + + /** Change the working path. + * + * @param path the path. + */ + void setWorkingPath(File path); + + /** Replies the SARL output path. + * + * @return the path. + */ + File getSarlOutputPath(); + + /** Replies the Java class output path. + * + * @return the path. + */ + File getClassOutputPath(); + + /** Replies the working path. + * + * @return the path. + */ + File getWorkingPath(); + + /** Resolve the paths. + * + * @param args the command line arguments to consider. + */ + void resolve(List args); + +} diff --git a/products/sarlc/src/main/java/io/sarl/lang/sarlc/modules/CompilerModule.java b/products/sarlc/src/main/java/io/sarl/lang/sarlc/modules/CompilerModule.java index 190f33564f..b8ddf31171 100644 --- a/products/sarlc/src/main/java/io/sarl/lang/sarlc/modules/CompilerModule.java +++ b/products/sarlc/src/main/java/io/sarl/lang/sarlc/modules/CompilerModule.java @@ -53,6 +53,7 @@ import io.sarl.lang.compiler.GeneratorConfig2; import io.sarl.lang.compiler.batch.SarlBatchCompiler; import io.sarl.lang.sarlc.commands.CompilerCommand; +import io.sarl.lang.sarlc.commands.PathDetector; import io.sarl.lang.sarlc.configs.CompilerConfig; import io.sarl.lang.sarlc.configs.LoggingConfig; import io.sarl.lang.sarlc.configs.SarlcConfig; @@ -190,14 +191,15 @@ public CompilerConfig provideCompilerConfig(ConfigurationFactory configFactory, * * @param compiler the compiler. * @param configuration the SARLC configuration. + * @param pathDetector the detector of paths. * @return the command. */ @SuppressWarnings("static-method") @Provides @Singleton public CompilerCommand provideSarlcCompilerCommand(Provider compiler, - Provider configuration) { - return new CompilerCommand(compiler, configuration); + Provider configuration, Provider pathDetector) { + return new CompilerCommand(compiler, configuration, pathDetector); } /** Replies the SARL batch compiler.