Skip to content

Commit 92147d4

Browse files
committed
Cleared fastlane image directories on init
1 parent 536d870 commit 92147d4

File tree

7 files changed

+97
-58
lines changed

7 files changed

+97
-58
lines changed

lib/config.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Config {
2121

2222
/// Check emulators and simulators are installed,
2323
/// matching screen is available and tests exist.
24-
Future<bool> validate() async {
25-
final Map screens = await Screens().init();
24+
Future<bool> validate(Screens screens) async {
25+
// final Map screens = await Screens().init();
2626

2727
if (config['devices']['android'] != null) {
2828
// check emulators
@@ -83,7 +83,7 @@ class Config {
8383
return true;
8484
}
8585

86-
void configGuide(Map screens) {
86+
void configGuide(Screens screens) {
8787
installedEmulators(utils.emulators());
8888
installedSimulators(utils.simulators());
8989
supportedDevices(screens);
@@ -93,8 +93,8 @@ class Config {
9393
}
9494

9595
// check screen is available for device
96-
void screenAvailable(Map screens, String deviceName) {
97-
if (Screens().screenProps(screens, deviceName) == null) {
96+
void screenAvailable(Screens screens, String deviceName) {
97+
if (screens.screenProps(deviceName) == null) {
9898
stderr.write(
9999
'configuration error: screen not available for device \'$deviceName\' in $configPath.\n');
100100
stdout.write('\n Use a supported device in $configPath.\n\n'
@@ -108,9 +108,9 @@ class Config {
108108
}
109109
}
110110

111-
void supportedDevices(Map screens) {
111+
void supportedDevices(Screens screens) {
112112
stdout.write('\n Currently supported devices:\n');
113-
screens.forEach((os, v) {
113+
screens.screens.forEach((os, v) {
114114
stdout.write(' $os:\n');
115115
v.value.forEach((screenNum, screenProps) {
116116
for (String device in screenProps['devices']) {

lib/fastlane.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import 'package:screenshots/screens.dart';
12
import 'package:screenshots/screenshots.dart';
3+
import 'package:screenshots/utils.dart' as utils;
24

35
// ios/fastlane/screenshots/en-US/*[iPad|iPhone]*
46
// android/fastlane/metadata/android/en-US/images/phoneScreenshots
@@ -20,3 +22,34 @@ String path(DeviceType deviceType, String locale,
2022
}
2123
return path;
2224
}
25+
26+
/// Clear image destination.
27+
Future clearFastlaneDir(
28+
Screens screens, deviceName, locale, DeviceType deviceType) async {
29+
final Map screenProps = screens.screenProps(deviceName);
30+
31+
final dstDir = path(deviceType, locale, '', screenProps['destName']);
32+
33+
print('Clearing images in $dstDir for \'$deviceName\'...');
34+
await utils.clearDirectory(dstDir);
35+
}
36+
37+
/// clear configured fastlane directories.
38+
Future clearFastlaneDirs(Map config, Screens screens) async {
39+
// final config = Config('test/test_config.yaml').config;
40+
// final Map screens = await Screens().init();
41+
42+
if (config['devices']['ios'] != null)
43+
for (String emulatorName in config['devices']['ios']) {
44+
for (final locale in config['locales']) {
45+
await clearFastlaneDir(screens, emulatorName, locale, DeviceType.ios);
46+
}
47+
}
48+
if (config['devices']['android'] != null)
49+
for (String simulatorName in config['devices']['android']) {
50+
for (final locale in config['locales']) {
51+
await clearFastlaneDir(
52+
screens, simulatorName, locale, DeviceType.android);
53+
}
54+
}
55+
}

lib/process_images.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import 'package:path/path.dart' as p;
2020
///
2121
/// After processing, screenshots are handed off for upload via fastlane.
2222
///
23-
void process(Map screens, Map config, DeviceType deviceType, String deviceName,
24-
String locale) async {
25-
final Map screenProps = Screens().screenProps(screens, deviceName);
23+
void process(Screens screens, Map config, DeviceType deviceType,
24+
String deviceName, String locale) async {
25+
final Map screenProps = screens.screenProps(deviceName);
2626
final staging = config['staging'];
2727
final Map screenResources = screenProps['resources'];
2828
// print('screenResources=$screenResources');

lib/screens.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@ import 'package:resource/resource.dart';
77
///
88
class Screens {
99
static const devicePath = 'resources/screens.yaml';
10+
Map _screens;
1011

1112
///
1213
/// Get screens yaml file from resources and parse.
1314
///
14-
Future<Map> init() async {
15+
Future<void> init() async {
1516
final resource = Resource("package:screenshots/$devicePath");
1617
String screens = await resource.readAsString(encoding: utf8);
17-
return loadYaml(screens) as Map;
18+
_screens = loadYaml(screens) as Map;
1819
}
1920

21+
/// Get screen information
22+
Map get screens => _screens;
23+
2024
///
2125
/// Get map of screen properties from screens yaml file
2226
///
23-
Map screenProps(Map screens, String deviceName) {
27+
Map screenProps(String deviceName) {
2428
Map screenProps;
2529

2630
(screens as YamlNode).value.forEach((os, v) {

lib/screenshots.dart

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,56 @@ enum DeviceType { android, ios }
2323
/// 4. Move processed screenshots to fastlane destination for upload to stores.
2424
/// 5. Stop emulator/simulator.
2525
Future<void> run([String configPath = kConfigFileName]) async {
26-
final _config = Config(configPath);
27-
// validate config file
28-
await _config.validate();
26+
final screens = await Screens();
27+
await screens.init();
28+
// final Map screens = _screens.screens;
2929

30-
final Map config = _config.config;
31-
final Map screens = await Screens().init();
30+
final config = Config(configPath);
31+
// validate config file
32+
await config.validate(screens);
33+
final Map configInfo = config.config;
3234

3335
// init
34-
final stagingDir = config['staging'];
36+
final stagingDir = configInfo['staging'];
3537
await Directory(stagingDir + '/test').create(recursive: true);
3638
await resources.unpackScripts(stagingDir);
39+
await fastlane.clearFastlaneDirs(configInfo, screens);
3740

3841
// run integration tests in each android emulator for each locale and
3942
// process screenshots
40-
if (config['devices']['android'] != null)
41-
for (final emulatorName in config['devices']['android']) {
42-
for (final locale in config['locales']) {
43+
if (configInfo['devices']['android'] != null)
44+
for (final emulatorName in configInfo['devices']['android']) {
45+
for (final locale in configInfo['locales']) {
4346
await emulator(emulatorName, true, stagingDir, locale);
44-
await clearFastlaneDir(
45-
screens, emulatorName, locale, DeviceType.android);
47+
// await clearFastlaneDir(
48+
// screens, emulatorName, locale, DeviceType.android);
4649

47-
for (final testPath in config['tests']) {
50+
for (final testPath in configInfo['tests']) {
4851
print(
4952
'Capturing screenshots with test $testPath on emulator $emulatorName in locale $locale ...');
5053
await screenshots(testPath, stagingDir);
5154
// process screenshots
5255
await processImages.process(
53-
screens, config, DeviceType.android, emulatorName, locale);
56+
screens, configInfo, DeviceType.android, emulatorName, locale);
5457
}
5558
await emulator(emulatorName, false, stagingDir);
5659
}
5760
}
5861

5962
// run integration tests in each ios simulator for each locale and
6063
// process screenshots
61-
if (config['devices']['ios'] != null)
62-
for (final simulatorName in config['devices']['ios']) {
63-
for (final locale in config['locales']) {
64+
if (configInfo['devices']['ios'] != null)
65+
for (final simulatorName in configInfo['devices']['ios']) {
66+
for (final locale in configInfo['locales']) {
6467
simulator(simulatorName, true, stagingDir, locale);
65-
await clearFastlaneDir(screens, simulatorName, locale, DeviceType.ios);
66-
for (final testPath in config['tests']) {
68+
// await clearFastlaneDir(screens, simulatorName, locale, DeviceType.ios);
69+
for (final testPath in configInfo['tests']) {
6770
print(
6871
'Capturing screenshots with test $testPath on simulator $simulatorName in locale $locale ...');
6972
await screenshots(testPath, stagingDir);
7073
// process screenshots
7174
await processImages.process(
72-
screens, config, DeviceType.ios, simulatorName, locale);
75+
screens, configInfo, DeviceType.ios, simulatorName, locale);
7376
}
7477
simulator(simulatorName, false);
7578
}
@@ -84,17 +87,6 @@ Future<void> run([String configPath = kConfigFileName]) async {
8487
print('\nscreenshots completed successfully.');
8588
}
8689

87-
/// Clear image destination
88-
Future clearFastlaneDir(
89-
Map screens, deviceName, locale, DeviceType deviceType) async {
90-
final Map screenProps = Screens().screenProps(screens, deviceName);
91-
92-
final dstDir = fastlane.path(deviceType, locale, '', screenProps['destName']);
93-
94-
print('Clearing images in $dstDir ...');
95-
await utils.clearDirectory(dstDir);
96-
}
97-
9890
///
9991
/// Run the screenshot integration test on current emulator or simulator.
10092
///

test/screenshots_test.dart

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:screenshots/resources.dart';
77
import 'package:screenshots/screenshots.dart';
88
import 'package:screenshots/utils.dart';
99
import 'package:test/test.dart';
10+
import 'package:screenshots/fastlane.dart' as fastlane;
1011

1112
void main() {
1213
test('screen info for device: Nexus 5X', () async {
@@ -23,8 +24,8 @@ void main() {
2324
'size': '1080x1920'
2425
};
2526
final Screens screens = Screens();
26-
final screensInfo = await Screens().init();
27-
Map screen = screens.screenProps(screensInfo, 'Nexus 5X');
27+
await Screens().init();
28+
Map screen = screens.screenProps('Nexus 5X');
2829
expect(screen, expected);
2930
});
3031

@@ -37,8 +38,8 @@ void main() {
3738
'size': '2436×1125'
3839
};
3940
final Screens screens = Screens();
40-
final screensInfo = await Screens().init();
41-
Map screen = screens.screenProps(screensInfo, 'iPhone X');
41+
await Screens().init();
42+
Map screen = screens.screenProps('iPhone X');
4243
expect(screen, expected);
4344
});
4445

@@ -61,8 +62,8 @@ void main() {
6162

6263
test('overlay statusbar', () async {
6364
final Screens screens = Screens();
64-
final screensInfo = await screens.init();
65-
Map screen = screens.screenProps(screensInfo, 'Nexus 6P');
65+
await screens.init();
66+
Map screen = screens.screenProps('Nexus 6P');
6667
final Config config = Config('test/test_config.yaml');
6768
Map appConfig = config.config;
6869

@@ -93,9 +94,9 @@ void main() {
9394

9495
test('unpack screen resource images', () async {
9596
final Screens screens = Screens();
96-
final screensInfo = await screens.init();
97+
await screens.init();
9798
// Map screen = screens.screen(screensInfo, 'Nexus 5X');
98-
Map screen = screens.screenProps(screensInfo, 'iPhone 7 Plus');
99+
Map screen = screens.screenProps('iPhone 7 Plus');
99100
final Config config = Config('test/test_config.yaml');
100101
Map appConfig = config.config;
101102

@@ -114,8 +115,8 @@ void main() {
114115

115116
test('append navbar', () async {
116117
final Screens screens = Screens();
117-
final screensInfo = await screens.init();
118-
Map screen = screens.screenProps(screensInfo, 'Nexus 6P');
118+
await screens.init();
119+
Map screen = screens.screenProps('Nexus 6P');
119120
final Config config = Config('test/test_config.yaml');
120121
Map appConfig = config.config;
121122

@@ -136,8 +137,8 @@ void main() {
136137

137138
test('frame screenshot', () async {
138139
final Screens screens = Screens();
139-
final screensInfo = await screens.init();
140-
Map screen = screens.screenProps(screensInfo, 'Nexus 6P');
140+
await screens.init();
141+
Map screen = screens.screenProps('Nexus 6P');
141142
final Config config = Config('test/test_config.yaml');
142143
Map appConfig = config.config;
143144

@@ -204,8 +205,10 @@ void main() {
204205
});
205206

206207
test('validate config file', () async {
208+
final Screens screens = Screens();
209+
await screens.init();
207210
final Config config = Config('test/test_config.yaml');
208-
expect(await config.validate(), true);
211+
expect(await config.validate(screens), true);
209212
});
210213

211214
test('rooted emulator', () {
@@ -251,4 +254,11 @@ void main() {
251254
// await stdout.done;
252255
await streamCmd('ls', ['-33']);
253256
});
257+
258+
test('clear all destination directories on init', () async {
259+
final Screens screens = Screens();
260+
await screens.init();
261+
final Config config = Config('test/test_config.yaml');
262+
await fastlane.clearFastlaneDirs(config.config, screens);
263+
});
254264
}

test/test_config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ locales:
1515
# A list of devices to emulate
1616
devices:
1717
ios:
18-
- iPhone X
18+
# - iPhone X
1919
# - iPhone 7 Plus
20-
- iPad Pro (12.9-inch) (2nd generation)
20+
# - iPad Pro (12.9-inch) (2nd generation)
2121
# "iPhone 6",
2222
# "iPhone 6 Plus",
2323
# "iPhone 5",

0 commit comments

Comments
 (0)