/// Create and compile a workspace through a BasicCALServices
//
WorkspaceManager workspaceManager = calServices.getWorkspaceManager();
MessageLogger msgLogger = new MessageLogger();
StatusListener.StatusListenerAdapter statusListener = new StatusListener.StatusListenerAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void setModuleStatus(StatusListener.Status.Module moduleStatus, ModuleName moduleName) {
if (moduleStatus == StatusListener.SM_GENCODE) {
logger.fine("Compiling " + moduleName);
}
}
};
logger.info("Compiling CAL workspace...");
calServices.compileWorkspace(statusListener, msgLogger);
if (msgLogger.getNErrors() == 0) {
logger.info("Compilation successful");
////
/// We only run the CarBuilder if everything compiled properly without errors
//
CarBuilder.Monitor monitor = new CarBuilder.Monitor() {
public boolean isCanceled() {
return false;
}
public void operationStarted(int nCars, int nTotalModules) {}
public void showMessages(String[] messages) {
for (final String element : messages) {
logger.info(element);
}
}
public void carBuildingStarted(String carName, int nModules) {
logger.info("Creating the Car file " + carName);
}
public void processingModule(String carName, ModuleName moduleName) {
logger.info("Adding module " + moduleName + " to the Car...");
}
public void carBuildingDone(String carName) {
logger.info("Done creating the Car file " + carName);
}
public void operationDone() {
logger.info("All done");
}
};
///
// Command line syntax:
//
// [-notVerbose] [-keepsource] [-nocws | -nosuffix] [-s] [-excludeCarsInDirs ... --] [-excludeCarJarsInDirs ... --] [-jar] outputDirectory [carSpecName1 carSpecName2...]
// OR
// [-notVerbose] [-keepsource] [-nocws | -nosuffix] [-s] [-excludeCarsInDirs ... --] [-excludeCarJarsInDirs ... --] [-jar] -d outputDirectory
boolean verboseMode = true; // default: verbose
if (arguments.getArgument().equals("-notVerbose")) {
verboseMode = false;
arguments.consumeArgument();
}
boolean shouldBuildSourcelessModules = true; // default: generate sourceless modules
if (arguments.getArgument().equals("-keepsource")) {
shouldBuildSourcelessModules = false;
arguments.consumeArgument();
}
boolean shouldGenerateCorrespWorspaceDecl = true; // default: generated the cws files
boolean noCarSuffixInWorkspaceDeclName = false; // default: generated cws files end with .car.cws
if (arguments.getArgument().equals("-nocws")) {
shouldGenerateCorrespWorspaceDecl = false;
arguments.consumeArgument();
} else if (arguments.getArgument().equals("-nosuffix")) {
noCarSuffixInWorkspaceDeclName = true;
arguments.consumeArgument();
}
boolean shouldSkipModulesAlreadyInCars = false; // default: do not skip modules already in Cars
if (arguments.getArgument().equals("-s")) {
shouldSkipModulesAlreadyInCars = true;
arguments.consumeArgument();
}
Set<String> carsToExclude = new HashSet<String>(additionalCarsToExclude);
if (arguments.getArgument().equals("-excludeCarsInDirs")) {
arguments.consumeArgument();
while (!arguments.getArgument().equals("--")) {
File directory = new File(arguments.getAndConsumeArgument(), "Car");
File[] files = directory.listFiles();
if (files == null) {
logger.warning("Folder does not exist: " + directory);
} else {
for (final File element : files) {
carsToExclude.add(element.getName());
}
}
}
arguments.consumeArgument(); // consume the -- delimiter
}
if (arguments.getArgument().equals("-excludeCarJarsInDirs")) {
arguments.consumeArgument();
while (!arguments.getArgument().equals("--")) {
File directory = new File(arguments.getAndConsumeArgument());
File[] files = directory.listFiles();
if (files == null) {
logger.warning("Folder does not exist: " + directory);
} else {
for (final File element : files) {
String fileName = element.getName();
if (fileName.endsWith(CarBuilder.DOT_CAR_DOT_JAR)) {
String carName = fileName.substring(0, fileName.length() - 4); // chop ".jar" off the end
carsToExclude.add(carName);
}
}
}
}
arguments.consumeArgument(); // consume the -- delimiter
}
boolean shouldGenerateCarJarSuffix = false; // default: generate Cars and not Car-jars.
if (arguments.getArgument().equals("-jar")) {
shouldGenerateCarJarSuffix = true;
arguments.consumeArgument();
}
if (verboseMode) {
System.out.println();
System.out.println(workspaceManager.getDebugInfo());
System.out.println();
}
CarBuilder.BuilderOptions options = new CarBuilder.BuilderOptions(
shouldSkipModulesAlreadyInCars,
shouldGenerateCorrespWorspaceDecl,
noCarSuffixInWorkspaceDeclName,
shouldBuildSourcelessModules,
carsToExclude,
shouldGenerateCarJarSuffix);
if (arguments.getArgument().equals("-d")) {
// the user wants one Car per workspace declaration file
arguments.consumeArgument();
final String outputDir = arguments.getAndConsumeArgument();
arguments.noMoreArgumentsAllowed();
final File outputDirectory = new File(outputDir);
try {
String[] carsBuilt = CarBuilder.buildOneCarPerWorkspaceDeclaration(workspaceManager, monitor, outputDirectory, options);
return carsBuilt;
} catch (IOException e) {
logger.info("Error occurred generating the Car files: " + e.getMessage());
throw new OperationFailedException();
}
} else {
// the user wants just one Car for all modules in the workspace
if (!carsToExclude.equals(additionalCarsToExclude)) {
logger.info("The option -excludeCarsInDirs does not apply for building just one Car. Ignoring...");
}
final String carName = CarBuilder.makeCarNameFromSourceWorkspaceDeclName(workspaceDeclarationName);
final String outputDir = arguments.getAndConsumeArgument();
final File outputDirectory = new File(outputDir);
CarBuilder.Configuration config =
CarBuilder.Configuration.makeConfigOptionallySkippingModulesAlreadyInCars(workspaceManager, options);
for (Iterator<String> it = arguments.getRemainingArgumentsIterator(); it.hasNext(); ) {
String carSpecName = it.next();
File specFile = new File(carSpecName);
config.addFileBasedCarSpec(specFile.getName(), specFile);
}
config.setMonitor(monitor);
try {
boolean notCanceled = CarBuilder.buildCar(config, outputDirectory, carName, options);
if (notCanceled) {
return new String[] {carName};
}
} catch (IOException e) {
logger.info("Error occurred generating the Car file: " + e.getMessage());
throw new OperationFailedException();
}
}
} else {
logger.severe("Compilation failed:");
List<CompilerMessage> messages = msgLogger.getCompilerMessages();
for (int i = 0, n = messages.size(); i < n; i++) {
logger.info(" " + messages.get(i).toString());
}
throw new OperationFailedException();