Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate conversion of options to arguments #70

Merged
merged 12 commits into from
Jul 16, 2014
115 changes: 64 additions & 51 deletions lib/command_archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,78 @@

var async = require('grunt').util.async;
var grunt = require('grunt');
var ArgUtil = require('flopmang');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new module that I created.


module.exports = function (task, exec, done) {
var argUtil = new ArgUtil(task, [
{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each object contains configuration values that affect how the CLI flags are generated.

// --format=<fmt>
// Format of the resulting archive: tar or zip. If this option is not given, and the output file is specified, the format is inferred from the filename if possible (e.g. writing to "foo.zip" makes the output to be in the zip format). Otherwise the output format is tar.
option: 'format',
defaultValue: null,
useAsFlag: true,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If useAsFlag is true, then the option's label will be included in the args.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff!

useValue: true
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If useValue is true, then the option's value will be included in the args.

},
{
// --prefix=<prefix>/
// Prepend <prefix>/ to each filename in the archive.
option: 'prefix',
defaultValue: null,
useAsFlag: true,
useValue: true
},
{
// --output=<file>
// Write the archive to <file> instead of stdout.
option: 'output',
defaultValue: null,
useAsFlag: true,
useValue: true
},
{
// --remote=<repo>
// Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository.
// Note: It seems that GitHub does not support the remote archiving feature.
option: 'remote',
defaultValue: null,
useAsFlag: true,
useValue: true
},
{
// <tree-ish>
// The tree or commit to produce an archive for.
option: 'treeIsh',
defaultValue: 'master',
useAsFlag: false,
useValue: true,
required: true
},
{
// <path>
// Without an optional path parameter, all files and subdirectories of the current working directory are included in the archive. If one or more paths are specified, only these are included.
option: 'path',
defaultValue: null,
useAsFlag: false,
useValue: true,
customValueFn: function (arg) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Custom function to maintain backwards compatibility

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly needed, but nice.

if (arg.value) {
if (grunt.util.kindOf(arg.value) === 'string') {
// Backwards compatible to <= 0.2.8.
arg.value = [arg.value];
}
return arg.value;
}
return null;
}
}
]);
var options = task.options({
treeIsh: 'master'
});

var args = ['archive'];

var args = ['archive'].concat(argUtil.getArgFlags());
// git archive --format=<format> --prefix=<base_directory>/ treeIsh --output=<output file>

if (!options.treeIsh || options.treeIsh.trim() === '') {
done(new Error('gitarchive requires a treeIsh parameter.'));
return;
}

// --format=<fmt>
// Format of the resulting archive: tar or zip. If this option is not given, and the output file is specified, the format is inferred from the filename if possible (e.g. writing to "foo.zip" makes the output to be in the zip format). Otherwise the output format is tar.
if (options.format && options.format.trim() !== '') {
args.push('--format');
args.push(options.format.trim());
}

// --prefix=<prefix>/
// Prepend <prefix>/ to each filename in the archive.
if (options.prefix && options.prefix.trim() !== '') {
args.push('--prefix');
args.push(options.prefix.trim());
}

// --output=<file>
// Write the archive to <file> instead of stdout.
if (options.output && options.output.trim() !== '') {
args.push('--output');
args.push(options.output.trim());
}

// --remote=<repo>
// Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository.
// Note: It seems that GitHub does not support the remote archiving feature.
if (options.remote && options.remote.trim() !== '') {
args.push('--remote');
args.push(options.remote.trim());
}

// <tree-ish>
// The tree or commit to produce an archive for.
args.push(options.treeIsh.trim());

// <path>
// Without an optional path parameter, all files and subdirectories of the current working directory are included in the archive. If one or more paths are specified, only these are included.
if (options.path) {
if (grunt.util.kindOf(options.path) === 'string') {
// Backwards compatible to <= 0.2.8.
options.path = [options.path];
}

args = args.concat(options.path);
}

// Add callback
args.push(done);

Expand Down
47 changes: 26 additions & 21 deletions lib/command_checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@

var async = require('grunt').util.async;
var grunt = require('grunt');
var ArgUtil = require('flopmang');

module.exports = function (task, exec, done) {
var options = task.options({
branch: null,
create: false,
overwrite: false
});

var args = ['checkout'];

if (!options.branch) {
done(new Error('gitcheckout tasks requires that you specify a "branch" (this can be a commit ID)'));
return;
}

if (options.overwrite) {
args.push('-B');
} else if (options.create) {
args.push('-b');
}


args.push(options.branch);
var argUtil = new ArgUtil(task, [
{
option: 'create',
defaultValue: false,
useAsFlag: true,
useValue: false,
flag: '-b'
},
{
option: 'overwrite',
defaultValue: false,
useAsFlag: true,
useValue: false,
flag: '-B'
},
{
option: 'branch',
defaultValue: undefined,
useAsFlag: false,
useValue: true,
required: true
}
]);

var args = ['checkout'].concat(argUtil.getArgFlags());

// Add callback
args.push(done);
Expand Down
86 changes: 53 additions & 33 deletions lib/command_clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,62 @@

var async = require('grunt').util.async;
var grunt = require('grunt');
var ArgUtil = require('flopmang');

module.exports = function (task, exec, done) {
var options = task.options({
force: true,
dry: false,
quiet: false,
exclude: false,
onlyignoredfiles: false,
nonstandard: false,
directories: false
});

var args = ['clean'];
var argUtil = new ArgUtil(task, [
{
option: 'force',
defaultValue: true,
useAsFlag: true,
useValue: false,
flag: '-f'
},
{
option: 'dry',
defaultValue: false,
useAsFlag: true,
useValue: false,
flag: '-n'
},
{
option: 'quiet',
defaultValue: false,
useAsFlag: true,
useValue: false,
flag: '-q'
},
{
option: 'exclude',
defaultValue: false,
useAsFlag: true,
useValue: true,
flag: '-e'
},
{
option: 'onlyignoredfiles',
defaultValue: false,
useAsFlag: true,
useValue: false,
flag: '-X'
},
{
option: 'nonstandard',
defaultValue: false,
useAsFlag: true,
useValue: false,
flag: '-x'
},
{
option: 'directories',
defaultValue: false,
useAsFlag: true,
useValue: false,
flag: '-d'
},
]);

// Add the options to the command line arguments
if (options.force && options.dry === false) {
args.push('-f');
}
if (options.dry) {
args.push('-n');
}
if (options.quiet) {
args.push('-q');
}
if (options.exclude) {
args.push('-e ' + options.exclude);
}
if (options.onlyignoredfiles) {
args.push('-X');
}
if (options.nonstandard) {
args.push('-x');
}
if (options.directories) {
args.push('-d');
}
var args = ['clean'].concat(argUtil.getArgFlags());

// Add the file paths to the arguments.
task.files.forEach(function (files) {
Expand Down
83 changes: 42 additions & 41 deletions lib/command_clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,50 @@

var async = require('grunt').util.async;
var grunt = require('grunt');
var ArgUtil = require('flopmang');

module.exports = function (task, exec, done) {
var options = task.options({
bare: false,
recursive: false,
branch: false,
repository: false,
directory: false
});

var args = ['clone'];

// repo is the sole required option
if (!options.repository) {
done(new Error('gitclone tasks requires that you specify a "repository"'));
return;
}

if (options.bare) {
args.push('--bare');
}

if (options.recursive) {
args.push('--recursive');
}

if (options.branch && !options.bare) {
args.push('--branch');
args.push(options.branch);
}

if (typeof options.depth !== 'undefined') {
args.push('--depth');
args.push(options.depth);
}

// repo comes after the options
args.push(options.repository);

// final argument is checkout directory (optional)
if (options.directory) {
args.push(options.directory);
}
var argUtil = new ArgUtil(task, [
{
option: 'bare',
defaultValue: false,
useAsFlag: true,
useValue: false
},
{
option: 'recursive',
defaultValue: false,
useAsFlag: true,
useValue: false
},
{
option: 'branch',
defaultValue: false,
useAsFlag: true,
useValue: true
},
{
option: 'depth',
defaultValue: null,
useAsFlag: true,
useValue: true
},
{
option: 'repository',
defaultValue: null,
useAsFlag: false,
useValue: true,
required: true
},
{
option: 'directory',
defaultValue: null,
useAsFlag: false,
useValue: true
}
]);

var args = ['clone'].concat(argUtil.getArgFlags());

// Add callback
args.push(done);
Expand Down
Loading