Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for passing of command line options from lfc to the generator #1631

Merged
merged 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions org.lflang/src/org/lflang/cli/CliBase.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package org.lflang.cli;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.Spec;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
Expand All @@ -29,8 +22,6 @@
import org.lflang.ErrorReporter;
import org.lflang.LFRuntimeModule;
import org.lflang.LFStandaloneSetup;
import org.lflang.LocalStrings;
import org.lflang.generator.LFGeneratorContext.BuildParm;
import org.lflang.util.FileUtil;
import com.google.inject.Inject;
import com.google.inject.Injector;
Expand All @@ -45,11 +36,6 @@
* @author Atharva Patil
*/
public abstract class CliBase implements Runnable {
/**
* Models a command specification, including the options, positional
* parameters and subcommands supported by the command.
*/
@Spec CommandSpec spec;

/**
* Options and parameters present in both Lfc and Lff.
Expand All @@ -65,8 +51,8 @@ public abstract class CliBase implements Runnable {
defaultValue = "",
fallbackValue = "",
description = "Specify the root output directory.")

private Path outputPath;

/**
* Used to collect all errors that happen during validation/generation.
*/
Expand Down Expand Up @@ -103,13 +89,6 @@ public abstract class CliBase implements Runnable {
@Inject
private IResourceValidator validator;

/** Name of the program, eg "lfc". */
private final String toolName;

protected CliBase(String toolName) {
this.toolName = toolName;
}

protected static void cliMain(
String toolName, Class<? extends CliBase> toolClass,
Io io, String[] args) {
Expand Down
98 changes: 42 additions & 56 deletions org.lflang/src/org/lflang/cli/Lfc.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package org.lflang.cli;


import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.OptionSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.GeneratorDelegate;
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
Expand Down Expand Up @@ -54,15 +46,8 @@ public class Lfc extends CliBase {
@Inject
private JavaIoFileSystemAccess fileAccess;

public Lfc() {
super("lfc");
}

/**
/*
* Supported CLI options.
*
* @author Marten Lohstroh
* @author Atharva Patil
*/

@Option(
Expand Down Expand Up @@ -101,7 +86,7 @@ public Lfc() {
@Option(
names = {"-l", "--lint"},
arity = "0",
description = "Enable or disable linting of generated code.")
description = "Enable linting of generated code.")
private boolean lint;

@Option(
Expand Down Expand Up @@ -143,7 +128,7 @@ public Lfc() {
@Option(
names = {"-w", "--workers"},
description = "Specify the default number of worker threads.")
private int workers;
private Integer workers;

/**
* Main function of the stand-alone compiler.
Expand Down Expand Up @@ -245,47 +230,48 @@ private Path getActualOutputPath(Path root, Path path) {
* @return Properties for the code generator.
*/
protected Properties filterPassOnProps() {
// Parameters corresponding to the options that need to be passed on to
// the generator as properties.
final Set<String> passOnParams = Stream.of(
BuildParm.BUILD_TYPE,
BuildParm.CLEAN,
BuildParm.TARGET_COMPILER,
BuildParm.EXTERNAL_RUNTIME_PATH,
BuildParm.LOGGING,
BuildParm.LINT,
BuildParm.NO_COMPILE,
BuildParm.QUIET,
BuildParm.RTI,
BuildParm.RUNTIME_VERSION,
BuildParm.SCHEDULER,
BuildParm.THREADING,
BuildParm.WORKERS)
.map(param -> param.getKey())
.collect(Collectors.toUnmodifiableSet());

Properties props = new Properties();

for (OptionSpec option : spec.options()) {
String optionName = option.longestName();
// Check whether this option needs to be passed on to the code
// generator as a property.
if (passOnParams.contains(optionName)) {
String value = "";
// Boolean or Integer option.
if (option.getValue() instanceof Boolean ||
option.getValue() instanceof Integer) {
value = String.valueOf(option.getValue());
// String option.
} else if (option.getValue() instanceof String) {
value = option.getValue();
// Path option.
} else if (option.getValue() instanceof Path) {
value = option.getValue().toString();
}
props.setProperty(optionName, value);
}
if (buildType != null) {
props.setProperty(BuildParm.BUILD_TYPE.getKey(), buildType);
}
if (clean) {
props.setProperty(BuildParm.CLEAN.getKey(), "true");
}
if (externalRuntimePath != null) {
props.setProperty(BuildParm.EXTERNAL_RUNTIME_PATH.getKey(), externalRuntimePath.toString());
}
if (lint) {
props.setProperty(BuildParm.LINT.getKey(), "true");
}
if (logging != null) {
props.setProperty(BuildParm.LOGGING.getKey(), logging);
}
if (noCompile) {
props.setProperty(BuildParm.NO_COMPILE.getKey(), "true");
}
if (targetCompiler != null) {
props.setProperty(BuildParm.TARGET_COMPILER.getKey(), targetCompiler);
}
if (quiet) {
props.setProperty(BuildParm.QUIET.getKey(), "true");
}
if (rti != null) {
props.setProperty(BuildParm.RTI.getKey(), rti);
}
if (runtimeVersion != null) {
props.setProperty(BuildParm.RUNTIME_VERSION.getKey(), runtimeVersion);
}
if (scheduler != null) {
props.setProperty(BuildParm.SCHEDULER.getKey(), scheduler);
}
if (threading != null) {
props.setProperty(BuildParm.THREADING.getKey(), threading);
}
if (workers != null) {
props.setProperty(BuildParm.WORKERS.getKey(), workers.toString());
}

return props;
}
}
12 changes: 0 additions & 12 deletions org.lflang/src/org/lflang/cli/Lff.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import org.eclipse.emf.ecore.resource.Resource;

import org.lflang.ast.FormattingUtils;
import org.lflang.LocalStrings;
import org.lflang.util.FileUtil;

/**
Expand Down Expand Up @@ -67,10 +59,6 @@ public class Lff extends CliBase {
description = "Print more details on files affected.")
private boolean verbose = false;

public Lff() {
super("lff");
}

/**
* Main function of the formatter.
* Caution: this will invoke System.exit.
Expand Down