From 16bacae7e32464a2e33072d1fd9ed9751e4f668f Mon Sep 17 00:00:00 2001 From: Aparajita Fishman Date: Tue, 29 Mar 2011 16:06:19 -0400 Subject: [PATCH] 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. --- Objective-J/CPLog.js | 13 ++-- Tools/nib2cib/Converter.j | 61 +++++++--------- Tools/nib2cib/main.j | 146 +++++++++++++++++++++++++++++++++----- 3 files changed, 159 insertions(+), 61 deletions(-) diff --git a/Objective-J/CPLog.js b/Objective-J/CPLog.js index 3a9e863e4..11a3676ee 100644 --- a/Objective-J/CPLog.js +++ b/Objective-J/CPLog.js @@ -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) diff --git a/Tools/nib2cib/Converter.j b/Tools/nib2cib/Converter.j index d841f6fd4..f8f747e38 100644 --- a/Tools/nib2cib/Converter.j +++ b/Tools/nib2cib/Converter.j @@ -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("> %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(""); + stream.print(""); } function fail(message)