Skip to content

Commit

Permalink
Write Unused Keys as Comments (#22)
Browse files Browse the repository at this point in the history
* Write Unused Keys as Comments

* Exclude certain keys from appearing as comments

* Comment only Locale + Version
  • Loading branch information
Trenly committed Oct 13, 2021
1 parent eb7636c commit 99051df
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions Tools/YamlCreate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ Function Read-WinGet-InstallerValues {
}

if (String.Validate $PackageFamilyName -MaxLength $Patterns.FamilyNameMaxLength -MatchPattern $Patterns.FamilyName -AllowNull) {
if (String.Validate $PackageFamilyName -IsNull) { $PackageFamilyName = "$([char]0x2370)" }
$script:_returnValue = [ReturnValue]::Success()
} else {
if (String.Validate -not $PackageFamilyName -MaxLength $Patterns.FamilyNameMaxLength) {
Expand Down Expand Up @@ -663,13 +664,13 @@ Function Read-WinGet-InstallerValues {
foreach ($_Item in $_Switches.GetEnumerator()) {
If ($_Item.Value) { AddYamlParameter $_InstallerSwitches $_Item.Name $_Item.Value }
}
$_InstallerSwitches = SortYamlKeys $_InstallerSwitches $InstallerSwitchProperties
$_InstallerSwitches = SortYamlKeys $_InstallerSwitches $InstallerSwitchProperties -NoComments
$_Installer['InstallerSwitches'] = $_InstallerSwitches
}

If ($ProductCode) { AddYamlParameter $_Installer 'ProductCode' $ProductCode }
AddYamlParameter $_Installer 'UpgradeBehavior' $UpgradeBehavior
$_Installer = SortYamlKeys $_Installer $InstallerEntryProperties
$_Installer = SortYamlKeys $_Installer $InstallerEntryProperties -NoComments

$script:Installers += $_Installer

Expand Down Expand Up @@ -721,12 +722,32 @@ Function SortYamlKeys {
[Parameter(Mandatory = $true, Position = 0)]
[PSCustomObject] $InputObject,
[Parameter(Mandatory = $true, Position = 1)]
[PSCustomObject] $SortOrder
[PSCustomObject] $SortOrder,
[switch] $NoComments
)

$_ExcludedKeys = @(
'InstallerSwitches'
'Capabilities'
'RestrictedCapabilities'
'InstallerSuccessCodes'
'ProductCode'
'PackageFamilyName'
'InstallerLocale'
'InstallerType'
'Scope'
'UpgradeBehavior'
'Dependencies'
)

$_Temp = [ordered] @{}
$SortOrder.GetEnumerator() | ForEach-Object {
if ($InputObject.Contains($_)) {
$_Temp.Add($_, $InputObject[$_])
} else {
if (!$NoComments -and $_ -notin $_ExcludedKeys) {
$_Temp.Add($_, "$([char]0x2370)")
}
}
}
return $_Temp
Expand Down Expand Up @@ -1403,10 +1424,9 @@ Function Write-WinGet-VersionManifest-Yaml {
New-Item -ItemType 'Directory' -Force -Path $AppFolder | Out-Null
$VersionManifestPath = $AppFolder + "\$PackageIdentifier" + '.yaml'

#TO-DO: Write keys with no values as comments

$ScriptHeader + " using YAML parsing`n# yaml-language-server: `$schema=https://aka.ms/winget-manifest.version.1.0.0.schema.json`n" > $VersionManifestPath
ConvertTo-Yaml $VersionManifest >> $VersionManifestPath
$(Get-Content $VersionManifestPath -Encoding UTF8) -replace "(.*)$([char]0x2370)", "# `$1" | Out-File -FilePath $VersionManifestPath -Force
$MyRawString = Get-Content -Raw $VersionManifestPath | TrimString
[System.IO.File]::WriteAllLines($VersionManifestPath, $MyRawString, $Utf8NoBomEncoding)

Expand Down Expand Up @@ -1447,17 +1467,17 @@ Function Write-WinGet-InstallerManifest-Yaml {
AddYamlParameter $InstallerManifest 'ManifestType' 'installer'
AddYamlParameter $InstallerManifest 'ManifestVersion' $ManifestVersion
If ($InstallerManifest['Dependencies']) {
$InstallerManifest['Dependencies'] = SortYamlKeys $InstallerManifest['Dependencies'] $InstallerDependencyProperties
$InstallerManifest['Dependencies'] = SortYamlKeys $InstallerManifest['Dependencies'] $InstallerDependencyProperties -NoComments
}

$InstallerManifest = SortYamlKeys $InstallerManifest $InstallerProperties
$InstallerManifest = SortYamlKeys $InstallerManifest $InstallerProperties -NoComments

New-Item -ItemType 'Directory' -Force -Path $AppFolder | Out-Null
$InstallerManifestPath = $AppFolder + "\$PackageIdentifier" + '.installer' + '.yaml'
#TO-DO: Write keys with no values as comments

$ScriptHeader + " using YAML parsing`n# yaml-language-server: `$schema=https://aka.ms/winget-manifest.installer.1.0.0.schema.json`n" > $InstallerManifestPath
ConvertTo-Yaml $InstallerManifest >> $InstallerManifestPath
$(Get-Content $InstallerManifestPath -Encoding UTF8) -replace "(.*)$([char]0x2370)", "# `$1" | Out-File -FilePath $InstallerManifestPath -Force
$MyRawString = Get-Content -Raw $InstallerManifestPath | TrimString
[System.IO.File]::WriteAllLines($InstallerManifestPath, $MyRawString, $Utf8NoBomEncoding)

Expand Down Expand Up @@ -1508,10 +1528,9 @@ Function Write-WinGet-LocaleManifest-Yaml {
New-Item -ItemType 'Directory' -Force -Path $AppFolder | Out-Null
$LocaleManifestPath = $AppFolder + "\$PackageIdentifier" + '.locale.' + "$PackageLocale" + '.yaml'

#TO-DO: Write keys with no values as comments

$ScriptHeader + " using YAML parsing`n$yamlServer`n" > $LocaleManifestPath
ConvertTo-Yaml $LocaleManifest >> $LocaleManifestPath
$(Get-Content $LocaleManifestPath -Encoding UTF8) -replace "(.*)$([char]0x2370)", "# `$1" | Out-File -FilePath $LocaleManifestPath -Force
$MyRawString = Get-Content -Raw $LocaleManifestPath | TrimString
[System.IO.File]::WriteAllLines($LocaleManifestPath, $MyRawString, $Utf8NoBomEncoding)

Expand All @@ -1527,6 +1546,7 @@ Function Write-WinGet-LocaleManifest-Yaml {

$ScriptHeader + " using YAML parsing`n$yamlServer`n" > ($AppFolder + '\' + $DifLocale.Name)
ConvertTo-Yaml $OldLocaleManifest >> ($AppFolder + '\' + $DifLocale.Name)
$(Get-Content $($AppFolder + '\' + $DifLocale.Name) -Encoding UTF8) -replace "(.*)$([char]0x2370)", "# `$1" | Out-File -FilePath $($AppFolder + '\' + $DifLocale.Name) -Force
$MyRawString = Get-Content -Raw $($AppFolder + '\' + $DifLocale.Name) | TrimString
[System.IO.File]::WriteAllLines($($AppFolder + '\' + $DifLocale.Name), $MyRawString, $Utf8NoBomEncoding)
}
Expand Down

0 comments on commit 99051df

Please sign in to comment.