Add SortYaml recipe to streamline yaml files (gitlab ci, spring boot properties...)
What's changed?
Allowing yaml files to be sorted according to a list of input. Could help in creating a gitlab-ci.yaml order.
Anything in particular you'd like reviewers to focus on?
Took some assumptions on the input variables -> please validate if this would be workable. Should we go for JsonPath instead? That would require us to support way more notations even.
Have you considered any alternatives or workarounds?
Not really, like mentioned I considered JsonPath but that's perhaps too elaborate. Maybe use jsonpath notations (with $) and in validation validate it's only going to be one of the 3 supported notations
Any additional context
The recipe supports several features in this version:
- fixed property/path
- Wildcards that match single property (
*) - Wildcards that match multiple properties (
**) - Property pickers (
[]) to sort on their value instead of the mentioned property key
Checklist
- [x] I've added unit tests to cover both positive and negative cases
- [x] I've read and applied the recipe conventions and best practices
- [x] I've used the IntelliJ IDEA auto-formatter on affected files
Personally, I think either syntax would be acceptable to use for the sortBy option. The current syntax does keep things pretty simple with its limited syntax options.
@timtebeek, do you have any thoughts?
⚠️ This recipe fails when the Yaml file contains comments. Though I do not suggest to add support for it (the recipe would be a lot harder to write, see the MergeYaml for example to get an idea), maybe it's a good idea to just skip yaml files with comments for now. Then at least you don't end up with files where the comments are all of a sudden on totally different lines.
Well spotted (I take it you found out the hard way with #5036) Will try to add this ignore check + get it documented. A comment would indeed become very strange to be sorted as it might occur on top of a yaml property but also as a separate commented property.
Should we add dropComments to allow sorting, removing the comments?
IMO if we want to sort, we should run some RemoveComments recipe first (single responsability)
Maybe it helps to look at how we similarly sort elements in OrderPomElements https://github.com/openrewrite/rewrite/blob/fcbc210f7cf6c561d10f7d670b765e20a8f3dd2c/rewrite-maven/src/main/java/org/openrewrite/maven/OrderPomElements.java#L57-L133
And note that that uses a stable list of tags in order; perhaps not quite as flexible as the input to this recipe https://github.com/openrewrite/rewrite/blob/d4042a039fa687bd8b917a9a30bfddb69d0a1053/rewrite-maven/src/main/java/org/openrewrite/maven/MavenTagInsertionComparator.java#L33-L66
I had worked locally on supporting comments, but it's really a pain with the prefixes containing all the comments. Perhaps a remove comments boolean input and then:
- if true, remove the comments
- if false, do not sort But not a big fan of that myself (single responsability)
This PR is stale because it has been open for 90 days with no activity. Remove stale label or comment or this will be closed in two weeks. PRs may be reopened when there is renewed interest.