String appName = parsedArgs.getString(Parameters.APP_NAME);
String targetDirPath = parsedArgs.getString(Parameters.TARGET_DIR);
boolean warExport = parsedArgs.getBoolean("war");
boolean hasExplicitExportDirArg = (targetDirPath != null);
App app = brjs.app(appName);
File targetDir = brjs.storageDir("built-apps");
File appExportDir;
File warExportFile;
if (hasExplicitExportDirArg)
{
targetDir = new File(targetDirPath);
if (!targetDir.isDirectory())
{
targetDir = brjs.file("sdk/" + targetDirPath);
}
appExportDir = targetDir;
warExportFile = new File(targetDir, appName+".war");
}
else {
appExportDir = new File(targetDir, appName);
warExportFile = new File(targetDir, appName+".war");
if (warExport && warExportFile.exists()) {
boolean deleted = FileUtils.deleteQuietly(warExportFile);
if (!deleted) {
File oldWarExportFile = warExportFile;
warExportFile = new File(targetDir, appName+"_"+getBuiltAppTimestamp()+".war");
brjs.logger(this.getClass()).warn( Messages.UNABLE_TO_DELETE_BULIT_APP_EXCEPTION, RelativePathUtility.get(brjs.getFileInfoAccessor(), app.dir(), oldWarExportFile), RelativePathUtility.get(brjs.getFileInfoAccessor(), app.dir(), warExportFile));
}
} else if (!warExport && appExportDir.exists()){
boolean deleted = FileUtils.deleteQuietly(appExportDir);
if (!deleted) {
File oldAppExportDir = appExportDir;
appExportDir = new File(targetDir, appName+"_"+getBuiltAppTimestamp());
brjs.logger(this.getClass()).warn( Messages.UNABLE_TO_DELETE_BULIT_APP_EXCEPTION, RelativePathUtility.get(brjs.getFileInfoAccessor(), app.dir(), oldAppExportDir), RelativePathUtility.get(brjs.getFileInfoAccessor(), app.dir(), appExportDir));
}
}
targetDir.mkdirs();
}
if(!app.dirExists()) throw new NodeDoesNotExistException(app, this);
if(!targetDir.isDirectory()) throw new DirectoryDoesNotExistCommandException(targetDirPath, this);
try {
if (warExport) {
if(warExportFile.exists()) throw new DirectoryAlreadyExistsCommandException(warExportFile.getPath(), this);
app.buildWar(warExportFile);
logger.println(Messages.APP_BUILT_CONSOLE_MSG, appName, warExportFile.getCanonicalPath());
} else {
if (hasExplicitExportDirArg) {
if (appExportDir.listFiles().length > 0) throw new DirectoryNotEmptyCommandException(appExportDir.getPath(), this);
} else {
appExportDir.mkdir();
}
app.build(appExportDir);
logger.println(Messages.APP_BUILT_CONSOLE_MSG, appName, appExportDir.getCanonicalPath());
}
}
catch (ModelOperationException | IOException e) {
throw new CommandOperationException(e);