blob: 2cdaefba34a59259fe0b37f57019633e61160ab5 [file] [log] [blame]
Ian Hickson449f4a62019-11-27 15:04:02 -08001// Copyright 2014 The Flutter Authors. All rights reserved.
Sarah Zakariasd97b13b2017-06-26 12:15:24 +02002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Sarah Zakariasd97b13b2017-06-26 12:15:24 +02005import 'dart:io';
Chris Bracken7022f982019-01-10 15:23:59 -08006import 'dart:math';
Sarah Zakariasd97b13b2017-06-26 12:15:24 +02007
8import 'package:archive/archive.dart';
9import 'package:http/http.dart' as http;
10
11const 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 Ardhuind340e2f2018-10-04 18:44:23 +020015Future<void> main(List<String> args) async {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020016 final String engineVersion = File('bin/internal/engine.version').readAsStringSync().trim();
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020017
godofredocda6528c2021-02-04 18:44:47 -080018 final String javadocUrl = 'https://storage.googleapis.com/flutter_infra_release/flutter/$engineVersion/android-javadoc.zip';
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020019 generateDocs(javadocUrl, 'javadoc', 'io/flutter/view/FlutterView.html');
20
godofredocda6528c2021-02-04 18:44:47 -080021 final String objcdocUrl = 'https://storage.googleapis.com/flutter_infra_release/flutter/$engineVersion/ios-objcdoc.zip';
Ian Hickson66550742017-07-28 15:44:38 -070022 generateDocs(objcdocUrl, 'objcdoc', 'Classes/FlutterViewController.html');
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020023}
24
Chris Bracken7022f982019-01-10 15:23:59 -080025/// Fetches the zip archive at the specified url.
26///
27/// Returns null if the archive fails to download after [maxTries] attempts.
Christopher Fujino8654e4a2021-04-23 20:24:03 -070028Future<Archive?> fetchArchive(String url, int maxTries) async {
29 List<int>? responseBytes;
Chris Bracken7022f982019-01-10 15:23:59 -080030 for (int i = 0; i < maxTries; i++) {
Nate Boschdcc4fdd2021-01-20 09:59:03 -080031 final http.Response response = await http.get(Uri.parse(url));
Chris Bracken7022f982019-01-10 15:23:59 -080032 if (response.statusCode == 200) {
33 responseBytes = response.bodyBytes;
34 break;
35 }
36 stderr.writeln('Failed attempt ${i+1} to fetch $url.');
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020037
Chris Bracken7022f982019-01-10 15:23:59 -080038 // On failure print a short snipped from the body in case it's helpful.
Chris Bracken17d741e2019-01-10 16:05:39 -080039 final int bodyLength = min(1024, response.body.length);
Alexandre Ardhuin34059ee2021-06-01 20:14:06 +020040 stderr.writeln('Response status code ${response.statusCode}. Body: ${response.body.substring(0, bodyLength)}');
Chris Bracken7022f982019-01-10 15:23:59 -080041 sleep(const Duration(seconds: 1));
42 }
43 return responseBytes == null ? null : ZipDecoder().decodeBytes(responseBytes);
44}
45
46Future<void> generateDocs(String url, String docName, String checkFile) async {
47 const int maxTries = 5;
Christopher Fujino8654e4a2021-04-23 20:24:03 -070048 final Archive? archive = await fetchArchive(url, maxTries);
Chris Bracken7022f982019-01-10 15:23:59 -080049 if (archive == null) {
50 stderr.writeln('Failed to fetch zip archive from: $url after $maxTries attempts. Giving up.');
51 exit(1);
52 }
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020053
Alexandre Ardhuind927c932018-09-12 08:29:29 +020054 final Directory output = Directory('$kDocRoot/$docName');
Ian Hickson6e872922018-01-30 09:00:45 -080055 print('Extracting $docName to ${output.path}');
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020056 output.createSync(recursive: true);
57
Alexandre Ardhuin4f9b6cf2020-01-07 16:32:04 +010058 for (final ArchiveFile af in archive) {
Dan Field496c5732019-01-24 16:02:45 -080059 if (!af.name.endsWith('/')) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020060 final File file = File('${output.path}/${af.name}');
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020061 file.createSync(recursive: true);
Alexandre Ardhuinec1a0152019-12-05 22:34:06 +010062 file.writeAsBytesSync(af.content as List<int>);
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020063 }
64 }
65
Alexandre Ardhuind927c932018-09-12 08:29:29 +020066 final File testFile = File('${output.path}/$checkFile');
Sarah Zakariasd97b13b2017-06-26 12:15:24 +020067 if (!testFile.existsSync()) {
68 print('Expected file ${testFile.path} not found');
69 exit(1);
70 }
71 print('$docName ready to go!');
72}