Add command to travis CI to analyze if update-code-segments has been run for Flutter Gallery (#219)

* Add command to travis CI to analyze if update-code-segments has been run
pull/224/head
Per Classon 5 years ago committed by GitHub
parent eea6f180ee
commit 00306723ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,11 +0,0 @@
# Files and directories created by pub
.dart_tool/
.packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock
# Conventional directory for build outputs
build/
# Directory created by dartdoc
doc/api/

@ -1,3 +0,0 @@
## 1.0.0
- Initial version

@ -1,39 +0,0 @@
# Defines a default set of lint rules enforced for
# projects at Google. For details and rationale,
# see https://github.com/dart-lang/pedantic#enabled-lints.
include: package:pedantic/analysis_options.yaml
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
# Uncomment to specify additional rules.
# linter:
# rules:
# - camel_case_types
analyzer:
# exclude:
# - path/to/excluded/files/**
linter:
rules:
- avoid_types_on_closure_parameters
- avoid_void_async
- await_only_futures
- camel_case_types
- cancel_subscriptions
- close_sinks
- constant_identifier_names
- control_flow_in_finally
- empty_statements
- hash_and_equals
- implementation_imports
- non_constant_identifier_names
- package_api_docs
- package_names
- package_prefixed_library_names
- test_types_in_equals
- throw_in_finally
- unnecessary_brace_in_string_interps
- unnecessary_getters_setters
- unnecessary_new
- unnecessary_statements
- directives_ordering

@ -1,12 +0,0 @@
// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:codeviewer_cli/segment_generator.dart';
main(List<String> arguments) {
writeSegments(
sourceDirectoryPath: '../gallery/lib/demos',
targetFilePath: '../gallery/lib/codeviewer/code_segments.dart',
);
}

@ -1,12 +0,0 @@
name: codeviewer_cli
description: A command-line application to highlight dart source code.
environment:
sdk: '>=2.4.0 <3.0.0'
dependencies:
string_scanner: 1.0.5
dev_dependencies:
pedantic: 1.8.0
test: ^1.5.0

File diff suppressed because it is too large Load Diff

@ -365,7 +365,7 @@ packages:
source: hosted
version: "2.0.0"
string_scanner:
dependency: transitive
dependency: "direct dev"
description:
name: string_scanner
url: "https://pub.dartlang.org"

@ -33,6 +33,7 @@ dev_dependencies:
flutter_test:
sdk: flutter
grinder: ^0.8.0
string_scanner: 1.0.5
flutter:
assets:

@ -0,0 +1,31 @@
// Copyright 2019 The Flutter team. 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:io';
import 'package:args/args.dart';
import 'package:path/path.dart' as path;
import 'segment_generator.dart';
void main(List<String> arguments) {
final parser = ArgParser()
..addOption(
'target',
help: 'The file path for the output target file.',
defaultsTo: path.join(
Directory.current.path, 'lib', 'codeviewer', 'code_segments.dart'),
)
..addFlag(
'dry-run',
help: 'Write the output to stdout.',
);
final argResults = parser.parse(arguments);
writeSegments(
sourceDirectoryPath: path.join(Directory.current.path, 'lib', 'demos'),
targetFilePath: argResults['target'] as String,
isDryRun: argResults['dry-run'] as bool,
);
}

@ -213,13 +213,11 @@ class TaggedString {
final double order;
}
void _formatSegments(Map<String, String> segments, String targetFilePath) {
File targetFile = File(targetFilePath);
IOSink output = targetFile.openWrite();
void _formatSegments(Map<String, String> segments, IOSink output) {
output.write(_globalPrologue);
for (final name in segments.keys) {
final sortedNames = segments.keys.toList()..sort();
for (final name in sortedNames) {
String code = segments[name];
output.writeln(' static TextSpan $name (BuildContext context) {');
@ -248,16 +246,19 @@ void _formatSegments(Map<String, String> segments, String targetFilePath) {
/// [sourceDirectoryPath] and reads every file in it,
/// collects code segments marked by "// BEGIN <segment_name>" and "// END",
/// highlights them, and writes to the file specified by
/// [targetFilePath].
/// [targetFilePath]. If [isDryRun] is true, the output will
/// be written to stdout.
///
/// The output file is a dart source file with a class "CodeSegments" and
/// static methods of type TextSpan(BuildContext context).
/// Each method generates a widget that displays a segment of code.
///
/// The target file is overwritten.
void writeSegments({String sourceDirectoryPath, String targetFilePath}) {
void writeSegments(
{String sourceDirectoryPath, String targetFilePath, bool isDryRun}) {
Map<String, String> segments = _createSegments(sourceDirectoryPath);
_formatSegments(segments, targetFilePath);
IOSink output = isDryRun ? stdout : File(targetFilePath).openWrite();
_formatSegments(segments, output);
}
class PreformatterException implements Exception {

@ -2,6 +2,8 @@
// 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:grinder/grinder.dart';
@ -54,12 +56,41 @@ Future<void> l10n() async {
@Task('Update code segments')
Future<void> updateCodeSegments() async {
final codeviewerPath =
path.join(Directory.current.parent.path, 'codeviewer_cli');
await pubGet(directory: codeviewerPath);
path.join(Directory.current.path, 'tool', 'codeviewer_cli', 'main.dart');
Dart.run(path.join(codeviewerPath, 'bin', 'main.dart'));
final targetFilePath = path.join('lib', 'codeviewer', 'code_segments.dart');
await format(path: targetFilePath);
Dart.run(codeviewerPath);
final codeSegmentsPath = path.join('lib', 'codeviewer', 'code_segments.dart');
await format(path: codeSegmentsPath);
}
@Task('Verify code segments')
Future<void> verifyCodeSegments() async {
final codeviewerPath =
path.join(Directory.current.path, 'tool', 'codeviewer_cli', 'main.dart');
// We use stdin and stdout to write and format the code segments, to avoid
// creating any files.
final codeSegmentsUnformatted =
Dart.run(codeviewerPath, arguments: ['--dry-run'], quiet: true);
final codeSegmentsFormatted = await _startProcess(
path.normalize(path.join(dartVM.path, '../dartfmt')),
input: codeSegmentsUnformatted,
);
// Read the original code segments file.
final codeSegmentsPath = path.join(
Directory.current.path, 'lib', 'codeviewer', 'code_segments.dart');
final expectedCodeSegmentsOutput =
await File(codeSegmentsPath).readAsString();
if (codeSegmentsFormatted.trim() != expectedCodeSegmentsOutput.trim()) {
stderr.writeln(
'The contents of $codeSegmentsPath are different from that produced by '
'codeviewer_cli. Did you forget to run update-code-segments after '
'updating a demo?',
);
exit(1);
}
}
Future<void> _runProcess(String executable, List<String> arguments) async {
@ -68,6 +99,32 @@ Future<void> _runProcess(String executable, List<String> arguments) async {
stderr.write(result.stderr);
}
// Function to make sure we capture all of the stdout.
// Reference: https://github.com/dart-lang/sdk/issues/31666
Future<String> _startProcess(String executable,
{List<String> arguments = const [], String input}) async {
final output = <int>[];
final completer = Completer<int>();
final process = await Process.start(executable, arguments);
process.stdin.writeln(input);
process.stdout.listen(
(event) {
output.addAll(event);
},
onDone: () async => completer.complete(await process.exitCode),
);
await process.stdin.close();
final exitCode = await completer.future;
if (exitCode != 0) {
stderr.write(
'Running "$executable ${arguments.join(' ')}" failed with $exitCode.\n',
);
exit(exitCode);
}
return Future<String>.value(utf8.decoder.convert(output));
}
/// Return the flutter root path from the environment variables.
String _flutterRootPath() {
final flutterBinPath =

@ -57,6 +57,11 @@ do
popd
done
echo "Run code segments check for 'gallery/gallery'."
pushd gallery/gallery
"${localSdkPath}/bin/flutter" pub run grinder verify-code-segments
popd
echo "Building the aar files for 'flutter_module'."
pushd add_to_app/flutter_module
"${localSdkPath}/bin/flutter" build aar

Loading…
Cancel
Save