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