|
| 1 | +// Copyright 2023, the Chromium project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:path/path.dart' as path; |
| 9 | + |
| 10 | +String prompt(String tag) { |
| 11 | + stdout.write('$tag?: '); |
| 12 | + final answer = stdin.readLineSync(); |
| 13 | + |
| 14 | + if (answer == null || answer.isEmpty) { |
| 15 | + throw Exception('$tag is required'); |
| 16 | + } |
| 17 | + |
| 18 | + return answer; |
| 19 | +} |
| 20 | + |
| 21 | +Future<void> main(List<String> args) async { |
| 22 | + final name = prompt('Label name'); |
| 23 | + final description = prompt('Label description'); |
| 24 | + final englishTranslation = prompt('English translation'); |
| 25 | + |
| 26 | + final cwd = Directory.current.path; |
| 27 | + final l10nSrc = Directory(path.join(cwd, 'lib', 'l10n')); |
| 28 | + |
| 29 | + final files = l10nSrc.listSync().whereType<File>().toList(); |
| 30 | + final futures = files.map((e) async { |
| 31 | + final newContent = await addLabel(e, name, description, englishTranslation); |
| 32 | + final b = StringBuffer(); |
| 33 | + final string = const JsonEncoder.withIndent(' ').convert(newContent); |
| 34 | + b.write(string); |
| 35 | + b.write('\n'); |
| 36 | + |
| 37 | + await e.writeAsString(b.toString()); |
| 38 | + }); |
| 39 | + |
| 40 | + await Future.wait(futures); |
| 41 | + stdout.writeln('Done!'); |
| 42 | +} |
| 43 | + |
| 44 | +Future<Map<String, dynamic>> addLabel( |
| 45 | + File file, |
| 46 | + String name, |
| 47 | + String description, |
| 48 | + String englishTranslation, |
| 49 | +) async { |
| 50 | + final content = jsonDecode(await file.readAsString()) as Map<String, dynamic>; |
| 51 | + return { |
| 52 | + ...content, |
| 53 | + "@@last_modified": DateTime.now().toIso8601String(), |
| 54 | + name: englishTranslation, |
| 55 | + "@$name": { |
| 56 | + "description": description, |
| 57 | + "placeholders": {}, |
| 58 | + }, |
| 59 | + }; |
| 60 | +} |
0 commit comments