Skip to content

Commit 1ad8316

Browse files
committed
Add and use an abstraction over system and URL paths
1 parent 7ed1cef commit 1ad8316

File tree

4 files changed

+125
-35
lines changed

4 files changed

+125
-35
lines changed

lib/src/executable.dart

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,43 @@ import 'dart:io';
44
import 'package:analyzer/dart/analysis/utilities.dart';
55
import 'package:args/command_runner.dart';
66
import 'package:dart_dev/dart_dev.dart';
7-
import 'package:dart_dev/src/dart_dev_tool.dart';
8-
import 'package:dart_dev/src/utils/format_tool_builder.dart';
9-
import 'package:dart_dev/src/utils/get_dart_version_comment.dart';
10-
import 'package:dart_dev/src/utils/parse_flag_from_args.dart';
117
import 'package:io/ansi.dart';
128
import 'package:io/io.dart' show ExitCode;
139
import 'package:logging/logging.dart';
1410
import 'package:path/path.dart' as p;
1511

16-
import '../utils.dart';
1712
import 'dart_dev_runner.dart';
1813
import 'tools/over_react_format_tool.dart';
1914
import 'utils/assert_dir_is_dart_package.dart';
15+
import 'utils/cached_pubspec.dart';
16+
import 'utils/dart_dev_paths.dart';
2017
import 'utils/dart_tool_cache.dart';
2118
import 'utils/ensure_process_exit.dart';
19+
import 'utils/format_tool_builder.dart';
20+
import 'utils/get_dart_version_comment.dart';
2221
import 'utils/logging.dart';
22+
import 'utils/parse_flag_from_args.dart';
2323

2424
typedef _ConfigGetter = Map<String, DevTool> Function();
2525

26-
final _runScriptPath = p.join(cacheDirPath, 'run.dart');
26+
final paths = DartDevPaths();
2727

28-
final _runScript = File(_runScriptPath);
29-
30-
final _configPath = p.join('tool', 'dart_dev', 'config.dart');
31-
32-
final _oldDevDartPath = p.join('tool', 'dev.dart');
33-
34-
final _relativeDevDartPath = p.relative(
35-
p.absolute(_configPath),
36-
from: p.absolute(p.dirname(_runScriptPath)),
37-
);
28+
final _runScript = File(paths.runScript);
3829

3930
Future<void> run(List<String> args) async {
4031
attachLoggerToStdio(args);
41-
final configExists = File(_configPath).existsSync();
42-
final oldDevDartExists = File(_oldDevDartPath).existsSync();
32+
33+
final configExists = File(paths.config).existsSync();
34+
final oldDevDartExists = File(paths.legacyConfig).existsSync();
4335

4436
if (!configExists) {
45-
log.fine('No custom `$_configPath` file found; '
37+
log.fine('No custom `${paths.config}` file found; '
4638
'using default config.');
4739
}
4840
if (oldDevDartExists) {
4941
log.warning(yellow.wrap(
50-
'dart_dev v3 now expects configuration to be at `$_configPath`,\n'
51-
'but `$_oldDevDartPath` still exists. View the guide to see how to upgrade:\n'
42+
'dart_dev v3 now expects configuration to be at `${paths.config}`,\n'
43+
'but `${paths.legacyConfig}` still exists. View the guide to see how to upgrade:\n'
5244
'https://github.com/Workiva/dart_dev/blob/master/doc/v3-upgrade-guide.md'));
5345
}
5446

@@ -60,7 +52,7 @@ Future<void> run(List<String> args) async {
6052

6153
generateRunScript();
6254
final process = await Process.start(
63-
Platform.executable, [_runScriptPath, ...args],
55+
Platform.executable, [paths.runScript, ...args],
6456
mode: ProcessStartMode.inheritStdio);
6557
ensureProcessExit(process);
6658
exitCode = await process.exitCode;
@@ -70,7 +62,7 @@ Future<void> handleFastFormat(List<String> args) async {
7062
assertDirIsDartPackage();
7163

7264
DevTool formatTool;
73-
final configFile = File(_configPath);
65+
final configFile = File(paths.config);
7466
if (configFile.existsSync()) {
7567
final toolBuilder = FormatToolBuilder();
7668
parseString(content: configFile.readAsStringSync())
@@ -119,11 +111,11 @@ bool get shouldWriteRunScript =>
119111
const _isDartDevNullSafe = false;
120112

121113
String buildDartDevRunScriptContents() {
122-
final hasCustomToolDevDart = File(_configPath).existsSync();
114+
final hasCustomToolDevDart = File(paths.config).existsSync();
123115
// If the config has a dart version comment (e.g., if it opts out of null safety),
124116
// copy it over to the entrypoint so the program is run in that language version.
125117
var dartVersionComment = hasCustomToolDevDart
126-
? getDartVersionComment(File(_configPath).readAsStringSync())
118+
? getDartVersionComment(File(paths.config).readAsStringSync())
127119
: null;
128120
// If dart_dev itself is not null-safe, opt the entrypoint out of null-safety
129121
// so the entrypoint doesn't fail to run in packages that have opted into null-safety.
@@ -137,7 +129,7 @@ import 'dart:io';
137129
138130
import 'package:dart_dev/src/core_config.dart';
139131
import 'package:dart_dev/src/executable.dart' as executable;
140-
${hasCustomToolDevDart ? "import '$_relativeDevDartPath' as custom_dev;" : ""}
132+
${hasCustomToolDevDart ? "import '${paths.configFromRunScriptForDart}' as custom_dev;" : ""}
141133
142134
void main(List<String> args) async {
143135
await executable.runWithConfig(args,
@@ -162,7 +154,7 @@ Future<void> runWithConfig(
162154
config = configGetter();
163155
} catch (error) {
164156
stderr
165-
..writeln('Invalid "$_configPath" in ${p.absolute(p.current)}')
157+
..writeln('Invalid "${paths.config}" in ${p.absolute(p.current)}')
166158
..writeln()
167159
..writeln('It should provide a `Map<String, DevTool> config;` getter,'
168160
' but it either does not exist or threw unexpectedly:')

lib/src/utils/dart_dev_paths.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:path/path.dart' as p;
2+
3+
/// A collection of paths to files and directories constructed to be compatible
4+
/// with a given [p.Context].
5+
class DartDevPaths {
6+
final p.Context _context;
7+
8+
DartDevPaths({p.Context context}) : _context = context ?? p.context;
9+
10+
String cache([String subPath]) => _context.normalize(
11+
_context.joinAll([..._cacheParts, if (subPath != null) subPath]));
12+
13+
String get _cacheForDart => p.url.joinAll(_cacheParts);
14+
15+
final List<String> _cacheParts = ['.dart_tool', 'dart_dev'];
16+
17+
String get config => _context.joinAll(_configParts);
18+
19+
String get configForDart => p.url.joinAll(_configParts);
20+
21+
final List<String> _configParts = ['tool', 'dart_dev', 'config.dart'];
22+
23+
String get configFromRunScriptForDart => p.url.relative(
24+
p.url.absolute(configForDart),
25+
from: p.url.absolute(_cacheForDart),
26+
);
27+
28+
String get legacyConfig => _context.join('tool', 'dev.dart');
29+
30+
String get runScript => cache('run.dart');
31+
}

lib/src/utils/dart_tool_cache.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import 'dart:io';
22

3-
import 'package:path/path.dart' as p;
4-
5-
const cacheDirPath = '.dart_tool/dart_dev';
3+
import 'package:dart_dev/src/utils/dart_dev_paths.dart';
64

75
void createCacheDir({String subPath}) {
8-
var path = cacheDirPath;
9-
if (subPath != null) {
10-
path = p.join(path, subPath);
11-
}
12-
final dir = Directory(path);
6+
final dir = Directory(DartDevPaths().cache(subPath));
137
if (!dir.existsSync()) {
148
dir.createSync(recursive: true);
159
}

test/utils/dart_dev_paths_test.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import 'package:dart_dev/src/utils/dart_dev_paths.dart';
2+
import 'package:path/path.dart' as p;
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
group('DartDevPaths', () {
7+
group('posix', () {
8+
DartDevPaths paths;
9+
10+
setUp(() {
11+
paths = DartDevPaths(context: p.posix);
12+
});
13+
14+
test('cache', () {
15+
expect(paths.cache(), '.dart_tool/dart_dev');
16+
});
17+
18+
test('cache with subpath', () {
19+
expect(paths.cache('sub/path'), '.dart_tool/dart_dev/sub/path');
20+
});
21+
22+
test('config', () {
23+
expect(paths.config, 'tool/dart_dev/config.dart');
24+
});
25+
26+
test('configFromRunScriptForDart', () {
27+
expect(paths.configFromRunScriptForDart,
28+
'../../tool/dart_dev/config.dart');
29+
});
30+
31+
test('legacyConfig', () {
32+
expect(paths.legacyConfig, 'tool/dev.dart');
33+
});
34+
35+
test('runScript', () {
36+
expect(paths.runScript, '.dart_tool/dart_dev/run.dart');
37+
});
38+
});
39+
40+
group('windows', () {
41+
DartDevPaths paths;
42+
43+
setUp(() {
44+
paths = DartDevPaths(context: p.windows);
45+
});
46+
47+
test('cache', () {
48+
expect(paths.cache(), r'.dart_tool\dart_dev');
49+
});
50+
51+
test('cache with subpath', () {
52+
expect(paths.cache('sub/path'), r'.dart_tool\dart_dev\sub\path');
53+
});
54+
55+
test('config', () {
56+
expect(paths.config, r'tool\dart_dev\config.dart');
57+
});
58+
59+
test('configFromRunScriptForDart', () {
60+
expect(paths.configFromRunScriptForDart,
61+
r'../../tool/dart_dev/config.dart');
62+
});
63+
64+
test('legacyConfig', () {
65+
expect(paths.legacyConfig, r'tool\dev.dart');
66+
});
67+
68+
test('runScript', () {
69+
expect(paths.runScript, r'.dart_tool\dart_dev\run.dart');
70+
});
71+
});
72+
});
73+
}

0 commit comments

Comments
 (0)