From 0d88fd6a7cc3e0d4f1df5a84911e08b8ccb9cac2 Mon Sep 17 00:00:00 2001 From: Martin Kustermann Date: Thu, 19 Dec 2024 12:10:28 +0100 Subject: [PATCH] Pass FileSystem+Uri around to avoid assumptions in codebase --- .../lib/src/build_runner/build_runner.dart | 29 +++++++++---------- .../dependencies_hash_file.dart | 11 +++++-- .../lib/src/locking/locking.dart | 10 +++---- .../src/package_layout/package_layout.dart | 10 +++++-- .../lib/src/utils/run_process.dart | 8 +++-- .../dependencies_hash_file_test.dart | 4 +-- pkgs/native_assets_builder/test/helpers.dart | 2 ++ .../test/locking/locking_test_helper.dart | 2 +- 8 files changed, 45 insertions(+), 31 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 4e5e571d8..07c4c40b7 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -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, @@ -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); @@ -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 { @@ -468,6 +466,7 @@ ${e.message} if (resources != null) resources.toFilePath(), ]; final result = await runProcess( + filesystem: _fileSystem, workingDirectory: workingDirectory, executable: dartExecutable, arguments: arguments, @@ -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 @@ -642,6 +640,7 @@ ${e.message} scriptUri.toFilePath(), ]; final compileResult = await runProcess( + filesystem: _fileSystem, workingDirectory: workingDirectory, executable: dartExecutable, arguments: compileArguments, diff --git a/pkgs/native_assets_builder/lib/src/dependencies_hash_file/dependencies_hash_file.dart b/pkgs/native_assets_builder/lib/src/dependencies_hash_file/dependencies_hash_file.dart index 681e14586..2c2f9c806 100644 --- a/pkgs/native_assets_builder/lib/src/dependencies_hash_file/dependencies_hash_file.dart +++ b/pkgs/native_assets_builder/lib/src/dependencies_hash_file/dependencies_hash_file.dart @@ -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 get fileSystemEntities => _hashes.files.map((e) => e.path).toList(); + Future exists() async => await _fileSystem.file(fileUri).exists(); + Future delete() async => await _fileSystem.file(fileUri).delete(); + Future _readFile() async { + final file = _fileSystem.file(fileUri); if (!await file.exists()) { _hashes = FileSystemHashes(); return; @@ -75,7 +79,8 @@ class DependenciesHashFile { return modifiedAfterTimeStamp; } - Future _persist() => file.writeAsString(json.encode(_hashes.toJson())); + Future _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. diff --git a/pkgs/native_assets_builder/lib/src/locking/locking.dart b/pkgs/native_assets_builder/lib/src/locking/locking.dart index f2351233f..7a1c26f73 100644 --- a/pkgs/native_assets_builder/lib/src/locking/locking.dart +++ b/pkgs/native_assets_builder/lib/src/locking/locking.dart @@ -10,7 +10,7 @@ import 'package:logging/logging.dart'; Future runUnderDirectoriesLock( FileSystem fileSystem, - List directories, + List directories, Future Function() callback, { Duration? timeout, Logger? logger, @@ -46,13 +46,13 @@ Future runUnderDirectoriesLock( /// also streams error messages. Future runUnderDirectoryLock( FileSystem fileSystem, - Directory directory, + Uri directory, Future 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, @@ -61,11 +61,11 @@ Future runUnderDirectoryLock( ); } -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]. diff --git a/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart b/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart index 8e8c19871..1e24aa3b2 100644 --- a/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart +++ b/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart @@ -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 fromRootPackageRoot( - FileSystem fileSystem, Uri rootPackageRoot) async { + FileSystem fileSystem, + Uri rootPackageRoot, + ) async { rootPackageRoot = rootPackageRoot.normalizePath(); final packageConfigUri = rootPackageRoot.resolve('.dart_tool/package_config.json'); diff --git a/pkgs/native_assets_builder/lib/src/utils/run_process.dart b/pkgs/native_assets_builder/lib/src/utils/run_process.dart index efb712012..2da342848 100644 --- a/pkgs/native_assets_builder/lib/src/utils/run_process.dart +++ b/pkgs/native_assets_builder/lib/src/utils/run_process.dart @@ -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]. @@ -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 runProcess({ + required FileSystem filesystem, required Uri executable, List arguments = const [], Uri? workingDirectory, @@ -25,8 +27,8 @@ Future 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}'), diff --git a/pkgs/native_assets_builder/test/dependencies_hash_file/dependencies_hash_file_test.dart b/pkgs/native_assets_builder/test/dependencies_hash_file/dependencies_hash_file_test.dart index dcd1b7e6b..b97dc1d91 100644 --- a/pkgs/native_assets_builder/test/dependencies_hash_file/dependencies_hash_file_test.dart +++ b/pkgs/native_assets_builder/test/dependencies_hash_file/dependencies_hash_file_test.dart @@ -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 reset() async { await tempFile.create(recursive: true); diff --git a/pkgs/native_assets_builder/test/helpers.dart b/pkgs/native_assets_builder/test/helpers.dart index 0242bcd06..c64a7f79e 100644 --- a/pkgs/native_assets_builder/test/helpers.dart +++ b/pkgs/native_assets_builder/test/helpers.dart @@ -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; @@ -75,6 +76,7 @@ Future runProcess({ bool throwOnUnexpectedExitCode = false, }) => run_process.runProcess( + filesystem: const LocalFileSystem(), executable: executable, arguments: arguments, workingDirectory: workingDirectory, diff --git a/pkgs/native_assets_builder/test/locking/locking_test_helper.dart b/pkgs/native_assets_builder/test/locking/locking_test_helper.dart index 9332b9383..c807900ca 100644 --- a/pkgs/native_assets_builder/test/locking/locking_test_helper.dart +++ b/pkgs/native_assets_builder/test/locking/locking_test_helper.dart @@ -16,7 +16,7 @@ void main(List args) async { print('locking directory'); await runUnderDirectoryLock( fileSystem, - directory, + directory.uri, timeout: timeout, () async { print('directory locked');