From 92147d4d396efbebc5ffb547684421534b2990e5 Mon Sep 17 00:00:00 2001 From: Maurice McCabe Date: Tue, 12 Feb 2019 16:53:21 -0800 Subject: [PATCH] Cleared fastlane image directories on init --- lib/config.dart | 14 +++++----- lib/fastlane.dart | 33 ++++++++++++++++++++++++ lib/process_images.dart | 6 ++--- lib/screens.dart | 10 +++++--- lib/screenshots.dart | 52 ++++++++++++++++---------------------- test/screenshots_test.dart | 36 ++++++++++++++++---------- test/test_config.yaml | 4 +-- 7 files changed, 97 insertions(+), 58 deletions(-) diff --git a/lib/config.dart b/lib/config.dart index a0ceb45a..3dce4acf 100644 --- a/lib/config.dart +++ b/lib/config.dart @@ -21,8 +21,8 @@ class Config { /// Check emulators and simulators are installed, /// matching screen is available and tests exist. - Future validate() async { - final Map screens = await Screens().init(); + Future validate(Screens screens) async { +// final Map screens = await Screens().init(); if (config['devices']['android'] != null) { // check emulators @@ -83,7 +83,7 @@ class Config { return true; } - void configGuide(Map screens) { + void configGuide(Screens screens) { installedEmulators(utils.emulators()); installedSimulators(utils.simulators()); supportedDevices(screens); @@ -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' @@ -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']) { diff --git a/lib/fastlane.dart b/lib/fastlane.dart index d91426d1..6a99a42b 100644 --- a/lib/fastlane.dart +++ b/lib/fastlane.dart @@ -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 @@ -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); + } + } +} diff --git a/lib/process_images.dart b/lib/process_images.dart index a08fedd7..ad3af70c 100644 --- a/lib/process_images.dart +++ b/lib/process_images.dart @@ -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'); diff --git a/lib/screens.dart b/lib/screens.dart index d8fdf0e8..dbe6c446 100644 --- a/lib/screens.dart +++ b/lib/screens.dart @@ -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 init() async { + Future 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) { diff --git a/lib/screenshots.dart b/lib/screenshots.dart index 9ce23356..e848649e 100644 --- a/lib/screenshots.dart +++ b/lib/screenshots.dart @@ -23,34 +23,37 @@ enum DeviceType { android, ios } /// 4. Move processed screenshots to fastlane destination for upload to stores. /// 5. Stop emulator/simulator. Future 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); } @@ -58,18 +61,18 @@ Future run([String configPath = kConfigFileName]) async { // 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); } @@ -84,17 +87,6 @@ Future 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. /// diff --git a/test/screenshots_test.dart b/test/screenshots_test.dart index 9a4de6e0..fa18a93d 100644 --- a/test/screenshots_test.dart +++ b/test/screenshots_test.dart @@ -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 { @@ -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); }); @@ -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); }); @@ -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; @@ -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; @@ -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; @@ -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; @@ -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', () { @@ -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); + }); } diff --git a/test/test_config.yaml b/test/test_config.yaml index 891396a2..b369b9ff 100644 --- a/test/test_config.yaml +++ b/test/test_config.yaml @@ -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",