From 5fc441825e8f2a2acf2d05c0bcc855b5ff8f35a4 Mon Sep 17 00:00:00 2001 From: thomashii Date: Sun, 18 Jul 2021 12:22:11 +0800 Subject: [PATCH] Fixed init --- .gitignore | 28 ++--------- .vscode/settings.json | 5 ++ CHANGELOG.md | 1 + lib/src/commands/init.dart | 1 + lib/src/commands/rename.dart | 94 +++++++++++++++++++++++++----------- pubspec.yaml | 2 +- test/replace_name.dart | 53 ++++++++++++++++++++ 7 files changed, 131 insertions(+), 53 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 test/replace_name.dart diff --git a/.gitignore b/.gitignore index b0dcebd..3d99e33 100644 --- a/.gitignore +++ b/.gitignore @@ -3,27 +3,7 @@ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 -# User-specific stuff: -.idea/workspace.xml -.idea/tasks.xml -.idea/dictionaries -.idea/vcs.xml -.idea/jsLibraryMappings.xml -# Sensitive or high-churn files: -.idea/dataSources.ids -.idea/dataSources.xml -.idea/dataSources.local.xml -.idea/sqlDataSources.xml -.idea/dynamic.xml -.idea/uiDesigner.xml - -# Gradle: -.idea/gradle.xml -.idea/libraries - -# Mongo Explorer plugin: -.idea/mongoSettings.xml ## File-based project format: *.iws @@ -33,8 +13,6 @@ # IntelliJ /out/ -# mpeltonen/sbt-idea plugin -.idea_modules/ # JIRA plugin atlassian-ide-plugin.xml @@ -77,4 +55,8 @@ pubspec.lock /sample_project/ sample_project/ sample-project -.dart_tool \ No newline at end of file +.dart_tool +.metals + +.vscode +!settings.json \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..32cfc61 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.watcherExclude": { + "**/target": true + } +} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 206c158..1849e70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Fixed NNBD issues * Updated to use `angel3` packages +* Fixed incorrect import for new project ## 3.0.0-beta.2 diff --git a/lib/src/commands/init.dart b/lib/src/commands/init.dart index d79c611..0ea3038 100644 --- a/lib/src/commands/init.dart +++ b/lib/src/commands/init.dart @@ -149,6 +149,7 @@ class InitCommand extends Command { // var boilerplate = basicBoilerplate; print('Choose a project type before continuing:'); + var boilerplate = prompts.choose( 'Choose a project type before continuing', boilerplates) ?? basicBoilerplate; diff --git a/lib/src/commands/rename.dart b/lib/src/commands/rename.dart index a2a61fb..7e2f46f 100644 --- a/lib/src/commands/rename.dart +++ b/lib/src/commands/rename.dart @@ -89,9 +89,8 @@ Future renameDartFiles(Directory dir, String oldName, String newName) async { // Try to replace MongoDB URL // Replace name in config directory - var configGlob = Glob('config/**/*.yaml'); - try { + var configGlob = Glob('config/**/*.yaml'); await for (var yamlFile in configGlob.list(root: dir.absolute.path)) { if (yamlFile is File) { print( @@ -105,23 +104,6 @@ Future renameDartFiles(Directory dir, String oldName, String newName) async { } } catch (_) {} - // Replace name in bin directory - var binGlob = Glob('bin/**/*.dart'); - - try { - await for (var dartFile in binGlob.list(root: dir.absolute.path)) { - if (dartFile is File) { - print( - 'Replacing occurrences of "$oldName" with "$newName" in file "${dartFile.absolute.path}"...'); - if (dartFile is File) { - var contents = (dartFile as File).readAsStringSync(); - contents = contents.replaceAll(oldName, newName); - (dartFile as File).writeAsStringSync(contents); - } - } - } - } catch (_) {} - var entry = File.fromUri(dir.uri.resolve('lib/$oldName.dart')); if (await entry.exists()) { @@ -129,12 +111,58 @@ Future renameDartFiles(Directory dir, String oldName, String newName) async { print('Renaming library file `${entry.absolute.path}`...'); } + // Replace package:oldName/oldName.dart with package:newName/newName.dart + // Replace package:oldName/ with package:newName/ + String updateImport(String content, String oldName, String newName) { + if (!content.startsWith('import')) { + return content; + } + + if (content.contains('package:$oldName/$oldName.dart')) { + return content.replaceFirst( + 'package:$oldName/$oldName.dart', 'package:$newName/$newName.dart'); + } + + if (content.contains('package:$oldName/')) { + return content.replaceFirst('package:$oldName/', 'package:$newName/'); + } + + return content; + } + + // Replace mustache {{oldName}} with newName + String updateMustacheBinding(String content, String oldName, String newName) { + if (content.contains('{{$oldName}}')) { + return content.replaceAll('{{$oldName}}', '$newName'); + } + + return content; + } + + var fmt = DartFormatter(); + await for (FileSystemEntity file in dir.list(recursive: true)) { + if (file is File && file.path.endsWith('.dart')) { + var lineList = await file.readAsLines(); + + if (oldName.isNotEmpty && newName.isNotEmpty) { + var contents = lineList.fold('', (prev, cur) { + var updatedCur = updateImport(cur, oldName, newName); + updatedCur = updateMustacheBinding(updatedCur, oldName, newName); + return prev + '\n' + updatedCur; + }); + await file.writeAsString(fmt.format(contents)); + + print('Updated file `${file.absolute.path}`.'); + } + } + } + + /* Deprecated, Not working var fmt = DartFormatter(); await for (FileSystemEntity file in dir.list(recursive: true)) { if (file is File && file.path.endsWith('.dart')) { var contents = await file.readAsString(); - // TODO: Issue to be fixed: parseCompilationUnit uses Hubbub library which uses discontinued Google front_end library // front_end package. Temporarily commeted out //var ast = parseCompilationUnit(contents); var visitor = RenamingVisitor(oldName, newName); @@ -144,9 +172,9 @@ Future renameDartFiles(Directory dir, String oldName, String newName) async { visitor.replace.forEach((range, replacement) { if (range.first is int) { contents = contents.replaceRange( - range.first as int, range.last as int?, replacement!); + range.first as int, range.last as int?, replacement); } else if (range.first is String) { - contents = contents.replaceAll(range.first as String, replacement!); + contents = contents.replaceAll(range.first as String, replacement); } }); @@ -155,20 +183,21 @@ Future renameDartFiles(Directory dir, String oldName, String newName) async { } } } + */ } class RenamingVisitor extends RecursiveAstVisitor { final String oldName, newName; - final Map replace = {}; + final Map replace = {}; RenamingVisitor(this.oldName, this.newName) { replace[['{{$oldName}}']] = newName; } - String? updateUri(String? uri) { + String updateUri(String uri) { if (uri == 'package:$oldName/$oldName.dart') { return 'package:$newName/$newName.dart'; - } else if (uri!.startsWith('package:$oldName/')) { + } else if (uri.startsWith('package:$oldName/')) { return 'package:$newName/' + uri.replaceFirst('package:$oldName/', ''); } else { return uri; @@ -177,14 +206,21 @@ class RenamingVisitor extends RecursiveAstVisitor { @override void visitExportDirective(ExportDirective ctx) { - var uri = ctx.uri.stringValue, updated = updateUri(uri); - if (uri != updated) replace[[uri]] = updated; + var uri = ctx.uri.stringValue; + if (uri != null) { + var updated = updateUri(uri); + if (uri != updated) replace[[uri]] = updated; + } } @override void visitImportDirective(ImportDirective ctx) { - var uri = ctx.uri.stringValue, updated = updateUri(uri); - if (uri != updated) replace[[uri]] = updated; + var uri = ctx.uri.stringValue; + + if (uri != null) { + var updated = updateUri(uri); + if (uri != updated) replace[[uri]] = updated; + } } @override diff --git a/pubspec.yaml b/pubspec.yaml index 0a55e64..49f0d53 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_cli -version: 3.0.0-beta.2 +version: 3.0.0 description: Command line tools for the Angel3 framework, including scaffolding. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel3-cli diff --git a/test/replace_name.dart b/test/replace_name.dart new file mode 100644 index 0000000..bb88bef --- /dev/null +++ b/test/replace_name.dart @@ -0,0 +1,53 @@ +import 'dart:io'; + +import 'package:dart_style/dart_style.dart'; + +void main() async { + String updateImport(String content, String oldName, String newName) { + if (!content.startsWith('import')) { + return content; + } + + if (content.contains('package:$oldName/$oldName.dart')) { + return content.replaceFirst( + 'package:$oldName/$oldName.dart', 'package:$newName/$newName.dart'); + } + + if (content.contains('package:$oldName/')) { + return content.replaceFirst('package:$oldName/', 'package:$newName/'); + } + + return content; + } + + String updateMustacheBinding(String content, String oldName, String newName) { + if (content.contains('{{$oldName}}')) { + return content.replaceAll('{{$oldName}}', '$newName'); + } + + return content; + } + + var fmt = DartFormatter(); + var dir = Directory('graph'); + await for (FileSystemEntity file in dir.list(recursive: true)) { + if (file is File && file.path.endsWith('.dart')) { + var lineList = await file.readAsLines(); + + var oldName = 'angel'; + var newName = 'graph'; + var replace = {oldName: newName}; + + if (replace.isNotEmpty) { + var contents = lineList.fold('', (prev, cur) { + var updatedCur = updateImport(cur, oldName, newName); + updatedCur = updateMustacheBinding(updatedCur, oldName, newName); + return prev + '\n' + updatedCur; + }); + await file.writeAsString(fmt.format(contents)); + + print('Updated file `${file.absolute.path}`.'); + } + } + } +}