shyndman / pubx

The missing pub commands
Apache License 2.0
35 stars 3 forks source link

Attempt to add new dependencies in the correct position if deps are already sorted #8

Open shyndman opened 5 years ago

Vanethos commented 5 years ago

How to determine if dependencies are sorted? Is there a current flag ir should we search the list to verify if the dependencies are ordered alphabetically?

shyndman commented 5 years ago

I think we'll have to search the list. And it won't be straightforward, because YamlMap isn't using an ordered map under the covers (although someone's asked for it here https://github.com/dart-lang/yaml/issues/55), so it won't iterate in the same order it was defined in the file.

So instead we'll have to sort YamlMap.nodes.keys (which are instances of YamlScalar) by their location in the source code (via span), then again by their values, then compare the two lists. If they're ordered the same way, we'll know it's alphabetically ordered.

shyndman commented 5 years ago

What might even be better is to use the same insertion algorithm whether or not the deps are already sorted.

Sort the keys as I described above (using span), then walk them in order until the name of the package we're adding comes before the key we're looking at (alphabetically), and insert it there.

It keeps sorted deps sorted, and if they aren't sorted, it still inserts the dependency somewhere.