diff --git a/dev/devicelab/bin/tasks/android_sample_catalog_generator.dart b/dev/devicelab/bin/tasks/android_sample_catalog_generator.dart index b9550b36c6..0fa9d4fb26 100644 --- a/dev/devicelab/bin/tasks/android_sample_catalog_generator.dart +++ b/dev/devicelab/bin/tasks/android_sample_catalog_generator.dart @@ -6,9 +6,10 @@ import 'dart:async'; import 'package:flutter_devicelab/framework/adb.dart'; import 'package:flutter_devicelab/framework/framework.dart'; +import 'package:flutter_devicelab/framework/utils.dart'; import 'package:flutter_devicelab/tasks/sample_catalog_generator.dart'; -Future main() async { +Future main(List args) async { deviceOperatingSystem = DeviceOperatingSystem.android; - await task(samplePageCatalogGenerator); + await task(() => samplePageCatalogGenerator(extractCloudAuthTokenArg(args))); } diff --git a/dev/devicelab/bin/tasks/ios_sample_catalog_generator.dart b/dev/devicelab/bin/tasks/ios_sample_catalog_generator.dart index 94f14641c1..20efb83307 100644 --- a/dev/devicelab/bin/tasks/ios_sample_catalog_generator.dart +++ b/dev/devicelab/bin/tasks/ios_sample_catalog_generator.dart @@ -6,9 +6,10 @@ import 'dart:async'; import 'package:flutter_devicelab/framework/adb.dart'; import 'package:flutter_devicelab/framework/framework.dart'; +import 'package:flutter_devicelab/framework/utils.dart'; import 'package:flutter_devicelab/tasks/sample_catalog_generator.dart'; -Future main() async { +Future main(List args) async { deviceOperatingSystem = DeviceOperatingSystem.ios; - await task(samplePageCatalogGenerator); + await task(() => samplePageCatalogGenerator(extractCloudAuthTokenArg(args))); } diff --git a/dev/devicelab/lib/framework/utils.dart b/dev/devicelab/lib/framework/utils.dart index 06de108b5f..08fa44e7ae 100644 --- a/dev/devicelab/lib/framework/utils.dart +++ b/dev/devicelab/lib/framework/utils.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:args/args.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; import 'package:process/process.dart'; @@ -452,3 +453,23 @@ Future findAvailablePort() async { } bool canRun(String path) => _processManager.canRun(path); + +String extractCloudAuthTokenArg(List rawArgs) { + final ArgParser argParser = new ArgParser()..addOption('cloud-auth-token'); + ArgResults args; + try { + args = argParser.parse(rawArgs); + } on FormatException catch(error) { + stderr.writeln('${error.message}\n'); + stderr.writeln('Usage:\n'); + stderr.writeln(argParser.usage); + return null; + } + + final String token = args['cloud-auth-token']; + if (token == null) { + stderr.writeln('Required option --cloud-auth-token not found'); + return null; + } + return token; +} diff --git a/dev/devicelab/lib/tasks/sample_catalog_generator.dart b/dev/devicelab/lib/tasks/sample_catalog_generator.dart index ecd4ce78c6..8b005d1179 100644 --- a/dev/devicelab/lib/tasks/sample_catalog_generator.dart +++ b/dev/devicelab/lib/tasks/sample_catalog_generator.dart @@ -10,7 +10,7 @@ import '../framework/framework.dart'; import '../framework/ios.dart'; import '../framework/utils.dart'; -Future samplePageCatalogGenerator() async { +Future samplePageCatalogGenerator(String authorizationToken) async { final Device device = await devices.workingDevice; await device.unlock(); final String deviceId = device.deviceId; @@ -30,6 +30,12 @@ Future samplePageCatalogGenerator() async { '--device-id', deviceId, ]); + + await dart([ + 'bin/save_screenshots.dart', + await getCurrentFlutterRepoCommit(), + authorizationToken, + ]); }); return new TaskResult.success(null); diff --git a/examples/catalog/animated_list.iml b/examples/catalog/animated_list.iml deleted file mode 100644 index f2b096d125..0000000000 --- a/examples/catalog/animated_list.iml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/examples/catalog/bin/sample_page.dart b/examples/catalog/bin/sample_page.dart index 2ebbb471e1..c9f1ac14cd 100644 --- a/examples/catalog/bin/sample_page.dart +++ b/examples/catalog/bin/sample_page.dart @@ -7,11 +7,13 @@ import 'dart:io'; +import 'package:path/path.dart'; + class SampleError extends Error { SampleError(this.message); final String message; @override - String toString() => message; + String toString() => 'SampleError($message)'; } // Sample apps are .dart files in the lib directory which contain a block @@ -83,14 +85,7 @@ class SampleGenerator { // If sourceFile is lib/foo.dart then sourceName is foo. The sourceName // is used to create derived filenames like foo.md or foo.png. - String get sourceName { - // In /foo/bar/baz.dart, matches baz.dart, match[1] == 'baz' - final RegExp nameRE = new RegExp(r'(\w+)\.dart$'); - final Match nameMatch = nameRE.firstMatch(sourceFile.path); - if (nameMatch.groupCount != 1) - throw new SampleError('bad source file name ${sourceFile.path}'); - return nameMatch[1]; - } + String get sourceName => basenameWithoutExtension(sourceFile.path); // The name of the widget class that defines this sample app, like 'FooSample'. String get sampleClass => commentValues["sample"]; @@ -161,6 +156,8 @@ void generate() { } }); + // Causes the generated imports to appear in alphabetical order. + // Avoid complaints from flutter lint. samples.sort((SampleGenerator a, SampleGenerator b) { return a.sourceName.compareTo(b.sourceName); }); diff --git a/examples/catalog/bin/save_screenshots.dart b/examples/catalog/bin/save_screenshots.dart new file mode 100644 index 0000000000..fd78f02c07 --- /dev/null +++ b/examples/catalog/bin/save_screenshots.dart @@ -0,0 +1,135 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; +import 'dart:math' as math; + +import 'package:image/image.dart'; +import 'package:path/path.dart'; + +String authorizationToken; + +class UploadError extends Error { + UploadError(this.message); + final String message; + @override + String toString() => 'UploadError($message)'; +} + +void logMessage(String s) { print(s); } + +class Upload { + Upload(this.fromPath, this.largeName, this.smallName); + + static math.Random random; + static const String uriAuthority = 'www.googleapis.com'; + static const String uriPath = 'upload/storage/v1/b/flutter-catalog/o'; + + final String fromPath; + final String largeName; + final String smallName; + + List largeImage; + List smallImage; + bool largeImageSaved; + int retryCount = 0; + bool isComplete = false; + + // Exponential backoff per https://cloud.google.com/storage/docs/exponential-backoff + Duration get timeLimit { + if (retryCount == 0) + return const Duration(milliseconds: 1000); + random ??= new math.Random(); + return new Duration(milliseconds: random.nextInt(1000) + math.pow(2, retryCount) * 1000); + } + + Future save(HttpClient client, String name, List content) async { + try { + final Uri uri = new Uri.https(uriAuthority, uriPath, { + 'uploadType': 'media', + 'name': name, + }); + final HttpClientRequest request = await client.postUrl(uri); + request + ..headers.contentType = new ContentType('image', 'png') + ..headers.add('Authorization', 'Bearer $authorizationToken') + ..add(content); + + final HttpClientResponse response = await request.close().timeout(timeLimit); + if (response.statusCode == HttpStatus.OK) { + await response.drain(); + } else { + // TODO(hansmuller): only retry on 5xx and 429 responses + logMessage('Request to save "$name" (length ${content.length}) failed with status ${response.statusCode}, will retry'); + logMessage(await response.transform(UTF8.decoder).join()); + } + return response.statusCode == HttpStatus.OK; + } on TimeoutException catch (_) { + logMessage('Request to save "$name" (length ${content.length}) timed out, will retry'); + return false; + } + } + + Future run(HttpClient client) async { + assert(!isComplete); + if (retryCount > 2) + throw new UploadError('upload of "$fromPath" to "$largeName" and "$smallName" failed after 2 retries'); + + largeImage ??= await new File(fromPath).readAsBytes(); + smallImage ??= encodePng(copyResize(decodePng(largeImage), 400)); + + if (!largeImageSaved) + largeImageSaved = await save(client, largeName, largeImage); + isComplete = largeImageSaved && await save(client, smallName, smallImage); + + retryCount += 1; + return isComplete; + } + + static bool isNotComplete(Upload upload) => !upload.isComplete; +} + +Future saveScreenshots(List fromPaths, List largeNames, List smallNames) async { + assert(fromPaths.length == largeNames.length); + assert(fromPaths.length == smallNames.length); + + List uploads = new List(fromPaths.length); + for (int index = 0; index < uploads.length; index += 1) + uploads[index] = new Upload(fromPaths[index], largeNames[index], smallNames[index]); + + final HttpClient client = new HttpClient(); + while(uploads.any(Upload.isNotComplete)) { + uploads = uploads.where(Upload.isNotComplete).toList(); + await Future.wait(uploads.map((Upload upload) => upload.run(client))); + } + client.close(); +} + + +// If path is lib/foo.png then screenshotName is foo. +String screenshotName(String path) => basenameWithoutExtension(path); + +Future main(List args) async { + if (args.length != 2) + throw new UploadError('Usage: dart bin/save_screenshots.dart commit authorization'); + + final Directory outputDirectory = new Directory('.generated'); + final List screenshots = []; + outputDirectory.listSync().forEach((FileSystemEntity entity) { + if (entity is File && entity.path.endsWith('.png')) { + final File file = entity; + screenshots.add(file.path); + } + }); + + final String commit = args[0]; + final List largeNames = []; // Cloud storage names for the full res screenshots. + final List smallNames = []; // Likewise for the scaled down screenshots. + for (String path in screenshots) { + final String name = screenshotName(path); + largeNames.add('$commit/$name.png'); + smallNames.add('$commit/${name}_small.png'); + } + + authorizationToken = args[1]; + await saveScreenshots(screenshots, largeNames, smallNames); +} diff --git a/examples/catalog/bin/screenshot_test.dart.template b/examples/catalog/bin/screenshot_test.dart.template index 93f5f73f8f..4457b0617e 100644 --- a/examples/catalog/bin/screenshot_test.dart.template +++ b/examples/catalog/bin/screenshot_test.dart.template @@ -8,6 +8,7 @@ import 'package:test/test.dart'; void main() { group('sample screenshots', () { + final String prefix = Platform.isMacOS ? 'ios_' : ""; FlutterDriver driver; setUpAll(() async { @@ -19,7 +20,6 @@ void main() { }); test('take sample screenshots', () async { - final String prefix = Platform.isMacOS ? 'ios_' : ""; final List paths = [ @(paths) ]; diff --git a/examples/catalog/pubspec.yaml b/examples/catalog/pubspec.yaml index b8219d79a5..3c2aec5e80 100644 --- a/examples/catalog/pubspec.yaml +++ b/examples/catalog/pubspec.yaml @@ -1,8 +1,10 @@ -name: animated_list -description: A sample app for AnimatedList +name: sample_catalog +description: A collection of Flutter sample apps dependencies: flutter: sdk: flutter + image: + path: ^1.4.0 dev_dependencies: flutter_test: