Skip to content

Commit

Permalink
fix: fix #75
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent committed Jan 19, 2024
1 parent aafc206 commit 7f17800
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 150 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .

Expand Down
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include: package:flutter_lints/flutter.yaml
include: package:lints/recommended.yaml


analyzer:
Expand Down
26 changes: 13 additions & 13 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void main(List<String> 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
Expand All @@ -40,13 +40,13 @@ void main(List<String> 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')) {
Expand All @@ -55,7 +55,7 @@ void main(List<String> 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']);
}
}
}
Expand All @@ -69,16 +69,16 @@ void main(List<String> 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, '')));
}
Expand Down Expand Up @@ -113,14 +113,14 @@ void main(List<String> 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');
}
4 changes: 4 additions & 0 deletions example/example_app/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
analyzer:
errors:
unused_import: ignore
unnecessary_import: ignore
122 changes: 4 additions & 118 deletions example/example_app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// 🎯 Dart imports:
import 'dart:async';
import 'dart:io';
import 'dart:js';

// 🐦 Flutter imports:
import 'package:flutter/cupertino.dart';
Expand All @@ -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<MyHomePage> {
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: <Widget>[
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';
9 changes: 4 additions & 5 deletions example/example_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions lib/sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:io';
/// at index 1
ImportSortData sortImports(
List<String> lines,
String package_name,
String packageName,
bool emojis,
bool exitIfChanged,
bool noComments, {
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 1 addition & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
- Flutter Community <[email protected]>

environment:
sdk: ">=2.12.0 <4.0.0"
Expand All @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions test/sort_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''
Expand Down

0 comments on commit 7f17800

Please sign in to comment.