Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 5c4d739

Browse files
author
Roman Petrov
authored
feat: implement --sdk-path CLI option to use when running as compiled executable (#430)
* feat: implement --sdk-path CLI option to use when running as Windows EXE * docs: add link to official docs * fix: add "sdk-path" option to "CheckUnusedL10nCommand" * test: add tests for BaseCommand.validateSdkPath and detectSdkPath * chore: fix analyzer issue * fix: use sdk-path in check_unused_l10n.dart * refactor: remove code duplication * fix: remove unneeded override * refactor: address review comment * test: add test for 'findSdkPath` * test: add a test case for "detectSdkPath" * test: fix existing and add new test * docs: don't mention Windows platform * docs: fix changelog entry * refactor: convert "sdkPath" to named argument * docs: don't mention Windows in CHANGELOG.md
1 parent 6bbf52a commit 5c4d739

File tree

17 files changed

+235
-41
lines changed

17 files changed

+235
-41
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* CLI now can be compiled to and used as compiled executable.
6+
37
## 4.5.0
48

59
* feat: add static code diagnostics `avoid-nested-conditional-expressions`, `prefer-correct-identifier-length`, `prefer-correct-type-name`, `prefer-first`, `prefer-last`.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Usage: metrics [arguments...] <directories>
163163
164164
--root-folder=<./> Root folder.
165165
(defaults to current directory)
166+
--sdk-path=<directory-path> Dart SDK directory path. Should be provided only when you run the application as compiled executable(https://dart.dev/tools/dart-compile#exe) and automatic Dart SDK path detection fails.
166167
--exclude=<{/**.g.dart,/**.template.dart}> File paths in Glob syntax to be exclude.
167168
(defaults to "{/**.g.dart,/**.template.dart}")
168169

lib/src/analyzers/lint_analyzer/lint_analyzer.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ class LintAnalyzer {
7070
Future<Iterable<LintFileReport>> runCliAnalysis(
7171
Iterable<String> folders,
7272
String rootFolder,
73-
LintConfig config,
74-
) async {
75-
final collection = createAnalysisContextCollection(folders, rootFolder);
73+
LintConfig config, {
74+
String? sdkPath,
75+
}) async {
76+
final collection =
77+
createAnalysisContextCollection(folders, rootFolder, sdkPath);
7678

7779
final analyzerResult = <LintFileReport>[];
7880

lib/src/analyzers/unused_files_analyzer/unused_files_analyzer.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ class UnusedFilesAnalyzer {
3535
Future<Iterable<UnusedFilesFileReport>> runCliAnalysis(
3636
Iterable<String> folders,
3737
String rootFolder,
38-
UnusedFilesConfig config,
39-
) async {
40-
final collection = createAnalysisContextCollection(folders, rootFolder);
38+
UnusedFilesConfig config, {
39+
String? sdkPath,
40+
}) async {
41+
final collection =
42+
createAnalysisContextCollection(folders, rootFolder, sdkPath);
4143

4244
final unusedFiles = <String>{};
4345

lib/src/analyzers/unused_l10n_analyzer/unused_l10n_analyzer.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ class UnusedL10nAnalyzer {
4040
Future<Iterable<UnusedL10nFileReport>> runCliAnalysis(
4141
Iterable<String> folders,
4242
String rootFolder,
43-
UnusedL10nConfig config,
44-
) async {
45-
final collection = createAnalysisContextCollection(folders, rootFolder);
43+
UnusedL10nConfig config, {
44+
String? sdkPath,
45+
}) async {
46+
final collection =
47+
createAnalysisContextCollection(folders, rootFolder, sdkPath);
4648

4749
final localizationUsages = <ClassElement, Set<String>>{};
4850

lib/src/cli/commands/analyze.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ class AnalyzeCommand extends BaseCommand {
3333
_addFlags();
3434
}
3535

36-
@override
37-
void validateCommand() {
38-
validateRootFolderExist();
39-
validateTargetDirectories();
40-
}
41-
4236
@override
4337
Future<void> runCommand() async {
4438
final parsedArgs = ParsedArguments(
@@ -56,6 +50,7 @@ class AnalyzeCommand extends BaseCommand {
5650
argResults.rest,
5751
argResults[FlagNames.rootFolder] as String,
5852
config,
53+
sdkPath: findSdkPath(),
5954
);
6055

6156
await _analyzer
@@ -96,8 +91,7 @@ class AnalyzeCommand extends BaseCommand {
9691
void _addFlags() {
9792
_usesReporterOption();
9893
_usesMetricsThresholdOptions();
99-
usesRootFolderOption();
100-
usesExcludeOption();
94+
addCommonFlags();
10195
_usesExitOption();
10296
}
10397

lib/src/cli/commands/base_command.dart

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:path/path.dart';
99

1010
import '../exceptions/arguments_validation_exceptions.dart';
1111
import '../models/flag_names.dart';
12+
import '../utils/detect_sdk_path.dart';
1213

1314
abstract class BaseCommand extends Command<void> {
1415
@override
@@ -26,12 +27,15 @@ abstract class BaseCommand extends Command<void> {
2627
@override
2728
Future<void> run() => _verifyThenRunCommand();
2829

29-
@protected
30-
void validateCommand();
31-
3230
@protected
3331
Future<void> runCommand();
3432

33+
void validateCommand() {
34+
validateRootFolderExist();
35+
validateSdkPath();
36+
validateTargetDirectories();
37+
}
38+
3539
void usesRootFolderOption() {
3640
argParser
3741
..addSeparator('')
@@ -43,6 +47,15 @@ abstract class BaseCommand extends Command<void> {
4347
);
4448
}
4549

50+
void usesSdkPathOption() {
51+
argParser.addOption(
52+
FlagNames.sdkPath,
53+
help:
54+
'Dart SDK directory path. Should be provided only when you run the application as compiled executable(https://dart.dev/tools/dart-compile#exe) and automatic Dart SDK path detection fails.',
55+
valueHelp: 'directory-path',
56+
);
57+
}
58+
4659
void usesExcludeOption() {
4760
argParser.addOption(
4861
FlagNames.exclude,
@@ -62,6 +75,16 @@ abstract class BaseCommand extends Command<void> {
6275
}
6376
}
6477

78+
void validateSdkPath() {
79+
final sdkPath = argResults[FlagNames.sdkPath] as String?;
80+
if (sdkPath != null && !Directory(sdkPath).existsSync()) {
81+
final _exceptionMessage =
82+
'Dart SDK path $sdkPath does not exist or not a directory.';
83+
84+
throw InvalidArgumentException(_exceptionMessage);
85+
}
86+
}
87+
6588
void validateTargetDirectories() {
6689
if (argResults.rest.isEmpty) {
6790
const _exceptionMessage =
@@ -83,6 +106,20 @@ abstract class BaseCommand extends Command<void> {
83106
}
84107
}
85108

109+
void addCommonFlags() {
110+
usesRootFolderOption();
111+
usesSdkPathOption();
112+
usesExcludeOption();
113+
}
114+
115+
String? findSdkPath() =>
116+
argResults[FlagNames.sdkPath] as String? ??
117+
detectSdkPath(
118+
Platform.executable,
119+
Platform.environment,
120+
platformIsWindows: Platform.isWindows,
121+
);
122+
86123
Future<void> _verifyThenRunCommand() async {
87124
try {
88125
validateCommand();

lib/src/cli/commands/check_unused_files.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ class CheckUnusedFilesCommand extends BaseCommand {
2424
_addFlags();
2525
}
2626

27-
@override
28-
void validateCommand() {
29-
validateRootFolderExist();
30-
validateTargetDirectories();
31-
}
32-
3327
@override
3428
Future<void> runCommand() async {
3529
final rootFolder = argResults[FlagNames.rootFolder] as String;
@@ -43,6 +37,7 @@ class CheckUnusedFilesCommand extends BaseCommand {
4337
folders,
4438
rootFolder,
4539
config,
40+
sdkPath: findSdkPath(),
4641
);
4742

4843
await _analyzer
@@ -55,8 +50,7 @@ class CheckUnusedFilesCommand extends BaseCommand {
5550

5651
void _addFlags() {
5752
_usesReporterOption();
58-
usesRootFolderOption();
59-
usesExcludeOption();
53+
addCommonFlags();
6054
}
6155

6256
void _usesReporterOption() {

lib/src/cli/commands/check_unused_l10n.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ class CheckUnusedL10nCommand extends BaseCommand {
2424
_addFlags();
2525
}
2626

27-
@override
28-
void validateCommand() {
29-
validateRootFolderExist();
30-
validateTargetDirectories();
31-
}
32-
3327
@override
3428
Future<void> runCommand() async {
3529
final rootFolder = argResults[FlagNames.rootFolder] as String;
@@ -48,6 +42,7 @@ class CheckUnusedL10nCommand extends BaseCommand {
4842
folders,
4943
rootFolder,
5044
config,
45+
sdkPath: findSdkPath(),
5146
);
5247

5348
return _analyzer
@@ -61,8 +56,7 @@ class CheckUnusedL10nCommand extends BaseCommand {
6156
void _addFlags() {
6257
_usesL10nClassPatternOption();
6358
_usesReporterOption();
64-
usesRootFolderOption();
65-
usesExcludeOption();
59+
addCommonFlags();
6660
}
6761

6862
void _usesReporterOption() {

lib/src/cli/models/flag_names.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class FlagNames {
99
static const reporter = 'reporter';
1010
static const exclude = 'exclude';
1111
static const rootFolder = 'root-folder';
12+
static const sdkPath = 'sdk-path';
1213

1314
static const consoleReporter = ConsoleReporter.id;
1415
static const consoleVerboseReporter = ConsoleReporter.verboseId;

0 commit comments

Comments
 (0)