Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 4.0.0 #22

Merged
merged 4 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.0.0

- Add `--no-comments` flag ([#20](https://github.com/fluttercommunity/import_sorter/issues/20))
- Add `--exit-if-changed` flag ([#21](https://github.com/fluttercommunity/import_sorter/issues/21))

## 3.1.0

- Use regex for the `ignored_files` option.
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ import 'package:example_app/anotherFile2.dart';

## 🚀 Installing

Simply add `import_sorter: ^3.1.0` to your `pubspec.yaml`'s `dev_dependencies`
Simply add `import_sorter: ^4.0.0` to your `pubspec.yaml`'s `dev_dependencies`.

## 🏃‍♂️ Running

Once you've installed it simply run `flutter pub run import_sorter:main` (`pub run import_sorter:main` if normal dart application) to format every file dart file in your lib, bin, test, and tests folder! Don't worry if these folders don't exist.

## 💻 Command Line

- Add the `-e` flag to the run command and have emojis added to your imports 😄
- If your using a config in the `pubspec.yaml` you can have the program ignore it by adding `--ignore-config`
- Add the `-e` flag to the run command and have emojis added to your imports 😄.
- If your using a config in the `pubspec.yaml` you can have the program ignore it by adding `--ignore-config`.
- Want to make sure your files are sorted? Add `--exit-if-changed` to make sure the files are sorted. Good for things like CI.
- Have no comments before your imports by adding the `--no-comments` flag.
- Add the `-h` flag if you need any help from the command line!

## 🏗️ Config
Expand All @@ -93,11 +95,12 @@ If you use import_sorter a lot or need to ignore certain files you should really
```yaml
import_sorter:
emojis: true # Optional, defaults to false
comments: false # Optional, defaults to true
ignored_files: # Optional, defaults to []
- \/lib\/
```

If you need another example check the [example app](example/example_app/pubspec.yaml)
If you need another example check the [example app](example/example_app/pubspec.yaml).

## 🙋‍♀️🙋‍♂️ Contributing

Expand Down
33 changes: 14 additions & 19 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,11 @@ import 'package:import_sorter/sort.dart' as sort;
void main(List<String> args) {
// Setting arguments
final parser = ArgParser();
parser.addFlag(
'emojis',
abbr: 'e',
negatable: false,
);
parser.addFlag(
'ignore-config',
negatable: false,
);
parser.addFlag(
'help',
abbr: 'h',
negatable: false,
);
parser.addFlag('emojis', abbr: 'e', negatable: false);
parser.addFlag('ignore-config', negatable: false);
parser.addFlag('help', abbr: 'h', negatable: false);
parser.addFlag('exit-if-changed', negatable: false);
parser.addFlag('no-comments', negatable: false);
final argResults = parser.parse(args).arguments;
if (argResults.contains('-h') || argResults.contains('--help')) {
local_args.outputHelp();
Expand All @@ -54,13 +45,15 @@ void main(List<String> args) {
dependencies.addAll(pubspecLock['packages'].keys);

var emojis = false;
var noComments = false;
final ignored_files = [];

// Reading from config in pubspec.yaml safely
if (!argResults.contains('--ignore-config')) {
if (pubspecYaml.containsKey('import_sorter')) {
final config = pubspecYaml['import_sorter'];
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']);
}
Expand All @@ -69,6 +62,8 @@ void main(List<String> args) {

// Setting values from args
if (!emojis) emojis = argResults.contains('-e');
if (!noComments) noComments = argResults.contains('--no-comments');
final exitOnChange = argResults.contains('--exit-if-changed');

// Getting all the dart files for the project
final dartFiles = files.dartFiles(currentPath);
Expand All @@ -89,12 +84,12 @@ void main(List<String> args) {
stdout.write('\n┏━━🏭 Sorting Files');

// Sorting and writing to files
int filesFormatted = 0;
int importsSorted = 0;
var filesFormatted = 0;
var importsSorted = 0;

for (final String filePath in dartFiles.keys) {
final sortedFile = sort.sortImports(
dartFiles[filePath], packageName, dependencies, emojis);
for (final filePath in dartFiles.keys) {
final sortedFile = sort.sortImports(dartFiles[filePath], packageName,
dependencies, emojis, exitOnChange, noComments);
File(filePath).writeAsStringSync(sortedFile[0]);
importsSorted += sortedFile[1];
filesFormatted++;
Expand Down
2 changes: 1 addition & 1 deletion example/example_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
import_sorter: ^3.1.0
import_sorter: ^4.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
10 changes: 7 additions & 3 deletions lib/args.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ void outputHelp() {

stdout.write(fancyAsciiTitle);
stdout.write('\nFlags:');
stdout.write('\n --emojis, -e Add emojis to import comments');
stdout.write('\n --help, -h Display this help command');
stdout.write('\n --emojis, -e Add emojis to import comments.');
stdout.write('\n --help, -h Display this help command.');
stdout.write(
'\n --ignore-config Ignore configuration in pubspec.yaml (if there is any)\n');
'\n --ignore-config Ignore configuration in pubspec.yaml (if there is any).');
stdout.write(
"\n --exit-if-changed Return an error if any file isn't sorted. Good for CI.");
stdout.write(
"\n --no-comments Don't put any comments before the imports.\n");
exit(0);
}
35 changes: 27 additions & 8 deletions lib/sort.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 🎯 Dart imports:
import 'dart:io';

// 📦 Package imports:
import 'package:colorize/colorize.dart';

/// Sort the imports
/// Returns the sorted file as a string at
/// index 0 and the number of sorted imports
Expand All @@ -7,6 +13,8 @@ List sortImports(
String package_name,
List dependencies,
bool emojis,
bool exitIfChanged,
bool noComments,
) {
String dartImportComment(bool emojis) =>
'//${emojis ? ' 🎯 ' : ' '}Dart imports:';
Expand All @@ -33,7 +41,7 @@ List sortImports(

var isMultiLineString = false;

for (int i = 0; i < lines.length; i++) {
for (var i = 0; i < lines.length; i++) {
// Check if line is in multiline string
if (_timesContained(lines[i], "'''") == 1 ||
_timesContained(lines[i], '"""') == 1) {
Expand Down Expand Up @@ -104,19 +112,19 @@ List sortImports(
sortedLines.add('');
}
if (dartImports.isNotEmpty) {
sortedLines.add(dartImportComment(emojis));
if (!noComments) sortedLines.add(dartImportComment(emojis));
sortedLines.addAll(dartImports);
}
if (flutterImports.isNotEmpty) {
if (dartImports.isNotEmpty) sortedLines.add('');
sortedLines.add(flutterImportComment(emojis));
if (!noComments) sortedLines.add(flutterImportComment(emojis));
sortedLines.addAll(flutterImports);
}
if (packageImports.isNotEmpty) {
if (dartImports.isNotEmpty || flutterImports.isNotEmpty) {
sortedLines.add('');
}
sortedLines.add(packageImportComment(emojis));
if (!noComments) sortedLines.add(packageImportComment(emojis));
sortedLines.addAll(packageImports);
}
if (projectImports.isNotEmpty) {
Expand All @@ -125,14 +133,14 @@ List sortImports(
packageImports.isNotEmpty) {
sortedLines.add('');
}
sortedLines.add(projectImportComment(emojis));
if (!noComments) sortedLines.add(projectImportComment(emojis));
sortedLines.addAll(projectImports);
}

sortedLines.add('');

var addedCode = false;
for (int j = 0; j < afterImportLines.length; j++) {
for (var j = 0; j < afterImportLines.length; j++) {
if (afterImportLines[j] != '') {
sortedLines.add(afterImportLines[j]);
addedCode = true;
Expand All @@ -141,11 +149,22 @@ List sortImports(
sortedLines.add(afterImportLines[j]);
}
}

sortedLines.add('');

final sortedFile = sortedLines.join('\n');
if (exitIfChanged && lines.join('\n') + '\n' != sortedFile) {
stdout.write('\n┗━━🚨 ');
color(
'Please run import sorter!',
back: Styles.BOLD,
front: Styles.RED,
isBold: true,
);
exit(1);
}

return [
sortedLines.join('\n'),
sortedFile,
dartImports.length +
flutterImports.length +
packageImports.length +
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: import_sorter
description: Dart package that automatically sort all your dart imports. Support for any dart project.
version: 3.1.0
version: 4.0.0
repository: 'https://github.com/Matt-Gleich/import_sorter'
homepage: 'https://mattglei.ch'
maintainer: Matthew Gleich (@Matt-Gleich)
Expand Down
Loading