betto_builder_tools 0.1.0-dev.1 copy "betto_builder_tools: ^0.1.0-dev.1" to clipboard
betto_builder_tools: ^0.1.0-dev.1 copied to clipboard

A small set of tools to assist with code generation.

example/example.dart

/*
 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');
}
0
likes
160
points
84
downloads

Documentation

API reference

Publisher

verified publisherbettongia.com

Weekly Downloads

A small set of tools to assist with code generation.

Repository (GitHub)
View/report issues
Contributing

Topics

#codegen

License

Apache-2.0 (license)

Dependencies

archive, dart_style, http

More

Packages that depend on betto_builder_tools