Skip to content

Commit

Permalink
add --version; add pub global activate
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed Jan 14, 2015
1 parent 9b292d7 commit 13b0b6f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 21 deletions.
18 changes: 13 additions & 5 deletions changelog.md
@@ -1,5 +1,13 @@
# grinder.dart changes

## TODO:
- add method for Pub.global.activate and Pub.global.run
- add an optional `workingDirectory` argument to more methods
- added a `--version` command line flag
- have the version command check to see if there's a newer version of grinder
available
- the dart2js compile tasks now create the output directory if it doesn't exist

## 0.6.5
- added `defaultInit()` and `defaultClean()` methods, for common tasks

Expand Down Expand Up @@ -27,11 +35,11 @@ let you run grinder via:

pub run grinder test

They look for a corresponding grinder script in the `tool` directory (`bin/grind.dart`
looks for `tool/grind.dart` and `bin/grinder.dart` looks for `tool/grinder.dart`).
If they find a corresponding script they run it in a new Dart VM process. This
means that projects will no longer have to have a `grind.sh` script in the root
of each project.
They look for a corresponding grinder script in the `tool` directory
(`bin/grind.dart` looks for `tool/grind.dart` and `bin/grinder.dart` looks for
`tool/grinder.dart`). If they find a corresponding script they run it in a new
Dart VM process. This means that projects will no longer have to have a
`grind.sh` script in the root of each project.

PubTool's build methods now take an optional `workingDirectory` argument.

Expand Down
37 changes: 32 additions & 5 deletions lib/grinder.dart
Expand Up @@ -32,6 +32,7 @@
* usage: dart grinder.dart <options> target1 target2 ...
*
* valid options:
* --version print the version of grinder
* -h, --help show targets but don't build
* -d, --deps display the dependencies of targets
*/
Expand All @@ -41,13 +42,17 @@ export 'grinder_files.dart';
export 'grinder_tools.dart';

import 'dart:async';
import 'dart:convert' show JSON, UTF8;
import 'dart:io';

import 'package:args/args.dart';

final Grinder _grinder = new Grinder();
List<String> _args;

// This version must be updated in tandem with the pubspec version.
const String _APP_VERSION = '0.6.4+1';

/**
* Used to define a method body for a task.
*/
Expand Down Expand Up @@ -85,7 +90,27 @@ Future startGrinder(List<String> args) {
ArgParser parser = _createArgsParser();
ArgResults results = parser.parse(grinderArgs());

if (results['help']) {
if (results['version']) {
const String pubUrl = 'https://pub.dartlang.org/packages/grinder.json';

print('grinder version ${_APP_VERSION}');

HttpClient client = new HttpClient();
return client.getUrl(Uri.parse(pubUrl)).then((HttpClientRequest request) {
return request.close();
}).then((HttpClientResponse response) {
return response.toList();
}).then((List<List> data) {
String str = UTF8.decode(data.reduce((a, b) => a.addAll(b)));
List versions = JSON.decode(str)['versions'];
if (_APP_VERSION != versions.last) {
print("Version ${versions.last} is available! Run `pub global activate"
" grinder` to get the latest.");
} else {
print('grinder is up to date!');
}
}).catchError((e) => null);
} else if (results['help']) {
_printUsage(parser, _grinder);
} else if (results['deps']) {
_printDeps(_grinder);
Expand All @@ -107,6 +132,8 @@ Future startGrinder(List<String> args) {

ArgParser _createArgsParser() {
ArgParser parser = new ArgParser();
parser.addFlag('version', negatable: false,
help: "print the version of grinder");
parser.addFlag('help', abbr: 'h', negatable: false,
help: "show targets but don't build");
parser.addFlag('deps', abbr: 'd', negatable: false,
Expand Down Expand Up @@ -170,9 +197,9 @@ void _printDeps(Grinder grinder) {
*/
class GrinderContext {
/// The [Grinder] instance.
Grinder grinder;
final Grinder grinder;
/// The current running [GrinderTask].
GrinderTask task;
final GrinderTask task;

GrinderContext._(this.grinder, this.task);

Expand All @@ -198,10 +225,10 @@ class GrinderTask {
/// The name of the task.
final String name;
/// The function to execute when starting this task.
TaskFunction taskFunction;
final TaskFunction taskFunction;
/// The list of task dependencies; tasks that must run before this task should
/// execute.
List<String> depends;
final List<String> depends;
/// An optional description of the task.
final String description;

Expand Down
50 changes: 41 additions & 9 deletions lib/grinder_tools.dart
Expand Up @@ -189,17 +189,20 @@ void defaultClean(GrinderContext context) {
* Utility tasks for executing pub commands.
*/
class Pub {
static PubGlobal _global = new PubGlobal._();

/**
* Run `pub get` on the current project. If [force] is true, this will execute
* even if the pubspec.lock file is up-to-date with respect to the
* pubspec.yaml file.
*/
static void get(GrinderContext context, {bool force: false}) {
static void get(GrinderContext context,
{bool force: false, String workingDirectory}) {
FileSet pubspec = new FileSet.fromFile(new File('pubspec.yaml'));
FileSet publock = new FileSet.fromFile(new File('pubspec.lock'));

if (force || !publock.upToDate(pubspec)) {
_run(context, _execName('get'));
_run(context, _execName('get'), workingDirectory: workingDirectory);
}
}

Expand All @@ -208,12 +211,14 @@ class Pub {
* even if the pubspec.lock file is up-to-date with respect to the
* pubspec.yaml file.
*/
static Future getAsync(GrinderContext context, {bool force: false}) {
static Future getAsync(GrinderContext context,
{bool force: false, String workingDirectory}) {
FileSet pubspec = new FileSet.fromFile(new File('pubspec.yaml'));
FileSet publock = new FileSet.fromFile(new File('pubspec.lock'));

if (force || !publock.upToDate(pubspec)) {
return runProcessAsync(context, _execName('pub'), arguments: ['get']);
return runProcessAsync(context, _execName('pub'), arguments: ['get'],
workingDirectory: workingDirectory);
} else {
return new Future.value();
}
Expand All @@ -222,13 +227,16 @@ class Pub {
/**
* Run `pub upgrade` on the current project.
*/
static void upgrade(GrinderContext context) => _run(context, 'upgrade');
static void upgrade(GrinderContext context, {String workingDirectory}) {
_run(context, 'upgrade', workingDirectory: workingDirectory);
}

/**
* Run `pub upgrade` on the current project.
*/
static Future upgradeAsync(GrinderContext context) {
return runProcessAsync(context, _execName('pub'), arguments: ['upgrade']);
static Future upgradeAsync(GrinderContext context, {String workingDirectory}) {
return runProcessAsync(context, _execName('pub'), arguments: ['upgrade'],
workingDirectory: workingDirectory);
}

/**
Expand Down Expand Up @@ -267,8 +275,28 @@ class Pub {

static void version(GrinderContext context) => _run(context, '--version');

static void _run(GrinderContext context, String command) {
runProcess(context, _execName('pub'), arguments: [command]);
PubGlobal get global => _global;

static void _run(GrinderContext context, String command,
{String workingDirectory}) {
runProcess(context, _execName('pub'), arguments: [command],
workingDirectory: workingDirectory);
}
}

class PubGlobal {
PubGlobal._();

void activate(GrinderContext context, String package) {
runProcess(context, _execName('pub'), arguments: ['activate', package]);
}

void run(GrinderContext context, String package,
{List<String> arguments, String workingDirectory}) {
List args = ['run', package];
if (arguments != null) args.addAll(arguments);
runProcess(context, _execName('pub'), arguments: args,
workingDirectory: workingDirectory);
}
}

Expand All @@ -286,6 +314,8 @@ class Dart2js {
if (outDir == null) outDir = sourceFile.parent;
File outFile = joinFile(outDir, ["${fileName(sourceFile)}.js"]);

if (!outDir.existsSync()) outDir.createSync(recursive: true);

List args = [];
if (minify) args.add('--minify');
if (csp) args.add('--csp');
Expand All @@ -304,6 +334,8 @@ class Dart2js {
if (outDir == null) outDir = sourceFile.parent;
File outFile = joinFile(outDir, ["${fileName(sourceFile)}.js"]);

if (!outDir.existsSync()) outDir.createSync(recursive: true);

List args = [];
if (minify) args.add('--minify');
if (csp) args.add('--csp');
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
@@ -1,7 +1,8 @@
name: grinder
# This version must be updated in tandem with the lib/grinder.dart version.
version: 0.6.4+1

description: Grinder - a task based, dependency aware build system.

homepage: https://github.com/google/grinder.dart
author: Devon Carew <devoncarew@google.com>

Expand Down
1 change: 0 additions & 1 deletion test/cli_test.dart
Expand Up @@ -41,4 +41,3 @@ void _clear() => ranTasks.clear();
_fooTask(GrinderContext context) => ranTasks['foo'] = true;

_barTask(GrinderContext context) => ranTasks['bar'] = true;

0 comments on commit 13b0b6f

Please sign in to comment.