Skip to content

Migrate most of what's left in tools/ #2840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions tool/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'dart:async';

import 'package:build/build.dart';
Expand Down
28 changes: 12 additions & 16 deletions tool/doc_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

/// A CLI tool to generate documentation for packages from pub.dartlang.org.
library dartdoc.doc_packages;

Expand Down Expand Up @@ -86,7 +84,7 @@ void performGenerate(int page) {

_packageUrls(page).then((List<String> packages) {
return _getPackageInfos(packages).then((List<PackageInfo> infos) {
return Future.forEach(infos, (info) {
return Future.forEach(infos, (PackageInfo info) {
return _printGenerationResult(info, _generateFor(info));
});
});
Expand Down Expand Up @@ -147,7 +145,7 @@ Future<List<PackageInfo>> _getPackageInfos(List<String> packageUrls) {
return Future.wait(futures);
}

StringBuffer _logBuffer;
StringBuffer? _logBuffer;

/// Generate the docs for the given package into _rootDir. Return whether
/// generation was performed or was skipped (due to an older package).
Expand Down Expand Up @@ -195,7 +193,7 @@ Future<bool> _generateFor(PackageInfo package) async {
}

Future<void> _exec(String command, List<String> args,
{String cwd,
{String? cwd,
bool quiet = false,
Duration timeout = const Duration(seconds: 60)}) {
return Process.start(command, args, workingDirectory: cwd)
Expand All @@ -209,20 +207,16 @@ Future<void> _exec(String command, List<String> args,
if (code != 0) throw code;
});

if (timeout != null) {
return f.timeout(timeout, onTimeout: () {
_log('Timing out operation $command.');
process.kill();
throw 'timeout on $command';
});
} else {
return f;
}
return f.timeout(timeout, onTimeout: () {
_log('Timing out operation $command.');
process.kill();
throw 'timeout on $command';
});
});
}

bool _isOldSdkConstraint(Map<String, dynamic> pubspecInfo) {
var environment = pubspecInfo['environment'] as Map;
var environment = pubspecInfo['environment'] as Map?;
if (environment != null) {
var sdk = environment['sdk'];
if (sdk != null) {
Expand All @@ -243,8 +237,10 @@ bool _isOldSdkConstraint(Map<String, dynamic> pubspecInfo) {
return false;
}

/// Log entries will be dropped if [_logBuffer] has not been initialized.
void _log(String str) {
_logBuffer.write(str);
assert(_logBuffer != null);
_logBuffer?.write(str);
}

class PackageInfo {
Expand Down
6 changes: 3 additions & 3 deletions tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ Future<void> testWithAnalyzerSdk() async {
workingDirectory: sdkDartdoc);
}

Future<List<Map<Object, Object>>> _buildSdkDocs(
Future<Iterable<Map<String, Object>>> _buildSdkDocs(
String sdkDocsPath, Future<String> futureCwd,
[String label]) async {
label ??= '';
Expand All @@ -576,7 +576,7 @@ Future<List<Map<Object, Object>>> _buildSdkDocs(
workingDirectory: cwd);
}

Future<List<Map<Object, Object>>> _buildTestPackageDocs(
Future<Iterable<Map<String, Object>>> _buildTestPackageDocs(
String outputDir, String cwd,
{List<String> params, String label = '', String testPackagePath}) async {
if (label != '') label = '-$label';
Expand Down Expand Up @@ -924,7 +924,7 @@ class FlutterRepo {
SubprocessLauncher launcher;
}

Future<List<Map<Object, Object>>> _buildFlutterDocs(
Future<Iterable<Map<String, Object>>> _buildFlutterDocs(
String flutterPath, Future<String> futureCwd, Map<String, String> env,
[String label]) async {
var flutterRepo = await FlutterRepo.copyFromExistingFlutterRepo(
Expand Down
93 changes: 42 additions & 51 deletions tool/subprocess_launcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand All @@ -14,11 +12,9 @@ import 'package:path/path.dart' as p;
/// Keeps track of coverage data automatically for any processes run by this
/// [CoverageSubprocessLauncher]. Requires that these be dart processes.
class CoverageSubprocessLauncher extends SubprocessLauncher {
CoverageSubprocessLauncher(String context, [Map<String, String> environment])
: super(context, environment) {
environment ??= {};
environment['DARTDOC_COVERAGE_DATA'] = tempDir.path;
}
CoverageSubprocessLauncher(String context, [Map<String, String>? environment])
: super(
context, {...?environment, 'DARTDOC_COVERAGE_DATA': tempDir.path});

static int nextRun = 0;

Expand All @@ -31,24 +27,20 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
// is enabled.
static List<Future<Iterable<Map<Object, Object>>>> coverageResults = [];

static Directory _tempDir;
static Directory get tempDir {
if (_tempDir == null) {
if (Platform.environment['DARTDOC_COVERAGE_DATA'] != null) {
_tempDir = Directory(Platform.environment['DARTDOC_COVERAGE_DATA']);
} else {
_tempDir = Directory.systemTemp.createTempSync('dartdoc_coverage_data');
}
static Directory tempDir = () {
var coverageData = Platform.environment['DARTDOC_COVERAGE_DATA'];
if (coverageData != null) {
return Directory(coverageData);
}
return _tempDir;
}
return Directory.systemTemp.createTempSync('dartdoc_coverage_data');
}();

static String buildNextCoverageFilename() =>
p.join(tempDir.path, 'dart-cov-$pid-${nextRun++}.json');

/// Call once all coverage runs have been generated by calling runStreamed
/// on all [CoverageSubprocessLaunchers].
static Future<void> generateCoverageToFile(
static Future<dynamic> generateCoverageToFile(
File outputFile, ResourceProvider resourceProvider) async {
if (!coverageEnabled) return Future.value(null);
var currentCoverageResults = coverageResults;
Expand All @@ -75,12 +67,12 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
}

@override
Future<Iterable<Map<Object, Object>>> runStreamed(
Future<Iterable<Map<String, Object>>> runStreamed(
String executable, List<String> arguments,
{String workingDirectory,
Map<String, String> environment,
{String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
void Function(String) perLine}) async {
void Function(String)? perLine}) async {
environment ??= {};
assert(
executable == Platform.executable ||
Expand All @@ -91,16 +83,17 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
void parsePortAsString(String line) {
if (!portAsString.isCompleted && coverageEnabled) {
var m = _observatoryPortRegexp.matchAsPrefix(line);
if (m?.group(1) != null) portAsString.complete(m.group(1));
if (m != null) {
if (m.group(1) != null) portAsString.complete(m.group(1));
}
} else {
if (perLine != null) perLine(line);
}
}

Completer<Iterable<Map<Object, Object>>> coverageResult;
Completer<Iterable<Map<Object, Object>>> coverageResult = Completer();

if (coverageEnabled) {
coverageResult = Completer();
// This must be added before awaiting in this method.
coverageResults.add(coverageResult.future);
arguments = [
Expand Down Expand Up @@ -139,29 +132,27 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {

class SubprocessLauncher {
final String context;
final Map<String, String> environmentDefaults;
final Map<String, String> environmentDefaults = {};

String get prefix => context.isNotEmpty ? '$context: ' : '';

// from flutter:dev/tools/dartdoc.dart, modified
static Future<void> _printStream(Stream<List<int>> stream, Stdout output,
{String prefix = '', Iterable<String> Function(String line) filter}) {
assert(prefix != null);
filter ??= (line) => [line];
{String prefix = '',
required Iterable<String> Function(String line) filter}) {
return stream
.transform(utf8.decoder)
.transform(const LineSplitter())
.expand(filter)
.listen((String line) {
if (line != null) {
output.write('$prefix$line'.trim());
output.write('\n');
}
output.write('$prefix$line'.trim());
output.write('\n');
}).asFuture();
}

SubprocessLauncher(this.context, [Map<String, String> environment])
: environmentDefaults = environment ?? <String, String>{};
SubprocessLauncher(this.context, [Map<String, String>? environment]) {
environmentDefaults.addAll(environment ?? {});
}

/// A wrapper around start/await process.exitCode that will display the
/// output of the executable continuously and fail on non-zero exit codes.
Expand All @@ -173,22 +164,23 @@ class SubprocessLauncher {
/// Windows (though some of the bashisms will no longer make sense).
/// TODO(jcollins-g): refactor to return a stream of stderr/stdout lines
/// and their associated JSON objects.
Future<Iterable<Map<Object, Object>>> runStreamed(
Future<Iterable<Map<String, Object>>> runStreamed(
String executable, List<String> arguments,
{String workingDirectory,
Map<String, String> environment,
{String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
void Function(String) perLine}) async {
environment ??= {};
environment.addAll(environmentDefaults);
List<Map<Object, Object>> jsonObjects;
void Function(String)? perLine}) async {
environment = {}
..addAll(environmentDefaults)
..addAll(environment ?? {});
List<Map<String, Object>> jsonObjects = [];

/// Allow us to pretend we didn't pass the JSON flag in to dartdoc by
/// printing what dartdoc would have printed without it, yet storing
/// json objects into [jsonObjects].
Iterable<String> jsonCallback(String line) {
if (perLine != null) perLine(line);
Map<Object, Object> result;
Map<String, Object>? result;
try {
result = json.decoder.convert(line);
} on FormatException {
Expand All @@ -198,10 +190,9 @@ class SubprocessLauncher {
// line. Just ignore it and leave result null.
}
if (result != null) {
jsonObjects ??= [];
jsonObjects.add(result);
if (result.containsKey('message')) {
line = result['message'];
line = result['message'] as String;
} else if (result.containsKey('data')) {
var data = result['data'] as Map;
line = data['text'];
Expand All @@ -212,12 +203,12 @@ class SubprocessLauncher {

stderr.write('$prefix+ ');
if (workingDirectory != null) stderr.write('(cd "$workingDirectory" && ');
if (environment != null) {
stderr.write(environment.keys.map((String key) {
if (environment[key].contains(_quotables)) {
return "$key='${environment[key]}'";
if (environment.isNotEmpty) {
stderr.write(environment.entries.map((MapEntry<String, String> entry) {
if (entry.key.contains(_quotables)) {
return "${entry.key}='${entry.value}'";
} else {
return '$key=${environment[key]}';
return '${entry.key}=${entry.value}';
}
}).join(' '));
stderr.write(' ');
Expand All @@ -235,7 +226,7 @@ class SubprocessLauncher {
if (workingDirectory != null) stderr.write(')');
stderr.write('\n');

if (Platform.environment.containsKey('DRY_RUN')) return null;
if (Platform.environment.containsKey('DRY_RUN')) return {};

var realExecutable = executable;
var realArguments = <String>[];
Expand Down