* Automatically figure out what the required classes and functions are, generate them, output to a file.
* Does nothing if there was a problem with the command-line options.
*/
private void fitToFile(File outputFolder) {
ModuleDefn defn = autoFit();
if (defn == null) {
jfitLogger.info("");
jfitLogger.info("Generation failed.");
return;
}
String sourceText = defn.toSourceText();
String[] moduleNameQualifier = defn.getModuleName().getQualifier().getComponents();
String unqualifiedModuleName = defn.getModuleName().getUnqualifiedModuleName();
File fileFolder = outputFolder;
for (final String component : moduleNameQualifier) {
if (fileFolder == null) {
fileFolder = new File(component);
} else {
fileFolder = new File(fileFolder, component);
}
}
if (fileFolder != null) {
if (!FileSystemHelper.ensureDirectoryExists(fileFolder)) {
jfitLogger.severe("The folder \'" + fileFolder.getPath() + "\' does not exist, and could not be created.");
return;
}
}
String fileName = unqualifiedModuleName + "." + CALSourcePathMapper.INSTANCE.getFileExtension();
File outputFile = fileFolder == null ? new File(fileName) : new File(fileFolder, fileName);
jfitLogger.info("Writing file: " + outputFile.getAbsolutePath() + "..");
boolean wroteFile;
Writer writer = null;
try {
writer = new BufferedWriter(new FileWriter(outputFile));
writer.write(sourceText);
wroteFile = true;
} catch (IOException e) {
jfitLogger.log(Level.SEVERE, "Error writing file: " + outputFile.getPath(), e);
wroteFile = false;
} finally {
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
// Not much we can do about this.
}
}
}
if (wroteFile) {
int nGeneratedFunctions = 0;
int nGeneratedTypes = 0;
int nTopLevelDefns = defn.getNTopLevelDefns();
for (int i = 0; i < nTopLevelDefns; i++) {
TopLevelSourceElement nthTopLevelDefn = defn.getNthTopLevelDefn(i);
if (nthTopLevelDefn instanceof SourceModel.FunctionDefn.Foreign) {
nGeneratedFunctions++;
} else if (nthTopLevelDefn instanceof SourceModel.TypeConstructorDefn.ForeignType) {