generateBuildScript function
Implementation
Future<String> generateBuildScript() async {
buildLog.doing('Generating the build script.');
final info = await findBuildScriptOptions();
final builders = info.builderApplications;
final library = Library(
(b) => b.body.addAll([
declareFinal('_builders')
.assign(
literalList(
builders,
refer(
'BuilderApplication',
'package:build_runner_core/build_runner_core.dart',
),
),
)
.statement,
_main(),
]),
);
final emitter = DartEmitter(
allocator: Allocator.simplePrefixing(),
useNullSafetySyntax: true,
);
try {
// The `build_runner` version number is included to force a rebuild if the
// script was generated by a different version. It only needs updating if
// the host<->isolate relationship changed in a breaking way, for example
// if command line args or messages passed via sendports have changed
// in a breaking way.
return DartFormatter(languageVersion: _lastShortFormatDartVersion).format(
'''
// @dart=${_lastShortFormatDartVersion.major}.${_lastShortFormatDartVersion.minor}
// ignore_for_file: directives_ordering
// build_runner >=2.4.16
${library.accept(emitter)}
''',
);
} on FormatterException {
buildLog.error(
'Generated build script could not be parsed. '
'Check builder definitions.',
);
throw const CannotBuildException();
}
}