java_and_objc_doc.dart 2.73 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';
6
import 'dart:math';
7 8 9 10 11 12 13 14

import 'package:archive/archive.dart';
import 'package:http/http.dart' as http;

const String kDocRoot = 'dev/docs/doc';

/// This script downloads an archive of Javadoc and objc doc for the engine from
/// the artifact store and extracts them to the location used for Dartdoc.
15
Future<void> main(List<String> args) async {
16
  final String engineVersion = File('bin/internal/engine.version').readAsStringSync().trim();
17

18
  final String javadocUrl = 'https://storage.googleapis.com/flutter_infra_release/flutter/$engineVersion/android-javadoc.zip';
19 20
  generateDocs(javadocUrl, 'javadoc', 'io/flutter/view/FlutterView.html');

21
  final String objcdocUrl = 'https://storage.googleapis.com/flutter_infra_release/flutter/$engineVersion/ios-objcdoc.zip';
22
  generateDocs(objcdocUrl, 'objcdoc', 'Classes/FlutterViewController.html');
23 24
}

25 26 27 28 29 30
/// Fetches the zip archive at the specified url.
///
/// Returns null if the archive fails to download after [maxTries] attempts.
Future<Archive> fetchArchive(String url, int maxTries) async {
  List<int> responseBytes;
  for (int i = 0; i < maxTries; i++) {
31
    final http.Response response = await http.get(Uri.parse(url));
32 33 34 35 36 37 38
    if (response.statusCode == 200) {
      responseBytes = response.bodyBytes;
      break;
    }
    stderr.writeln('Failed attempt ${i+1} to fetch $url.');

    // On failure print a short snipped from the body in case it's helpful.
39
    final int bodyLength = min(1024, response.body.length);
40 41 42 43 44
    stderr.writeln('Response status code ${response.statusCode}. Body: ' + response.body.substring(0, bodyLength));
    sleep(const Duration(seconds: 1));
  }
  return responseBytes == null ? null : ZipDecoder().decodeBytes(responseBytes);
}
45

46 47 48 49 50 51 52
Future<void> generateDocs(String url, String docName, String checkFile) async {
  const int maxTries = 5;
  final Archive archive = await fetchArchive(url, maxTries);
  if (archive == null) {
    stderr.writeln('Failed to fetch zip archive from: $url after $maxTries attempts. Giving up.');
    exit(1);
  }
53

54
  final Directory output = Directory('$kDocRoot/$docName');
55
  print('Extracting $docName to ${output.path}');
56 57
  output.createSync(recursive: true);

58
  for (final ArchiveFile af in archive) {
Dan Field's avatar
Dan Field committed
59
    if (!af.name.endsWith('/')) {
60
      final File file = File('${output.path}/${af.name}');
61
      file.createSync(recursive: true);
62
      file.writeAsBytesSync(af.content as List<int>);
63 64 65
    }
  }

66
  final File testFile = File('${output.path}/$checkFile');
67 68 69 70 71 72
  if (!testFile.existsSync()) {
    print('Expected file ${testFile.path} not found');
    exit(1);
  }
  print('$docName ready to go!');
}