2016-05-01 22:52:51 +00:00
|
|
|
// Copyright 2016 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:path/path.dart' as path;
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
const String kDocRoot = 'dev/docs/doc';
|
|
|
|
|
2016-05-01 22:52:51 +00:00
|
|
|
/// This script expects to run with the cwd as the root of the flutter repo. It
|
|
|
|
/// will generate documentation for the packages in `//packages/` and write the
|
|
|
|
/// documentation to `//dev/docs/doc/api/`.
|
2016-05-16 15:28:58 +00:00
|
|
|
///
|
|
|
|
/// This script also updates the index.html file so that it can be placed
|
|
|
|
/// at the root of docs.flutter.io. We are keeping the files inside of
|
|
|
|
/// docs.flutter.io/flutter for now, so we need to manipulate paths
|
|
|
|
/// a bit. See https://github.com/flutter/flutter/issues/3900 for more info.
|
2016-05-04 18:43:01 +00:00
|
|
|
Future<Null> main(List<String> args) async {
|
2016-05-01 22:52:51 +00:00
|
|
|
// If we're run from the `tools` dir, set the cwd to the repo root.
|
|
|
|
if (path.basename(Directory.current.path) == 'tools')
|
|
|
|
Directory.current = Directory.current.parent.parent;
|
|
|
|
|
|
|
|
// Create the pubspec.yaml file.
|
|
|
|
StringBuffer buf = new StringBuffer('''
|
|
|
|
name: Flutter
|
|
|
|
dependencies:
|
|
|
|
''');
|
2016-05-16 15:28:58 +00:00
|
|
|
for (String package in findPackageNames()) {
|
2016-05-01 22:52:51 +00:00
|
|
|
buf.writeln(' $package:');
|
|
|
|
buf.writeln(' path: ../../packages/$package');
|
|
|
|
}
|
|
|
|
new File('dev/docs/pubspec.yaml').writeAsStringSync(buf.toString());
|
|
|
|
|
|
|
|
// Create the library file.
|
|
|
|
Directory libDir = new Directory('dev/docs/lib');
|
|
|
|
libDir.createSync();
|
|
|
|
|
|
|
|
StringBuffer contents = new StringBuffer('library temp_doc;\n\n');
|
2016-05-16 15:28:58 +00:00
|
|
|
for (String libraryRef in libraryRefs()) {
|
2016-05-01 22:52:51 +00:00
|
|
|
contents.writeln('import \'package:$libraryRef\';');
|
|
|
|
}
|
|
|
|
new File('dev/docs/lib/temp_doc.dart').writeAsStringSync(contents.toString());
|
|
|
|
|
|
|
|
// Run pub.
|
|
|
|
Process process = await Process.start('pub', <String>['get'], workingDirectory: 'dev/docs');
|
2016-05-16 15:28:58 +00:00
|
|
|
printStream(process.stdout);
|
|
|
|
printStream(process.stderr);
|
2016-05-01 22:52:51 +00:00
|
|
|
int code = await process.exitCode;
|
|
|
|
if (code != 0)
|
|
|
|
exit(code);
|
|
|
|
|
|
|
|
// Generate the documentation; we require dartdoc >= 0.9.4.
|
|
|
|
List<String> args = <String>[
|
|
|
|
'global', 'run', 'dartdoc',
|
|
|
|
'--header', 'styles.html',
|
|
|
|
'--header', 'analytics.html',
|
|
|
|
'--dart-sdk', '../../bin/cache/dart-sdk',
|
|
|
|
'--exclude', 'temp_doc',
|
|
|
|
'--favicon=favicon.ico',
|
|
|
|
'--use-categories'
|
|
|
|
];
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
for (String libraryRef in libraryRefs()) {
|
2016-05-01 22:52:51 +00:00
|
|
|
String name = path.basename(libraryRef);
|
|
|
|
args.add('--include-external');
|
|
|
|
args.add(name.substring(0, name.length - 5));
|
|
|
|
}
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
findSkyServicesLibraryNames().forEach((String libName) {
|
2016-05-01 22:52:51 +00:00
|
|
|
args.add('--include-external');
|
|
|
|
args.add(libName);
|
|
|
|
});
|
|
|
|
|
|
|
|
process = await Process.start('pub', args, workingDirectory: 'dev/docs');
|
2016-05-16 15:28:58 +00:00
|
|
|
printStream(process.stdout);
|
|
|
|
printStream(process.stderr);
|
|
|
|
int exitCode = await process.exitCode;
|
|
|
|
|
|
|
|
if (exitCode != 0)
|
|
|
|
exit(exitCode);
|
|
|
|
|
|
|
|
createIndexAndCleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a custom index.html because we try to maintain old
|
|
|
|
/// paths. Cleanup unused index.html files no longer needed.
|
|
|
|
void createIndexAndCleanup() {
|
|
|
|
print('\nCreating a custom index.html in $kDocRoot/index.html');
|
2016-05-16 20:05:13 +00:00
|
|
|
removeOldFlutterDocsDir();
|
2016-05-16 15:28:58 +00:00
|
|
|
renameApiDir();
|
|
|
|
copyIndexToRootOfDocs();
|
|
|
|
addHtmlBaseToIndex();
|
|
|
|
putRedirectInOldIndexLocation();
|
|
|
|
print('\nDocs ready to go!');
|
|
|
|
}
|
|
|
|
|
2016-05-16 20:05:13 +00:00
|
|
|
void removeOldFlutterDocsDir() {
|
|
|
|
try {
|
|
|
|
new Directory('$kDocRoot/flutter').deleteSync(recursive: true);
|
|
|
|
} catch(e) {
|
|
|
|
// If the directory does not exist, that's OK.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
void renameApiDir() {
|
|
|
|
new Directory('$kDocRoot/api').renameSync('$kDocRoot/flutter');
|
|
|
|
}
|
|
|
|
|
|
|
|
File copyIndexToRootOfDocs() {
|
|
|
|
return new File('$kDocRoot/flutter/index.html').copySync('$kDocRoot/index.html');
|
|
|
|
}
|
|
|
|
|
|
|
|
void addHtmlBaseToIndex() {
|
|
|
|
File indexFile = new File('$kDocRoot/index.html');
|
|
|
|
String indexContents = indexFile.readAsStringSync();
|
|
|
|
indexContents = indexContents.replaceFirst('</title>\n',
|
|
|
|
'</title>\n <base href="./flutter/">\n');
|
|
|
|
indexFile.writeAsStringSync(indexContents);
|
|
|
|
}
|
|
|
|
|
|
|
|
void putRedirectInOldIndexLocation() {
|
|
|
|
String metaTag = '<meta http-equiv="refresh" content="0;URL=../index.html">';
|
|
|
|
new File('$kDocRoot/flutter/index.html').writeAsStringSync(metaTag);
|
2016-05-01 22:52:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
List<String> findSkyServicesLibraryNames() {
|
2016-05-01 22:52:51 +00:00
|
|
|
Directory skyServicesLocation = new Directory('bin/cache/pkg/sky_services/lib');
|
|
|
|
if (!skyServicesLocation.existsSync()) {
|
|
|
|
throw 'Did not find sky_services package location in ${skyServicesLocation.path}.';
|
|
|
|
}
|
|
|
|
return skyServicesLocation.listSync(followLinks: false, recursive: true)
|
|
|
|
.where((FileSystemEntity entity) {
|
|
|
|
return entity is File && entity.path.endsWith('.mojom.dart');
|
|
|
|
}).map((FileSystemEntity entity) {
|
|
|
|
String basename = path.basename(entity.path);
|
|
|
|
basename = basename.substring(0, basename.length-('.dart'.length));
|
|
|
|
return basename.replaceAll('.', '_');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
List<String> findPackageNames() {
|
|
|
|
return findPackages().map((Directory dir) => path.basename(dir.path)).toList();
|
2016-05-01 22:52:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
List<Directory> findPackages() {
|
2016-05-01 22:52:51 +00:00
|
|
|
return new Directory('packages')
|
|
|
|
.listSync()
|
|
|
|
.where((FileSystemEntity entity) => entity is Directory)
|
|
|
|
.where((Directory dir) {
|
|
|
|
File pubspec = new File('${dir.path}/pubspec.yaml');
|
|
|
|
bool nodoc = pubspec.readAsStringSync().contains('nodoc: true');
|
|
|
|
return !nodoc;
|
|
|
|
})
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
Iterable<String> libraryRefs() sync* {
|
|
|
|
for (Directory dir in findPackages()) {
|
2016-05-01 22:52:51 +00:00
|
|
|
String dirName = path.basename(dir.path);
|
|
|
|
|
|
|
|
for (FileSystemEntity file in new Directory('${dir.path}/lib').listSync()) {
|
|
|
|
if (file is File && file.path.endsWith('.dart'))
|
|
|
|
yield '$dirName/${path.basename(file.path)}';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 15:28:58 +00:00
|
|
|
void printStream(Stream<List<int>> stream) {
|
2016-05-01 22:52:51 +00:00
|
|
|
stream
|
|
|
|
.transform(UTF8.decoder)
|
|
|
|
.transform(const LineSplitter())
|
|
|
|
.listen(print);
|
|
|
|
}
|