Files
sdk/pkg/status_file/bin/normalize.dart
T
Morten Krogh-Jespersen 39c399ca98 Add normalizer to normalize status files.
The test in this CL will test if the transformation of a status file produce an
equivalent status file. It does this by checking the number of entries and
making sure to find ONE entry in the produced status file.

As an invariant it needs that no duplicate entries exists in sections.

Bug:
Change-Id: I6aec3b20ef1bca89d6a7cf72d85487a95c3d1f0a
Reviewed-on: https://dart-review.googlesource.com/21341
Reviewed-by: Alexander Thomas <athom@google.com>
2017-12-01 09:10:51 +00:00

84 lines
2.5 KiB
Dart

// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// 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.
import 'dart:io';
import 'package:args/args.dart';
import 'package:status_file/canonical_status_file.dart';
import 'package:status_file/status_file.dart' as status_file;
import 'package:status_file/status_file_linter.dart';
import 'package:status_file/status_file_normalizer.dart';
import 'package:status_file/utils.dart';
ArgParser buildParser() {
var parser = new ArgParser();
parser.addFlag("overwrite",
abbr: 'w',
negatable: false,
defaultsTo: false,
help: "Overwrite input files with formatted output.");
parser.addFlag("help",
abbr: "h",
negatable: false,
defaultsTo: false,
help: "Show help and commands for this tool.");
return parser;
}
void printHelp(ArgParser parser) {
print("Usage: dart status_file/bin/normalize.dart <path>");
print(parser.usage);
}
void main(List<String> arguments) {
var parser = buildParser();
var results = parser.parse(arguments);
if (results["help"]) {
printHelp(parser);
return;
}
if (results.rest.isEmpty) {
printHelp(parser);
exit(1);
}
print("");
bool overwrite = results["overwrite"];
for (var path in results.rest) {
if (FileSystemEntity.isFileSync(path)) {
normalizeFile(path, overwrite);
} else if (FileSystemEntity.isDirectorySync(path)) {
new Directory(path).listSync(recursive: true).forEach((entry) {
if (!canLint(entry.path)) {
return;
}
normalizeFile(entry.path, overwrite);
});
}
}
}
bool normalizeFile(String path, bool writeFile) {
try {
var statusFile = new StatusFile.read(path);
var normalizedStatusFile = normalizeStatusFile(statusFile);
if (writeFile) {
new File(path).writeAsStringSync(normalizedStatusFile.toString());
print("Normalized $path");
} else {
print(normalizedStatusFile);
}
// Check if there are linting errors remaining, such as line comments,
// that needs to be handled manually.
var lintErrors = lint(normalizedStatusFile);
if (lintErrors.isNotEmpty) {
print("The normalizer could not remove all linting errors. The following "
"has to be removed manually:");
lintErrors.forEach(print);
}
} on status_file.SyntaxError catch (error) {
stderr.writeln("Could not parse $path:\n$error");
}
return false;
}