2017-02-21 12:56:28 +00:00
|
|
|
// 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.
|
|
|
|
|
2022-02-17 19:09:52 +00:00
|
|
|
import 'dart:convert' show JsonEncoder;
|
2017-02-21 12:56:28 +00:00
|
|
|
import 'dart:io' show File, exit, stderr;
|
|
|
|
import 'dart:isolate' show RawReceivePort;
|
|
|
|
|
|
|
|
import 'package:yaml/yaml.dart' show loadYaml;
|
|
|
|
|
2022-01-10 08:26:20 +00:00
|
|
|
main(List<String> rawArguments) {
|
2022-02-17 19:09:52 +00:00
|
|
|
var port = RawReceivePort();
|
2022-01-06 18:36:55 +00:00
|
|
|
bool check = false;
|
2022-01-10 08:26:20 +00:00
|
|
|
String? relative;
|
|
|
|
List<String> arguments = [];
|
|
|
|
for (String argument in rawArguments) {
|
|
|
|
if (argument == '--check') {
|
|
|
|
check = true;
|
|
|
|
} else if (argument.startsWith('--relative=')) {
|
|
|
|
relative = argument.substring('--relative='.length);
|
|
|
|
} else {
|
|
|
|
arguments.add(argument);
|
|
|
|
}
|
2022-01-06 18:36:55 +00:00
|
|
|
}
|
2017-02-21 12:56:28 +00:00
|
|
|
if (arguments.length != 2) {
|
2022-01-06 18:36:55 +00:00
|
|
|
stderr.writeln("Usage: yaml2json.dart input.yaml output.json [--check]");
|
2017-02-21 12:56:28 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2022-02-17 19:09:52 +00:00
|
|
|
Uri input = File(arguments[0]).absolute.uri;
|
|
|
|
Uri output = File(arguments[1]).absolute.uri;
|
2022-01-10 08:26:20 +00:00
|
|
|
String inputString = arguments[0];
|
|
|
|
String outputString = arguments[1];
|
|
|
|
if (relative != null) {
|
2022-02-17 19:09:52 +00:00
|
|
|
String relativeTo = File(relative).absolute.uri.toString();
|
2022-01-10 08:26:20 +00:00
|
|
|
if (input.toString().startsWith(relativeTo)) {
|
|
|
|
inputString = input.toString().substring(relativeTo.length);
|
|
|
|
}
|
|
|
|
if (output.toString().startsWith(relativeTo)) {
|
|
|
|
outputString = output.toString().substring(relativeTo.length);
|
|
|
|
}
|
|
|
|
}
|
2022-02-17 19:09:52 +00:00
|
|
|
Map yaml = loadYaml(File.fromUri(input).readAsStringSync());
|
|
|
|
Map<String, dynamic> result = <String, dynamic>{};
|
2017-11-01 12:55:42 +00:00
|
|
|
result["comment:0"] = "NOTE: THIS FILE IS GENERATED. DO NOT EDIT.";
|
|
|
|
result["comment:1"] =
|
2022-01-10 08:26:20 +00:00
|
|
|
"Instead modify '$inputString' and follow the instructions therein.";
|
2017-11-01 12:55:42 +00:00
|
|
|
for (String key in yaml.keys) {
|
|
|
|
result[key] = yaml[key];
|
|
|
|
}
|
2022-02-17 19:09:52 +00:00
|
|
|
File file = File.fromUri(output);
|
2022-01-06 18:36:55 +00:00
|
|
|
String text = const JsonEncoder.withIndent(" ").convert(result);
|
|
|
|
if (check) {
|
|
|
|
bool needsUpdate = true;
|
|
|
|
if (file.existsSync()) {
|
|
|
|
String existingText = file.readAsStringSync();
|
|
|
|
needsUpdate = text != existingText;
|
|
|
|
}
|
|
|
|
if (needsUpdate) {
|
|
|
|
stderr.write('''
|
2022-01-10 08:26:20 +00:00
|
|
|
The file $outputString is not up to date. Regenerate using
|
2022-01-06 18:36:55 +00:00
|
|
|
|
2022-01-10 08:26:20 +00:00
|
|
|
dart tools/yaml2json.dart $inputString $outputString
|
2022-01-06 18:36:55 +00:00
|
|
|
''');
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file.writeAsStringSync(text);
|
|
|
|
}
|
2017-02-21 12:56:28 +00:00
|
|
|
port.close();
|
|
|
|
}
|