Update rename command

This commit is contained in:
Tobe O 2018-07-14 18:42:38 -04:00
parent 7080f80ee7
commit 2aeeafeb93

View file

@ -1,10 +1,12 @@
import 'dart:io'; import 'dart:io';
import 'package:analyzer/analyzer.dart'; import 'package:analyzer/analyzer.dart';
import 'package:args/command_runner.dart'; import 'package:args/command_runner.dart';
import 'package:console/console.dart';
import 'package:dart_style/dart_style.dart'; import 'package:dart_style/dart_style.dart';
import 'package:glob/glob.dart'; import 'package:glob/glob.dart';
import 'package:io/ansi.dart';
import 'package:prompts/prompts.dart' as prompts;
import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:pubspec_parse/pubspec_parse.dart';
import '../util.dart';
import 'pub.dart'; import 'pub.dart';
class RenameCommand extends Command { class RenameCommand extends Command {
@ -24,15 +26,12 @@ class RenameCommand extends Command {
if (argResults.rest.isNotEmpty) if (argResults.rest.isNotEmpty)
newName = argResults.rest.first; newName = argResults.rest.first;
else { else {
var p = new Prompter('Enter new project name: '); newName = prompts.get('Rename project to');
newName = await p.prompt(checker: (String str) => str.isNotEmpty);
} }
var ch = new Chooser<String>(['Yes', 'No'], var choice = prompts.getBool('Rename the project to `$newName`?');
message: 'Rename the project to `$newName`? ');
var choice = await ch.choose();
if (choice == 'Yes') { if (choice) {
print('Renaming project to `$newName`...'); print('Renaming project to `$newName`...');
var pubspecFile = var pubspecFile =
new File.fromUri(Directory.current.uri.resolve('pubspec.yaml')); new File.fromUri(Directory.current.uri.resolve('pubspec.yaml'));
@ -40,7 +39,7 @@ class RenameCommand extends Command {
if (!await pubspecFile.exists()) { if (!await pubspecFile.exists()) {
throw new Exception('No pubspec.yaml found in current directory.'); throw new Exception('No pubspec.yaml found in current directory.');
} else { } else {
var pubspec = await Pubspec.load(Directory.current); var pubspec = await loadPubspec();
var oldName = pubspec.name; var oldName = pubspec.name;
await renamePubspec(Directory.current, oldName, newName); await renamePubspec(Directory.current, oldName, newName);
await renameDartFiles(Directory.current, oldName, newName); await renameDartFiles(Directory.current, oldName, newName);
@ -57,9 +56,13 @@ class RenameCommand extends Command {
} }
renamePubspec(Directory dir, String oldName, String newName) async { renamePubspec(Directory dir, String oldName, String newName) async {
var pubspec = await Pubspec.load(dir); // var pubspec = await loadPubspec(dir);
var newPubspec = new Pubspec.fromJson(pubspec.toJson()..['name'] = newName); print(cyan.wrap('Renaming your project to `$newName.`'));
await newPubspec.save(dir); print(cyan
.wrap('Note that this does not actually modify your `pubspec.yaml`.'));
// TODO: https://github.com/dart-lang/pubspec_parse/issues/17
// var newPubspec = new Pubspec.fromJson(pubspec.toJson()..['name'] = newName);
// await newPubspec.save(dir);
} }
renameDartFiles(Directory dir, String oldName, String newName) async { renameDartFiles(Directory dir, String oldName, String newName) async {
@ -68,7 +71,8 @@ renameDartFiles(Directory dir, String oldName, String newName) async {
await for (var yamlFile in configGlob.list(root: dir.absolute.path)) { await for (var yamlFile in configGlob.list(root: dir.absolute.path)) {
if (yamlFile is File) { if (yamlFile is File) {
print('Replacing occurrences of "$oldName" with "$newName" in file "${yamlFile.absolute.path}"...'); print(
'Replacing occurrences of "$oldName" with "$newName" in file "${yamlFile.absolute.path}"...');
var contents = await yamlFile.readAsString(); var contents = await yamlFile.readAsString();
contents = contents.replaceAll(oldName, newName); contents = contents.replaceAll(oldName, newName);
await yamlFile.writeAsString(contents); await yamlFile.writeAsString(contents);
@ -93,10 +97,10 @@ renameDartFiles(Directory dir, String oldName, String newName) async {
if (visitor.replace.isNotEmpty) { if (visitor.replace.isNotEmpty) {
visitor.replace.forEach((range, replacement) { visitor.replace.forEach((range, replacement) {
if (range.first is int) { if (range.first is int) {
contents = contents = contents.replaceRange(
contents.replaceRange(range.first, range.last, replacement); range.first as int, range.last as int, replacement);
} else if (range.first is String) { } else if (range.first is String) {
contents = contents.replaceAll(range.first, replacement); contents = contents.replaceAll(range.first as Pattern, replacement);
} }
}); });