Skip to content

Commit

Permalink
[native_assets_cli] Move config.buildMode to `config.codeConfig.bui…
Browse files Browse the repository at this point in the history
…ldMode`
  • Loading branch information
dcharkes committed Dec 10, 2024
1 parent da5dc14 commit a46bfcd
Show file tree
Hide file tree
Showing 40 changed files with 201 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class NativeAssetsBuildRunner {
required BuildValidator buildValidator,
required ApplicationAssetValidator applicationAssetValidator,
required OS targetOS,
required BuildMode buildMode,
required Uri workingDirectory,
PackageLayout? packageLayout,
String? runPackageName,
Expand Down Expand Up @@ -124,7 +123,6 @@ class NativeAssetsBuildRunner {
..setupHookConfig(
targetOS: targetOS,
buildAssetTypes: buildAssetTypes,
buildMode: buildMode,
packageName: package.name,
packageRoot: packageLayout.packageRoot(package.name),
)
Expand Down Expand Up @@ -200,7 +198,6 @@ class NativeAssetsBuildRunner {
required LinkConfigValidator configValidator,
required LinkValidator linkValidator,
required OS targetOS,
required BuildMode buildMode,
required Uri workingDirectory,
required ApplicationAssetValidator applicationAssetValidator,
PackageLayout? packageLayout,
Expand All @@ -225,7 +222,6 @@ class NativeAssetsBuildRunner {
..setupHookConfig(
targetOS: targetOS,
buildAssetTypes: buildAssetTypes,
buildMode: buildMode,
packageName: package.name,
packageRoot: packageLayout.packageRoot(package.name),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ void main() async {
..setupCodeConfig(
targetArchitecture: Architecture.current,
linkModePreference: LinkModePreference.dynamic,
buildMode: BuildMode.release,
);

await buildRunner.build(
configCreator: configCreator,
targetOS: OS.current,
buildMode: BuildMode.release,
workingDirectory: packageUri,
linkingEnabled: false,
buildAssetTypes: [],
Expand All @@ -46,7 +46,6 @@ void main() async {
);
await buildRunner.build(
configCreator: configCreator,
buildMode: BuildMode.release,
targetOS: OS.current,
workingDirectory: packageUri,
linkingEnabled: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void main(List<String> args) async {
dartExecutable: dartExecutable,
).build(
configCreator: BuildConfigBuilder.new,
buildMode: BuildMode.release,
targetOS: target.os,
workingDirectory: packageUri,
linkingEnabled: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ void main(List<String> args) async {
linkModePreference: LinkModePreference.dynamic,
cCompilerConfig: dartCICompilerConfig,
targetMacOSVersion: OS.current == OS.macOS ? defaultMacOSVersion : null,
buildMode: BuildMode.release,
),
buildMode: BuildMode.release,
targetOS: OS.current,
workingDirectory: packageUri,
linkingEnabled: false,
Expand Down
8 changes: 4 additions & 4 deletions pkgs/native_assets_builder/test/build_runner/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ Future<BuildResult?> build(
targetMacOSVersion: targetMacOSVersion ??
(targetOS == OS.macOS ? defaultMacOSVersion : null),
targetAndroidNdkApi: targetAndroidNdkApi,
buildMode: BuildMode.release,
);
}
return configBuilder;
},
configValidator: configValidator,
buildMode: BuildMode.release,
targetOS: targetOS,
workingDirectory: packageUri,
packageLayout: packageLayout,
Expand Down Expand Up @@ -132,12 +132,12 @@ Future<LinkResult?> link(
targetMacOSVersion: targetMacOSVersion ??
(targetOS == OS.macOS ? defaultMacOSVersion : null),
targetAndroidNdkApi: targetAndroidNdkApi,
buildMode: BuildMode.release,
);
}
return configBuilder;
},
configValidator: configValidator,
buildMode: BuildMode.release,
targetOS: target?.os ?? OS.current,
workingDirectory: packageUri,
packageLayout: packageLayout,
Expand Down Expand Up @@ -193,9 +193,9 @@ Future<(BuildResult?, LinkResult?)> buildAndLink(
targetIOSVersion: targetIOSVersion,
targetMacOSVersion: targetMacOSVersion,
targetAndroidNdkApi: targetAndroidNdkApi,
buildMode: BuildMode.release,
),
configValidator: buildConfigValidator,
buildMode: BuildMode.release,
targetOS: target?.os ?? OS.current,
workingDirectory: packageUri,
packageLayout: packageLayout,
Expand Down Expand Up @@ -226,9 +226,9 @@ Future<(BuildResult?, LinkResult?)> buildAndLink(
targetIOSVersion: targetIOSVersion,
targetMacOSVersion: targetMacOSVersion,
targetAndroidNdkApi: targetAndroidNdkApi,
buildMode: BuildMode.release,
),
configValidator: linkConfigValidator,
buildMode: BuildMode.release,
targetOS: target?.os ?? OS.current,
workingDirectory: packageUri,
packageLayout: packageLayout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ void main() async {

final configBuilder = BuildConfigBuilder()
..setupHookConfig(
packageName: name,
packageRoot: testPackageUri,
targetOS: OS.current,
buildAssetTypes: [CodeAsset.type],
buildMode: BuildMode.debug)
packageName: name,
packageRoot: testPackageUri,
targetOS: OS.current,
buildAssetTypes: [CodeAsset.type],
)
..setupBuildConfig(dryRun: false, linkingEnabled: false)
..setupBuildRunConfig(
outputDirectory: outputDirectory,
outputDirectoryShared: outputDirectoryShared)
outputDirectory: outputDirectory,
outputDirectoryShared: outputDirectoryShared,
)
..setupCodeConfig(
targetArchitecture: Architecture.current,
linkModePreference: LinkModePreference.dynamic,
cCompilerConfig: cCompiler,
buildMode: BuildMode.debug,
);

final buildConfigUri = testTempUri.resolve('build_config.json');
Expand Down
11 changes: 6 additions & 5 deletions pkgs/native_assets_builder/test/test_data/transformer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,19 @@ void main() async {
Future<void> runBuild(Architecture architecture) async {
final configBuilder = BuildConfigBuilder()
..setupHookConfig(
packageName: packageName,
packageRoot: packageUri,
targetOS: OS.current,
buildAssetTypes: [DataAsset.type],
buildMode: BuildMode.debug)
packageName: packageName,
packageRoot: packageUri,
targetOS: OS.current,
buildAssetTypes: [DataAsset.type],
)
..setupBuildConfig(dryRun: false, linkingEnabled: false)
..setupBuildRunConfig(
outputDirectory: outputDirectory,
outputDirectoryShared: outputDirectoryShared)
..setupCodeConfig(
targetArchitecture: architecture,
linkModePreference: LinkModePreference.dynamic,
buildMode: BuildMode.debug,
);

final buildConfigUri = testTempUri.resolve('build_config.json');
Expand Down
2 changes: 2 additions & 0 deletions pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
missing. This enables (1) code to run if an asset is missing but that code is
not invoked at runtime, and (2) doing fallback implementations in Dart if an
asset is missing.
- **Breaking change** Move `buildMode` to `codeConfig.buildMode`. It is tailored
to code assets.
- Update pubspec.yaml of examples to use 0.9.0 of `package:native_assets_cli`.
- Consolidate [CodeAsset] specific things into `lib/src/code_assets/*`

Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/lib/code_assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export 'native_assets_cli.dart'
EncodedAsset,
EncodedAssetBuildOutputBuilder,
EncodedAssetLinkOutputBuilder;
export 'src/build_mode.dart' show BuildMode;
export 'src/code_assets/architecture.dart' show Architecture;
export 'src/code_assets/build_mode.dart' show BuildMode;
export 'src/code_assets/c_compiler_config.dart' show CCompilerConfig;
export 'src/code_assets/code_asset.dart' show CodeAsset, OSLibraryNaming;
export 'src/code_assets/config.dart'
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/lib/native_assets_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export 'src/api/build.dart' show build;
export 'src/api/builder.dart' show Builder;
export 'src/api/link.dart' show link;
export 'src/api/linker.dart' show Linker;
export 'src/build_mode.dart' show BuildMode;
export 'src/code_assets/build_mode.dart' show BuildMode;
export 'src/config.dart'
show
BuildConfig,
Expand Down
23 changes: 23 additions & 0 deletions pkgs/native_assets_cli/lib/src/code_assets/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../config.dart';
import '../json_utils.dart';

import 'architecture.dart';
import 'build_mode.dart';
import 'c_compiler_config.dart';
import 'code_asset.dart';
import 'ios_sdk.dart';
Expand Down Expand Up @@ -47,6 +48,7 @@ class CodeConfig {
final int? targetMacOSVersion;
final int? targetAndroidNdkApi;
final IOSSdk? targetIOSSdk;
final BuildMode? _buildMode;

CodeConfig(HookConfig config)
: linkModePreference = LinkModePreference.fromString(
Expand All @@ -66,6 +68,10 @@ class CodeConfig {
targetIOSSdk = switch (config.json.optionalString(_targetIOSSdkKey)) {
final String value => IOSSdk.fromString(value),
null => null,
},
_buildMode = switch (config.json.optionalString(_buildModeConfigKey)) {
String value => BuildMode.fromString(value),
null => null,
};

Architecture get targetArchitecture {
Expand All @@ -74,6 +80,18 @@ class CodeConfig {
}
return _targetArchitecture;
}

/// The [BuildMode] that the code should be compiled in.
///
/// Currently [BuildMode.debug] and [BuildMode.release] are the only modes.
///
/// Not available during a dry run.
BuildMode get buildMode {
if (_buildMode == null) {
throw StateError('Build mode should not be accessed in dry-run mode.');
}
return _buildMode;
}
}

/// Extension to the [BuildOutputBuilder] providing access to emitting code
Expand Down Expand Up @@ -127,6 +145,7 @@ extension CodeAssetBuildConfigBuilder on HookConfigBuilder {
int? targetMacOSVersion,
int? targetAndroidNdkApi,
IOSSdk? targetIOSSdk,
required BuildMode? buildMode,
}) {
if (targetArchitecture != null) {
json[_targetArchitectureKey] = targetArchitecture.toString();
Expand All @@ -148,6 +167,9 @@ extension CodeAssetBuildConfigBuilder on HookConfigBuilder {
if (targetIOSSdk != null) {
json[_targetIOSSdkKey] = targetIOSSdk.toString();
}
if (buildMode != null) {
json[_buildModeConfigKey] = buildMode.toString();
}
}
}

Expand All @@ -169,6 +191,7 @@ extension CodeAssetLinkOutput on LinkOutput {
.toList();
}

const _buildModeConfigKey = 'build_mode';
const String _compilerKey = 'c_compiler';
const String _targetArchitectureKey = 'target_architecture';
const String _targetIOSSdkKey = 'target_ios_sdk';
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/lib/src/code_assets/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ Future<void> testCodeBuildHook({
targetIOSVersion: targetIOSVersion,
targetMacOSVersion: targetMacOSVersion,
targetAndroidNdkApi: targetAndroidNdkApi,
buildMode: buildMode,
);
},
check: (config, output) async {
expect(await validateCodeAssetBuildOutput(config, output), isEmpty);
await check(config, output);
},
buildMode: buildMode,
targetOS: targetOS,
buildAssetTypes: buildAssetTypes,
linkingEnabled: linkingEnabled,
Expand Down
26 changes: 1 addition & 25 deletions pkgs/native_assets_cli/lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:crypto/crypto.dart' show sha256;
import 'package:pub_semver/pub_semver.dart';

import 'api/deprecation_messages.dart';
import 'build_mode.dart';
import 'code_assets/architecture.dart';
import 'encoded_asset.dart';
import 'json_utils.dart';
Expand Down Expand Up @@ -65,13 +64,6 @@ sealed class HookConfig {
/// The operating system being compiled for.
final OS targetOS;

/// The [BuildMode] that the code should be compiled in.
///
/// Currently [BuildMode.debug] and [BuildMode.release] are the only modes.
///
/// Not available during a dry run.
final BuildMode? _buildMode;

/// The asset types that the invoker of this hook supports.
final List<String> buildAssetTypes;

Expand All @@ -91,18 +83,7 @@ sealed class HookConfig {
targetOS = OS.fromString(json.string(_targetOSConfigKey)),
buildAssetTypes = json.optionalStringList(_buildAssetTypesKey) ??
json.optionalStringList(_supportedAssetTypesKey) ??
const [],
_buildMode = switch (json.optionalString(_buildModeConfigKey)) {
String value => BuildMode.fromString(value),
null => null,
};

BuildMode get buildMode {
if (_buildMode == null) {
throw StateError('Build mode should not be accessed in dry-run mode.');
}
return _buildMode;
}
const [];

@override
String toString() => const JsonEncoder.withIndent(' ').convert(json);
Expand All @@ -118,16 +99,12 @@ sealed class HookConfigBuilder {
required String packageName,
required OS targetOS,
required List<String> buildAssetTypes,
required BuildMode? buildMode,
}) {
json[_packageNameConfigKey] = packageName;
json[_packageRootConfigKey] = packageRoot.toFilePath();
json[_targetOSConfigKey] = targetOS.toString();
json[_buildAssetTypesKey] = buildAssetTypes;
json[_supportedAssetTypesKey] = buildAssetTypes;
if (buildMode != null) {
json[_buildModeConfigKey] = buildMode.toString();
}
}

/// Constructs a checksum for a [BuildConfig].
Expand Down Expand Up @@ -159,7 +136,6 @@ sealed class HookConfigBuilder {
}

const _targetOSConfigKey = 'target_os';
const _buildModeConfigKey = 'build_mode';
const _metadataConfigKey = 'metadata';
const _outDirConfigKey = 'out_dir';
const _outDirSharedConfigKey = 'out_dir_shared';
Expand Down
2 changes: 0 additions & 2 deletions pkgs/native_assets_cli/lib/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Future<void> testBuildHook({
required FutureOr<void> Function(List<String> arguments) mainMethod,
required FutureOr<void> Function(BuildConfig config, BuildOutput output)
check,
BuildMode? buildMode,
OS? targetOS,
List<String>? buildAssetTypes,
bool? linkingEnabled,
Expand All @@ -44,7 +43,6 @@ Future<void> testBuildHook({
packageName: _readPackageNameFromPubspec(),
targetOS: targetOS ?? OS.current,
buildAssetTypes: buildAssetTypes ?? [],
buildMode: buildMode ?? BuildMode.release,
)
..setupBuildConfig(
dryRun: false,
Expand Down
1 change: 0 additions & 1 deletion pkgs/native_assets_cli/test/api/build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void main() async {
packageName: packageName,
targetOS: OS.iOS,
buildAssetTypes: ['foo'],
buildMode: BuildMode.release,
)
..setupBuildConfig(
dryRun: false,
Expand Down
Loading

0 comments on commit a46bfcd

Please sign in to comment.