Skip to content

Commit 3b66738

Browse files
authored
[native_assets_cli] Cleanup constructors (#1867)
Addresses: * #1824 (comment) @mkustermann Do you prefer repeating the arguments to `CodeConfig` in `setupCodeConfig` leading to the following? ```dart ..setupCodeConfig( targetArchitecture: Architecture.current, targetOS: OS.current, macOSConfig: targetOS == OS.macOS ? MacOSConfig(targetVersion: defaultMacOSVersion) : null, linkModePreference: LinkModePreference.dynamic, ); ``` Or rather avoiding to repeat the arguments and have the following? ```dart ..setupCodeConfig(CodeConfig( targetArchitecture: Architecture.current, targetOS: OS.current, macOSConfig: targetOS == OS.macOS ? MacOSConfig(targetVersion: defaultMacOSVersion) : null, linkModePreference: LinkModePreference.dynamic, )); ``` I've done the former now, but can easily revert that commit.
1 parent 4ebaea4 commit 3b66738

File tree

1 file changed

+60
-39
lines changed

1 file changed

+60
-39
lines changed

pkgs/native_assets_cli/lib/src/code_assets/config.dart

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import 'os.dart';
1515
/// code assets (only available if code assets are supported).
1616
extension CodeAssetBuildConfig on BuildConfig {
1717
/// Code asset specific configuration.
18-
CodeConfig get codeConfig => CodeConfig(this);
18+
CodeConfig get codeConfig => CodeConfig.fromJson(json);
1919
}
2020

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

2828
// Returns the code assets that were sent to this linker.
2929
//
@@ -47,37 +47,56 @@ class CodeConfig {
4747
/// The operating system being compiled for.
4848
final OS targetOS;
4949

50-
late final IOSConfig? _iOSConfig;
51-
late final AndroidConfig? _androidConfig;
52-
late final MacOSConfig? _macOSConfig;
53-
54-
CodeConfig(HookConfig config)
55-
: linkModePreference = LinkModePreference.fromString(
56-
config.json.string(_linkModePreferenceKey)),
57-
// ignore: deprecated_member_use_from_same_package
58-
_targetArchitecture = (config is BuildConfig && config.dryRun)
59-
? null
60-
: Architecture.fromString(config.json.string(_targetArchitectureKey,
61-
validValues: Architecture.values.map((a) => a.name))),
62-
targetOS = OS.fromString(config.json.string(_targetOSConfigKey)),
63-
cCompiler = switch (config.json.optionalMap(_compilerKey)) {
64-
final Map<String, Object?> map => CCompilerConfig.fromJson(map),
65-
null => null,
66-
} {
67-
// ignore: deprecated_member_use_from_same_package
68-
_iOSConfig = (config is BuildConfig && config.dryRun) || targetOS != OS.iOS
50+
final IOSConfig? _iOSConfig;
51+
final AndroidConfig? _androidConfig;
52+
final MacOSConfig? _macOSConfig;
53+
54+
// Should not be made public, class will be replaced as a view on `json`.
55+
CodeConfig._({
56+
required Architecture? targetArchitecture,
57+
required this.targetOS,
58+
required this.linkModePreference,
59+
CCompilerConfig? cCompilerConfig,
60+
AndroidConfig? androidConfig,
61+
IOSConfig? iOSConfig,
62+
MacOSConfig? macOSConfig,
63+
}) : _targetArchitecture = targetArchitecture,
64+
cCompiler = cCompilerConfig,
65+
_iOSConfig = iOSConfig,
66+
_androidConfig = androidConfig,
67+
_macOSConfig = macOSConfig;
68+
69+
factory CodeConfig.fromJson(Map<String, Object?> json) {
70+
final dryRun = json.getOptional<bool>(_dryRunConfigKey) ?? false;
71+
72+
final linkModePreference =
73+
LinkModePreference.fromString(json.string(_linkModePreferenceKey));
74+
final targetArchitecture = dryRun
6975
? null
70-
: IOSConfig.fromHookConfig(config);
71-
_androidConfig =
72-
// ignore: deprecated_member_use_from_same_package
73-
(config is BuildConfig && config.dryRun) || targetOS != OS.android
74-
? null
75-
: AndroidConfig.fromHookConfig(config);
76-
_macOSConfig =
77-
// ignore: deprecated_member_use_from_same_package
78-
(config is BuildConfig && config.dryRun) || targetOS != OS.macOS
79-
? null
80-
: MacOSConfig.fromHookConfig(config);
76+
: Architecture.fromString(json.string(_targetArchitectureKey,
77+
validValues: Architecture.values.map((a) => a.name)));
78+
final targetOS = OS.fromString(json.string(_targetOSConfigKey));
79+
final cCompiler = switch (json.optionalMap(_compilerKey)) {
80+
final Map<String, Object?> map => CCompilerConfig.fromJson(map),
81+
null => null
82+
};
83+
84+
final iOSConfig =
85+
dryRun || targetOS != OS.iOS ? null : IOSConfig.fromJson(json);
86+
final androidConfig =
87+
dryRun || targetOS != OS.android ? null : AndroidConfig.fromJson(json);
88+
final macOSConfig =
89+
dryRun || targetOS != OS.macOS ? null : MacOSConfig.fromJson(json);
90+
91+
return CodeConfig._(
92+
targetArchitecture: targetArchitecture,
93+
targetOS: targetOS,
94+
linkModePreference: linkModePreference,
95+
cCompilerConfig: cCompiler,
96+
iOSConfig: iOSConfig,
97+
androidConfig: androidConfig,
98+
macOSConfig: macOSConfig,
99+
);
81100
}
82101

83102
Architecture get targetArchitecture {
@@ -132,9 +151,9 @@ class IOSConfig {
132151
}) : _targetSdk = targetSdk,
133152
_targetVersion = targetVersion;
134153

135-
IOSConfig.fromHookConfig(HookConfig config)
136-
: _targetVersion = config.json.optionalInt(_targetIOSVersionKey),
137-
_targetSdk = switch (config.json.optionalString(_targetIOSSdkKey)) {
154+
IOSConfig.fromJson(Map<String, Object?> json)
155+
: _targetVersion = json.optionalInt(_targetIOSVersionKey),
156+
_targetSdk = switch (json.optionalString(_targetIOSSdkKey)) {
138157
null => null,
139158
String e => IOSSdk.fromString(e)
140159
};
@@ -157,8 +176,8 @@ class AndroidConfig {
157176
required int targetNdkApi,
158177
}) : _targetNdkApi = targetNdkApi;
159178

160-
AndroidConfig.fromHookConfig(HookConfig config)
161-
: _targetNdkApi = config.json.optionalInt(_targetAndroidNdkApiKey);
179+
AndroidConfig.fromJson(Map<String, Object?> json)
180+
: _targetNdkApi = json.optionalInt(_targetAndroidNdkApiKey);
162181
}
163182

164183
extension AndroidConfigSyntactic on AndroidConfig {
@@ -176,8 +195,8 @@ class MacOSConfig {
176195
required int targetVersion,
177196
}) : _targetVersion = targetVersion;
178197

179-
MacOSConfig.fromHookConfig(HookConfig config)
180-
: _targetVersion = config.json.optionalInt(_targetMacOSVersionKey);
198+
MacOSConfig.fromJson(Map<String, Object?> json)
199+
: _targetVersion = json.optionalInt(_targetMacOSVersionKey);
181200
}
182201

183202
extension MacOSConfigSyntactic on MacOSConfig {
@@ -284,3 +303,5 @@ const String _targetIOSSdkKey = 'target_ios_sdk';
284303
const String _targetIOSVersionKey = 'target_ios_version';
285304
const String _targetMacOSVersionKey = 'target_macos_version';
286305
const String _targetOSConfigKey = 'target_os';
306+
307+
const _dryRunConfigKey = 'dry_run';

0 commit comments

Comments
 (0)