f3ath / cider

Tools for Dart package maintainers
https://pub.dev/packages/cider
MIT License
101 stars 17 forks source link

Pretty please can we keep comments in pubspec.yaml #4

Closed rubenvereecken closed 4 years ago

rubenvereecken commented 4 years ago

I desperately need a tool like this. Looks clean and neat and seems to do the job well. I don't mind the reformatting, that's a result of rewriting, but pretty please can it keep comments. I've got a flutter project and there are some really good links to documentation in the pubspec.yaml file that I don't want to lose.

f3ath commented 4 years ago

There is an upstream feature request https://github.com/alexei-sintotski/pubspec_yaml/issues/22 Also the discussion on the preceding project https://github.com/f3ath/pubspec-version/issues/17

Some thoughts on the topic.

I have not seen any yaml writers in Dart able to keep formatting or comments. As a workaround, we can probably search for a line like ^version: \d without parsing yaml at all. Although that's not a perfect solution as there might technically be multiline strings with the same pattern. In such cases we perhaps can compare the read version from the one read using the yaml parser. I guess we can add a sort of --keep-formatting flag to use the alternative algorithm.

rubenvereecken commented 4 years ago

I just had a quick look at pubspec_yaml and it looks like it would be non-trivial to change things. They have an intermediary step which loads the yaml into JSON which would get rid of comments. They also extract everything and keep no sense of document structure so this will always rewrite things heavily.

I like the idea of heuristically and minimally changing the document. It might even be made robust, in which case you could completely get rid of the pubspec_yaml dependency. Is this something you might be interested in? I'd be keen to chip in.

f3ath commented 4 years ago

@rubenvereecken sure, contributions are always welcomed. This is one is indeed non trivial. A simple regex can probably solve 99% of the cases, but that 1% might be tricky.

name: my_silly_package
version: '1.2.3' # i am the version line
description: 'This is a hypothetical package that changes lines like
version: 1.2.3
into
version: "1.2.3"
'
homepage: https://example.com

Which line to change here?

rubenvereecken commented 4 years ago

I happen to have a bit of experience with lexers though I'm afraid Dart will have limited support in the way of packages. I'll have a look after the weekend.

On Fri, 31 Jul 2020, 20:12 The Конь, notifications@github.com wrote:

@rubenvereecken https://github.com/rubenvereecken sure, contributions are always welcomed. This is one is indeed non trivial. A simple regex can probably solve 99% of the cases, but that 1% might be tricky.

version: "1.2.3" # i am the version linedescription: 'This is a hypothetical package that changes lines likeversion: 3.2.1intoversion: "1.2.3"'

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/f3ath/cider/issues/4#issuecomment-667306839, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHZSKIBXOD53KTJASZDWHDR6MJTPANCNFSM4PNJBESA .

f3ath commented 4 years ago

Yes, ideally this should be solved in the upstream dependencies. I'm not aware of any yaml parser/writer in Dart which supports comments or any extensive formatting.

f3ath commented 4 years ago

The issue should be fixed in 0.0.3. Please confirm.

rubenvereecken commented 4 years ago

Beautiful

image