From 36ddae542d6e5eb4112d5c8e4d84f4548e8d518a Mon Sep 17 00:00:00 2001 From: Vetrichelvan Date: Fri, 12 Jan 2024 18:39:02 +0530 Subject: [PATCH 1/3] feat(update): updated packages, added linting rules and updated github actions --- .github/workflows/contributors.yml | 6 ++++-- .github/workflows/format.yml | 24 ++++++++++++++---------- .github/workflows/test.yml | 14 ++++++++------ analysis_options.yaml | 19 +++++++++++++++++++ bin/main.dart | 6 +++--- example/example_app/lib/main.dart | 4 ---- lib/files.dart | 4 ++-- pubspec.yaml | 10 ++++++---- 8 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 analysis_options.yaml diff --git a/.github/workflows/contributors.yml b/.github/workflows/contributors.yml index fadc734..d70e815 100644 --- a/.github/workflows/contributors.yml +++ b/.github/workflows/contributors.yml @@ -9,8 +9,10 @@ jobs: contributor_list: runs-on: ubuntu-latest steps: - - uses: actions/checkout@master - - uses: cjdenio/contributor_list@master + - name: Checkout Repository + uses: actions/checkout@v4 + - name: Contributor List + uses: cjdenio/contributor_list@master with: commit_message: šŸ“ Update contributors list max_contributors: 10 diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 5ba349f..9c7985e 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -5,24 +5,28 @@ on: - pull_request jobs: - dartfmt: + format: runs-on: ubuntu-latest - container: - image: google/dart:latest steps: - name: Checkout Repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 + - name: Setup Dart SDK + uses: dart-lang/setup-dart@v1 + with: + sdk: stable - name: Run format - run: dartfmt --dry-run --set-exit-if-changed . + run: dart format --set-exit-if-changed . import_sorter: runs-on: ubuntu-latest - container: - image: google/dart:latest steps: - name: Checkout Repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 + - name: Setup Dart SDK + uses: dart-lang/setup-dart@v1 + with: + sdk: stable - name: Run import_sorter run: | - pub get - pub run import_sorter:main --exit-if-changed + dart pub get + dart run import_sorter:main --exit-if-changed diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 76621f2..b6af76b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,12 +8,14 @@ jobs: build: runs-on: ubuntu-latest - container: - image: google/dart:latest - steps: - - uses: actions/checkout@v2 + - name: Checkout Repository + uses: actions/checkout@v4 + - name: Setup Dart SDK + uses: dart-lang/setup-dart@v1 + with: + sdk: stable - name: Install dependencies - run: pub get + run: dart pub get - name: Run tests - run: pub run test + run: dart pub run test diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..cccf1f0 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,19 @@ +include: package:flutter_lints/flutter.yaml + + +analyzer: + errors: + prefer_const_constructors: error + annotate_overrides: error + prefer_single_quotes: error + sort_pub_dependencies: error + invalid_annotation_target: ignore + +linter: + rules: + prefer_double_quotes: false + sort_pub_dependencies: true + annotate_overrides: true + file_names: false + overridden_fields: false + prefer_single_quotes: true \ No newline at end of file diff --git a/bin/main.dart b/bin/main.dart index 38327a2..2752cfc 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -108,18 +108,18 @@ void main(List args) { // Outputting results if (sortedFiles.length > 1) { - stdout.write("\n"); + stdout.write('\n'); } for (int i = 0; i < sortedFiles.length; i++) { final file = dartFiles[sortedFiles[i]]; stdout.write( '${sortedFiles.length == 1 ? '\n' : ''}ā”ƒ ${i == sortedFiles.length - 1 ? 'ā”—' : 'ā”£'}ā”ā” ${success} Sorted imports for ${file?.path.replaceFirst(currentPath, '')}/'); String filename = file!.path.split(Platform.pathSeparator).last; - stdout.write(filename + "\n"); + stdout.write(filename + '\n'); } if (sortedFiles.length == 0) { - stdout.write("\n"); + stdout.write('\n'); } stdout.write( 'ā”—ā”ā” ${success} Sorted ${sortedFiles.length} files in ${stopwatch.elapsed.inSeconds}.${stopwatch.elapsedMilliseconds} seconds\n'); diff --git a/example/example_app/lib/main.dart b/example/example_app/lib/main.dart index dbf957b..a387907 100644 --- a/example/example_app/lib/main.dart +++ b/example/example_app/lib/main.dart @@ -1,7 +1,4 @@ // šŸŽÆ Dart imports: -import 'dart:async'; -import 'dart:io'; -import 'dart:js'; // šŸ¦ Flutter imports: import 'package:flutter/cupertino.dart'; @@ -17,7 +14,6 @@ import 'package:provider/provider.dart'; // šŸŒŽ Project imports: import 'package:example_app/anotherFile2.dart'; -import 'anotherFile.dart'; void main() { runApp(MyApp()); diff --git a/lib/files.dart b/lib/files.dart index a899e1c..304399e 100644 --- a/lib/files.dart +++ b/lib/files.dart @@ -23,12 +23,12 @@ Map dartFiles(String currentPath, List args) { var onlyCertainFiles = false; for (final arg in args) { if (!onlyCertainFiles) { - onlyCertainFiles = arg.endsWith("dart"); + onlyCertainFiles = arg.endsWith('dart'); } } if (onlyCertainFiles) { - final patterns = args.where((arg) => !arg.startsWith("-")); + final patterns = args.where((arg) => !arg.startsWith('-')); final filesToKeep = {}; for (final fileName in dartFiles.keys) { diff --git a/pubspec.yaml b/pubspec.yaml index 5fe7517..e57c5f7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,15 +9,17 @@ authors: - Flutter Community environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=2.12.0 <3.2.4" dependencies: - args: ^2.0.0 - tint: ^2.0.0 - yaml: ^3.1.0 + args: ^2.4.2 + tint: ^2.0.1 + yaml: ^3.1.2 dev_dependencies: test: ^1.16.7 + lints: ^3.0.0 + import_sorter: emojis: true From aafc20636e7f709d2bf0915f3ffcaee70a1df838 Mon Sep 17 00:00:00 2001 From: Vetrichelvan Date: Fri, 12 Jan 2024 18:44:39 +0530 Subject: [PATCH 2/3] feat(update): updated sdk constraints --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index e57c5f7..da7c317 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,7 +9,7 @@ authors: - Flutter Community environment: - sdk: ">=2.12.0 <3.2.4" + sdk: ">=2.12.0 <4.0.0" dependencies: args: ^2.4.2 From 7f17800069a8b85bd1aa54581846bfa34116c131 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 20 Jan 2024 00:44:14 +1300 Subject: [PATCH 3/3] fix: fix #75 --- .github/workflows/format.yml | 7 +- analysis_options.yaml | 2 +- bin/main.dart | 26 ++--- example/example_app/analysis_options.yaml | 4 + example/example_app/lib/main.dart | 122 +--------------------- example/example_app/pubspec.yaml | 9 +- lib/sort.dart | 10 +- pubspec.yaml | 5 +- test/sort_test.dart | 2 - 9 files changed, 37 insertions(+), 150 deletions(-) create mode 100644 example/example_app/analysis_options.yaml diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 9c7985e..1ea4f00 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -10,10 +10,13 @@ jobs: steps: - name: Checkout Repository uses: actions/checkout@v4 - - name: Setup Dart SDK - uses: dart-lang/setup-dart@v1 + - name: Setup Flutter SDK + uses: subosito/flutter-action@v2 with: sdk: stable + - name: Run pub get + run: dart pub get + working-directory: ./example/example_app - name: Run format run: dart format --set-exit-if-changed . diff --git a/analysis_options.yaml b/analysis_options.yaml index cccf1f0..38f100c 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,4 +1,4 @@ -include: package:flutter_lints/flutter.yaml +include: package:lints/recommended.yaml analyzer: diff --git a/bin/main.dart b/bin/main.dart index 2752cfc..ce34ccc 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -30,7 +30,7 @@ void main(List args) { Package name is one factor used to identify project imports Dependencies/dev_dependencies names are used to identify package imports */ - final pubspecYamlFile = File('${currentPath}/pubspec.yaml'); + final pubspecYamlFile = File('$currentPath/pubspec.yaml'); final pubspecYaml = loadYaml(pubspecYamlFile.readAsStringSync()); // Getting all dependencies and project package name @@ -40,13 +40,13 @@ void main(List args) { final stopwatch = Stopwatch(); stopwatch.start(); - final pubspecLockFile = File('${currentPath}/pubspec.lock'); + final pubspecLockFile = File('$currentPath/pubspec.lock'); final pubspecLock = loadYaml(pubspecLockFile.readAsStringSync()); dependencies.addAll(pubspecLock['packages'].keys); var emojis = false; var noComments = false; - final ignored_files = []; + final ignoredFiles = []; // Reading from config in pubspec.yaml safely if (!argResults.contains('--ignore-config')) { @@ -55,7 +55,7 @@ void main(List args) { if (config.containsKey('emojis')) emojis = config['emojis']; if (config.containsKey('comments')) noComments = !config['comments']; if (config.containsKey('ignored_files')) { - ignored_files.addAll(config['ignored_files']); + ignoredFiles.addAll(config['ignored_files']); } } } @@ -69,16 +69,16 @@ void main(List args) { final dartFiles = files.dartFiles(currentPath, args); final containsFlutter = dependencies.contains('flutter'); final containsRegistrant = dartFiles - .containsKey('${currentPath}/lib/generated_plugin_registrant.dart'); + .containsKey('$currentPath/lib/generated_plugin_registrant.dart'); - stdout.writeln('contains flutter: ${containsFlutter}'); - stdout.writeln('contains registrant: ${containsRegistrant}'); + stdout.writeln('contains flutter: $containsFlutter'); + stdout.writeln('contains registrant: $containsRegistrant'); if (containsFlutter && containsRegistrant) { - dartFiles.remove('${currentPath}/lib/generated_plugin_registrant.dart'); + dartFiles.remove('$currentPath/lib/generated_plugin_registrant.dart'); } - for (final pattern in ignored_files) { + for (final pattern in ignoredFiles) { dartFiles.removeWhere((key, _) => RegExp(pattern).hasMatch(key.replaceFirst(currentPath, ''))); } @@ -113,14 +113,14 @@ void main(List args) { for (int i = 0; i < sortedFiles.length; i++) { final file = dartFiles[sortedFiles[i]]; stdout.write( - '${sortedFiles.length == 1 ? '\n' : ''}ā”ƒ ${i == sortedFiles.length - 1 ? 'ā”—' : 'ā”£'}ā”ā” ${success} Sorted imports for ${file?.path.replaceFirst(currentPath, '')}/'); + '${sortedFiles.length == 1 ? '\n' : ''}ā”ƒ ${i == sortedFiles.length - 1 ? 'ā”—' : 'ā”£'}ā”ā” $success Sorted imports for ${file?.path.replaceFirst(currentPath, '')}/'); String filename = file!.path.split(Platform.pathSeparator).last; - stdout.write(filename + '\n'); + stdout.write('$filename\n'); } - if (sortedFiles.length == 0) { + if (sortedFiles.isEmpty) { stdout.write('\n'); } stdout.write( - 'ā”—ā”ā” ${success} Sorted ${sortedFiles.length} files in ${stopwatch.elapsed.inSeconds}.${stopwatch.elapsedMilliseconds} seconds\n'); + 'ā”—ā”ā” $success Sorted ${sortedFiles.length} files in ${stopwatch.elapsed.inSeconds}.${stopwatch.elapsedMilliseconds} seconds\n'); } diff --git a/example/example_app/analysis_options.yaml b/example/example_app/analysis_options.yaml new file mode 100644 index 0000000..95fa91a --- /dev/null +++ b/example/example_app/analysis_options.yaml @@ -0,0 +1,4 @@ +analyzer: + errors: + unused_import: ignore + unnecessary_import: ignore \ No newline at end of file diff --git a/example/example_app/lib/main.dart b/example/example_app/lib/main.dart index a387907..bab5376 100644 --- a/example/example_app/lib/main.dart +++ b/example/example_app/lib/main.dart @@ -1,4 +1,7 @@ // šŸŽÆ Dart imports: +import 'dart:async'; +import 'dart:io'; +import 'dart:js'; // šŸ¦ Flutter imports: import 'package:flutter/cupertino.dart'; @@ -7,126 +10,9 @@ import 'package:flutter/painting.dart'; import 'package:flutter/physics.dart'; // šŸ“¦ Package imports: -import 'package:flutter_gen/gen_l10n/translations.dart'; import 'package:intl/intl.dart'; -import 'package:mdi/mdi.dart'; import 'package:provider/provider.dart'; // šŸŒŽ Project imports: import 'package:example_app/anotherFile2.dart'; - -void main() { - runApp(MyApp()); -} - -class MyApp extends StatelessWidget { - // This widget is the root of your application. - @override - Widget build(BuildContext context) { - return MaterialApp( - title: 'Flutter Demo', - theme: ThemeData( - // This is the theme of your application. - // - // Try running your application with "flutter run". You'll see the - // application has a blue toolbar. Then, without quitting the app, try - // changing the primarySwatch below to Colors.green and then invoke - // "hot reload" (press "r" in the console where you ran "flutter run", - // or simply save your changes to "hot reload" in a Flutter IDE). - // Notice that the counter didn't reset back to zero; the application - // is not restarted. - primarySwatch: Colors.blue, - // This makes the visual density adapt to the platform that you run - // the app on. For desktop platforms, the controls will be smaller and - // closer together (more dense) than on mobile platforms. - visualDensity: VisualDensity.adaptivePlatformDensity, - ), - home: MyHomePage(title: 'Flutter Demo Home Page'), - ); - } -} - -class MyHomePage extends StatefulWidget { - MyHomePage({Key key, this.title}) : super(key: key); - - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - - final String title; - - @override - _MyHomePageState createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - - @override - Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. - return Scaffold( - appBar: AppBar( - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. - title: Text(widget.title), - ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. - child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Invoke "debug painting" (press "p" in the console, choose the - // "Toggle Debug Paint" action from the Flutter Inspector in Android - // Studio, or the "Toggle Debug Paint" command in Visual Studio Code) - // to see the wireframe for each widget. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headline4, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. - ); - } -} +import 'anotherFile.dart'; diff --git a/example/example_app/pubspec.yaml b/example/example_app/pubspec.yaml index 5c076c9..929f3e2 100644 --- a/example/example_app/pubspec.yaml +++ b/example/example_app/pubspec.yaml @@ -18,18 +18,17 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 environment: - sdk: '>=2.7.0 <3.0.0' + sdk: ">=2.12.0 <4.0.0" dependencies: flutter: sdk: flutter - provider: ^4.0.5 - mdi: ^2.0.0 - intl: ^0.16.1 + provider: ^6.1.1 + intl: ^0.19.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.3 + cupertino_icons: ^1.0.6 dev_dependencies: flutter_test: diff --git a/lib/sort.dart b/lib/sort.dart index f23721f..ba44e4e 100644 --- a/lib/sort.dart +++ b/lib/sort.dart @@ -7,7 +7,7 @@ import 'dart:io'; /// at index 1 ImportSortData sortImports( List lines, - String package_name, + String packageName, bool emojis, bool exitIfChanged, bool noComments, { @@ -55,7 +55,7 @@ ImportSortData sortImports( dartImports.add(lines[i]); } else if (lines[i].contains('package:flutter/')) { flutterImports.add(lines[i]); - } else if (lines[i].contains('package:$package_name/')) { + } else if (lines[i].contains('package:$packageName/')) { projectImports.add(lines[i]); } else if (lines[i].contains('package:')) { packageImports.add(lines[i]); @@ -152,11 +152,11 @@ ImportSortData sortImports( sortedLines.add(''); final sortedFile = sortedLines.join('\n'); - final original = lines.join('\n') + '\n'; + final original = '${lines.join('\n')}\n'; if (exitIfChanged && original != sortedFile) { if (filePath != null) { - stdout.writeln( - '\nā”—ā”ā”šŸšØ File ${filePath} does not have its imports sorted.'); + stdout + .writeln('\nā”—ā”ā”šŸšØ File $filePath does not have its imports sorted.'); } exit(1); } diff --git a/pubspec.yaml b/pubspec.yaml index da7c317..2c50b83 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,9 +4,6 @@ version: 4.6.0 repository: "https://github.com/fluttercommunity/import_sorter" homepage: "https://mattglei.ch" maintainer: Matthew Gleich (@gleich) -authors: - - Matthew Gleich - - Flutter Community environment: sdk: ">=2.12.0 <4.0.0" @@ -17,8 +14,8 @@ dependencies: yaml: ^3.1.2 dev_dependencies: - test: ^1.16.7 lints: ^3.0.0 + test: ^1.16.7 import_sorter: diff --git a/test/sort_test.dart b/test/sort_test.dart index 6817409..f0a299c 100644 --- a/test/sort_test.dart +++ b/test/sort_test.dart @@ -20,9 +20,7 @@ import 'package:flutter/painting.dart'; import 'package:flutter/physics.dart'; '''; const packageImports = ''' -import 'package:flutter_gen/gen_l10n/translations.dart'; import 'package:intl/intl.dart'; -import 'package:mdi/mdi.dart'; import 'package:provider/provider.dart'; '''; const projectImports = '''