Offline downloading

This commit is contained in:
thosakwe 2019-07-17 12:53:11 -04:00
parent ecdfacce27
commit efce4496fc
2 changed files with 75 additions and 26 deletions

View file

@ -23,6 +23,10 @@ class InitCommand extends Command {
InitCommand() { InitCommand() {
argParser argParser
..addFlag('offline',
help:
'Disable online fetching of boilerplates. Also disables `pub-get`.',
negatable: false)
..addFlag('pub-get', defaultsTo: true) ..addFlag('pub-get', defaultsTo: true)
..addOption('project-name', ..addOption('project-name',
abbr: 'n', help: 'The name for this project.'); abbr: 'n', help: 'The name for this project.');
@ -57,7 +61,7 @@ class InitCommand extends Command {
await renamePubspec(projectDir, 'angel', name); await renamePubspec(projectDir, 'angel', name);
await renameDartFiles(projectDir, 'angel', name); await renameDartFiles(projectDir, 'angel', name);
if (argResults['pub-get'] != false) { if (argResults['pub-get'] != false && argResults['offline'] == false) {
print('Now running pub get...'); print('Now running pub get...');
await _pubGet(projectDir); await _pubGet(projectDir);
} }
@ -116,6 +120,8 @@ class InitCommand extends Command {
} }
_cloneRepo(Directory projectDir) async { _cloneRepo(Directory projectDir) async {
Directory boilerplateDir;
try { try {
if (await projectDir.exists()) { if (await projectDir.exists()) {
var shouldDelete = prompts.getBool( var shouldDelete = prompts.getBool(
@ -138,15 +144,29 @@ class InitCommand extends Command {
// Ultimately, we want a clone of every boilerplate locally on the system. // Ultimately, we want a clone of every boilerplate locally on the system.
var boilerplateRootDir = Directory(p.join(angelDir.path, 'boilerplates')); var boilerplateRootDir = Directory(p.join(angelDir.path, 'boilerplates'));
var boilerplateDir = Directory(p.join(boilerplateRootDir.path, var boilerplateBasename = p.basenameWithoutExtension(boilerplate.url);
p.basenameWithoutExtension(boilerplate.url))); if (boilerplate.ref != null) boilerplateBasename += '.${boilerplate.ref}';
boilerplateDir =
Directory(p.join(boilerplateRootDir.path, boilerplateBasename));
await boilerplateRootDir.create(recursive: true); await boilerplateRootDir.create(recursive: true);
var branch = boilerplate.ref ?? 'master';
// If there is no clone existing, clone it. // If there is no clone existing, clone it.
if (!await boilerplateDir.exists()) { if (!await boilerplateDir.exists()) {
if (argResults['offline'] as bool) {
throw Exception(
'--offline was selected, but the "${boilerplate.name}" boilerplate has not yet been downloaded.');
}
print( print(
'Cloning "${boilerplate.name}" boilerplate from "${boilerplate.url}"...'); 'Cloning "${boilerplate.name}" boilerplate from "${boilerplate.url}"...');
var git = await Process.start( Process git;
if (boilerplate.ref == null) {
print(darkGray.wrap(
'\$ git clone --depth 1 ${boilerplate.url} ${boilerplateDir.absolute.path}'));
git = await Process.start(
"git", "git",
[ [
"clone", "clone",
@ -155,28 +175,46 @@ class InitCommand extends Command {
boilerplate.url, boilerplate.url,
boilerplateDir.absolute.path boilerplateDir.absolute.path
], ],
mode: ProcessStartMode.inheritStdio); mode: ProcessStartMode.inheritStdio,
);
} else {
// git clone --single-branch -b branch host:/dir.git
print(darkGray.wrap(
'\$ git clone --depth 1 --single-branch -b ${boilerplate.ref} ${boilerplate.url} ${boilerplateDir.absolute.path}'));
git = await Process.start(
"git",
[
"clone",
"--depth",
"1",
"--single-branch",
"-b",
boilerplate.ref,
boilerplate.url,
boilerplateDir.absolute.path
],
mode: ProcessStartMode.inheritStdio,
);
}
if (await git.exitCode != 0) { if (await git.exitCode != 0) {
throw new Exception("Could not clone repo."); throw new Exception("Could not clone repo.");
} }
} }
// Next, check out the given branch. // Otherwise, pull from git.
var branch = boilerplate.ref ?? 'master'; else if (!(argResults['offline'] as bool)) {
var git = await Process.start("git", ["checkout", 'origin/$branch'], print(darkGray.wrap('\$ git pull origin $branch'));
var git = await Process.start("git", ['pull', 'origin', '$branch'],
mode: ProcessStartMode.inheritStdio, mode: ProcessStartMode.inheritStdio,
workingDirectory: boilerplateDir.absolute.path); workingDirectory: boilerplateDir.absolute.path);
if (await git.exitCode != 0) { if (await git.exitCode != 0) {
throw new Exception("Could not checkout branch $branch."); print(yellow.wrap(
"Update of $branch failed. Attempting to continue with existing contents."));
} }
} else {
// Next, pull from git. print(darkGray.wrap(
git = await Process.start("git", ["pull", 'origin/$branch'], 'Using existing contents of "${boilerplate.name}" boilerplate.'));
mode: ProcessStartMode.inheritStdio,
workingDirectory: boilerplateDir.absolute.path);
if (await git.exitCode != 0) {
throw new Exception(
"Update of $branch failed. Attempting to continue with existing contents.");
} }
// Next, just copy everything into the given directory. // Next, just copy everything into the given directory.
@ -189,6 +227,8 @@ class InitCommand extends Command {
var gitDir = new Directory.fromUri(projectDir.uri.resolve(".git")); var gitDir = new Directory.fromUri(projectDir.uri.resolve(".git"));
if (await gitDir.exists()) await gitDir.delete(recursive: true); if (await gitDir.exists()) await gitDir.delete(recursive: true);
} catch (e) { } catch (e) {
await boilerplateDir.delete(recursive: true).catchError((_) => null);
if (e is! String) { if (e is! String) {
print(red.wrap("$ballot Could not initialize Angel project.")); print(red.wrap("$ballot Could not initialize Angel project."));
} }
@ -198,7 +238,8 @@ class InitCommand extends Command {
_pubGet(Directory projectDir) async { _pubGet(Directory projectDir) async {
var pubPath = resolvePub(); var pubPath = resolvePub();
print('Running pub at "$pubPath"...'); print(darkGray.wrap('Running pub at "$pubPath"...'));
print(darkGray.wrap('\$ $pubPath get'));
var pub = await Process.start(pubPath, ["get"], var pub = await Process.start(pubPath, ["get"],
workingDirectory: projectDir.absolute.path, workingDirectory: projectDir.absolute.path,
mode: ProcessStartMode.inheritStdio); mode: ProcessStartMode.inheritStdio);
@ -209,9 +250,11 @@ class InitCommand extends Command {
Future preBuild(Directory projectDir) async { Future preBuild(Directory projectDir) async {
// Run build // Run build
print('Running `pub run build_runner build`...'); // print('Running `pub run build_runner build`...');
print(darkGray.wrap('\$ pub run build_runner build'));
var build = await Process.start(resolvePub(), ['run', 'build'], var build = await Process.start(
resolvePub(), ['run', 'build_runner', 'build'],
workingDirectory: projectDir.absolute.path, workingDirectory: projectDir.absolute.path,
mode: ProcessStartMode.inheritStdio); mode: ProcessStartMode.inheritStdio);

View file

@ -26,19 +26,25 @@ Future<Pubspec> loadPubspec([Directory directory]) {
// From: https://gist.github.com/tobischw/98dcd2563eec9a2a87bda8299055358a // From: https://gist.github.com/tobischw/98dcd2563eec9a2a87bda8299055358a
Future<void> copyDirectory(Directory source, Directory destination) async { Future<void> copyDirectory(Directory source, Directory destination) async {
// if (!topLevel) stdout.write('\r');
// print(darkGray
// .wrap('Copying dir "${source.path}" -> "${destination.path}..."'));
await for (var entity in source.list(recursive: false)) { await for (var entity in source.list(recursive: false)) {
if (p.basename(entity.path) == '.git') continue;
if (entity is Directory) { if (entity is Directory) {
var newDirectory = var newDirectory =
Directory(p.join(destination.absolute.path, p.basename(entity.path))); Directory(p.join(destination.absolute.path, p.basename(entity.path)));
print('Copying dir "${entity.path}" -> "${newDirectory.path}..."');
await newDirectory.create(); await newDirectory.create();
await copyDirectory(entity.absolute, newDirectory); await copyDirectory(entity.absolute, newDirectory);
} else if (entity is File) { } else if (entity is File) {
var newPath = p.join(destination.path, p.basename(entity.path)); var newPath = p.join(destination.path, p.basename(entity.path));
print('Copying file "${entity.path}" -> "$newPath"'); // print(darkGray.wrap('\rCopying file "${entity.path}" -> "$newPath"'));
await entity.copy(newPath); await entity.copy(newPath);
} }
} }
// print('\rCopied "${source.path}" -> "${destination.path}.');
} }
Future savePubspec(Pubspec pubspec) async { Future savePubspec(Pubspec pubspec) async {