File swcFile = null;
File asSources = null;
Enunciate enunciate = getEnunciate();
if (isSwcDownloadable() || !flexApps.isEmpty()) {
if (this.flexHome == null) {
throw new EnunciateException("To compile a flex app you must specify the Flex SDK home directory, either in configuration, by setting the FLEX_HOME environment variable, or setting the 'flex.home' system property.");
}
File flexHomeDir = new File(this.flexHome);
if (!flexHomeDir.exists()) {
throw new EnunciateException("Flex home not found ('" + flexHomeDir.getAbsolutePath() + "').");
}
File javaBinDir = new File(System.getProperty("java.home"), "bin");
File javaExecutable = new File(javaBinDir, "java");
if (!javaExecutable.exists()) {
//append the "exe" for windows users.
javaExecutable = new File(javaBinDir, "java.exe");
}
String javaCommand = javaExecutable.getAbsolutePath();
if (!javaExecutable.exists()) {
warn("No java executable found in %s. We'll just hope the environment is set up to execute 'java'...", javaBinDir.getAbsolutePath());
javaCommand = "java";
}
int compileCommandIndex;
int outputFileIndex;
int sourcePathIndex;
int mainMxmlPathIndex;
List<String> commandLine = new ArrayList<String>();
int argIndex = 0;
commandLine.add(argIndex++, javaCommand);
for (String jvmarg : this.compilerConfig.getJVMArgs()) {
commandLine.add(argIndex++, jvmarg);
}
commandLine.add(argIndex++, "-cp");
File flexHomeLib = new File(flexHomeDir, "lib");
if (!flexHomeLib.exists()) {
throw new EnunciateException("File not found: " + flexHomeLib);
}
else {
StringBuilder builder = new StringBuilder();
Iterator<File> flexLibIt = Arrays.asList(flexHomeLib.listFiles()).iterator();
while (flexLibIt.hasNext()) {
File flexJar = flexLibIt.next();
if (flexJar.getAbsolutePath().endsWith("jar")) {
builder.append(flexJar.getAbsolutePath());
if (flexLibIt.hasNext()) {
builder.append(File.pathSeparatorChar);
}
}
else {
debug("File %s will not be included on the classpath because it's not a jar.", flexJar);
}
}
commandLine.add(argIndex++, builder.toString());
}
compileCommandIndex = argIndex;
commandLine.add(argIndex++, null);
commandLine.add(argIndex++, "-output");
outputFileIndex = argIndex;
commandLine.add(argIndex++, null);
if (compilerConfig.getFlexConfig() == null) {
compilerConfig.setFlexConfig(new File(new File(flexHome, "frameworks"), "flex-config.xml"));
}
if (compilerConfig.getFlexConfig().exists()) {
commandLine.add(argIndex++, "-load-config");
commandLine.add(argIndex++, compilerConfig.getFlexConfig().getAbsolutePath());
}
else {
warn("Configured flex configuration file %s doesn't exist. Ignoring...", compilerConfig.getFlexConfig());
}
if (compilerConfig.getContextRoot() == null) {
if (getEnunciate().getConfig().getLabel() != null) {
compilerConfig.setContextRoot("/" + getEnunciate().getConfig().getLabel());
}
else {
compilerConfig.setContextRoot("/enunciate");
}
}
commandLine.add(argIndex++, "-compiler.context-root");
commandLine.add(argIndex++, compilerConfig.getContextRoot());
if (compilerConfig.getLocale() != null) {
commandLine.add(argIndex++, "-compiler.locale");
commandLine.add(argIndex++, compilerConfig.getLocale());
}
if (compilerConfig.getLicenses().size() > 0) {
commandLine.add(argIndex++, "-licenses.license");
for (License license : compilerConfig.getLicenses()) {
commandLine.add(argIndex++, license.getProduct());
commandLine.add(argIndex++, license.getSerialNumber());
}
}
if (compilerConfig.getOptimize() != null && compilerConfig.getOptimize()) {
commandLine.add(argIndex++, "-compiler.optimize");
}
if (compilerConfig.getDebug() != null && compilerConfig.getDebug()) {
commandLine.add(argIndex++, "-compiler.debug=true");
}
if (compilerConfig.getStrict() != null && compilerConfig.getStrict()) {
commandLine.add(argIndex++, "-compiler.strict");
}
if (compilerConfig.getUseNetwork() != null && compilerConfig.getUseNetwork()) {
commandLine.add(argIndex++, "-use-network");
}
if (compilerConfig.getIncremental() != null && compilerConfig.getIncremental()) {
commandLine.add(argIndex++, "-compiler.incremental");
}
if (compilerConfig.getShowActionscriptWarnings() != null && compilerConfig.getShowActionscriptWarnings()) {
commandLine.add(argIndex++, "-show-actionscript-warnings");
}
if (compilerConfig.getShowBindingWarnings() != null && compilerConfig.getShowBindingWarnings()) {
commandLine.add(argIndex++, "-show-binding-warnings");
}
if (compilerConfig.getShowDeprecationWarnings() != null && compilerConfig.getShowDeprecationWarnings()) {
commandLine.add(argIndex++, "-show-deprecation-warnings");
}
for (String arg : this.compilerConfig.getArgs()) {
commandLine.add(argIndex++, arg);
}
commandLine.add(argIndex++, "-compiler.services");
File xmlGenerateDir = getXMLGenerateDir();
commandLine.add(argIndex++, new File(xmlGenerateDir, "merged-services-config.xml").getAbsolutePath());
commandLine.add(argIndex, "-include-sources");
File clientSideGenerateDir = getClientSideGenerateDir();
commandLine.add(argIndex + 1, clientSideGenerateDir.getAbsolutePath());
String swcName = getSwcName();
if (swcName == null) {
String label = "enunciate";
if (getLabel() != null) {
label = getLabel();
}
else if ((enunciate.getConfig() != null) && (enunciate.getConfig().getLabel() != null)) {
label = enunciate.getConfig().getLabel();
}
swcName = label + "-as3-client.swc";
}
File swcCompileDir = getSwcCompileDir();
swcFile = new File(swcCompileDir, swcName);
boolean swcUpToDate = swcFile.exists() &&
enunciate.isUpToDate(xmlGenerateDir, swcCompileDir) &&
enunciate.isUpToDate(clientSideGenerateDir, swcCompileDir);
if (!swcUpToDate) {
commandLine.set(compileCommandIndex, compilerConfig.getSwcCompileCommand());
commandLine.set(outputFileIndex, swcFile.getAbsolutePath());
debug("Compiling %s for the client-side ActionScript classes...", swcFile.getAbsolutePath());
if (enunciate.isDebug()) {
StringBuilder command = new StringBuilder();
for (String commandPiece : commandLine) {
command.append(' ').append(commandPiece);
}
debug("Executing SWC compile for client-side actionscript with the command: %s", command);
}
compileSwc(commandLine);
}
else {
info("Skipping compilation of %s as everything appears up-to-date...", swcFile.getAbsolutePath());
}
//swc is compiled
while (commandLine.size() > argIndex) {
//remove the compc-specific options...
commandLine.remove(argIndex);
}
if (compilerConfig.getProfile() != null && compilerConfig.getProfile()) {
commandLine.add(argIndex++, "-compiler.profile");
}
if (compilerConfig.getWarnings() != null && compilerConfig.getWarnings()) {
commandLine.add(argIndex++, "-warnings");
}
commandLine.add(argIndex++, "-source-path");
commandLine.add(argIndex++, clientSideGenerateDir.getAbsolutePath());
commandLine.add(argIndex++, "-source-path");
sourcePathIndex = argIndex;
commandLine.add(argIndex++, null);
commandLine.add(argIndex++, "--");
mainMxmlPathIndex = argIndex;
commandLine.add(argIndex++, null);
commandLine.set(compileCommandIndex, compilerConfig.getFlexCompileCommand());
File outputDirectory = getSwfCompileDir();
debug("Creating output directory: " + outputDirectory);
outputDirectory.mkdirs();
for (FlexApp flexApp : flexApps) {
String mainMxmlPath = flexApp.getMainMxmlFile();
if (mainMxmlPath == null) {
throw new EnunciateException("A main MXML file for the flex app '" + flexApp.getName() + "' must be supplied with the 'mainMxmlFile' attribute.");
}
File mainMxmlFile = enunciate.resolvePath(mainMxmlPath);
if (!mainMxmlFile.exists()) {
throw new EnunciateException("Main MXML file for the flex app '" + flexApp.getName() + "' doesn't exist.");
}
File swfDir = outputDirectory;
if (flexApp.getOutputPath() != null && !"".equals(flexApp.getOutputPath())) {
swfDir = new File(outputDirectory, flexApp.getOutputPath());
swfDir.mkdirs();
}
File swfFile = new File(swfDir, flexApp.getName() + ".swf");
File appSrcDir = enunciate.resolvePath(flexApp.getSrcDir());
String swfFilePath = swfFile.getAbsolutePath();
boolean swfUpToDate = swfFile.exists()
&& mainMxmlFile.lastModified() < swfFile.lastModified()
&& enunciate.isUpToDate(appSrcDir, swfFile);
if (!swfUpToDate) {
commandLine.set(outputFileIndex, swfFilePath);
commandLine.set(mainMxmlPathIndex, mainMxmlFile.getAbsolutePath());
commandLine.set(sourcePathIndex, appSrcDir.getAbsolutePath());
debug("Compiling %s ...", swfFilePath);
if (enunciate.isDebug()) {
StringBuilder command = new StringBuilder();
for (String commandPiece : commandLine) {
command.append(' ').append(commandPiece);
}
debug("Executing flex compile for module %s with the command: %s", flexApp.getName(), command);
}
ProcessBuilder processBuilder = new ProcessBuilder(commandLine.toArray(new String[commandLine.size()]));
processBuilder.directory(getSwfCompileDir());
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
BufferedReader procReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = procReader.readLine();
while (line != null) {
info(line);
line = procReader.readLine();
}
int procCode;
try {
procCode = process.waitFor();
}
catch (InterruptedException e1) {
throw new EnunciateException("Unexpected inturruption of the Flex compile process.");
}
if (procCode != 0) {
throw new EnunciateException("Flex compile failed for module " + flexApp.getName());
}
}
else {
info("Skipping compilation of %s as everything appears up-to-date...", swfFilePath);
}