diff --git a/bin/main.dart b/bin/main.dart index 13fa749..8884173 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -3,7 +3,7 @@ import 'dart:io'; // š¦ Package imports: import 'package:args/args.dart'; -import 'package:colorize/colorize.dart'; +import 'package:tint/tint.dart'; import 'package:yaml/yaml.dart'; // š Project imports: @@ -12,7 +12,7 @@ import 'package:import_sorter/files.dart' as files; import 'package:import_sorter/sort.dart' as sort; void main(List<String> args) { - // Setting arguments + // Parsing arguments final parser = ArgParser(); parser.addFlag('emojis', abbr: 'e', negatable: false); parser.addFlag('ignore-config', negatable: false); @@ -81,26 +81,27 @@ void main(List<String> args) { // Sorting and writing to files var filesFormatted = 0; - var totalImportsSorted = 0; + int totalImportsSorted = 0; for (final filePath in dartFiles.keys) { - final sortedFile = sort.sortImports(dartFiles[filePath], packageName, + final file = dartFiles[filePath]; + if (file == null) { + continue; + } + + final sortedFile = sort.sortImports(file.readAsLinesSync(), packageName, dependencies, emojis, exitOnChange, noComments); - final importsSorted = sortedFile[1]; + final importsSorted = sortedFile.importsChanged; filesFormatted++; totalImportsSorted += importsSorted; - File(filePath).writeAsStringSync(sortedFile[0]); - final dirChunks = filePath.replaceAll("$currentPath/", '').split('/'); + dartFiles[filePath]?.writeAsStringSync(sortedFile.sortedFile); stdout.write( - '${filesFormatted == 1 ? '\n' : ''}ā ${filesFormatted == dartFiles.keys.length ? 'ā' : 'ā£'}āā ā Sorted ${sortedFile[1]} out of ${sortedFile[2]} imports in ${dirChunks.getRange(0, dirChunks.length - 1).join('/')}/'); - color( - dirChunks.last, - back: Styles.BOLD, - front: importsSorted == 0 ? Styles.YELLOW : Styles.GREEN, - isBold: true, - ); + '${filesFormatted == 1 ? '\n' : ''}ā ${filesFormatted == dartFiles.keys.length ? 'ā' : 'ā£'}āā ā Sorted ${sortedFile.importsChanged} out of ${sortedFile.numberOfImports} imports in ${file.path.replaceFirst(currentPath, '')}/'); + String filename = file.path.split(Platform.pathSeparator).last; + filename = importsSorted == 0 ? filename.yellow() : filename.green(); + stdout.write(filename + "\n"); } stopwatch.stop(); stdout.write( diff --git a/lib/files.dart b/lib/files.dart index 0271b69..9b8cca2 100644 --- a/lib/files.dart +++ b/lib/files.dart @@ -2,8 +2,8 @@ import 'dart:io'; /// Get all the dart files for the project and the contents -Map<String, List<String>> dartFiles(String currentPath, List<String> args) { - final dartFiles = <String, List<String>>{}; +Map<String, File> dartFiles(String currentPath, List<String> args) { + final dartFiles = <String, File>{}; final allContents = [ ..._readDir(currentPath, 'lib'), ..._readDir(currentPath, 'bin'), @@ -14,19 +14,21 @@ Map<String, List<String>> dartFiles(String currentPath, List<String> args) { for (final fileOrDir in allContents) { if (fileOrDir is File && fileOrDir.path.endsWith('.dart')) { - dartFiles[fileOrDir.path] = fileOrDir.readAsLinesSync(); + dartFiles[fileOrDir.path] = fileOrDir; } } // If there are only certain files given via args filter the others out var onlyCertainFiles = false; for (final arg in args) { - onlyCertainFiles = arg.endsWith("dart"); + if (!onlyCertainFiles) { + onlyCertainFiles = arg.endsWith("dart"); + } } if (onlyCertainFiles) { final patterns = args.where((arg) => !arg.startsWith("-")); - final filesToRemove = []; + final filesToKeep = <String, File>{}; for (final fileName in dartFiles.keys) { var keep = false; @@ -36,12 +38,11 @@ Map<String, List<String>> dartFiles(String currentPath, List<String> args) { break; } } - if (!keep) { - filesToRemove.add(fileName); + if (keep) { + filesToKeep[fileName] = File(fileName); } } - - filesToRemove.forEach((file) => dartFiles.remove(file)); + return filesToKeep; } return dartFiles; diff --git a/lib/sort.dart b/lib/sort.dart index b186a92..f0c9b2b 100644 --- a/lib/sort.dart +++ b/lib/sort.dart @@ -2,13 +2,13 @@ import 'dart:io'; // š¦ Package imports: -import 'package:colorize/colorize.dart'; +import 'package:tint/tint.dart'; /// Sort the imports /// Returns the sorted file as a string at /// index 0 and the number of sorted imports /// at index 1 -List sortImports( +ImportSortData sortImports( List<String> lines, String package_name, List dependencies, @@ -88,18 +88,14 @@ List sortImports( } } - // If no import return original string of lines + // If no imports return original string of lines if (noImports()) { if (lines.length > 1) { if (lines.last != '') { - return [ - [...lines, ''].join('\n'), - 0, - 0 - ]; + return ImportSortData([...lines, ''].join('\n'), 0, 0); } } - return [lines.join('\n'), 0, 0]; + return ImportSortData(lines.join('\n'), 0, 0); } // Remove spaces @@ -169,26 +165,31 @@ List sortImports( projectImports.length; if (exitIfChanged && original != sortedFile) { stdout.write('\nāāāšØ '); - color( - 'Please run import sorter!', - back: Styles.BOLD, - front: Styles.RED, - isBold: true, - ); + stdout.write('Please run import sorter!'.bold().red()); exit(1); } if (original == sortedFile) { - return [original, 0, numberOfImports]; + return ImportSortData(original, 0, numberOfImports); } - return [ + return ImportSortData( sortedFile, numberOfImports, numberOfImports, - ]; + ); } /// Get the number of times a string contains another /// string int _timesContained(String string, String looking) => string.split(looking).length - 1; + +/// Data to return from a sort +class ImportSortData { + final String sortedFile; + final int importsChanged; + final int numberOfImports; + + const ImportSortData( + this.sortedFile, this.importsChanged, this.numberOfImports); +} diff --git a/pubspec.yaml b/pubspec.yaml index a7fb644..95d73b2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,15 +9,15 @@ authors: - Flutter Community <community@flutter.zone> environment: - sdk: '>=2.7.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dependencies: args: ^2.0.0 - colorize: ^2.0.0 + tint: ^2.0.0 yaml: ^3.1.0 dev_dependencies: - test: ^1.15.5 + test: ^1.16.7 import_sorter: emojis: true diff --git a/test/sort_test.dart b/test/sort_test.dart index d5d1391..97a71c2 100644 --- a/test/sort_test.dart +++ b/test/sort_test.dart @@ -72,7 +72,7 @@ void main(List<String> args) async { emojis, false, noComments, - )[0], + ).sortedFile, '', ); }, @@ -90,7 +90,7 @@ void main(List<String> args) async { noComments, ); expect( - sortedImports[0], + sortedImports.sortedFile, '$dartImportComment$dartImports\n$flutterImportComment$flutterImports\n$packageImportComment$packageImports\n$projectImportComment$projectImports\n', ); }, @@ -106,7 +106,7 @@ void main(List<String> args) async { false, noComments, ); - expect('${sortedImports[0]}\n', '$sampleProgram\n'); + expect('${sortedImports.sortedFile}\n', '$sampleProgram\n'); }, ); test( @@ -121,7 +121,7 @@ void main(List<String> args) async { noComments, ); expect( - sortedImports[0], + sortedImports.sortedFile, '$dartImportComment$dartImports\n$sampleProgram\n', ); }, @@ -138,7 +138,7 @@ void main(List<String> args) async { noComments, ); expect( - sortedImports[0], + sortedImports.sortedFile, '$flutterImportComment$flutterImports\n$sampleProgram\n', ); }, @@ -155,7 +155,7 @@ void main(List<String> args) async { noComments, ); expect( - sortedImports[0], + sortedImports.sortedFile, '$packageImportComment$packageImports\n$sampleProgram\n', ); }, @@ -172,7 +172,7 @@ void main(List<String> args) async { noComments, ); expect( - sortedImports[0], + sortedImports.sortedFile, '$projectImportComment$projectImports\n$sampleProgram\n', ); }, @@ -190,7 +190,7 @@ void main(List<String> args) async { noComments, ); expect( - sortedImports[0], + sortedImports.sortedFile, '$dartImportComment$dartImports\n$flutterImportComment$flutterImports\n$packageImportComment$packageImports\n$projectImportComment$projectImports\n$sampleProgram\n', ); }, @@ -208,7 +208,7 @@ void main(List<String> args) async { noComments, ); expect( - sortedImports[0], + sortedImports.sortedFile, 'library import_sorter;\n\n$dartImportComment$dartImports\n$flutterImportComment$flutterImports\n$packageImportComment$packageImports\n$projectImportComment$projectImports\n$sampleProgram\n', ); },