betto_builder_tools 0.1.0-dev.1
betto_builder_tools: ^0.1.0-dev.1 copied to clipboard
A small set of tools to assist with code generation.
/*
Copyright 2026 The Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import 'dart:convert';
import 'dart:io';
import 'package:betto_builder_tools/betto_builder_tools.dart';
import 'package:code_builder/code_builder.dart';
final copyrightHeader = '''
Copyright 2026 The Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
''';
const downloadUrl =
'https://raw.githubusercontent.com/stopwords-iso/stopwords-iso/'
'refs/heads/master/stopwords-iso.json';
const outputFilePath = 'example/stopwords.g.dart';
final dataOutputFile = File('example/stopwords-iso.json');
void main(List<String> args) {
dataOutputFile.parent.create(recursive: true);
print('Data files will be placed in ${dataOutputFile.absolute.path}');
loadData(
dataOutputFile.absolute.path,
downloadUrl,
compression: CompressionType.none,
).then((data) {
final Map<String, dynamic> listing = json.decode(data);
buildStopwordRegistry(listing, outputFilePath);
});
}
void buildStopwordRegistry(
Map<String, dynamic> listing,
String outputFilePath,
) {
final lib = Library(
(b) => b
..generatedByComment =
'DO NOT EDIT THIS FILE: Generated by tool/loader.dart'
..body.add(
Enum(
(eb) => eb
..name = 'Stopwords'
..docs.add('/// IANA language extensions')
..fields.addAll([
Field(
(b) => b
..name = 'listing'
..type = refer('Set<String>')
..modifier = FieldModifier.final$,
),
Field(
(b) => b
..name = 'languageCode'
..type = refer('String')
..modifier = FieldModifier.final$,
),
])
..constructors.add(
Constructor(
(b) => b
..constant = true
..requiredParameters.addAll([
Parameter(
(v) => v
..name = 'languageCode'
..toThis = true
..named = false,
),
Parameter(
(v) => v
..name = 'listing'
..toThis = true
..named = false,
),
]),
),
)
..values.addAll(
listing.entries.map(
(entry) => EnumValue(
(b) => b
..name = entry.key
..arguments.addAll([
literal(entry.key),
literalSet(
Set.from(entry.value).map(
(v) => v.contains("'")
? literal('$v')
: literalString(v, raw: true),
),
),
]),
),
),
),
),
),
);
final emitter = DartEmitter.scoped();
final code =
'/*\n${copyrightHeader.trim()}\n */\n\n'
'${lib.accept(emitter)}';
//print(code);
final formattedCode = dartfmt.format(code);
File(outputFilePath).writeAsString(formattedCode);
print('Generated code written to: $outputFilePath');
}