Skip to content

Commit

Permalink
Add and use an abstraction over system and URL paths
Browse files Browse the repository at this point in the history
  • Loading branch information
evanweible-wf committed Aug 25, 2022
1 parent 7ed1cef commit 1ad8316
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 35 deletions.
46 changes: 19 additions & 27 deletions lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,43 @@ import 'dart:io';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:args/command_runner.dart';
import 'package:dart_dev/dart_dev.dart';
import 'package:dart_dev/src/dart_dev_tool.dart';
import 'package:dart_dev/src/utils/format_tool_builder.dart';
import 'package:dart_dev/src/utils/get_dart_version_comment.dart';
import 'package:dart_dev/src/utils/parse_flag_from_args.dart';
import 'package:io/ansi.dart';
import 'package:io/io.dart' show ExitCode;
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;

import '../utils.dart';
import 'dart_dev_runner.dart';
import 'tools/over_react_format_tool.dart';
import 'utils/assert_dir_is_dart_package.dart';
import 'utils/cached_pubspec.dart';
import 'utils/dart_dev_paths.dart';
import 'utils/dart_tool_cache.dart';
import 'utils/ensure_process_exit.dart';
import 'utils/format_tool_builder.dart';
import 'utils/get_dart_version_comment.dart';
import 'utils/logging.dart';
import 'utils/parse_flag_from_args.dart';

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

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

final _runScript = File(_runScriptPath);

final _configPath = p.join('tool', 'dart_dev', 'config.dart');

final _oldDevDartPath = p.join('tool', 'dev.dart');

final _relativeDevDartPath = p.relative(
p.absolute(_configPath),
from: p.absolute(p.dirname(_runScriptPath)),
);
final _runScript = File(paths.runScript);

Future<void> run(List<String> args) async {
attachLoggerToStdio(args);
final configExists = File(_configPath).existsSync();
final oldDevDartExists = File(_oldDevDartPath).existsSync();

final configExists = File(paths.config).existsSync();
final oldDevDartExists = File(paths.legacyConfig).existsSync();

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

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

generateRunScript();
final process = await Process.start(
Platform.executable, [_runScriptPath, ...args],
Platform.executable, [paths.runScript, ...args],
mode: ProcessStartMode.inheritStdio);
ensureProcessExit(process);
exitCode = await process.exitCode;
Expand All @@ -70,7 +62,7 @@ Future<void> handleFastFormat(List<String> args) async {
assertDirIsDartPackage();

DevTool formatTool;
final configFile = File(_configPath);
final configFile = File(paths.config);
if (configFile.existsSync()) {
final toolBuilder = FormatToolBuilder();
parseString(content: configFile.readAsStringSync())
Expand Down Expand Up @@ -119,11 +111,11 @@ bool get shouldWriteRunScript =>
const _isDartDevNullSafe = false;

String buildDartDevRunScriptContents() {
final hasCustomToolDevDart = File(_configPath).existsSync();
final hasCustomToolDevDart = File(paths.config).existsSync();
// If the config has a dart version comment (e.g., if it opts out of null safety),
// copy it over to the entrypoint so the program is run in that language version.
var dartVersionComment = hasCustomToolDevDart
? getDartVersionComment(File(_configPath).readAsStringSync())
? getDartVersionComment(File(paths.config).readAsStringSync())
: null;
// If dart_dev itself is not null-safe, opt the entrypoint out of null-safety
// so the entrypoint doesn't fail to run in packages that have opted into null-safety.
Expand All @@ -137,7 +129,7 @@ import 'dart:io';
import 'package:dart_dev/src/core_config.dart';
import 'package:dart_dev/src/executable.dart' as executable;
${hasCustomToolDevDart ? "import '$_relativeDevDartPath' as custom_dev;" : ""}
${hasCustomToolDevDart ? "import '${paths.configFromRunScriptForDart}' as custom_dev;" : ""}
void main(List<String> args) async {
await executable.runWithConfig(args,
Expand All @@ -162,7 +154,7 @@ Future<void> runWithConfig(
config = configGetter();
} catch (error) {
stderr
..writeln('Invalid "$_configPath" in ${p.absolute(p.current)}')
..writeln('Invalid "${paths.config}" in ${p.absolute(p.current)}')
..writeln()
..writeln('It should provide a `Map<String, DevTool> config;` getter,'
' but it either does not exist or threw unexpectedly:')
Expand Down
31 changes: 31 additions & 0 deletions lib/src/utils/dart_dev_paths.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:path/path.dart' as p;

/// A collection of paths to files and directories constructed to be compatible
/// with a given [p.Context].
class DartDevPaths {
final p.Context _context;

DartDevPaths({p.Context context}) : _context = context ?? p.context;

String cache([String subPath]) => _context.normalize(
_context.joinAll([..._cacheParts, if (subPath != null) subPath]));

String get _cacheForDart => p.url.joinAll(_cacheParts);

final List<String> _cacheParts = ['.dart_tool', 'dart_dev'];

String get config => _context.joinAll(_configParts);

String get configForDart => p.url.joinAll(_configParts);

final List<String> _configParts = ['tool', 'dart_dev', 'config.dart'];

String get configFromRunScriptForDart => p.url.relative(
p.url.absolute(configForDart),
from: p.url.absolute(_cacheForDart),
);

String get legacyConfig => _context.join('tool', 'dev.dart');

String get runScript => cache('run.dart');
}
10 changes: 2 additions & 8 deletions lib/src/utils/dart_tool_cache.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import 'dart:io';

import 'package:path/path.dart' as p;

const cacheDirPath = '.dart_tool/dart_dev';
import 'package:dart_dev/src/utils/dart_dev_paths.dart';

void createCacheDir({String subPath}) {
var path = cacheDirPath;
if (subPath != null) {
path = p.join(path, subPath);
}
final dir = Directory(path);
final dir = Directory(DartDevPaths().cache(subPath));
if (!dir.existsSync()) {
dir.createSync(recursive: true);
}
Expand Down
73 changes: 73 additions & 0 deletions test/utils/dart_dev_paths_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:dart_dev/src/utils/dart_dev_paths.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

void main() {
group('DartDevPaths', () {
group('posix', () {
DartDevPaths paths;

setUp(() {
paths = DartDevPaths(context: p.posix);
});

test('cache', () {
expect(paths.cache(), '.dart_tool/dart_dev');
});

test('cache with subpath', () {
expect(paths.cache('sub/path'), '.dart_tool/dart_dev/sub/path');
});

test('config', () {
expect(paths.config, 'tool/dart_dev/config.dart');
});

test('configFromRunScriptForDart', () {
expect(paths.configFromRunScriptForDart,
'../../tool/dart_dev/config.dart');
});

test('legacyConfig', () {
expect(paths.legacyConfig, 'tool/dev.dart');
});

test('runScript', () {
expect(paths.runScript, '.dart_tool/dart_dev/run.dart');
});
});

group('windows', () {
DartDevPaths paths;

setUp(() {
paths = DartDevPaths(context: p.windows);
});

test('cache', () {
expect(paths.cache(), r'.dart_tool\dart_dev');
});

test('cache with subpath', () {
expect(paths.cache('sub/path'), r'.dart_tool\dart_dev\sub\path');
});

test('config', () {
expect(paths.config, r'tool\dart_dev\config.dart');
});

test('configFromRunScriptForDart', () {
expect(paths.configFromRunScriptForDart,
r'../../tool/dart_dev/config.dart');
});

test('legacyConfig', () {
expect(paths.legacyConfig, r'tool\dev.dart');
});

test('runScript', () {
expect(paths.runScript, r'.dart_tool\dart_dev\run.dart');
});
});
});
}

0 comments on commit 1ad8316

Please sign in to comment.