-
Notifications
You must be signed in to change notification settings - Fork 67
[native_assets_cli] Sharing work between build hooks #1379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
A variant of 1. that's more user-friendly:
|
API design: // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:native_assets_cli/locking.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
const assetName = 'asset.txt';
Future<void> main(List<String> args) async {
await build(args, (config, output) async {
final sharedDirectory = config.outputDirectoryShared;
final assetPath = sharedDirectory.resolve(assetName);
await runUnderDirectoryLock(
Directory.fromUri(sharedDirectory),
() async {
final packageName = config.packageName;
if (!config.dryRun) {
// Pretend we're downloading assets here.
await File.fromUri(assetPath).writeAsString('Hello world!');
}
output.addAsset(
DataAsset(
package: packageName,
name: assetName,
file: assetPath,
),
);
},
);
});
} We'll need to add |
This makes the native assets builder lock the build directories so that it can be invoked concurrently from multiple `dart` and `flutter` processes. Closes: #1319 The implementation of the locking is in `package:native_assets_cli/locking.dart` contains `runUnderDirectoryLock`, so that we can reuse it for #1379. (#1379 Requires more changes though, it needs to also give users access to a shared directory via the `BuildConfig`.)
This makes the native assets builder lock the build directories so that it can be invoked concurrently from multiple `dart` and `flutter` processes. Closes: #1319 The implementation of the locking is in `package:native_assets_cli/locking.dart` contains `runUnderDirectoryLock`, so that we can reuse it for #1379. (#1379 Requires more changes though, it needs to also give users access to a shared directory via the `BuildConfig`.)
Thinking about this, the shared directory should be locked in the native assets builder, due to #1534. Otherwise, the native assets builder could be copying the assets while another hook invocation is trying to delete them at the same time. The native assets builder should only be deleting the hook after it has copied the necessary files to a temp/target location. |
Note from discussion with @HosseinYousefi:
The API could look something like: Directory HookConfig.getSharedOutputDirectory({
bool splitOnTargetOS: false,
bool splitOnTargetArchitecture: false,
bool splitOnAndroidNdkVersion: false,
// ...
}); This feel natural because one has to last what config aspects are important to branch on. However, users are more likely to accidentally share and have weird behavior. The opposite would be quite weird: Directory HookConfig.getOutputDirectory({
bool shareAcrossTargetOS: false,
bool shareAcrossTargetArchitecture: false,
bool shareAcrossAndroidNdkVersion: false,
// ...
}); It would require users to list all the config params they don't care about. And this doesn't work with (a) us adding new parameters, and (b) some other embedder adding config parameters that we don't even know about. To make things work for params that we don't know about: Directory HookConfig.getOutputDirectory(List<Object> splitOn);
config.getOutputDirectory([config.targetOS, config.targetArchitecture, ...]); |
We have |
Build hooks are per OS/arch. However, if assets are identical across multiple invocations, and the work to produce those assets is non-trivial, such work should be shared.
Examples:
Non-examples:
We have multiple avenues of sharing the work:
.dart_tool/<your-package-name>
.Both of these options require hook writers to think about locking due to concurrent invocations of the build hook for different OSes/architectures. So whatever we come up with for #1319, should be made available in an API to hook writers.
Option (2) doesn't support sharing native code assets, so in for the second example, hook writers would still use option (1) even if we go for option (2).
The text was updated successfully, but these errors were encountered: