Update install command
This commit is contained in:
parent
7a5a2d8401
commit
9dca056e52
3 changed files with 29 additions and 45 deletions
|
@ -1,13 +1,15 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:args/command_runner.dart';
|
import 'package:args/command_runner.dart';
|
||||||
import 'package:console/console.dart';
|
|
||||||
import 'package:glob/glob.dart';
|
import 'package:glob/glob.dart';
|
||||||
import 'package:homedir/homedir.dart';
|
import 'package:homedir/homedir.dart';
|
||||||
|
import 'package:io/ansi.dart';
|
||||||
import 'package:mustache4dart/mustache4dart.dart' as mustache;
|
import 'package:mustache4dart/mustache4dart.dart' as mustache;
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
|
import 'package:prompts/prompts.dart' as prompts;
|
||||||
import 'package:pubspec_parse/pubspec_parse.dart';
|
import 'package:pubspec_parse/pubspec_parse.dart';
|
||||||
import 'package:yaml/yaml.dart' as yaml;
|
import 'package:yaml/yaml.dart' as yaml;
|
||||||
|
import '../util.dart';
|
||||||
import 'make/maker.dart';
|
import 'make/maker.dart';
|
||||||
|
|
||||||
class InstallCommand extends Command {
|
class InstallCommand extends Command {
|
||||||
|
@ -46,22 +48,22 @@ class InstallCommand extends Command {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
run() async {
|
run() async {
|
||||||
if (argResults['wipe']) {
|
if (argResults['wipe'] as bool) {
|
||||||
if (await installRepo.exists()) await installRepo.delete(recursive: true);
|
if (await installRepo.exists()) await installRepo.delete(recursive: true);
|
||||||
} else if (argResults['list']) {
|
} else if (argResults['list'] as bool) {
|
||||||
var addons = await list();
|
var addons = await list();
|
||||||
print('${addons.length} add-on(s) installed:');
|
print('${addons.length} add-on(s) installed:');
|
||||||
|
|
||||||
for (var addon in addons) {
|
for (var addon in addons) {
|
||||||
print(' * ${addon.name}@${addon.version}: ${addon.description}');
|
print(' * ${addon.name}@${addon.version}: ${addon.description}');
|
||||||
}
|
}
|
||||||
} else if (argResults['update']) {
|
} else if (argResults['update'] as bool) {
|
||||||
await update();
|
await update();
|
||||||
} else if (argResults.rest.isNotEmpty) {
|
} else if (argResults.rest.isNotEmpty) {
|
||||||
if (!await installRepo.exists())
|
if (!await installRepo.exists())
|
||||||
throw 'No local add-on database exists. Run `angel install --update` first.';
|
throw 'No local add-on database exists. Run `angel install --update` first.';
|
||||||
|
|
||||||
var pubspec = await Pubspec.load(Directory.current);
|
var pubspec = await loadPubspec();
|
||||||
|
|
||||||
for (var packageName in argResults.rest) {
|
for (var packageName in argResults.rest) {
|
||||||
var packageDir =
|
var packageDir =
|
||||||
|
@ -78,23 +80,21 @@ class InstallCommand extends Command {
|
||||||
|
|
||||||
List<Glob> globs = [];
|
List<Glob> globs = [];
|
||||||
|
|
||||||
var projectPubspec = await Pubspec.load(packageDir);
|
var projectPubspec = await loadPubspec(packageDir);
|
||||||
var deps = projectPubspec.dependencies.keys
|
var deps = projectPubspec.dependencies.keys
|
||||||
.map((k) {
|
.map((k) {
|
||||||
var dep = projectPubspec.dependencies[k];
|
var dep = projectPubspec.dependencies[k];
|
||||||
if (dep is HostedReference)
|
if (dep is HostedDependency)
|
||||||
return new MakerDependency(
|
return new MakerDependency(k, dep.version.toString());
|
||||||
k, dep.versionConstraint.toString());
|
return null;
|
||||||
return null;
|
})
|
||||||
})
|
|
||||||
.where((d) => d != null)
|
.where((d) => d != null)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
deps.addAll(projectPubspec.devDependencies.keys.map((k) {
|
deps.addAll(projectPubspec.devDependencies.keys.map((k) {
|
||||||
var dep = projectPubspec.devDependencies[k];
|
var dep = projectPubspec.devDependencies[k];
|
||||||
if (dep is HostedReference)
|
if (dep is HostedDependency)
|
||||||
return new MakerDependency(k, dep.versionConstraint.toString(),
|
return new MakerDependency(k, dep.version.toString(), dev: true);
|
||||||
dev: true);
|
|
||||||
return null;
|
return null;
|
||||||
}).where((d) => d != null));
|
}).where((d) => d != null));
|
||||||
|
|
||||||
|
@ -110,7 +110,8 @@ class InstallCommand extends Command {
|
||||||
|
|
||||||
// Loads globs
|
// Loads globs
|
||||||
if (cfg['templates'] is List) {
|
if (cfg['templates'] is List) {
|
||||||
globs.addAll(cfg['templates'].map((p) => new Glob(p)));
|
globs.addAll(
|
||||||
|
(cfg['templates'] as List).map((p) => new Glob(p.toString())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cfg['values'] is Map) {
|
if (cfg['values'] is Map) {
|
||||||
|
@ -120,25 +121,9 @@ class InstallCommand extends Command {
|
||||||
var desc = val[key]['description'] ?? key;
|
var desc = val[key]['description'] ?? key;
|
||||||
|
|
||||||
if (val[key]['type'] == 'prompt') {
|
if (val[key]['type'] == 'prompt') {
|
||||||
Prompter prompt;
|
values[key] = prompts.get(desc.toString(), defaultsTo: val[key]['default']?.toString());
|
||||||
|
|
||||||
if (val[key]['default'] != null) {
|
|
||||||
prompt = new Prompter('$desc (${val[key]['default']}): ');
|
|
||||||
} else {
|
|
||||||
prompt = new Prompter('$desc: ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (val[key]['default'] != null) {
|
|
||||||
var v = await prompt.prompt();
|
|
||||||
v = v.isNotEmpty ? v : val[key]['default'];
|
|
||||||
values[key] = v;
|
|
||||||
} else
|
|
||||||
values[key] =
|
|
||||||
await prompt.prompt(checker: (s) => s.isNotEmpty);
|
|
||||||
} else if (val[key]['type'] == 'choice') {
|
} else if (val[key]['type'] == 'choice') {
|
||||||
var chooser =
|
values[key] = prompts.choose(desc.toString(), val[key]['choices'] as Iterable);
|
||||||
new Chooser(val[key]['choices'], message: '$desc: ');
|
|
||||||
values[key] = await chooser.choose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -164,10 +149,7 @@ class InstallCommand extends Command {
|
||||||
|
|
||||||
if (!allClear) {
|
if (!allClear) {
|
||||||
print('The file ${entity.absolute.path} already exists.');
|
print('The file ${entity.absolute.path} already exists.');
|
||||||
var p = new Prompter('Overwrite the existing file? [y/N]');
|
allClear = prompts.getBool('Overwrite the existing file?');
|
||||||
var answer = await p.prompt(
|
|
||||||
checker: (s) => s.trim() == 'y' || s.trim() == 'N');
|
|
||||||
allClear = answer == 'y';
|
|
||||||
if (allClear) await targetFile.delete();
|
if (allClear) await targetFile.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,12 +158,12 @@ class InstallCommand extends Command {
|
||||||
var path = prefix.isEmpty ? name : '$prefix/$name';
|
var path = prefix.isEmpty ? name : '$prefix/$name';
|
||||||
|
|
||||||
if (globs.any((g) => g.matches(path))) {
|
if (globs.any((g) => g.matches(path))) {
|
||||||
print('Rendering Mustache template from ${entity.absolute
|
print(
|
||||||
.path} to ${targetFile.absolute.path}...');
|
'Rendering Mustache template from ${entity.absolute.path} to ${targetFile.absolute.path}...');
|
||||||
var contents = await entity.readAsString();
|
var contents = await entity.readAsString();
|
||||||
var renderer = mustache.compile(contents);
|
var renderer = mustache.compile(contents);
|
||||||
var generated = renderer(values);
|
var generated = renderer(values);
|
||||||
await targetFile.writeAsString(generated);
|
await targetFile.writeAsString(generated.toString());
|
||||||
} else {
|
} else {
|
||||||
print(
|
print(
|
||||||
'Copying ${entity.absolute.path} to ${targetFile.absolute.path}...');
|
'Copying ${entity.absolute.path} to ${targetFile.absolute.path}...');
|
||||||
|
@ -216,7 +198,7 @@ class InstallCommand extends Command {
|
||||||
await for (var entity in installRepo.list()) {
|
await for (var entity in installRepo.list()) {
|
||||||
if (entity is Directory) {
|
if (entity is Directory) {
|
||||||
try {
|
try {
|
||||||
repos.add(await Pubspec.load(entity));
|
repos.add(await loadPubspec(entity));
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
// Ignore failures...
|
// Ignore failures...
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,9 @@ final String checkmark = ansiOutputEnabled ? '\u2713' : '[Success]';
|
||||||
|
|
||||||
final String ballot = ansiOutputEnabled ? '\u2717' : '[Failure]';
|
final String ballot = ansiOutputEnabled ? '\u2717' : '[Failure]';
|
||||||
|
|
||||||
Future<Pubspec> loadPubspec() {
|
Future<Pubspec> loadPubspec([Directory directory]) {
|
||||||
var file = new File('pubspec.yaml');
|
directory ??= Directory.current;
|
||||||
|
var file = new File.fromUri(directory.uri.resolve('pubspec.yaml'));
|
||||||
return file
|
return file
|
||||||
.readAsString()
|
.readAsString()
|
||||||
.then((yaml) => new Pubspec.parse(yaml, sourceUrl: file.uri));
|
.then((yaml) => new Pubspec.parse(yaml, sourceUrl: file.uri));
|
||||||
|
@ -18,4 +19,4 @@ Future<Pubspec> loadPubspec() {
|
||||||
Future savePubspec(Pubspec pubspec) async {
|
Future savePubspec(Pubspec pubspec) async {
|
||||||
// TODO: Save pubspec for real?
|
// TODO: Save pubspec for real?
|
||||||
//var text = toYamlString(pubspec);
|
//var text = toYamlString(pubspec);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ dependencies:
|
||||||
id: ^1.0.0
|
id: ^1.0.0
|
||||||
io: ^0.3.2
|
io: ^0.3.2
|
||||||
inflection: ^0.4.1
|
inflection: ^0.4.1
|
||||||
|
mustache4dart: ^2.0.0
|
||||||
path: ^1.0.0
|
path: ^1.0.0
|
||||||
prompts: ^1.0.0
|
prompts: ^1.0.0
|
||||||
pubspec_parse: ^0.1.2
|
pubspec_parse: ^0.1.2
|
||||||
|
|
Loading…
Reference in a new issue