More cool stuff...

- The holy grail! I took Klaas' great autonib2cib and rolled it into nib2cib.
- Removed check for stream in CPLogColorize, it isn't necessary and reduces its general-purpose usage.
This commit is contained in:
Aparajita Fishman committed 2011-03-29 16:06:19 -04:00
1 parent d00a8b9d18
commit 16bacae7e3
3 files changed
+159 -61

No files matched your search

+4 -9
View File
@@ -166,16 +166,11 @@ var stream;
GLOBAL(CPLogColorize) = function(aString, aLevel)
{
if (stream)
{
// Try to determine if a colorizing stanza is already open, they can't be nested
if (/^.*\x00\w+\([^\x00]*$/.test(aString))
return aString;
else
return "\0" + (levelColorMap[aLevel] || "info") + "(" + aString + "\0)";
}
else
// Try to determine if a colorizing stanza is already open, they can't be nested
if (/^.*\x00\w+\([^\x00]*$/.test(aString))
return aString;
else
return "\0" + (levelColorMap[aLevel] || "info") + "(" + aString + "\0)";
}
GLOBAL(CPLogPrint) = function(aString, aLevel, aTitle, aFormatter)
+27 -34
View File
@@ -71,46 +71,39 @@ ConverterConversionException = @"ConverterConversionException";
- (void)convert
{
try
if ([resourcesPath length] && !FILE.isReadable(resourcesPath))
[CPException raise:ConverterConversionException reason:@"Could not read Resources at path \"" + resourcesPath + "\""];
var inferredFormat = format;
if (inferredFormat === NibFormatUndetermined)
{
if ([resourcesPath length] && !FILE.isReadable(resourcesPath))
[CPException raise:ConverterConversionException reason:@"Could not read Resources at path \"" + resourcesPath + "\""];
// Assume its a Mac file.
inferredFormat = NibFormatMac;
var inferredFormat = format;
if (inferredFormat === NibFormatUndetermined)
{
// Assume its a Mac file.
inferredFormat = NibFormatMac;
// Some .xibs are iPhone nibs, check the actual contents in this case.
if (FILE.extension(inputPath) !== ".nib" && FILE.isFile(inputPath) &&
FILE.read(inputPath, { charset:"UTF-8" }).indexOf("<archive type=\"com.apple.InterfaceBuilder3.CocoaTouch.XIB\"") !== -1)
inferredFormat = NibFormatIPhone;
if (inferredFormat === NibFormatMac)
CPLog.info("Auto-detected Cocoa Nib or Xib File");
else
CPLog.info("Auto-detected CocoaTouch Xib File");
}
var nibData = [self CPCompliantNibDataAtFilePath:inputPath];
// Some .xibs are iPhone nibs, check the actual contents in this case.
if (FILE.extension(inputPath) !== ".nib" && FILE.isFile(inputPath) &&
FILE.read(inputPath, { charset:"UTF-8" }).indexOf("<archive type=\"com.apple.InterfaceBuilder3.CocoaTouch.XIB\"") !== -1)
inferredFormat = NibFormatIPhone;
if (inferredFormat === NibFormatMac)
var convertedData = [self convertedDataFromMacData:nibData resourcesPath:resourcesPath];
CPLog.info("Auto-detected Cocoa Nib or Xib File");
else
[CPException raise:ConverterConversionException reason:@"nib2cib does not understand this nib format."];
if (![outputPath length])
outputPath = inputPath.substr(0, inputPath.length - FILE.extension(inputPath).length) + ".cib";
FILE.write(outputPath, [convertedData rawString], { charset:"UTF-8" });
CPLog.info("Conversion successful");
}
catch (anException)
{
CPLog.fatal(anException);
CPLog.info("Auto-detected CocoaTouch Xib File");
}
var nibData = [self CPCompliantNibDataAtFilePath:inputPath];
if (inferredFormat === NibFormatMac)
var convertedData = [self convertedDataFromMacData:nibData resourcesPath:resourcesPath];
else
[CPException raise:ConverterConversionException reason:@"nib2cib does not understand this nib format."];
if (![outputPath length])
outputPath = inputPath.substr(0, inputPath.length - FILE.extension(inputPath).length) + ".cib";
FILE.write(outputPath, [convertedData rawString], { charset:"UTF-8" });
CPLog.info(CPLogColorize("Conversion successful", "warn"));
}
- (CPData)CPCompliantNibDataAtFilePath:(CPString)aFilePath
+128 -18
View File
@@ -33,23 +33,35 @@
var FILE = require("file"),
OS = require("os"),
SYS = require("system"),
FileList = require("jake").FileList,
stream = require("narwhal/term").stream,
DefaultTheme = "Aristo",
BuildTypes = ["Debug", "Release"],
DefaultXibFile = "MainMenu.xib";
var parser = new (require("narwhal/args").Parser)();
var parser = new (require("narwhal/args").Parser)(),
nibInfo = {};
function main(args)
{
var options = parseOptions(args);
if (options.watch)
watch(options);
else
convert(options);
}
function convert(options, inputFile)
{
try
{
var options = parseOptions(args),
inputFile = getInputFile(options.args),
outputFile = getOutputFile(inputFile, options.args);
inputFile = inputFile || getInputFile(options.args);
var resourcesPath = "";
var outputFile = getOutputFile(inputFile, options.args),
resourcesPath = "";
if (options.resources)
{
@@ -95,16 +107,71 @@ function main(args)
{
[converter convert];
});
return true;
}
catch (anException)
{
CPLog.fatal([anException reason]);
return false;
}
}
function watch(options)
{
var verbosity = options.quiet ? -1 : options.verbosity;
// Turn on info messages
setLogLevel(1);
directory = FILE.canonical(options.args[0] || ".");
if (!FILE.isDirectory(directory))
fail("Cannot find the directory: " + directory);
CPLog.info("Watching: " + CPLogColorize(directory, "debug"));
CPLog.info("Press Control-C to stop...");
while (true)
{
var modifiedNibs = getModifiedNibs(directory);
for (var i = 0; i < modifiedNibs.length; ++i)
{
var action = modifiedNibs[i][0],
path = modifiedNibs[i][1],
label = action === "add" ? "Added:" : "Modified:",
level = action === "add" ? "info" : "debug";
CPLog.info(">> %s %s", CPLogColorize(label, level), path);
// Let the converter log however the user configured it
setLogLevel(verbosity);
var success = convert(options, path);
setLogLevel(1);
if (success)
{
if (verbosity > 0)
stream.print();
else
CPLog.warn("Conversion successful");
}
}
OS.sleep(1);
}
}
function parseOptions(args)
{
parser.usage("[INPUT_FILE [OUTPUT_FILE]]");
parser.usage("[--watch DIRECTORY] [INPUT_FILE [OUTPUT_FILE]]");
parser.option("--watch", "watch")
.set(true)
.help("Ask nib2cib to watch a directory for changes");
parser.option("-F", "framework", "frameworks")
.push()
@@ -131,7 +198,7 @@ function parseOptions(args)
// .set(NibFormatIPhone)
// .help("Set format to iPhone");
parser.option("-v", "--verbose", "verbose")
parser.option("-v", "--verbose", "verbosity")
.inc()
.help("Increase verbosity level");
@@ -153,20 +220,26 @@ function parseOptions(args)
OS.exit(0);
}
if (options.quiet) {}
else if (options.verbose === 0)
CPLogRegister(CPLogPrint, "warn", logFormatter);
else if (options.verbose === 1)
CPLogRegister(CPLogPrint, "info", logFormatter);
else
CPLogRegister(CPLogPrint, null, logFormatter);
setLogLevel(options.quiet ? -1 : options.verbosity);
if (!options.quiet && options.verbose > 0)
if (!options.quiet && options.verbosity > 0)
printVersion();
return options;
}
function setLogLevel(level)
{
CPLogUnregister(CPLogPrint);
if (level === 0)
CPLogRegister(CPLogPrint, "warn", logFormatter);
else if (level === 1)
CPLogRegister(CPLogPrint, "info", logFormatter);
else if (level > 1)
CPLogRegister(CPLogPrint, null, logFormatter);
}
function getInputFile(args)
{
var inputFile = args[0] || DefaultXibFile;
@@ -213,7 +286,7 @@ function loadFrameworks(frameworkPaths, aCallback)
frameworkPaths.forEach(function(aFrameworkPath)
{
print("Loading " + aFrameworkPath);
CPLog.info("Loading " + aFrameworkPath);
var frameworkBundle = [[CPBundle alloc] initWithPath:aFrameworkPath];
@@ -389,6 +462,43 @@ function setSystemFontAndSize(configFile, inputFile)
return configPath;
}
function getModifiedNibs(path)
{
var nibs = new FileList(FILE.join(path, "*.xib")).items(),
count = nibs.length,
newNibInfo = {},
modifiedNibs = [];
while (count--)
{
var nib = nibs[count];
newNibInfo[nib] = FILE.mtime(nib);
if (!nibInfo.hasOwnProperty(nib))
modifiedNibs.push(["add", nib]);
else
{
if (newNibInfo[nib] - nibInfo[nib] !== 0)
modifiedNibs.push(["mod", nib]);
// Remove matching nibs so that we leave
// deleted nibs in nibInfo.
delete nibInfo[nib];
}
}
for (var nib in nibInfo)
{
if (nibInfo.hasOwnProperty(nib))
CPLog.info(">> %s %s", CPLogColorize("Deleted:", "warn"), nib);
}
nibInfo = newNibInfo;
return modifiedNibs;
}
function printVersionAndExit()
{
printVersion();
@@ -429,11 +539,11 @@ function printVersion()
version = plist.valueForKey("CPBundleVersion");
if (version)
print("nib2cib v" + version);
stream.print("nib2cib v" + version);
}
if (!version)
print("<No version info available>");
stream.print("<No version info available>");
}
function fail(message)