google / grinder.dart

Dart workflows, automated
https://pub.dev/packages/grinder
BSD 3-Clause "New" or "Revised" License
265 stars 37 forks source link

Convert Pub, Dart, Dart2js, Analyzer static methods to constructors and instance methods #204

Open seaneagan opened 9 years ago

seaneagan commented 9 years ago

Using constructors and instance methods would allow this functionality to be:

So:

class Pub {
  Pub({String sdkPath});
  get(...);
  //...
  PubGlobal get global => new PubGlobal(pub: this);
}

class PubGlobal {
  PubGlobal({Pub pub});
  // The rest pretty much stays as is.
}

// Replaces [Dart].
class Vm {
  Vm({String sdkPath});
  run(...);
  //...
}

// Replaces [Dart2js].
class Compiler {
  Compiler({String sdkPath});
  compile(...);
  //...
}

class Analyzer {
  Analyzer.sdk({String sdkPath});
  /// Uses pub by default, or sdk if it matches constraint and saves downloading.
  Analyzer({VersionConstraint constraint});
  analyze(...);
  //...
}

// Calls `dartfmt`.
class Formatter {
  Formatter.sdk({String sdkPath});
  /// Uses pub by default, or sdk if it matches constraint and saves downloading.
  Formatter({VersionConstraint constraint});
  //...
}

Then for a little extra convenience, we could either expose them at the top-level:

final Pub pub = new Pub();
final Vm vm = new Vm();
final Compiler compiler = new Compiler();
final Analyzer analyzer = new Analyzer();
final Formatter formatter = new Formatter();

or as members of a reified SDK (which can also be configured, passed around, mocked, etc.):

final Sdk sdk = new Sdk();

class Sdk {

  final String path;
  Directory get dir => new Directory(path);

  /// Defaults the path using the current [getSdkDir] logic.
  Sdk({this.path});

  Vm get vm => new Vm(sdkPath: path) ;
  Compiler get compiler => new Compiler(sdkPath: path);
  Analyzer get analyzer => new Analyzer.sdk(sdkPath: path);
  Formatter get formatter => new Formatter.sdk(sdkPath: path);
  Pub get pub => new Pub(sdkPath: path);
}

or both:

final Pub pub = sdk.pub;
final Vm vm = sdk.vm;
final Compiler compiler = sdk.compiler;
final Analyzer analyzer = new Analyzer();
final Formatter formatter = new Formatter();

Example usage:

pub.get(...);
pub.global.run(...);
vm.run(...);
compiler.compile(...);
analyzer.analyze(...);
sdk.analyzer.analyze(...);
formatter.format(...);
sdk.formatter.format(...);

This would be a big breaking change, but well worth it IMO.

devoncarew commented 9 years ago

Resolution: yes! but let's try and be conscious of reducing the number of breaking changes.