Skip to content

Commit

Permalink
Cleared fastlane image directories on init
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcc007 committed Feb 13, 2019
1 parent 536d870 commit 92147d4
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 58 deletions.
14 changes: 7 additions & 7 deletions lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Config {

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

if (config['devices']['android'] != null) {
// check emulators
Expand Down Expand Up @@ -83,7 +83,7 @@ class Config {
return true;
}

void configGuide(Map screens) {
void configGuide(Screens screens) {
installedEmulators(utils.emulators());
installedSimulators(utils.simulators());
supportedDevices(screens);
Expand All @@ -93,8 +93,8 @@ class Config {
}

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

void supportedDevices(Map screens) {
void supportedDevices(Screens screens) {
stdout.write('\n Currently supported devices:\n');
screens.forEach((os, v) {
screens.screens.forEach((os, v) {
stdout.write(' $os:\n');
v.value.forEach((screenNum, screenProps) {
for (String device in screenProps['devices']) {
Expand Down
33 changes: 33 additions & 0 deletions lib/fastlane.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:screenshots/screens.dart';
import 'package:screenshots/screenshots.dart';
import 'package:screenshots/utils.dart' as utils;

// ios/fastlane/screenshots/en-US/*[iPad|iPhone]*
// android/fastlane/metadata/android/en-US/images/phoneScreenshots
Expand All @@ -20,3 +22,34 @@ String path(DeviceType deviceType, String locale,
}
return path;
}

/// Clear image destination.
Future clearFastlaneDir(
Screens screens, deviceName, locale, DeviceType deviceType) async {
final Map screenProps = screens.screenProps(deviceName);

final dstDir = path(deviceType, locale, '', screenProps['destName']);

print('Clearing images in $dstDir for \'$deviceName\'...');
await utils.clearDirectory(dstDir);
}

/// clear configured fastlane directories.
Future clearFastlaneDirs(Map config, Screens screens) async {
// final config = Config('test/test_config.yaml').config;
// final Map screens = await Screens().init();

if (config['devices']['ios'] != null)
for (String emulatorName in config['devices']['ios']) {
for (final locale in config['locales']) {
await clearFastlaneDir(screens, emulatorName, locale, DeviceType.ios);
}
}
if (config['devices']['android'] != null)
for (String simulatorName in config['devices']['android']) {
for (final locale in config['locales']) {
await clearFastlaneDir(
screens, simulatorName, locale, DeviceType.android);
}
}
}
6 changes: 3 additions & 3 deletions lib/process_images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import 'package:path/path.dart' as p;
///
/// After processing, screenshots are handed off for upload via fastlane.
///
void process(Map screens, Map config, DeviceType deviceType, String deviceName,
String locale) async {
final Map screenProps = Screens().screenProps(screens, deviceName);
void process(Screens screens, Map config, DeviceType deviceType,
String deviceName, String locale) async {
final Map screenProps = screens.screenProps(deviceName);
final staging = config['staging'];
final Map screenResources = screenProps['resources'];
// print('screenResources=$screenResources');
Expand Down
10 changes: 7 additions & 3 deletions lib/screens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import 'package:resource/resource.dart';
///
class Screens {
static const devicePath = 'resources/screens.yaml';
Map _screens;

///
/// Get screens yaml file from resources and parse.
///
Future<Map> init() async {
Future<void> init() async {
final resource = Resource("package:screenshots/$devicePath");
String screens = await resource.readAsString(encoding: utf8);
return loadYaml(screens) as Map;
_screens = loadYaml(screens) as Map;
}

/// Get screen information
Map get screens => _screens;

///
/// Get map of screen properties from screens yaml file
///
Map screenProps(Map screens, String deviceName) {
Map screenProps(String deviceName) {
Map screenProps;

(screens as YamlNode).value.forEach((os, v) {
Expand Down
52 changes: 22 additions & 30 deletions lib/screenshots.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,56 @@ enum DeviceType { android, ios }
/// 4. Move processed screenshots to fastlane destination for upload to stores.
/// 5. Stop emulator/simulator.
Future<void> run([String configPath = kConfigFileName]) async {
final _config = Config(configPath);
// validate config file
await _config.validate();
final screens = await Screens();
await screens.init();
// final Map screens = _screens.screens;

final Map config = _config.config;
final Map screens = await Screens().init();
final config = Config(configPath);
// validate config file
await config.validate(screens);
final Map configInfo = config.config;

// init
final stagingDir = config['staging'];
final stagingDir = configInfo['staging'];
await Directory(stagingDir + '/test').create(recursive: true);
await resources.unpackScripts(stagingDir);
await fastlane.clearFastlaneDirs(configInfo, screens);

// run integration tests in each android emulator for each locale and
// process screenshots
if (config['devices']['android'] != null)
for (final emulatorName in config['devices']['android']) {
for (final locale in config['locales']) {
if (configInfo['devices']['android'] != null)
for (final emulatorName in configInfo['devices']['android']) {
for (final locale in configInfo['locales']) {
await emulator(emulatorName, true, stagingDir, locale);
await clearFastlaneDir(
screens, emulatorName, locale, DeviceType.android);
// await clearFastlaneDir(
// screens, emulatorName, locale, DeviceType.android);

for (final testPath in config['tests']) {
for (final testPath in configInfo['tests']) {
print(
'Capturing screenshots with test $testPath on emulator $emulatorName in locale $locale ...');
await screenshots(testPath, stagingDir);
// process screenshots
await processImages.process(
screens, config, DeviceType.android, emulatorName, locale);
screens, configInfo, DeviceType.android, emulatorName, locale);
}
await emulator(emulatorName, false, stagingDir);
}
}

// run integration tests in each ios simulator for each locale and
// process screenshots
if (config['devices']['ios'] != null)
for (final simulatorName in config['devices']['ios']) {
for (final locale in config['locales']) {
if (configInfo['devices']['ios'] != null)
for (final simulatorName in configInfo['devices']['ios']) {
for (final locale in configInfo['locales']) {
simulator(simulatorName, true, stagingDir, locale);
await clearFastlaneDir(screens, simulatorName, locale, DeviceType.ios);
for (final testPath in config['tests']) {
// await clearFastlaneDir(screens, simulatorName, locale, DeviceType.ios);
for (final testPath in configInfo['tests']) {
print(
'Capturing screenshots with test $testPath on simulator $simulatorName in locale $locale ...');
await screenshots(testPath, stagingDir);
// process screenshots
await processImages.process(
screens, config, DeviceType.ios, simulatorName, locale);
screens, configInfo, DeviceType.ios, simulatorName, locale);
}
simulator(simulatorName, false);
}
Expand All @@ -84,17 +87,6 @@ Future<void> run([String configPath = kConfigFileName]) async {
print('\nscreenshots completed successfully.');
}

/// Clear image destination
Future clearFastlaneDir(
Map screens, deviceName, locale, DeviceType deviceType) async {
final Map screenProps = Screens().screenProps(screens, deviceName);

final dstDir = fastlane.path(deviceType, locale, '', screenProps['destName']);

print('Clearing images in $dstDir ...');
await utils.clearDirectory(dstDir);
}

///
/// Run the screenshot integration test on current emulator or simulator.
///
Expand Down
36 changes: 23 additions & 13 deletions test/screenshots_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:screenshots/resources.dart';
import 'package:screenshots/screenshots.dart';
import 'package:screenshots/utils.dart';
import 'package:test/test.dart';
import 'package:screenshots/fastlane.dart' as fastlane;

void main() {
test('screen info for device: Nexus 5X', () async {
Expand All @@ -23,8 +24,8 @@ void main() {
'size': '1080x1920'
};
final Screens screens = Screens();
final screensInfo = await Screens().init();
Map screen = screens.screenProps(screensInfo, 'Nexus 5X');
await Screens().init();
Map screen = screens.screenProps('Nexus 5X');
expect(screen, expected);
});

Expand All @@ -37,8 +38,8 @@ void main() {
'size': '2436×1125'
};
final Screens screens = Screens();
final screensInfo = await Screens().init();
Map screen = screens.screenProps(screensInfo, 'iPhone X');
await Screens().init();
Map screen = screens.screenProps('iPhone X');
expect(screen, expected);
});

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

test('overlay statusbar', () async {
final Screens screens = Screens();
final screensInfo = await screens.init();
Map screen = screens.screenProps(screensInfo, 'Nexus 6P');
await screens.init();
Map screen = screens.screenProps('Nexus 6P');
final Config config = Config('test/test_config.yaml');
Map appConfig = config.config;

Expand Down Expand Up @@ -93,9 +94,9 @@ void main() {

test('unpack screen resource images', () async {
final Screens screens = Screens();
final screensInfo = await screens.init();
await screens.init();
// Map screen = screens.screen(screensInfo, 'Nexus 5X');
Map screen = screens.screenProps(screensInfo, 'iPhone 7 Plus');
Map screen = screens.screenProps('iPhone 7 Plus');
final Config config = Config('test/test_config.yaml');
Map appConfig = config.config;

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

test('append navbar', () async {
final Screens screens = Screens();
final screensInfo = await screens.init();
Map screen = screens.screenProps(screensInfo, 'Nexus 6P');
await screens.init();
Map screen = screens.screenProps('Nexus 6P');
final Config config = Config('test/test_config.yaml');
Map appConfig = config.config;

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

test('frame screenshot', () async {
final Screens screens = Screens();
final screensInfo = await screens.init();
Map screen = screens.screenProps(screensInfo, 'Nexus 6P');
await screens.init();
Map screen = screens.screenProps('Nexus 6P');
final Config config = Config('test/test_config.yaml');
Map appConfig = config.config;

Expand Down Expand Up @@ -204,8 +205,10 @@ void main() {
});

test('validate config file', () async {
final Screens screens = Screens();
await screens.init();
final Config config = Config('test/test_config.yaml');
expect(await config.validate(), true);
expect(await config.validate(screens), true);
});

test('rooted emulator', () {
Expand Down Expand Up @@ -251,4 +254,11 @@ void main() {
// await stdout.done;
await streamCmd('ls', ['-33']);
});

test('clear all destination directories on init', () async {
final Screens screens = Screens();
await screens.init();
final Config config = Config('test/test_config.yaml');
await fastlane.clearFastlaneDirs(config.config, screens);
});
}
4 changes: 2 additions & 2 deletions test/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ locales:
# A list of devices to emulate
devices:
ios:
- iPhone X
# - iPhone X
# - iPhone 7 Plus
- iPad Pro (12.9-inch) (2nd generation)
# - iPad Pro (12.9-inch) (2nd generation)
# "iPhone 6",
# "iPhone 6 Plus",
# "iPhone 5",
Expand Down

0 comments on commit 92147d4

Please sign in to comment.