Skip to content

Commit

Permalink
Pass FileSystem+Uri around to avoid assumptions in codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mkustermann committed Dec 19, 2024
1 parent 1ee0111 commit 0d88fd6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 31 deletions.
29 changes: 14 additions & 15 deletions pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ class NativeAssetsBuildRunner {
return await runUnderDirectoriesLock(
_fileSystem,
[
_fileSystem.directory(config.outputDirectoryShared).parent,
_fileSystem.directory(config.outputDirectory).parent,
_fileSystem.directory(config.outputDirectoryShared).parent.uri,
_fileSystem.directory(config.outputDirectory).parent.uri,
],
timeout: singleHookTimeout,
logger: logger,
Expand All @@ -346,14 +346,12 @@ class NativeAssetsBuildRunner {

final buildOutputFile =
_fileSystem.file(config.outputDirectory.resolve(hook.outputName));
final dependenciesHashFile = _fileSystem.file(
config.outputDirectory
.resolve('../dependencies.dependencies_hash_file.json'),
);
final dependenciesHashFile = config.outputDirectory
.resolve('../dependencies.dependencies_hash_file.json');
final dependenciesHashes =
DependenciesHashFile(_fileSystem, file: dependenciesHashFile);
DependenciesHashFile(_fileSystem, fileUri: dependenciesHashFile);
final lastModifiedCutoffTime = DateTime.now();
if (buildOutputFile.existsSync() && dependenciesHashFile.existsSync()) {
if (buildOutputFile.existsSync() && await dependenciesHashes.exists()) {
late final HookOutput output;
try {
output = _readHookOutputFromUri(hook, buildOutputFile);
Expand Down Expand Up @@ -398,8 +396,8 @@ ${e.message}
environment,
);
if (result == null) {
if (await dependenciesHashFile.exists()) {
await dependenciesHashFile.delete();
if (await dependenciesHashes.exists()) {
await dependenciesHashes.delete();
}
return null;
} else {
Expand Down Expand Up @@ -468,6 +466,7 @@ ${e.message}
if (resources != null) resources.toFilePath(),
];
final result = await runProcess(
filesystem: _fileSystem,
workingDirectory: workingDirectory,
executable: dartExecutable,
arguments: arguments,
Expand Down Expand Up @@ -572,14 +571,13 @@ ${e.message}
final depFile = _fileSystem.file(
outputDirectory.resolve('../hook.dill.d'),
);
final dependenciesHashFile = _fileSystem.file(
outputDirectory.resolve('../hook.dependencies_hash_file.json'),
);
final dependenciesHashFile =
outputDirectory.resolve('../hook.dependencies_hash_file.json');
final dependenciesHashes =
DependenciesHashFile(_fileSystem, file: dependenciesHashFile);
DependenciesHashFile(_fileSystem, fileUri: dependenciesHashFile);
final lastModifiedCutoffTime = DateTime.now();
var mustCompile = false;
if (!await dependenciesHashFile.exists()) {
if (!await dependenciesHashes.exists()) {
mustCompile = true;
} else {
final outdatedDependency = await dependenciesHashes
Expand Down Expand Up @@ -642,6 +640,7 @@ ${e.message}
scriptUri.toFilePath(),
];
final compileResult = await runProcess(
filesystem: _fileSystem,
workingDirectory: workingDirectory,
executable: dartExecutable,
arguments: compileArguments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ import '../utils/uri.dart';
class DependenciesHashFile {
DependenciesHashFile(
this._fileSystem, {
required this.file,
required this.fileUri,
});

final FileSystem _fileSystem;
final File file;
final Uri fileUri;
FileSystemHashes _hashes = FileSystemHashes();

List<Uri> get fileSystemEntities => _hashes.files.map((e) => e.path).toList();

Future<bool> exists() async => await _fileSystem.file(fileUri).exists();
Future<void> delete() async => await _fileSystem.file(fileUri).delete();

Future<void> _readFile() async {
final file = _fileSystem.file(fileUri);
if (!await file.exists()) {
_hashes = FileSystemHashes();
return;
Expand Down Expand Up @@ -75,7 +79,8 @@ class DependenciesHashFile {
return modifiedAfterTimeStamp;
}

Future<void> _persist() => file.writeAsString(json.encode(_hashes.toJson()));
Future<void> _persist() =>
_fileSystem.file(fileUri).writeAsString(json.encode(_hashes.toJson()));

/// Reads the file with hashes and reports if there is an outdated file,
/// directory or environment variable.
Expand Down
10 changes: 5 additions & 5 deletions pkgs/native_assets_builder/lib/src/locking/locking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:logging/logging.dart';

Future<T> runUnderDirectoriesLock<T>(
FileSystem fileSystem,
List<Directory> directories,
List<Uri> directories,
Future<T> Function() callback, {
Duration? timeout,
Logger? logger,
Expand Down Expand Up @@ -46,13 +46,13 @@ Future<T> runUnderDirectoriesLock<T>(
/// also streams error messages.
Future<T> runUnderDirectoryLock<T>(
FileSystem fileSystem,
Directory directory,
Uri directory,
Future<T> Function() callback, {
Duration? timeout,
Logger? logger,
}) async {
const lockFileName = '.lock';
final lockFile = _fileInDir(fileSystem, directory, lockFileName);
final lockFile = _fileInDir(fileSystem.directory(directory), lockFileName);
return _runUnderFileLock(
lockFile,
callback,
Expand All @@ -61,11 +61,11 @@ Future<T> runUnderDirectoryLock<T>(
);
}

File _fileInDir(FileSystem fileSystem, Directory path, String filename) {
File _fileInDir(Directory path, String filename) {
final dirPath = path.path;
var separator = Platform.pathSeparator;
if (dirPath.endsWith(separator)) separator = '';
return fileSystem.file('$dirPath$separator$filename');
return path.fileSystem.file('$dirPath$separator$filename');
}

/// Run [callback] with this Dart process having exclusive access to [file].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ class PackageLayout {
packageConfigUri = packageConfigUri.normalizePath();
final rootPackageRoot = packageConfigUri.resolve('../');
return PackageLayout._(
fileSystem, rootPackageRoot, packageConfig, packageConfigUri);
fileSystem,
rootPackageRoot,
packageConfig,
packageConfigUri,
);
}

static Future<PackageLayout> fromRootPackageRoot(
FileSystem fileSystem, Uri rootPackageRoot) async {
FileSystem fileSystem,
Uri rootPackageRoot,
) async {
rootPackageRoot = rootPackageRoot.normalizePath();
final packageConfigUri =
rootPackageRoot.resolve('.dart_tool/package_config.json');
Expand Down
8 changes: 5 additions & 3 deletions pkgs/native_assets_builder/lib/src/utils/run_process.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:io' show Platform, Process, ProcessException, ProcessResult;

import 'package:file/file.dart';
import 'package:logging/logging.dart';

/// Runs a [Process].
Expand All @@ -15,6 +16,7 @@ import 'package:logging/logging.dart';
/// If [captureOutput], captures stdout and stderr.
// TODO(dacoharkes): Share between package:native_toolchain_c and here.
Future<RunProcessResult> runProcess({
required FileSystem filesystem,
required Uri executable,
List<String> arguments = const [],
Uri? workingDirectory,
Expand All @@ -25,8 +27,8 @@ Future<RunProcessResult> runProcess({
int expectedExitCode = 0,
bool throwOnUnexpectedExitCode = false,
}) async {
final printWorkingDir =
workingDirectory != null && workingDirectory != Directory.current.uri;
final printWorkingDir = workingDirectory != null &&
workingDirectory != filesystem.currentDirectory.uri;
final commandString = [
if (printWorkingDir) '(cd ${workingDirectory.toFilePath()};',
...?environment?.entries.map((entry) => '${entry.key}=${entry.value}'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void main() async {
final tempSubDir = fileSystem.directory(tempUri.resolve('subdir/'));
final subFile = fileSystem.file(tempSubDir.uri.resolve('bar.txt'));

final hashesFile = fileSystem.file(tempUri.resolve('hashes.json'));
final hashes = DependenciesHashFile(fileSystem, file: hashesFile);
final hashesFileUri = tempUri.resolve('hashes.json');
final hashes = DependenciesHashFile(fileSystem, fileUri: hashesFileUri);

Future<void> reset() async {
await tempFile.create(recursive: true);
Expand Down
2 changes: 2 additions & 0 deletions pkgs/native_assets_builder/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:io';

import 'package:file/local.dart' show LocalFileSystem;
import 'package:logging/logging.dart';
import 'package:native_assets_builder/src/utils/run_process.dart'
as run_process;
Expand Down Expand Up @@ -75,6 +76,7 @@ Future<run_process.RunProcessResult> runProcess({
bool throwOnUnexpectedExitCode = false,
}) =>
run_process.runProcess(
filesystem: const LocalFileSystem(),
executable: executable,
arguments: arguments,
workingDirectory: workingDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main(List<String> args) async {
print('locking directory');
await runUnderDirectoryLock<void>(
fileSystem,
directory,
directory.uri,
timeout: timeout,
() async {
print('directory locked');
Expand Down

0 comments on commit 0d88fd6

Please sign in to comment.