Skip to content

Commit

Permalink
Merge pull request #398 from Workiva/auto-switch-to-replacement-comma…
Browse files Browse the repository at this point in the history
…nds-for-analyze-and-format

Auto-switch analyze and format commands when on certain Dart SDKs
  • Loading branch information
rmconsole5-wk authored Jan 31, 2023
2 parents f72aeed + 1e0fbd0 commit 902dd07
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dart_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
sdk: [ 2.13.4, stable, beta, dev ]
sdk: [ 2.13.4, 2.18.7, stable, beta ]
steps:
- uses: actions/checkout@v2
- uses: dart-lang/[email protected]
Expand Down
11 changes: 11 additions & 0 deletions .pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Dockerfile
skynet.yaml
.dart_tool/
.idea/
.packages
.pub/
build/
/doc/api/
/pubspec.lock
test/
tool/
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [3.9.2](https://github.com/Workiva/dart_dev/compare/3.9.1...3.9.2)

- Automatically use `dart analyze` instead of `dartanalyzer` when on Dart SDK
v2.18 or higher (`dartanalyzer` was removed in this version).
- Automatically use `dart format` instead of `dartfmt` when on Dart SDK v2.15 or
higher (`dartfmt` was removed in this version).

## [3.9.1](https://github.com/Workiva/dart_dev/compare/3.9.0...3.9.1)

- Fixes to be compatible with Windows.
Expand Down
4 changes: 4 additions & 0 deletions lib/src/tools/analyze_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:logging/logging.dart';
import '../dart_dev_tool.dart';
import '../utils/arg_results_utils.dart';
import '../utils/assert_no_positional_args_nor_args_after_separator.dart';
import '../utils/dart_semver_version.dart';
import '../utils/executables.dart' as exe;
import '../utils/logging.dart';
import '../utils/process_declaration.dart';
Expand Down Expand Up @@ -75,6 +76,9 @@ class AnalyzeTool extends DevTool {
@override
FutureOr<int> run([DevToolExecutionContext context]) {
useDartAnalyze ??= false;
if (!dartVersionHasDartanalyzer) {
useDartAnalyze = true;
}
return runProcessAndEnsureExit(
buildProcess(context ?? DevToolExecutionContext(),
configuredAnalyzerArgs: analyzerArgs,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/tools/format_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import '../../utils.dart';
import '../dart_dev_tool.dart';
import '../utils/arg_results_utils.dart';
import '../utils/assert_no_positional_args_nor_args_after_separator.dart';
import '../utils/dart_semver_version.dart';
import '../utils/executables.dart' as exe;
import '../utils/logging.dart';
import '../utils/organize_directives/organize_directives_in_paths.dart';
Expand Down Expand Up @@ -108,6 +109,9 @@ class FormatTool extends DevTool {
@override
FutureOr<int> run([DevToolExecutionContext context]) async {
context ??= DevToolExecutionContext();
if (formatter == Formatter.dartfmt && !dartVersionHasDartfmt) {
formatter = Formatter.dartFormat;
}
final formatExecution = buildExecution(
context,
configuredFormatterArgs: formatterArgs,
Expand Down
13 changes: 13 additions & 0 deletions lib/src/utils/dart_semver_version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'dart:io';

import 'package:pub_semver/pub_semver.dart';

final versionPattern = RegExp(r'(\d+.\d+.\d+)');

Version get dartSemverVersion =>
Version.parse(versionPattern.firstMatch(Platform.version).group(1));

bool get dartVersionHasDartanalyzer =>
dartSemverVersion < Version.parse('2.18.0');

bool get dartVersionHasDartfmt => dartSemverVersion < Version.parse('2.15.0');
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
sdk: ">=2.11.0 <3.0.0"

dependencies:
analyzer: '>=1.0.0 <3.0.0'
analyzer: '>=1.0.0 <6.0.0'
args: ^2.0.0
async: ^2.3.0
build_runner: '>=1.0.0 <3.0.0'
Expand Down
8 changes: 7 additions & 1 deletion test/functional.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ Future<TestProcess> runDevToolFunctionalTest(
.where((f) => p.basename(f.path) == 'pubspec.yaml')
.map((f) => f.absolute);
final pathDepPattern = RegExp(r'path: (.*)');

for (final pubspec in pubspecs) {
final updated =
pubspec.readAsStringSync().replaceAllMapped(pathDepPattern, (match) {
final relDepPath = match.group(1);
final relPubspecPath = p.relative(pubspec.path, from: d.sandbox);
final absPath = p.absolute(p.normalize(
var absPath = p.absolute(p.normalize(
p.join(templateDir.path, relPubspecPath, relDepPath, '..')));
// Since pubspec paths should always be posix style or dart analyze
// complains, switch to forward slashes on windows
if (Platform.isWindows) {
absPath = absPath.replaceAll('\\', '/');
}
return 'path: $absPath';
});
pubspec.writeAsStringSync(updated);
Expand Down

0 comments on commit 902dd07

Please sign in to comment.