-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
formatjson.ps1
- Sort manifest root keys by schema.json
and child keys alphabetically
#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?
Changes from 11 commits
21fd5d8
16ea655
98ce740
5983d28
cd06740
a579f69
c0b41a2
25a0b59
2c21e14
d9f07a3
37aa459
e440802
802b26f
37d4073
13bd45f
8f696f8
d25b11e
f3dd97e
1cdae35
18a5f00
c02d6d8
80ab7ac
9f429a1
203e5a4
981c302
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ param( | |
[String] $App = '*', | ||
[Parameter(Mandatory = $true)] | ||
[ValidateScript( { | ||
if (!(Test-Path $_ -Type Container)) { | ||
if (-not (Test-Path $_ -Type Container)) { | ||
throw "$_ is not a directory!" | ||
} else { | ||
$true | ||
|
@@ -34,11 +34,21 @@ param( | |
$Dir = Convert-Path $Dir | ||
|
||
Get-ChildItem $Dir -Filter "$App.json" -Recurse | ForEach-Object { | ||
$file = $_.FullName | ||
# beautify | ||
$json = parse_json $file | ConvertToPrettyJson | ||
# Path of file | ||
$file = [string] $_.'FullName' | ||
|
||
# convert to 4 spaces | ||
$json = $json -replace "`t", ' ' | ||
# Parse JSON | ||
$json = [PSCustomObject](parse_json -path $file) | ||
|
||
# Sort JSON root properties | ||
$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 commentThe 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 commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does it? It will fail execution currently.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a check for parent keys not present in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, thanks for reminding.
Totally agree. Great. |
||
|
||
# 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.
Is it possible to sort the keys recursively with global/local order?
Uh oh!
There was an error while loading. Please reload this page.
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.
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.
Scoop/schema.json
Lines 217 to 231 in 37aa459
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.Uh oh!
There was an error while loading. Please reload this page.
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.