Skip to content

Commit

Permalink
feat!: v0.0.1-dev.16 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Dec 2, 2020
1 parent 01a7ec9 commit 05ed3df
Show file tree
Hide file tree
Showing 22 changed files with 414 additions and 196 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ build/
doc/api/

# Temporary Files
.tmp/
.tmp/

# Local Mason Files
.mason/
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 0.0.1-dev.16

- **BREAKING**: `mason make` creates subcommands for all available bricks
- `mason make <BRICK_NAME> -- --var1 value1 --var2 value2` -> `mason make <BRICK_NAME> --var1 value1 --var2 value2`
- feat: `mason make -h` provides a list of available subcommands based on available bricks
- feat: add `mason get` to get all bricks
- feat: support for `mason get --force`
- feat: add local cache all bricks
- feat: improve error handling and messaging
- feat: require brick name consistency between `mason.yaml` and `brick.yaml`
- fix: handle empty or malformed `mason.yaml`
- fix: handle empty or malformed `brick.yaml`

# 0.0.1-dev.15

- feat: add `mason new` to create a new brick
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@ bricks:
path: bricks/hello
```
To get all bricks registered in `mason.yaml` run:

```sh
$ mason get
```

Then you can use `mason make` to generate your first file:

```sh
mason make hello
$ mason make hello
```

### Command Line Variables

Any variables can be passed as command line args.

```sh
$ mason make hello -- --name Felix
$ mason make hello --name Felix
```

### Variable Prompts
Expand Down Expand Up @@ -141,6 +147,7 @@ Global options:
--version Print the current version.
Available commands:
get Gets all bricks.
init Initialize mason in the current directory.
make Generate code using an existing brick template.
new Creates a new brick template.
Expand Down
4 changes: 3 additions & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
Run the following command in the current directory:

```sh
mason make greetings -- --name Felix
mason get # only first time
mason make greeting --name Felix
```

`GREETINGS.md` should be created in the current directory with the following contents:
Expand All @@ -19,6 +20,7 @@ mason make greetings -- --name Felix
Run the following command in the current directory:

```sh
mason get # only first time
mason make todos --json todos.json
```

Expand Down
2 changes: 1 addition & 1 deletion example/mason.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bricks:
path: ../bricks/app_icon
documentation:
path: ../bricks/documentation
greetings:
greeting:
path: ../bricks/greeting
todos:
path: ../bricks/todos
Expand Down
15 changes: 10 additions & 5 deletions lib/src/brick_yaml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ part 'brick_yaml.g.dart';
@JsonSerializable()
class BrickYaml {
/// {@macro mason_yaml}
const BrickYaml(
this.name,
this.description,
this.vars,
);
const BrickYaml(this.name, this.description, this.vars, {this.path});

/// Converts [Map] to [BrickYaml]
factory BrickYaml.fromJson(Map<dynamic, dynamic> json) =>
Expand All @@ -38,4 +34,13 @@ class BrickYaml {

/// List of variables used when templating a brick.
final List<String> vars;

/// Path to the [BrickYaml] file.
final String path;

/// Returns a copy of the current [BrickYaml] with
/// an overridden [path].
BrickYaml copyWith({String path}) {
return BrickYaml(name, description, vars, path: path ?? this.path);
}
}
5 changes: 4 additions & 1 deletion lib/src/brick_yaml.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 123 additions & 11 deletions lib/src/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,148 @@ import 'package:args/command_runner.dart';
import 'package:checked_yaml/checked_yaml.dart';
import 'package:path/path.dart' as p;

import 'brick_yaml.dart';
import 'exception.dart';
import 'logger.dart';
import 'mason_cache.dart';
import 'mason_yaml.dart';

/// {@template brick_not_found_exception}
/// Thrown when a brick registered in the `mason.yaml` cannot be found locally.
/// {@endtemplate}
class BrickNotFoundException extends MasonException {
/// {@macro brick_not_found_exception}
const BrickNotFoundException(String path)
: super('Could not find brick at $path');
}

/// {@template mason_yaml_name_mismatch}
/// Thrown when a brick's name in `mason.yaml` does not match
/// the name in `brick.yaml`.
/// {@endtemplate}
class MasonYamlNameMismatch extends MasonException {
/// {@macro mason_yaml_name_mismatch}
MasonYamlNameMismatch(String message) : super(message);
}

/// {@template mason_yaml_not_found_exception}
/// Thrown when a `mason.yaml` cannot be found locally.
/// {@endtemplate}
class MasonYamlNotFoundException extends MasonException {
/// {@macro mason_yaml_not_found_exception}
const MasonYamlNotFoundException(String message) : super(message);
}

/// {@template mason_yaml_parse_exception}
/// Thrown when a `mason.yaml` cannot be parsed.
/// {@endtemplate}
class MasonYamlParseException extends MasonException {
/// {@macro mason_yaml_parse_exception}
const MasonYamlParseException(String message) : super(message);
}

/// {@template brick_yaml_parse_exception}
/// Thrown when a `brick.yaml` cannot be parsed.
/// {@endtemplate}
class BrickYamlParseException extends MasonException {
/// {@macro brick_yaml_parse_exception}
const BrickYamlParseException(String message) : super(message);
}

/// The base class for all mason executable commands.
abstract class MasonCommand extends Command<int> {
/// [MasonCache] which contains all remote brick templates.
MasonCache get cache => _cache ??= MasonCache();
MasonCache get cache => _cache ??= MasonCache(bricksJson);

MasonCache _cache;

/// Gets the directory containing the nearest `mason.yaml`.
Directory get entryPoint => _entryPoint ??= MasonYaml.findNearest(cwd).parent;
Directory get entryPoint {
if (_entryPoint != null) return _entryPoint;
final nearestMasonYaml = MasonYaml.findNearest(cwd);
if (nearestMasonYaml == null) {
throw const MasonYamlNotFoundException(
'Could not find ${MasonYaml.file}.\nDid you forget to run mason init?',
);
}
return nearestMasonYaml.parent;
}

Directory _entryPoint;

/// Gets the `bricks.json` file for the current [entryPoint].
File get bricksJson => File(p.join(entryPoint.path, '.mason', 'bricks.json'));

/// Gets all [BrickYaml] contents for bricks registered in the `mason.yaml`.
Set<BrickYaml> get bricks {
if (_bricks != null) return _bricks;
final bricks = <BrickYaml>{};
for (final entry in masonYaml.bricks.entries) {
final brick = entry.value;
final dirPath = cache.read(brick.path ?? brick.git.url);
if (dirPath == null) break;
final filePath = brick.path != null
? p.join(dirPath, BrickYaml.file)
: p.join(dirPath, brick.git.path ?? '', BrickYaml.file);
final file = File(filePath);
if (!file.existsSync()) {
throw BrickNotFoundException(filePath);
}
try {
final brickYaml = checkedYamlDecode(
file.readAsStringSync(),
(m) => BrickYaml.fromJson(m),
).copyWith(path: filePath);
if (brickYaml.name != entry.key) {
throw MasonYamlNameMismatch(
'brick name "${brickYaml.name}": '
'doesn\'t match provided name "${entry.key}" in ${MasonYaml.file}.',
);
}
bricks.add(brickYaml);
} on ParsedYamlException catch (e) {
throw BrickYamlParseException(
'Malformed ${BrickYaml.file} at ${file.path}\n${e.message}',
);
}
}
_bricks = bricks;
return _bricks;
}

Set<BrickYaml> _bricks;

/// Gets path to `mason_config.json`.
String get masonConfigPath {
return p.join(entryPoint.path, '.mason_tool', 'mason_config.json');
}

/// Gets the nearest `mason.yaml` file.
File get masonYamlFile => File(p.join(entryPoint.path, MasonYaml.file));
File get masonYamlFile {
final file = File(p.join(entryPoint.path, MasonYaml.file));
if (!file.existsSync()) {
throw const MasonYamlNotFoundException(
'Cannot find ${MasonYaml.file}.\nDid you forget to run mason init?',
);
}
return file;
}

/// Gets the nearest [MasonYaml].
MasonYaml get masonYaml {
if (_masonYaml != null) return _masonYaml;
final masonYamlContent = File(
p.join(entryPoint.path, MasonYaml.file),
).readAsStringSync();
_masonYaml = checkedYamlDecode(
masonYamlContent,
(m) => MasonYaml.fromJson(m),
);
return _masonYaml;
final masonYamlContent = masonYamlFile.readAsStringSync();
try {
_masonYaml = checkedYamlDecode(
masonYamlContent,
(m) => MasonYaml.fromJson(m),
);
return _masonYaml;
} on ParsedYamlException catch (e) {
throw MasonYamlParseException(
'Malformed ${MasonYaml.file} at ${masonYamlFile.path}\n${e.message}',
);
}
}

MasonYaml _masonYaml;
Expand Down
20 changes: 14 additions & 6 deletions lib/src/command_runner.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:io/io.dart';

import 'commands/commands.dart';
import 'exception.dart';
import 'logger.dart';
import 'version.dart';

Expand All @@ -19,10 +22,15 @@ class MasonCommandRunner extends CommandRunner<int> {
negatable: false,
help: 'Print the current version.',
);
addCommand(InitCommand());
addCommand(InstallCommand());
addCommand(MakeCommand());
addCommand(NewCommand());
try {
addCommand(InitCommand());
addCommand(GetCommand());
addCommand(MakeCommand());
addCommand(NewCommand());
} on MasonException catch (e) {
_logger.err(e.message);
exit(ExitCode.usage.code);
}
}

final Logger _logger;
Expand All @@ -36,6 +44,7 @@ class MasonCommandRunner extends CommandRunner<int> {
Future<int> run(Iterable<String> args) async {
try {
_argResults = parse(args);
return await runCommand(_argResults) ?? ExitCode.success.code;
} on FormatException catch (e) {
_logger
..err(e.message)
Expand All @@ -49,7 +58,6 @@ class MasonCommandRunner extends CommandRunner<int> {
..info(usage);
return ExitCode.usage.code;
}
return await runCommand(_argResults) ?? ExitCode.success.code;
}

@override
Expand All @@ -58,6 +66,6 @@ class MasonCommandRunner extends CommandRunner<int> {
_logger.info('mason version: $packageVersion');
return ExitCode.success.code;
}
return await super.runCommand(topLevelResults);
return super.runCommand(topLevelResults);
}
}
2 changes: 1 addition & 1 deletion lib/src/commands/commands.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export 'get.dart';
export 'init.dart';
export 'install.dart';
export 'make.dart';
export 'new.dart';
Loading

0 comments on commit 05ed3df

Please sign in to comment.