dart-lang / source_gen

Automatic source code generation for Dart
https://pub.dev/packages/source_gen
BSD 3-Clause "New" or "Revised" License
482 stars 105 forks source link

Disable Header Should Prevent Any Extra Output #706

Open TheMaverickProgrammer opened 2 months ago

TheMaverickProgrammer commented 2 months ago

Dart SDK version: 3.0.4 (stable) (Wed Jun 7 14:55:32 2023 +0000) on "windows_x64"

I'm using a custom builder to generate JSON from some annotations used in another program. You can think of it like Protobuf. This is working fine but I was surprised that using a header value of an empty string still produced the auto-generated message in the output .json file, which is not compliant to the .json spec. My work-around at the moment is just to trim the file contents after the last line but it seems counter-intuitive to have to do this at all.

Example:

Builder yarnJsonBuilder(BuilderOptions options) => LibraryBuilder(
    YarnGenerator(),
    generatedExtension: '.yarn.json',
    formatOutput: (code) => code
        .split(
            r'// **************************************************************************')
        .last
        .trim(),
    header: '');

// ...
jakemac53 commented 2 months ago

This package is only intended for generating Dart files from Dart files, and it assumes it is safe to put Dart comments in the code. These comments are meant to separate the sections of the file generated from different generators, and tell you which generator produced the following output, which is different from the header which just goes at the top of the file.

TheMaverickProgrammer commented 2 months ago

I'd kindly request to consider emphasizing the exclusivity with dartlang then because the fact source_gen can support many output file extensions (and not just dart/part files), plus the way the docs were worded, led me to think it could be used to generate anything we wanted and not just Dart source files.

Otherwise, it works great. Thank you for helping me.

jakemac53 commented 2 months ago

Fwiw it would likely be quite possible to support your use case as well (maybe all you need is some option to not output these comments). I was more just trying to explain what was happening, based on the Dart->Dart assumptions, than shutting the door entirely on this use case :).

It isn't likely something we could prioritize at this time, so you might be best served just forking the package for the time being. Or, you could work on a pull request. We do require tests etc for pull requests though so keep in mind that it would be more work than just the actual feature implementation.

TheMaverickProgrammer commented 2 months ago

Roger. I assume these are the minimal necessary steps: https://github.com/dart-lang/sdk/blob/main/CONTRIBUTING.md

Leaving this here to reference for myself.

jakemac53 commented 2 months ago

https://github.com/dart-lang/sdk/blob/main/CONTRIBUTING.md

Working in the SDK is a lot more complicated than this repo. Our CONTRIBUTING.md is pretty boilerplatey so it doesn't have much useful information (other than the info about the CLA). But, you can develop this repo just like any other git repo, no special steps.