Skip to content

Commit

Permalink
feat: MasonGenerator.fromGitPath (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Jan 5, 2021
1 parent 8ed4676 commit f777697
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.0.1-dev.21

- feat: export `MasonGenerator` and relevant objects to allow `mason` to be consumed as a library
- feat: expose `fromGitPath` on `MasonGenerator`

# 0.0.1-dev.20

- fix: file loop content template variable resolution
Expand Down
5 changes: 5 additions & 0 deletions lib/mason.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
/// mason --help
/// ```
library mason;

export 'src/exception.dart' show MasonException;
export 'src/generator.dart' show MasonGenerator, DirectoryGeneratorTarget;
export 'src/logger.dart' show Logger;
export 'src/mason_yaml.dart' show GitPath;
18 changes: 1 addition & 17 deletions lib/src/commands/get.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:io/io.dart';
import 'package:path/path.dart' as p;

import '../command.dart';
import '../git.dart';
import '../mason_yaml.dart';

/// {@template get_command}
Expand Down Expand Up @@ -51,22 +50,7 @@ class GetCommand extends MasonCommand {
);
}
if (brick.git != null && (cache.read(brick.git.url) == null)) {
final dirName = brick.git.ref != null
? '${brick.git.url}-${brick.git.ref}'
: brick.git.url;
final directory = Directory(p.join(cache.rootDir, 'git', dirName));
if (directory.existsSync()) {
await directory.delete(recursive: true);
}
await directory.create(recursive: true);
await Git.run(['clone', brick.git.url, directory.path]);
if (brick.git.ref != null) {
await Git.run(
['checkout', brick.git.ref],
processWorkingDir: directory.path,
);
}
return cache.write(brick.git.url, directory.path);
await cache.downloadRemoteBrick(brick.git);
}
}
}
15 changes: 15 additions & 0 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io' show Directory, File;

import 'package:checked_yaml/checked_yaml.dart';
import 'package:collection/collection.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as p;

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

Expand Down Expand Up @@ -72,6 +74,19 @@ class MasonGenerator extends Generator {
);
}

/// Factory which creates a [MasonGenerator] based on
/// a [GitPath] for a remote [BrickYaml] file.
static Future<MasonGenerator> fromGitPath(GitPath gitPath) async {
final cache = MasonCache.empty();
final directory = await cache.downloadRemoteBrick(gitPath);
final file = File(p.join(directory, gitPath.path, BrickYaml.file));
final brickYaml = checkedYamlDecode(
file.readAsStringSync(),
(m) => BrickYaml.fromJson(m),
).copyWith(path: file.path);
return MasonGenerator.fromBrickYaml(brickYaml);
}

/// Optional list of variables which will be used to populate
/// the corresponding mustache variables within the template.
final List<String> vars;
Expand Down
24 changes: 24 additions & 0 deletions lib/src/mason_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'dart:io';

import 'package:path/path.dart' as p;

import 'git.dart';
import 'mason_yaml.dart';

/// {@template mason_cache}
/// A local cache for mason bricks.
///
Expand Down Expand Up @@ -61,6 +64,27 @@ class MasonCache {
void write(String remote, String local) {
_cache[remote] = local;
}

/// Downloads remote brick at [gitPath] to local `.brick-cache`
/// and returns the local path to the brick.
Future<String> downloadRemoteBrick(GitPath gitPath) async {
final dirName =
gitPath.ref != null ? '${gitPath.url}-${gitPath.ref}' : gitPath.url;
final directory = Directory(p.join(rootDir, 'git', dirName));
if (directory.existsSync()) {
await directory.delete(recursive: true);
}
await directory.create(recursive: true);
await Git.run(['clone', gitPath.url, directory.path]);
if (gitPath.ref != null) {
await Git.run(
['checkout', gitPath.ref],
processWorkingDir: directory.path,
);
}
write(gitPath.url, directory.path);
return directory.path;
}
}

String _masonCacheDir() {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

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

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: mason
description: >
A Dart template generator which helps teams generate files quickly and consistently.
version: 0.0.1-dev.20
version: 0.0.1-dev.21
homepage: https://github.com/felangel/mason

environment:
Expand Down

0 comments on commit f777697

Please sign in to comment.