Skip to content

Commit

Permalink
feature: added support for dry runs
Browse files Browse the repository at this point in the history
  • Loading branch information
oppahansi committed Dec 23, 2023
1 parent 100fb7a commit d67eb8f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 2 deletions.
Binary file added bin/better_imports.exe
Binary file not shown.
5 changes: 5 additions & 0 deletions lib/src/arg_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final argParser = ArgParser()
defaultsTo: false,
negatable: false,
)
..addFlag(
Constants.dryRunFlag,
defaultsTo: false,
negatable: false,
)
..addOption(
Constants.cfgPathOption,
mandatory: false,
Expand Down
16 changes: 16 additions & 0 deletions lib/src/cfg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Cfg {
late bool silent;
late bool trace;
late bool relative;
late bool dryRun;
late List<String> folders;
late List<String> files;
late List<String> ignoreFiles;
Expand Down Expand Up @@ -48,6 +49,7 @@ class Cfg {
silent = false;
trace = false;
relative = false;
dryRun = false;

folders = <String>[
"lib",
Expand Down Expand Up @@ -90,6 +92,7 @@ class Cfg {
_setSilent();
_setTrace();
_setRelative();
_setDryRun();

_setFolders();

Expand Down Expand Up @@ -251,6 +254,19 @@ class Cfg {
}
}

void _setDryRun() {
log.fine("┠─ Setting dry run..");

if (_biYamlSection != null &&
_biYamlSection![Constants.dryRunKey] != null) {
dryRun = _biYamlSection![Constants.dryRunKey];
}

if (_argResults.wasParsed(Constants.dryRunFlag)) {
dryRun = _argResults[Constants.dryRunFlag];
}
}

void _setFolders() {
log.fine("┠─ Setting folders..");

Expand Down
7 changes: 6 additions & 1 deletion lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Constants {

static const betterImports = "better_imports";

static const dryRunKey = "dry_run";

static const helpFlag = "help";
static const helpFlagAbbr = "h";

Expand All @@ -33,6 +35,8 @@ class Constants {

static const traceFlag = "trace";

static const dryRunFlag = "dry-run";

static const foldersOption = "folders";

static const filesOption = "files";
Expand Down Expand Up @@ -77,7 +81,8 @@ class Constants {
--silent -s Disables results output in console.
--relative Converts all project package imports to relative project imports.
--no-comments Removes comments from import types / sections.
--trace
--trace Prints extended logs to console.
--dry-run Prints the result of the sort without writing it to the file.
OPTIONS
Expand Down
2 changes: 1 addition & 1 deletion lib/src/import_sorter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Sorter {
for (var path in _collectorResult.filteredPaths) {
var sortedResult = _sortFile(path);

if (sortedResult.changed) {
if (sortedResult.changed && !_cfg.dryRun) {
sortedResult.file.writeAsStringSync(sortedResult.formattedContent);
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/sort_cmd.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,9 @@ class SortCmd {
"$success Sorted $sortedCount out of ${sorted.length} files in ${stopwatch.elapsed.inMilliseconds} ms\n");

log.fine("Printed results.");

if (cfg.dryRun) {
log.info("Dry run. No files were changed.");
}
}
}
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ better_imports:
# Flag to use to log everything happening to console
trace: false

# Flag to use to run a dry run without changing files
dry_run: false

# Folder names used for collecting dart files
folders:
- lib
Expand Down
3 changes: 3 additions & 0 deletions test/cfg_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void main() {
expect(cfg.silent, false);
expect(cfg.relative, false);
expect(cfg.trace, false);
expect(cfg.dryRun, false);
expect(cfg.folders, [
"lib",
"bin",
Expand Down Expand Up @@ -66,6 +67,7 @@ void main() {
"-s",
"--relative",
"--trace",
"--dry-run",
"--project-name",
"argsProjectName",
"--folders",
Expand All @@ -88,6 +90,7 @@ void main() {
expect(cfg.silent, true);
expect(cfg.relative, true);
expect(cfg.trace, true);
expect(cfg.dryRun, true);
expect(cfg.folders, [
"lib/cmds",
"lib/collectors",
Expand Down

0 comments on commit d67eb8f

Please sign in to comment.