-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
formatjson.ps1
- Sort manifest root properties
#6494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
formatjson.ps1
- Sort manifest root properties
#6494
Conversation
@deevus @HUMORCE @rashil2000 @z-Fng: What do you guys think? |
Warning Rate limit exceeded@o-l-a-v has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 6 minutes and 37 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (5)
✨ Finishing touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
@coderabbitai review |
bin/formatjson.ps1
Outdated
# Convert to 4 spaces | ||
$json = [string]($json -replace "`t", ' ') | ||
|
||
# Overwrite file content | ||
[System.IO.File]::WriteAllLines($file, $json) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When JSON parsing fails, it will clear the manifest content. Should this behavior be adjusted to prevent contributors from losing their work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, maybe we should add some error handling.
bin/formatjson.ps1
Outdated
$json = [PSCustomObject](parse_json -path $file) | ||
|
||
# Sort JSON root properties | ||
$json = [PSCustomObject](Sort-ScoopManifestRootProperties -JsonAsObject $json) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to sort the keys recursively with global/local order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand what you're asking for here, about the local/global order.
Recursice ordering keys by the schema.json should be possible. Or just sort child keys alphabetically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local/global order
My description might be redundant. Jsut ignore it. Focus on recursive sorting.
I thought that when sorting keys like architecture.64bit/32bit , local order are used because the configuration file contains architecture.64bit/32bit. However, when sorting keys like architecture.64bit.url/hash, the configuration file lacks this entry, causing it to fall back to the global order.
As I'm not sure how to describe this, I called it local/global order.
Lines 217 to 231 in 37aa459
"architecture": { | |
"type": "object", | |
"additionalProperties": false, | |
"properties": { | |
"32bit": { | |
"$ref": "#/definitions/autoupdateArch" | |
}, | |
"64bit": { | |
"$ref": "#/definitions/autoupdateArch" | |
}, | |
"arm64": { | |
"$ref": "#/definitions/autoupdateArch" | |
} | |
} | |
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added sorting of first level child keys if parent is of type PSCustomObject
. Going even deeper will be more complex. It's doable, but I think what we got now is a good start.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there are existing PowerShell or C# projects that can sort a JSON file by schema.json
and handle all the complexity of going deeper we can use?
In Python there is: https://github.com/ikonst/jschon-sort.
But as I already said, I think this serves as a very good start for getting more uniform manifests.
$json = [PSCustomObject](Sort-ScoopManifestRootProperties -JsonAsObject $json) | ||
|
||
# Beautify | ||
$json = [string](ConvertToPrettyJson -data $json) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am concerned whether the sort is a stable sort algorithm for keys that are not present in the index list.
Could multiple executions yield different results?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO there could and should be a check for keys we don't want to be present. Doesn't this exist already?
To make formatjson.ps1 safer we could do: If key is not present in the schema.json
- Optian a) fail execution
- Option b) remove key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this exist already?
Does it? It will fail execution currently.
Sort-Object: D:\Scoop\apps\scoop\current\lib\json.ps1:241:9
Line |
241 | Sort-Object -Property @{
| ~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot convert value "-1" to type "System.Byte". Error: "Value was either too large or too small for an unsigned byte."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking more about that other parts of the validation should catch manifest errors before it gets to formatjson.ps1
. Thus validating that the manifest doesn't contain invalid keys is a task that formatjson.ps1
shouldn't try to solve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems that's what the validator is for?
https://github.com/ScoopInstaller/Scoop/tree/master/supporting/validator
Thus I don't think the new function Sort-ScoopManifestRootProperties
should have a lot of validation logic. It's a feature that it throws if manifest does not comply with schema.json
IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a check for parent keys not present in schema.json
inside Sort-ScoopManifestRootProperties
. Then users will get a better error message than what you posted in #6494 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we could write a warning, and just return the object as is / not try to sort the object at all. Instead of throwing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems that's what the validator is for?
Yes, thanks for reminding.
I added a check for parent keys not present in
schema.json
insideSort-ScoopManifestRootProperties
. Then users will get a better error message than what you posted.
Totally agree. Great.
Description
schema.json
so it matches the wanted order.json.ps1
namedSort-ScoopManifestRootProperties
that:schema.json
properties.PSCustomObject
.formatjson.ps1
use this new function.Motivation and Context
Closes #6491.
How Has This Been Tested?
Checklist:
develop
branch.formatjson.ps1
was added with feat: Add lint checks GithubActions#61: https://github.com/ScoopInstaller/GithubActions/wiki/Pull-Request-Checks