Better error checking, error messages, and help.

This commit is contained in:
Aparajita Fishman authored and Randall Luecke committed 2011-03-23 21:47:02 -04:00
1 parent ea25d22628
commit 7b6713aa90
3 files changed
+154 -77

No files matched your search

+57 -23
View File
@@ -112,6 +112,11 @@ var DefaultDictionary = nil,
[dictionary setObject:aString forKey:aKey];
}
- (void)removeValueForKey:(CPString)aKey
{
[dictionary removeObjectForKey:aKey];
}
- (void)setTemporaryValue:(CPString)aString forKey:(CPString)aKey
{
[temporaryDictionary setObject:aString forKey:aKey];
@@ -131,36 +136,55 @@ var DefaultDictionary = nil,
function config(/*va_args*/)
{
var index = 0,
count = arguments.length,
key = NULL,
value = NULL,
shouldGet = NO,
shouldList = NO;
var count = arguments.length;
for (; index < count; ++index)
if (count === 0 || count > 2)
{
var argument = arguments[index];
printUsage();
return;
}
switch (argument)
{
case "--get": shouldGet = YES;
break;
var argument = arguments[0],
key = nil,
value = nil,
action = nil,
valid = YES;
case "-l":
case "--list": shouldList = YES;
break;
switch (argument)
{
case "--get":
case "--remove": action = argument.substring(2);
default: if (key === NULL)
key = argument;
if (count === 2)
key = arguments[1];
else
value = argument;
}
valid = NO;
break;
case "-l":
case "--list": action = "list";
valid = count === 1;
break;
default: action = "set";
key = argument;
if (count === 2)
value = arguments[1];
else
valid = NO;
}
if (!valid)
{
printUsage();
return;
}
var configuration = [Configuration userConfiguration];
if (shouldList)
if (action === "list")
{
var key = nil,
keyEnumerator = [configuration storedKeyEnumerator];
@@ -168,14 +192,24 @@ function config(/*va_args*/)
while (key = [keyEnumerator nextObject])
print(key + '=' + [configuration valueForKey:key]);
}
else if (shouldGet)
else if (action === "get")
{
var value = [configuration valueForKey:key];
if (value)
if (value != nil)
print(value);
}
else if (key !== NULL && value !== NULL)
else if (action === "remove")
{
var value = [configuration valueForKey:key];
if (value != nil)
{
[configuration removeValueForKey:key];
[configuration save];
}
}
else if (key !== nil && value !== nil)
{
[configuration setValue:value forKey:key];
[configuration save];
+75 -40
View File
@@ -4,11 +4,10 @@
var OS = require("os"),
SYSTEM = require("system"),
FILE = require("file"),
OBJJ = require("objective-j");
OBJJ = require("objective-j"),
var stream = require("narwhal/term").stream;
var parser = new (require("narwhal/args").Parser)();
stream = require("narwhal/term").stream,
parser = new (require("narwhal/args").Parser)();
parser.usage("DESTINATION_DIRECTORY");
@@ -50,7 +49,7 @@ parser.option("--force", "force")
parser.option("--noconfig", "noconfig")
.set(true)
.help("Selects a project template to use.");
.help("Use the default configuration, ignore your configuration.");
parser.option("--list-templates", "listTemplates")
.set(true)
@@ -63,13 +62,13 @@ parser.option("--list-frameworks", "listFrameworks")
parser.helpful();
// FIXME: better way to do this:
var CAPP_HOME = require("narwhal/packages").catalog["cappuccino"].directory;
var templatesDirectory = FILE.join(CAPP_HOME, "lib", "capp", "Resources", "Templates");
var CAPP_HOME = require("narwhal/packages").catalog["cappuccino"].directory,
templatesDirectory = FILE.join(CAPP_HOME, "lib", "capp", "Resources", "Templates");
function gen(/*va_args*/)
{
var args = ["capp gen"].concat(Array.prototype.slice.call(arguments));
var options = parser.parse(args, null, null, true);
var args = ["capp gen"].concat(Array.prototype.slice.call(arguments)),
options = parser.parse(args, null, null, true);
if (options.args.length > 1)
{
@@ -106,10 +105,17 @@ function gen(/*va_args*/)
var sourceTemplate = null;
if (FILE.isAbsolute(options.template))
sourceTemplate = FILE.join(options.template);
sourceTemplate = options.template;
else
sourceTemplate = FILE.join(templatesDirectory, options.template);
if (!FILE.isDirectory(sourceTemplate))
{
stream.print(colorize("Error: ", "red") + "The template " + logPath(sourceTemplate) + " cannot be found. Available templates are:");
listTemplates();
OS.exit(1);
}
var configFile = FILE.join(sourceTemplate, "template.config"),
config = {};
@@ -117,9 +123,9 @@ function gen(/*va_args*/)
config = JSON.parse(FILE.read(configFile, { charset:"UTF-8" }));
var destinationProject = destination,
configuration = options.noconfig ? [Configuration defaultConfiguration] : [Configuration userConfiguration];
configuration = options.noconfig ? [Configuration defaultConfiguration] : [Configuration userConfiguration],
frameworks = options.frameworks;
var frameworks = options.frameworks;
if (!options.noFrameworks)
frameworks.push("Objective-J", "Foundation", "AppKit");
@@ -132,7 +138,6 @@ function gen(/*va_args*/)
FILE.copyTree(sourceTemplate, destinationProject);
var files = FILE.glob(FILE.join(destinationProject, "**", "*")),
index = 0,
count = files.length,
name = FILE.basename(destinationProject),
orgIdentifier = [configuration valueForKey:@"organization.identifier"] || "";
@@ -141,7 +146,7 @@ function gen(/*va_args*/)
[configuration setTemporaryValue:orgIdentifier + '.' + toIdentifier(name) forKey:@"project.identifier"];
[configuration setTemporaryValue:toIdentifier(name) forKey:@"project.nameasidentifier"];
for (; index < count; ++index)
for (var index = 0; index < count; ++index)
{
var path = files[index];
@@ -158,7 +163,7 @@ function gen(/*va_args*/)
try
{
var contents = FILE.read(path, { charset : "UTF-8" }),
key = nil,
key = null,
keyEnumerator = [configuration keyEnumerator];
while (key = [keyEnumerator nextObject])
@@ -168,7 +173,7 @@ function gen(/*va_args*/)
}
catch (anException)
{
stream.print("Copying and modifying " + path + " failed.");
warn("An error occurred (" + anException.toString() + ") while applying the " + (options.noconfig ? "default" : "user") + " configuration to: " + logPath(path));
}
}
@@ -182,8 +187,7 @@ function gen(/*va_args*/)
else
{
stream.print("Directory already exists");
OS.exit(1);
fail("The directory " + FILE.absolute(destinationProject) + " already exists.");
}
}
@@ -192,24 +196,23 @@ function createFrameworksInFile(/*Array*/ frameworks, /*String*/ aFile, /*Boolea
var destination = FILE.path(FILE.absolute(aFile));
if (!destination.isDirectory())
throw new Error("Can't create Frameworks. Directory does not exist: " + destination);
fail("Cannot create Frameworks. The directory does not exist: " + destination);
var destinationFrameworks = destination.join("Frameworks"),
destinationDebugFrameworks = destination.join("Frameworks", "Debug");
stream.print("Creating Frameworks directory in " + destinationFrameworks + ".");
stream.print("Creating Frameworks directory in " + logPath(destinationFrameworks) + "...");
//destinationFrameworks.mkdirs(); // redundant
destinationDebugFrameworks.mkdirs();
if (build)
{
if (!(SYSTEM.env["CAPP_BUILD"] || SYSTEM.env["STEAM_BUILD"]))
throw "CAPP_BUILD or STEAM_BUILD must be defined";
if (!(SYSTEM.env["CAPP_BUILD"]))
fail("$CAPP_BUILD must be defined to use the --build or -l option.");
var builtFrameworks = FILE.path(SYSTEM.env["CAPP_BUILD"] || SYSTEM.env["STEAM_BUILD"]);
var sourceFrameworks = builtFrameworks.join("Release"),
var builtFrameworks = FILE.path(SYSTEM.env["CAPP_BUILD"]),
sourceFrameworks = builtFrameworks.join("Release"),
sourceDebugFrameworks = builtFrameworks.join("Debug");
frameworks.forEach(function(framework)
@@ -218,7 +221,6 @@ function createFrameworksInFile(/*Array*/ frameworks, /*String*/ aFile, /*Boolea
installFramework(sourceDebugFrameworks.join(framework), destinationDebugFrameworks.join(framework), force, symlink);
});
}
else
{
// Frameworks. Search frameworks paths
@@ -228,9 +230,9 @@ function createFrameworksInFile(/*Array*/ frameworks, /*String*/ aFile, /*Boolea
if (framework === "Objective-J")
{
// Objective-J. Take from OBJJ_HOME.
var objjHome = FILE.path(OBJJ.OBJJ_HOME);
var objjPath = objjHome.join("Frameworks", "Objective-J");
var objjDebugPath = objjHome.join("Frameworks", "Debug", "Objective-J");
var objjHome = FILE.path(OBJJ.OBJJ_HOME),
objjPath = objjHome.join("Frameworks", "Objective-J"),
objjDebugPath = objjHome.join("Frameworks", "Debug", "Objective-J");
installFramework(objjPath, destinationFrameworks.join("Objective-J"), force, symlink);
installFramework(objjDebugPath, destinationDebugFrameworks.join("Objective-J"), force, symlink);
@@ -238,31 +240,37 @@ function createFrameworksInFile(/*Array*/ frameworks, /*String*/ aFile, /*Boolea
return;
}
var found;
var found = false;
for (var i = 0, found = false; !found && i < OBJJ.objj_frameworks.length; i++)
for (var i = 0; i < OBJJ.objj_frameworks.length; i++)
{
var sourceFramework = FILE.path(OBJJ.objj_frameworks[i]).join(framework);
if (FILE.isDirectory(sourceFramework))
{
installFramework(sourceFramework, destinationFrameworks.join(framework), force, symlink);
found = true;
break;
}
}
if (!found)
stream.print("\0yellow(Warning:\0) Couldn't find framework \0cyan(" + framework +"\0)");
for (var i = 0, found = false; !found && i < OBJJ.objj_debug_frameworks.length; i++)
if (!found)
warn("Couldn't find the framework: " + logPath(framework));
for (var i = 0, found = false; i < OBJJ.objj_debug_frameworks.length; i++)
{
var sourceDebugFramework = FILE.path(OBJJ.objj_debug_frameworks[i]).join(framework);
if (FILE.isDirectory(sourceDebugFramework))
{
installFramework(sourceDebugFramework, destinationDebugFrameworks.join(framework), force, symlink);
found = true;
break;
}
}
if (!found)
stream.print("\0yellow(Warning:\0) Couldn't find debug framework \0cyan(" + framework +"\0)");
warn("Couldn't find the debug framework: " + logPath(framework));
});
}
}
@@ -276,32 +284,33 @@ function installFramework(source, dest, force, symlink)
else
{
stream.print("\0yellow(Warning:\0) " + dest + " already exists. Use --force to overwrite.");
warn(logPath(dest) + " already exists. Use --force to overwrite.");
return;
}
}
if (source.exists())
{
stream.print((symlink ? "Symlinking " : "Copying ") + source + " to " + dest);
stream.print((symlink ? "Symlinking " : "Copying ") + logPath(source) + " ==> " + logPath(dest));
if (symlink)
FILE.symlink(source, dest);
else
FILE.copyTree(source, dest);
}
else
stream.print("\0yellow(Warning:\0) "+source+" doesn't exist.");
warn("Cannot find: " + logPath(source));
}
function toIdentifier(/*String*/ aString)
{
var identifier = "",
index = 0,
count = aString.length,
capitalize = NO,
firstRegex = new RegExp("^[a-zA-Z_$]"),
regex = new RegExp("^[a-zA-Z_$0-9]");
for (; index < count; ++index)
for (var index = 0; index < count; ++index)
{
var character = aString.charAt(index);
@@ -332,21 +341,47 @@ function listTemplates()
function listFrameworks()
{
stream.print("Frameworks:");
OBJJ.objj_frameworks.forEach(function(frameworksDirectory)
{
stream.print(" " + frameworksDirectory);
FILE.list(frameworksDirectory).forEach(function(templateName)
{
stream.print(" + " + templateName);
});
});
stream.print("Frameworks (Debug):");
OBJJ.objj_debug_frameworks.forEach(function(frameworksDirectory)
{
stream.print(" " + frameworksDirectory);
FILE.list(frameworksDirectory).forEach(function(frameworkName)
{
stream.print(" + " + frameworkName);
});
});
}
function colorize(message, color)
{
return "\0" + color + "(" + message + "\0)";
}
function logPath(path)
{
return colorize(path, "cyan");
}
function warn(message)
{
stream.print(colorize("Warning: ", "yellow") + message);
}
function fail(message)
{
stream.print(colorize(message, "red"));
OS.exit(1);
}
+22 -14
View File
@@ -38,22 +38,30 @@ function main(args)
function printUsage()
{
print("capp [--version] COMMAND [ARGS]");
print(" --version Print version");
print(" -h, --help Print usage");
print("capp [--version] COMMAND [OPTIONS] [ARGS]");
print(" --version Print version");
print(" -h, --help Print this help");
print("");
print(" gen PATH Generate new project at PATH from a predefined template");
print(" -l Symlink the Frameworks folder to your $CAPP_BUILD or $STEAM_BUILD directory");
print(" -t, --template Specify the template name to use (listed in capp/Resources/Templates)");
print(" -f, --frameworks Create only frameworks, not a full application");
print(" --force Overwrite Frameworks directory if it already exists");
print(" --symlink Create a symlink to the source Frameworks");
print(" --build Source the Frameworks directory files from your $CAPP_BUILD or $STEAM_BUILD directory");
print(" gen [OPTIONS] PATH Generate a new project at PATH from a predefined template");
print(" -l Same as --symlink --build, symlinks $CAPP_BUILD Frameworks into your project");
print(" -t, --template NAME Specify the template name to use (see `capp gen --list-templates`)");
print(" -f, --frameworks Copy/symlink *only* the Frameworks directory to a new or existing project");
print(" -F, --framework NAME Additional framework to copy/symlink (default: Objective-J, Foundation, AppKit)")
print(" --force Overwrite Frameworks directory if it already exists");
print(" --symlink Symlink the source Frameworks directory to the project, don't copy");
print(" --build Copy/symlink the Frameworks directory files from your $CAPP_BUILD directory");
print(" --noconfig Use the default configuration when replacing template variables");
print("");
print(" config ");
print(" name value Set a value for a given key");
print(" -l, --list List all variables set in config file.");
print(" --get name Get the value for a given key");
print(" Without -l or --build, frameworks from your narwhal installation are copied/symlinked")
print("");
print(" gen --list-templates List the template names available for use with `capp gen -t/--template`");
print(" gen --list-frameworks List the framework names available for use with `capp gen -F/--framework`");
print("");
print(" config ...");
print(" KEY VALUE Set a value for a given key");
print(" -l, --list List all variables set in config file.");
print(" --get KEY Get the value for a given key");
print(" --remove KEY Remove the value for a given key");
}
function getFiles(/*File*/ sourceDirectory, /*nil|String|Array<String>*/ extensions, /*Array*/ exclusions)