Skip to content

Commit f7ea1cd

Browse files
committed
Refactor tizen_tpk.dart
1 parent c9f3fc3 commit f7ea1cd

File tree

4 files changed

+58
-125
lines changed

4 files changed

+58
-125
lines changed

lib/executable.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ Future<void> main(List<String> args) async {
139139
osUtils: globals.os,
140140
),
141141
TemplateRenderer: () => const MustacheTemplateRenderer(),
142-
ApplicationPackageFactory: () => TizenApplicationPackageFactory(),
142+
ApplicationPackageFactory: () => TizenApplicationPackageFactory(
143+
androidSdk: globals.androidSdk,
144+
processManager: globals.processManager,
145+
logger: globals.logger,
146+
userMessages: globals.userMessages,
147+
fileSystem: globals.fs,
148+
),
143149
DeviceManager: () => TizenDeviceManager(
144150
fileSystem: globals.fs,
145151
logger: globals.logger,

lib/tizen_build_target.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,11 @@ class DotnetTpk {
501501

502502
if (securityProfile != null) {
503503
if (tizenSdk.securityProfiles == null ||
504-
!tizenSdk.securityProfiles.names.contains(securityProfile)) {
504+
!tizenSdk.securityProfiles.contains(securityProfile)) {
505505
throwToolExit('The profile $securityProfile does not exist.');
506506
}
507507
}
508-
securityProfile ??= tizenSdk.securityProfiles?.active?.name;
508+
securityProfile ??= tizenSdk.securityProfiles?.active;
509509

510510
if (securityProfile != null) {
511511
environment.logger
@@ -713,11 +713,11 @@ class NativeTpk {
713713
String securityProfile = buildInfo.securityProfile;
714714
if (securityProfile != null) {
715715
if (tizenSdk.securityProfiles == null ||
716-
!tizenSdk.securityProfiles.names.contains(securityProfile)) {
716+
!tizenSdk.securityProfiles.contains(securityProfile)) {
717717
throwToolExit('The profile $securityProfile does not exist.');
718718
}
719719
}
720-
securityProfile ??= tizenSdk.securityProfiles?.active?.name;
720+
securityProfile ??= tizenSdk.securityProfiles?.active;
721721

722722
if (securityProfile != null) {
723723
environment.logger

lib/tizen_device.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class TizenDevice extends Device {
257257
}
258258

259259
final Version deviceVersion = Version.parse(_platformVersion);
260-
final Version apiVersion = Version.parse(app.manifest?.apiVersion);
260+
final Version apiVersion = Version.parse(app.manifest.apiVersion);
261261
if (deviceVersion != null &&
262262
apiVersion != null &&
263263
apiVersion > deviceVersion) {
@@ -336,7 +336,7 @@ class TizenDevice extends Device {
336336
);
337337
// Package has been built, so we can get the updated application id and
338338
// activity name from the tpk.
339-
package = await TizenTpk.fromTizenProject(project);
339+
package = await TizenTpk.fromProject(project);
340340
}
341341
if (package == null) {
342342
throwToolExit('Problem building an application: see above error(s).');

lib/tizen_tpk.dart

+45-118
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,37 @@
77
import 'dart:io';
88

99
import 'package:file/file.dart';
10+
import 'package:flutter_tools/src/android/android_sdk.dart';
1011
import 'package:flutter_tools/src/android/application_package.dart';
1112
import 'package:flutter_tools/src/application_package.dart';
12-
import 'package:flutter_tools/src/flutter_application_package.dart';
1313
import 'package:flutter_tools/src/base/common.dart';
14+
import 'package:flutter_tools/src/base/logger.dart';
1415
import 'package:flutter_tools/src/base/terminal.dart';
16+
import 'package:flutter_tools/src/base/user_messages.dart';
1517
import 'package:flutter_tools/src/build_info.dart';
18+
import 'package:flutter_tools/src/flutter_application_package.dart';
1619
import 'package:flutter_tools/src/globals.dart' as globals;
1720
import 'package:flutter_tools/src/project.dart';
1821
import 'package:meta/meta.dart';
22+
import 'package:process/process.dart';
1923
import 'package:xml/xml.dart';
2024

2125
import 'tizen_project.dart';
2226

2327
/// [FlutterApplicationPackageFactory] extended for Tizen.
2428
class TizenApplicationPackageFactory extends FlutterApplicationPackageFactory {
25-
TizenApplicationPackageFactory()
26-
: super(
27-
androidSdk: globals.androidSdk,
28-
processManager: globals.processManager,
29-
logger: globals.logger,
30-
userMessages: globals.userMessages,
31-
fileSystem: globals.fs,
29+
TizenApplicationPackageFactory({
30+
@required AndroidSdk androidSdk,
31+
@required ProcessManager processManager,
32+
@required Logger logger,
33+
@required UserMessages userMessages,
34+
@required FileSystem fileSystem,
35+
}) : super(
36+
androidSdk: androidSdk,
37+
processManager: processManager,
38+
logger: logger,
39+
userMessages: userMessages,
40+
fileSystem: fileSystem,
3241
);
3342

3443
@override
@@ -39,7 +48,7 @@ class TizenApplicationPackageFactory extends FlutterApplicationPackageFactory {
3948
}) async {
4049
if (platform == TargetPlatform.tester) {
4150
return applicationBinary == null
42-
? await TizenTpk.fromTizenProject(FlutterProject.current())
51+
? await TizenTpk.fromProject(FlutterProject.current())
4352
: await TizenTpk.fromTpk(applicationBinary);
4453
}
4554
return super.getPackageForPlatform(platform,
@@ -54,7 +63,8 @@ class TizenTpk extends ApplicationPackage {
5463
@required this.manifest,
5564
this.signature,
5665
}) : assert(file != null),
57-
super(id: manifest?.packageId);
66+
assert(manifest != null),
67+
super(id: manifest.packageId);
5868

5969
static Future<TizenTpk> fromTpk(File tpkFile) async {
6070
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync();
@@ -86,8 +96,7 @@ class TizenTpk extends ApplicationPackage {
8696
);
8797
}
8898

89-
static Future<TizenTpk> fromTizenProject(
90-
FlutterProject flutterProject) async {
99+
static Future<TizenTpk> fromProject(FlutterProject flutterProject) async {
91100
final TizenProject project = TizenProject.fromFlutter(flutterProject);
92101

93102
final File tpkFile = flutterProject.directory
@@ -115,7 +124,7 @@ class TizenTpk extends ApplicationPackage {
115124
final Signature signature;
116125

117126
/// The application id if applicable.
118-
String get applicationId => manifest?.applicationId;
127+
String get applicationId => manifest.applicationId;
119128

120129
@override
121130
String get name => file.basename;
@@ -134,7 +143,7 @@ class TizenTpk extends ApplicationPackage {
134143
class TizenManifest {
135144
TizenManifest(this._document) : applicationId = _findApplicationId(_document);
136145

137-
factory TizenManifest.parseFromXml(File xmlFile) {
146+
static TizenManifest parseFromXml(File xmlFile) {
138147
if (xmlFile == null || !xmlFile.existsSync()) {
139148
throwToolExit('tizen-manifest.xml could not be found.');
140149
}
@@ -164,7 +173,7 @@ class TizenManifest {
164173
set version(String value) => _manifest.setAttribute('version', value);
165174

166175
/// The target API version number.
167-
String get apiVersion => _manifest.getAttribute('api-version');
176+
String get apiVersion => _manifest.getAttribute('api-version') ?? '4.0';
168177

169178
XmlElement get _profile {
170179
if (_manifest.findElements('profile').isEmpty) {
@@ -198,7 +207,7 @@ class TizenManifest {
198207
}
199208
if (applicationId == null) {
200209
throwToolExit('Found no *-application element with appid attribute'
201-
' in tizen-manifest.xml');
210+
' in tizen-manifest.xml.');
202211
}
203212
if (!_warningShown) {
204213
if (count > 1) {
@@ -220,7 +229,7 @@ class TizenManifest {
220229
return applicationId;
221230
}
222231

223-
/// To prevent spamming log with warnings, remember they have been shown
232+
/// To prevent spamming log with warnings, remember they have been shown.
224233
static bool _warningShown = false;
225234

226235
@override
@@ -231,7 +240,7 @@ class TizenManifest {
231240
class Signature {
232241
const Signature(this._document);
233242

234-
factory Signature.parseFromXml(File xmlFile) {
243+
static Signature parseFromXml(File xmlFile) {
235244
if (xmlFile == null || !xmlFile.existsSync()) {
236245
return null;
237246
}
@@ -245,10 +254,7 @@ class Signature {
245254
try {
246255
document = XmlDocument.parse(data);
247256
} on XmlException catch (ex) {
248-
globals.printStatus(
249-
'Warning: Failed to parse ${xmlFile.basename}: $ex',
250-
color: TerminalColor.yellow,
251-
);
257+
globals.printError('Failed to parse ${xmlFile.basename}: $ex');
252258
return null;
253259
}
254260
return Signature(document);
@@ -265,81 +271,10 @@ class Signature {
265271
}
266272
}
267273

268-
class Certificate {
269-
Certificate._({
270-
@required this.key,
271-
@required this.password,
272-
@required this.distributorNumber,
273-
@required this.ca,
274-
});
275-
276-
factory Certificate.parseFromXmlElement(XmlElement profileItem) {
277-
final String ca = profileItem.getAttribute('ca');
278-
final String key = profileItem.getAttribute('key');
279-
final String password = profileItem.getAttribute('password');
280-
final String distributorNumber = profileItem.getAttribute('distributor');
281-
282-
// The data doesn't exist and the xml element exists only as a placeholder.
283-
if (key.isEmpty || password.isEmpty) {
284-
return null;
285-
}
286-
287-
return Certificate._(
288-
key: key,
289-
password: password,
290-
distributorNumber: distributorNumber,
291-
ca: ca,
292-
);
293-
}
294-
295-
final String key;
296-
final String password;
297-
final String distributorNumber;
298-
final String ca;
299-
}
300-
301-
class SecurityProfile {
302-
SecurityProfile(
303-
this.name, {
304-
@required this.authorCertificate,
305-
@required this.distributorCertificates,
306-
});
307-
308-
factory SecurityProfile.parseFromXmlElement(XmlElement profile) {
309-
Certificate authorCertificate;
310-
final List<Certificate> distributorCertificates = <Certificate>[];
311-
312-
// The element that holds a single certifcate key, password pair
313-
for (final XmlElement profileItem
314-
in profile.findAllElements('profileitem')) {
315-
final Certificate certificate =
316-
Certificate.parseFromXmlElement(profileItem);
317-
if (certificate != null) {
318-
// distributor number 0 specifies an author certificate
319-
if (certificate.distributorNumber == '0') {
320-
authorCertificate = certificate;
321-
} else {
322-
distributorCertificates.add(certificate);
323-
}
324-
}
325-
}
326-
327-
return SecurityProfile(
328-
profile.getAttribute('name'),
329-
authorCertificate: authorCertificate,
330-
distributorCertificates: distributorCertificates,
331-
);
332-
}
333-
334-
final String name;
335-
final Certificate authorCertificate;
336-
final List<Certificate> distributorCertificates;
337-
}
338-
339274
class SecurityProfiles {
340-
SecurityProfiles._(this.active, this._profiles);
275+
SecurityProfiles._(this.profiles, {@required this.active});
341276

342-
factory SecurityProfiles.parseFromXml(File xmlFile) {
277+
static SecurityProfiles parseFromXml(File xmlFile) {
343278
if (xmlFile == null || !xmlFile.existsSync()) {
344279
return null;
345280
}
@@ -353,37 +288,29 @@ class SecurityProfiles {
353288
try {
354289
document = XmlDocument.parse(data);
355290
} on XmlException catch (ex) {
356-
throwToolExit('Failed to parse ${xmlFile.basename}: $ex');
291+
globals.printError('Failed to parse ${xmlFile.basename}: $ex');
292+
return null;
357293
}
358294

359-
final String activeName = document.rootElement.getAttribute('active');
360-
361-
SecurityProfile activeProfile;
362-
final List<SecurityProfile> profiles = <SecurityProfile>[];
295+
String active = document.rootElement.getAttribute('active');
296+
if (active != null && active.isEmpty) {
297+
active = null;
298+
}
363299

364-
for (final XmlElement profileXml
300+
final List<String> profiles = <String>[];
301+
for (final XmlElement profile
365302
in document.rootElement.findAllElements('profile')) {
366-
final SecurityProfile profile =
367-
SecurityProfile.parseFromXmlElement(profileXml);
368-
profiles.add(profile);
369-
if (profile.name == activeName) {
370-
activeProfile = profile;
303+
final String name = profile.getAttribute('name');
304+
if (name != null) {
305+
profiles.add(name);
371306
}
372307
}
373308

374-
return SecurityProfiles._(activeProfile, profiles);
309+
return SecurityProfiles._(profiles, active: active);
375310
}
376311

377-
final SecurityProfile active;
378-
final List<SecurityProfile> _profiles;
312+
final List<String> profiles;
313+
final String active;
379314

380-
List<String> get names =>
381-
_profiles.map((SecurityProfile profile) => profile.name).toList();
382-
383-
SecurityProfile getProfile(String name) {
384-
return _profiles.firstWhere(
385-
(SecurityProfile profile) => profile.name == name,
386-
orElse: () => null,
387-
);
388-
}
315+
bool contains(String name) => profiles.contains(name);
389316
}

0 commit comments

Comments
 (0)