2017-02-01 22:56:04 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:analyzer/analyzer.dart';
|
|
|
|
import 'package:args/command_runner.dart';
|
|
|
|
import 'package:console/console.dart';
|
2017-08-04 15:42:07 +00:00
|
|
|
import 'package:dart_style/dart_style.dart';
|
2017-02-01 22:56:04 +00:00
|
|
|
import 'package:pubspec/pubspec.dart';
|
2017-06-16 02:57:35 +00:00
|
|
|
import 'pub.dart';
|
2017-02-01 22:56:04 +00:00
|
|
|
|
|
|
|
class RenameCommand extends Command {
|
|
|
|
@override
|
|
|
|
String get name => 'rename';
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get description => 'Renames the current project.';
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get invocation => '$name <new name>';
|
|
|
|
|
|
|
|
@override
|
|
|
|
run() async {
|
|
|
|
String newName;
|
|
|
|
|
|
|
|
if (argResults.rest.isNotEmpty)
|
|
|
|
newName = argResults.rest.first;
|
|
|
|
else {
|
|
|
|
var p = new Prompter('Enter new project name: ');
|
|
|
|
newName = await p.prompt(checker: (String str) => str.isNotEmpty);
|
|
|
|
}
|
|
|
|
|
|
|
|
var ch = new Chooser<String>(['Yes', 'No'],
|
|
|
|
message: 'Rename the project to `$newName`? ');
|
|
|
|
var choice = await ch.choose();
|
|
|
|
|
|
|
|
if (choice == 'Yes') {
|
|
|
|
print('Renaming project to `$newName`...');
|
|
|
|
var pubspecFile =
|
|
|
|
new File.fromUri(Directory.current.uri.resolve('pubspec.yaml'));
|
|
|
|
|
|
|
|
if (!await pubspecFile.exists()) {
|
|
|
|
throw new Exception('No pubspec.yaml found in current directory.');
|
|
|
|
} else {
|
|
|
|
var pubspec = await PubSpec.load(Directory.current);
|
|
|
|
var oldName = pubspec.name;
|
2017-03-04 21:34:49 +00:00
|
|
|
await renamePubspec(Directory.current, oldName, newName);
|
2017-03-04 04:14:39 +00:00
|
|
|
await renameDartFiles(Directory.current, oldName, newName);
|
2017-02-01 22:56:04 +00:00
|
|
|
print('Now running `pub get`...');
|
2017-08-04 15:42:07 +00:00
|
|
|
var pubPath = resolvePub();
|
|
|
|
print('Pub path: $pubPath');
|
|
|
|
var pub = await Process.start(pubPath, ['get']);
|
2017-02-01 22:56:04 +00:00
|
|
|
stdout.addStream(pub.stdout);
|
|
|
|
stderr.addStream(pub.stderr);
|
|
|
|
await pub.exitCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-04 04:14:39 +00:00
|
|
|
}
|
2017-02-01 22:56:04 +00:00
|
|
|
|
2017-03-04 21:34:49 +00:00
|
|
|
renamePubspec(Directory dir, String oldName, String newName) async {
|
|
|
|
var pubspec = await PubSpec.load(dir);
|
|
|
|
var newPubspec = new PubSpec.fromJson(pubspec.toJson()..['name'] = newName);
|
|
|
|
await newPubspec.save(dir);
|
|
|
|
}
|
|
|
|
|
2017-03-04 04:14:39 +00:00
|
|
|
renameDartFiles(Directory dir, String oldName, String newName) async {
|
2017-03-04 21:34:49 +00:00
|
|
|
// Try to replace MongoDB URL
|
|
|
|
var defaultYaml = new File.fromUri(dir.uri.resolve('config/default.yaml'));
|
|
|
|
|
|
|
|
if (await defaultYaml.exists()) {
|
|
|
|
print('Changing MongoDB URL in file "${defaultYaml.absolute.path}"...');
|
|
|
|
var contents = await defaultYaml.readAsString();
|
|
|
|
contents = contents.replaceAll('mongodb://localhost:27017/$oldName',
|
|
|
|
'mongodb://localhost:27017/$newName');
|
|
|
|
await defaultYaml.writeAsString(contents);
|
|
|
|
}
|
|
|
|
|
2017-03-04 04:14:39 +00:00
|
|
|
var entry = new File.fromUri(dir.uri.resolve('lib/$oldName.dart'));
|
2017-02-01 22:56:04 +00:00
|
|
|
|
2017-03-04 04:14:39 +00:00
|
|
|
if (await entry.exists()) {
|
2017-03-04 21:34:49 +00:00
|
|
|
await entry.rename(dir.uri.resolve('lib/$newName.dart').toFilePath());
|
2017-03-04 04:14:39 +00:00
|
|
|
print('Renaming library file `${entry.absolute.path}`...');
|
|
|
|
}
|
2017-02-01 22:56:04 +00:00
|
|
|
|
2017-08-04 15:42:07 +00:00
|
|
|
var fmt = new DartFormatter();
|
2017-03-04 04:14:39 +00:00
|
|
|
await for (FileSystemEntity file in dir.list(recursive: true)) {
|
|
|
|
if (file is File && file.path.endsWith('.dart')) {
|
|
|
|
var contents = await file.readAsString();
|
|
|
|
var ast = parseCompilationUnit(contents);
|
|
|
|
var visitor = new RenamingVisitor(oldName, newName)
|
|
|
|
..visitCompilationUnit(ast);
|
|
|
|
|
|
|
|
if (visitor.replace.isNotEmpty) {
|
|
|
|
visitor.replace.forEach((range, replacement) {
|
|
|
|
if (range.first is int) {
|
|
|
|
contents =
|
|
|
|
contents.replaceRange(range.first, range.last, replacement);
|
|
|
|
} else if (range.first is String) {
|
|
|
|
contents = contents.replaceAll(range.first, replacement);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-04 15:42:07 +00:00
|
|
|
await file.writeAsString(fmt.format(contents));
|
2017-03-04 04:14:39 +00:00
|
|
|
print('Updated file `${file.absolute.path}`.');
|
2017-02-01 22:56:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RenamingVisitor extends RecursiveAstVisitor {
|
|
|
|
final String oldName, newName;
|
|
|
|
final Map<List, String> replace = {};
|
|
|
|
|
|
|
|
RenamingVisitor(this.oldName, this.newName);
|
|
|
|
|
|
|
|
String updateUri(String uri) {
|
|
|
|
if (uri == 'package:$oldName/$oldName.dart') {
|
|
|
|
return 'package:$newName/$newName.dart';
|
|
|
|
} else if (uri.startsWith('package:$oldName/')) {
|
|
|
|
return 'package:$newName/' + uri.replaceFirst('package:$oldName/', '');
|
|
|
|
} else
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
visitExportDirective(ExportDirective ctx) {
|
|
|
|
var uri = ctx.uri.stringValue, updated = updateUri(uri);
|
|
|
|
if (uri != updated) replace[[uri]] = updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
visitImportDirective(ImportDirective ctx) {
|
|
|
|
var uri = ctx.uri.stringValue, updated = updateUri(uri);
|
|
|
|
if (uri != updated) replace[[uri]] = updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
visitLibraryDirective(LibraryDirective ctx) {
|
|
|
|
var name = ctx.name.name;
|
|
|
|
|
|
|
|
if (name.startsWith(oldName)) {
|
|
|
|
replace[[ctx.offset, ctx.end]] =
|
|
|
|
'library ' + name.replaceFirst(oldName, newName) + ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
visitPartOfDirective(PartOfDirective ctx) {
|
|
|
|
var name = ctx.libraryName.name;
|
|
|
|
|
|
|
|
if (name.startsWith(oldName)) {
|
|
|
|
replace[[ctx.offset, ctx.end]] =
|
|
|
|
'part of ' + name.replaceFirst(oldName, newName) + ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|