Skip to content
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

[native_assets_cli] Cleanup constructors #1867

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 59 additions & 39 deletions pkgs/native_assets_cli/lib/src/code_assets/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import 'os.dart';
/// code assets (only available if code assets are supported).
extension CodeAssetBuildConfig on BuildConfig {
/// Code asset specific configuration.
CodeConfig get codeConfig => CodeConfig(this);
CodeConfig get codeConfig => CodeConfig.fromJson(json);
}

/// Extension to the [LinkConfig] providing access to configuration specific to
/// code assets as well as code asset inputs to the linker (only available if
/// code assets are supported).
extension CodeAssetLinkConfig on LinkConfig {
/// Code asset specific configuration.
CodeConfig get codeConfig => CodeConfig(this);
CodeConfig get codeConfig => CodeConfig.fromJson(json);

// Returns the code assets that were sent to this linker.
//
Expand All @@ -47,37 +47,55 @@ class CodeConfig {
/// The operating system being compiled for.
final OS targetOS;

late final IOSConfig? _iOSConfig;
late final AndroidConfig? _androidConfig;
late final MacOSConfig? _macOSConfig;

CodeConfig(HookConfig config)
: linkModePreference = LinkModePreference.fromString(
config.json.string(_linkModePreferenceKey)),
// ignore: deprecated_member_use_from_same_package
_targetArchitecture = (config is BuildConfig && config.dryRun)
? null
: Architecture.fromString(config.json.string(_targetArchitectureKey,
validValues: Architecture.values.map((a) => a.name))),
targetOS = OS.fromString(config.json.string(_targetOSConfigKey)),
cCompiler = switch (config.json.optionalMap(_compilerKey)) {
final Map<String, Object?> map => CCompilerConfig.fromJson(map),
null => null,
} {
// ignore: deprecated_member_use_from_same_package
_iOSConfig = (config is BuildConfig && config.dryRun) || targetOS != OS.iOS
final IOSConfig? _iOSConfig;
final AndroidConfig? _androidConfig;
final MacOSConfig? _macOSConfig;

CodeConfig._({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider keeping the old code here and renaming this to

// TODO: Merge with `.fromJson` once dry-run is gone.
CodeConfig._fromJsonAndDryRun(Map<String, Object?> json, bool dryRun) {}

factory CodeConfig.fromJson(Map<String, Object?) json) {
  return CodeConfig._fromJsonAndDryRun(json, json.getOptional<bool>(_dryRunConfigKey) ?? false);
}

Otherwise there's high chance someone may want to make it public in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map<String, Object?> json, bool dryRun

There is no nesting currently, so dryRun is in the same root level as the code config fields.

Otherwise there's high chance someone may want to make it public in the future.

Left a comment to avoid someone doing this.

required Architecture? targetArchitecture,
required this.targetOS,
required this.linkModePreference,
CCompilerConfig? cCompilerConfig,
AndroidConfig? androidConfig,
IOSConfig? iOSConfig,
MacOSConfig? macOSConfig,
}) : _targetArchitecture = targetArchitecture,
cCompiler = cCompilerConfig,
_iOSConfig = iOSConfig,
_androidConfig = androidConfig,
_macOSConfig = macOSConfig;

factory CodeConfig.fromJson(Map<String, Object?> json) {
final dryRun = json.getOptional<bool>(_dryRunConfigKey) ?? false;

final linkModePreference =
LinkModePreference.fromString(json.string(_linkModePreferenceKey));
final targetArchitecture = dryRun
? null
: IOSConfig.fromHookConfig(config);
_androidConfig =
// ignore: deprecated_member_use_from_same_package
(config is BuildConfig && config.dryRun) || targetOS != OS.android
? null
: AndroidConfig.fromHookConfig(config);
_macOSConfig =
// ignore: deprecated_member_use_from_same_package
(config is BuildConfig && config.dryRun) || targetOS != OS.macOS
? null
: MacOSConfig.fromHookConfig(config);
: Architecture.fromString(json.string(_targetArchitectureKey,
validValues: Architecture.values.map((a) => a.name)));
final targetOS = OS.fromString(json.string(_targetOSConfigKey));
final cCompiler = switch (json.optionalMap(_compilerKey)) {
final Map<String, Object?> map => CCompilerConfig.fromJson(map),
null => null
};

final iOSConfig =
dryRun || targetOS != OS.iOS ? null : IOSConfig.fromJson(json);
final androidConfig =
dryRun || targetOS != OS.android ? null : AndroidConfig.fromJson(json);
final macOSConfig =
dryRun || targetOS != OS.macOS ? null : MacOSConfig.fromJson(json);

return CodeConfig._(
targetArchitecture: targetArchitecture,
targetOS: targetOS,
linkModePreference: linkModePreference,
cCompilerConfig: cCompiler,
iOSConfig: iOSConfig,
androidConfig: androidConfig,
macOSConfig: macOSConfig,
);
}

Architecture get targetArchitecture {
Expand Down Expand Up @@ -132,9 +150,9 @@ class IOSConfig {
}) : _targetSdk = targetSdk,
_targetVersion = targetVersion;

IOSConfig.fromHookConfig(HookConfig config)
: _targetVersion = config.json.optionalInt(_targetIOSVersionKey),
_targetSdk = switch (config.json.optionalString(_targetIOSSdkKey)) {
IOSConfig.fromJson(Map<String, Object?> json)
: _targetVersion = json.optionalInt(_targetIOSVersionKey),
_targetSdk = switch (json.optionalString(_targetIOSSdkKey)) {
null => null,
String e => IOSSdk.fromString(e)
};
Expand All @@ -157,8 +175,8 @@ class AndroidConfig {
required int targetNdkApi,
}) : _targetNdkApi = targetNdkApi;

AndroidConfig.fromHookConfig(HookConfig config)
: _targetNdkApi = config.json.optionalInt(_targetAndroidNdkApiKey);
AndroidConfig.fromJson(Map<String, Object?> json)
: _targetNdkApi = json.optionalInt(_targetAndroidNdkApiKey);
}

extension AndroidConfigSyntactic on AndroidConfig {
Expand All @@ -176,8 +194,8 @@ class MacOSConfig {
required int targetVersion,
}) : _targetVersion = targetVersion;

MacOSConfig.fromHookConfig(HookConfig config)
: _targetVersion = config.json.optionalInt(_targetMacOSVersionKey);
MacOSConfig.fromJson(Map<String, Object?> json)
: _targetVersion = json.optionalInt(_targetMacOSVersionKey);
}

extension MacOSConfigSyntactic on MacOSConfig {
Expand Down Expand Up @@ -284,3 +302,5 @@ const String _targetIOSSdkKey = 'target_ios_sdk';
const String _targetIOSVersionKey = 'target_ios_version';
const String _targetMacOSVersionKey = 'target_macos_version';
const String _targetOSConfigKey = 'target_os';

const _dryRunConfigKey = 'dry_run';
Loading