Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 5 | import 'dart:io'; |
Chris Bracken | 7022f98 | 2019-01-10 15:23:59 -0800 | [diff] [blame] | 6 | import 'dart:math'; |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 7 | |
| 8 | import 'package:archive/archive.dart'; |
| 9 | import 'package:http/http.dart' as http; |
| 10 | |
| 11 | const String kDocRoot = 'dev/docs/doc'; |
| 12 | |
| 13 | /// This script downloads an archive of Javadoc and objc doc for the engine from |
| 14 | /// the artifact store and extracts them to the location used for Dartdoc. |
Alexandre Ardhuin | d340e2f | 2018-10-04 18:44:23 +0200 | [diff] [blame] | 15 | Future<void> main(List<String> args) async { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 16 | final String engineVersion = File('bin/internal/engine.version').readAsStringSync().trim(); |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 17 | |
godofredoc | da6528c | 2021-02-04 18:44:47 -0800 | [diff] [blame] | 18 | final String javadocUrl = 'https://storage.googleapis.com/flutter_infra_release/flutter/$engineVersion/android-javadoc.zip'; |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 19 | generateDocs(javadocUrl, 'javadoc', 'io/flutter/view/FlutterView.html'); |
| 20 | |
godofredoc | da6528c | 2021-02-04 18:44:47 -0800 | [diff] [blame] | 21 | final String objcdocUrl = 'https://storage.googleapis.com/flutter_infra_release/flutter/$engineVersion/ios-objcdoc.zip'; |
Ian Hickson | 6655074 | 2017-07-28 15:44:38 -0700 | [diff] [blame] | 22 | generateDocs(objcdocUrl, 'objcdoc', 'Classes/FlutterViewController.html'); |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 23 | } |
| 24 | |
Chris Bracken | 7022f98 | 2019-01-10 15:23:59 -0800 | [diff] [blame] | 25 | /// Fetches the zip archive at the specified url. |
| 26 | /// |
| 27 | /// Returns null if the archive fails to download after [maxTries] attempts. |
Christopher Fujino | 8654e4a | 2021-04-23 20:24:03 -0700 | [diff] [blame] | 28 | Future<Archive?> fetchArchive(String url, int maxTries) async { |
| 29 | List<int>? responseBytes; |
Chris Bracken | 7022f98 | 2019-01-10 15:23:59 -0800 | [diff] [blame] | 30 | for (int i = 0; i < maxTries; i++) { |
Nate Bosch | dcc4fdd | 2021-01-20 09:59:03 -0800 | [diff] [blame] | 31 | final http.Response response = await http.get(Uri.parse(url)); |
Chris Bracken | 7022f98 | 2019-01-10 15:23:59 -0800 | [diff] [blame] | 32 | if (response.statusCode == 200) { |
| 33 | responseBytes = response.bodyBytes; |
| 34 | break; |
| 35 | } |
| 36 | stderr.writeln('Failed attempt ${i+1} to fetch $url.'); |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 37 | |
Chris Bracken | 7022f98 | 2019-01-10 15:23:59 -0800 | [diff] [blame] | 38 | // On failure print a short snipped from the body in case it's helpful. |
Chris Bracken | 17d741e | 2019-01-10 16:05:39 -0800 | [diff] [blame] | 39 | final int bodyLength = min(1024, response.body.length); |
Alexandre Ardhuin | 34059ee | 2021-06-01 20:14:06 +0200 | [diff] [blame] | 40 | stderr.writeln('Response status code ${response.statusCode}. Body: ${response.body.substring(0, bodyLength)}'); |
Chris Bracken | 7022f98 | 2019-01-10 15:23:59 -0800 | [diff] [blame] | 41 | sleep(const Duration(seconds: 1)); |
| 42 | } |
| 43 | return responseBytes == null ? null : ZipDecoder().decodeBytes(responseBytes); |
| 44 | } |
| 45 | |
| 46 | Future<void> generateDocs(String url, String docName, String checkFile) async { |
| 47 | const int maxTries = 5; |
Christopher Fujino | 8654e4a | 2021-04-23 20:24:03 -0700 | [diff] [blame] | 48 | final Archive? archive = await fetchArchive(url, maxTries); |
Chris Bracken | 7022f98 | 2019-01-10 15:23:59 -0800 | [diff] [blame] | 49 | if (archive == null) { |
| 50 | stderr.writeln('Failed to fetch zip archive from: $url after $maxTries attempts. Giving up.'); |
| 51 | exit(1); |
| 52 | } |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 53 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 54 | final Directory output = Directory('$kDocRoot/$docName'); |
Ian Hickson | 6e87292 | 2018-01-30 09:00:45 -0800 | [diff] [blame] | 55 | print('Extracting $docName to ${output.path}'); |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 56 | output.createSync(recursive: true); |
| 57 | |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 58 | for (final ArchiveFile af in archive) { |
Dan Field | 496c573 | 2019-01-24 16:02:45 -0800 | [diff] [blame] | 59 | if (!af.name.endsWith('/')) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 60 | final File file = File('${output.path}/${af.name}'); |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 61 | file.createSync(recursive: true); |
Alexandre Ardhuin | ec1a015 | 2019-12-05 22:34:06 +0100 | [diff] [blame] | 62 | file.writeAsBytesSync(af.content as List<int>); |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 66 | final File testFile = File('${output.path}/$checkFile'); |
Sarah Zakarias | d97b13b | 2017-06-26 12:15:24 +0200 | [diff] [blame] | 67 | if (!testFile.existsSync()) { |
| 68 | print('Expected file ${testFile.path} not found'); |
| 69 | exit(1); |
| 70 | } |
| 71 | print('$docName ready to go!'); |
| 72 | } |