Skip to content
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

Fixes microsoft#21219 + some improvements #27

Merged
merged 14 commits into from
Sep 18, 2021
Merged
115 changes: 65 additions & 50 deletions Tools/YamlCreate.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#Requires -Version 5
$PSVersion = (Get-Host).Version.Major
$ScriptHeader = '# Created with YamlCreate.ps1 v2.0.0'
$ManifestVersion = '1.0.0'
$PSDefaultParameterValues = @{ '*:Encoding' = 'UTF8' }
Expand Down Expand Up @@ -29,11 +28,7 @@ $ofs = ', '
https://github.com/microsoft/winget-pkgs/blob/master/Tools/YamlCreate.ps1
#>

<#
TO-DO:
- Handle writing null parameters as comments
- Ensure licensing for powershell-yaml is met
#>
# TO-DO: Ensure licensing for powershell-yaml is met

if (-not(Get-Module -ListAvailable -Name powershell-yaml)) {
try {
Expand Down Expand Up @@ -247,18 +242,21 @@ Function Show-OptionMenu {
Clear-Host
Write-Host -ForegroundColor 'Cyan' 'Select Mode'
Write-Colors "`n[", '1', "] New Manifest or Package Version`n" 'DarkCyan', 'White', 'DarkCyan'
Write-Colors "`n[", '2', "] Update Package Metadata`n" 'DarkCyan', 'White', 'DarkCyan'
Write-Colors "`n[", '3', "] New Locale`n" 'DarkCyan', 'White', 'DarkCyan'
Write-Colors "`n[", '2', "] Quick Update Package Version ", "(Note: Must be used only when previous version`'s metadata is complete.)`n" 'DarkCyan', 'White', 'DarkCyan', 'Green'
Write-Colors "`n[", '3', "] Update Package Metadata`n" 'DarkCyan', 'White', 'DarkCyan'
Write-Colors "`n[", '4', "] New Locale`n" 'DarkCyan', 'White', 'DarkCyan'
Write-Colors "`n[", 'q', ']', " Any key to quit`n" 'DarkCyan', 'White', 'DarkCyan', 'Red'
Write-Colors "`nSelection: " 'White'

$Keys = @{
[ConsoleKey]::D1 = '1';
[ConsoleKey]::D2 = '2';
[ConsoleKey]::D3 = '3';
[ConsoleKey]::D4 = '4';
[ConsoleKey]::NumPad1 = '1';
[ConsoleKey]::NumPad2 = '2';
[ConsoleKey]::NumPad3 = '3';
[ConsoleKey]::NumPad4 = '4';
}

do {
Expand All @@ -267,8 +265,9 @@ Function Show-OptionMenu {

switch ($Keys[$keyInfo.Key]) {
'1' { $script:Option = 'New' }
'2' { $script:Option = 'EditMetadata' }
'3' { $script:Option = 'NewLocale' }
'2' { $script:Option = 'QuickUpdateVerison' }
'3' { $script:Option = 'EditMetadata' }
'4' { $script:Option = 'NewLocale' }
default { Write-Host; exit }
}
}
Expand Down Expand Up @@ -391,13 +390,24 @@ Function Read-WinGet-InstallerValues {
exit 1
} finally {
Write-Host "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)" -ForegroundColor Green

$InstallerSha256 = (Get-FileHash -Path $script:dest -Algorithm SHA256).Hash
$FileInformation = Get-AppLockerFileInformation -Path $script:dest | Select-Object Publisher | Select-String -Pattern '{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}'
$MSIProductCode = $FileInformation.Matches

if ($script:dest.EndsWith('msix','CurrentCultureIgnoreCase') -or $script:dest.EndsWith('msixbundle','CurrentCultureIgnoreCase')) { $InstallerType = 'msix' }
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
elseif ($script:dest.EndsWith('msi','CurrentCultureIgnoreCase')) { $InstallerType = 'msi' }
elseif ($script:dest.EndsWith('appx','CurrentCultureIgnoreCase') -or $script:dest.EndsWith('appxbundle','CurrentCultureIgnoreCase')) { $InstallerType = 'appx' }
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
elseif ($script:dest.EndsWith('zip','CurrentCultureIgnoreCase')) { $InstallerType = 'zip' }

if ($InstallerUrl -match [regex]('\bx64\b') -or $InstallerUrl -match [regex]('\bwin64\b') -or $InstallerUrl -match [regex]('\b64\b')) {$architecture = 'x64'}
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
elseif ($InstallerUrl -match [regex]('\bx86\b') -or $InstallerUrl -match ('\bwin32\b') -or $InstallerUrl -match [regex]('\bia32\b') -or $InstallerUrl -match [regex]('\b86\b')) {$architecture = 'x86'}
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
elseif ($InstallerUrl -match [regex]('\barm64\b') -or $InstallerUrl -match [regex]('\baarch64\b')) { $architecture = 'arm64' }
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
elseif ($InstallerUrl -match [regex]('\barm\b')) { $architecture = 'arm' }

$MSIProductCode = $(Get-AppLockerFileInformation -Path $script:dest | Select-Object Publisher | Select-String -Pattern '{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}').Matches

if ($script:SaveOption -eq '1' -and -not($script:dest.EndsWith('appx', 'CurrentCultureIgnoreCase') -or $script:dest.EndsWith('msix', 'CurrentCultureIgnoreCase') -or $script:dest.EndsWith('appxbundle', 'CurrentCultureIgnoreCase') -or $script:dest.EndsWith('msixbundle', 'CurrentCultureIgnoreCase'))) { Remove-Item -Path $script:dest }
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
}
}

else {
Write-Host
do {
Expand All @@ -413,27 +423,31 @@ Function Read-WinGet-InstallerValues {
} until ($script:_returnValue.StatusCode -eq [ReturnValue]::Success().StatusCode)
}

do {
Write-Host -ForegroundColor 'Red' $script:_returnValue.ErrorString()
Write-Host -ForegroundColor 'Green' -Object '[Required] Enter the architecture. Options:' , @($Patterns.ValidArchitectures -join ', ')
$architecture = Read-Host -Prompt 'Architecture' | TrimString
if ($architecture -Cin @($Patterns.ValidArchitectures)) {
$script:_returnValue = [ReturnValue]::Success()
} else {
$script:_returnValue = [ReturnValue]::new(400, 'Invalid Architecture', "Value must exist in the enum - $(@($Patterns.ValidArchitectures -join ', '))", 2)
}
} until ($script:_returnValue.StatusCode -eq [ReturnValue]::Success().StatusCode)
if (-not $architecture -Cin @($Patterns.ValidArchitectures)) {
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
do {
Write-Host -ForegroundColor 'Red' $script:_returnValue.ErrorString()
Write-Host -ForegroundColor 'Green' -Object '[Required] Enter the architecture. Options:' , @($Patterns.ValidArchitectures -join ', ')
$architecture = Read-Host -Prompt 'Architecture' | TrimString
if ($architecture -Cin @($Patterns.ValidArchitectures)) {
$script:_returnValue = [ReturnValue]::Success()
} else {
$script:_returnValue = [ReturnValue]::new(400, 'Invalid Architecture', "Value must exist in the enum - $(@($Patterns.ValidArchitectures -join ', '))", 2)
}
} until ($script:_returnValue.StatusCode -eq [ReturnValue]::Success().StatusCode)
}

do {
Write-Host -ForegroundColor 'Red' $script:_returnValue.ErrorString()
Write-Host -ForegroundColor 'Green' -Object '[Required] Enter the InstallerType. Options:' , @($Patterns.ValidInstallerTypes -join ', ' )
$InstallerType = Read-Host -Prompt 'InstallerType' | TrimString
if ($InstallerType -Cin @($Patterns.ValidInstallerTypes)) {
$script:_returnValue = [ReturnValue]::Success()
} else {
$script:_returnValue = [ReturnValue]::new(400, 'Invalid Installer Type', "Value must exist in the enum - $(@($Patterns.ValidInstallerTypes -join ', '))", 2)
}
} until ($script:_returnValue.StatusCode -eq [ReturnValue]::Success().StatusCode)
if (-not $InstallerType -Cin @($Patterns.ValidInstallerTypes)) {
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
do {
Write-Host -ForegroundColor 'Red' $script:_returnValue.ErrorString()
Write-Host -ForegroundColor 'Green' -Object '[Required] Enter the InstallerType. Options:' , @($Patterns.ValidInstallerTypes -join ', ' )
$InstallerType = Read-Host -Prompt 'InstallerType' | TrimString
if ($InstallerType -Cin @($Patterns.ValidInstallerTypes)) {
$script:_returnValue = [ReturnValue]::Success()
} else {
$script:_returnValue = [ReturnValue]::new(400, 'Invalid Installer Type', "Value must exist in the enum - $(@($Patterns.ValidInstallerTypes -join ', '))", 2)
}
} until ($script:_returnValue.StatusCode -eq [ReturnValue]::Success().StatusCode)
}

if ($InstallerType -ieq 'exe') {
do {
Expand Down Expand Up @@ -530,7 +544,7 @@ Function Read-WinGet-InstallerValues {
$_menu = @{
entries = @('*[F] Find Automatically [Note: This will install the package to find Family Name and then removes it.]'; '[M] Manually Enter PackageFamilyName')
Prompt = 'Discover the package family name?'
DefaultString = 'M'
DefaultString = 'F'
}

switch ( KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString']) {
Expand All @@ -547,8 +561,6 @@ Function Read-WinGet-InstallerValues {
if (String.Validate $PackageFamilyName -IsNull) {
$script:_returnValue = [ReturnValue]::new(500, 'Could not find PackageFamilyName', 'Value should be entered manually', 1)
}
} else {
Write-Host $null
}

do {
Expand Down Expand Up @@ -675,7 +687,7 @@ Function Read-WinGet-InstallerValues {
$script:Installers += $_Installer

$_menu = @{
entries = @(
entries = @(
'[Y] Yes'
'*[N] No'
)
Expand Down Expand Up @@ -1311,6 +1323,11 @@ Function Submit-Manifest {
Write-Host
if ($PromptSubmit -eq '0') {
switch ($Option) {
'QuickUpdateVerison' {
if ($script:LastVersion -lt $script:PackageVersion ) { $CommitType = 'New version' }
elseif ($script:PackageVersion -in $script:ExistingVersions) { $CommitType = 'Update' }
elseif ($script:LastVersion -gt $script:PackageVersion ) { $CommitType = 'Add version' }
}
'New' {
if ( $script:OldManifestType -eq 'None' ) { $CommitType = 'New package' }
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
elseif ($script:LastVersion -lt $script:PackageVersion ) { $CommitType = 'New version' }
Expand Down Expand Up @@ -1560,7 +1577,7 @@ Function Write-WinGet-LocaleManifest-Yaml {

Function Read-PreviousWinGet-Manifest-Yaml {

if (($Option -eq 'NewLocale') -or ($Option -eq 'EditMetadata')) {
if (($script:Option -eq 'NewLocale') -or ($script:Option -eq 'EditMetadata')) {
if (Test-Path -Path "$AppFolder\..\$PackageVersion") {
$script:OldManifests = Get-ChildItem -Path "$AppFolder\..\$PackageVersion"
$LastVersion = $PackageVersion
Expand All @@ -1580,6 +1597,7 @@ Function Read-PreviousWinGet-Manifest-Yaml {
}

if (-not (Test-Path -Path "$AppFolder\..")) {
if ($script:Option -eq 'QuickUpdateVerison') { Write-Host -ForegroundColor Red "This option requires manifest of previous version of the package. If you want to create a new package, please select Option 1."; exit }
vedantmgoyal9 marked this conversation as resolved.
Show resolved Hide resolved
$script:OldManifestType = 'None'
return
}
Expand All @@ -1601,11 +1619,11 @@ Function Read-PreviousWinGet-Manifest-Yaml {
$script:OldLocaleManifest = ConvertFrom-Yaml -Yaml ($(Get-Content -Path $(Resolve-Path "$AppFolder\..\$LastVersion\$PackageIdentifier.locale.en-US.yaml") -Encoding UTF8) -join "`n") -Ordered
$script:OldVersionManifest = ConvertFrom-Yaml -Yaml ($(Get-Content -Path $(Resolve-Path "$AppFolder\..\$LastVersion\$PackageIdentifier.yaml") -Encoding UTF8) -join "`n") -Ordered
} elseif ($OldManifests.Name -eq "$PackageIdentifier.yaml") {
if ($Option -eq 'NewLocale') { Throw 'Error: MultiManifest Required' }
if ($script:Option -eq 'NewLocale') { Throw 'Error: MultiManifest Required' }
$script:OldManifestType = 'Singleton'
$script:OldVersionManifest = ConvertFrom-Yaml -Yaml ($(Get-Content -Path $(Resolve-Path "$AppFolder\..\$LastVersion\$PackageIdentifier.yaml") -Encoding UTF8) -join "`n") -Ordered
} else {
if ($Option -ne 'New') { Throw "Error: Version $LastVersion does not contain the required manifests" }
if ($script:Option -ne 'New') { Throw "Error: Version $LastVersion does not contain the required manifests" }
$script:OldManifestType = 'None'
return
}
Expand Down Expand Up @@ -1642,21 +1660,19 @@ Show-OptionMenu
Read-WinGet-MandatoryInfo
Read-PreviousWinGet-Manifest-Yaml

Switch ($Option) {

'New' {
Switch ($script:Option) {
'QuickUpdateVerison' {
Read-WinGet-InstallerValues
Read-WinGet-InstallerManifest
New-Variable -Name 'PackageLocale' -Value 'en-US' -Scope 'Script' -Force
Read-WinGet-LocaleManifest
Write-WinGet-LocaleManifest-Yaml
Write-WinGet-InstallerManifest-Yaml
Write-WinGet-VersionManifest-Yaml
Write-WinGet-LocaleManifest-Yaml
Test-Manifest
Submit-Manifest
}

'EditMetadata' {
'New' {
Read-WinGet-InstallerValues
Read-WinGet-InstallerManifest
New-Variable -Name 'PackageLocale' -Value 'en-US' -Scope 'Script' -Force
Read-WinGet-LocaleManifest
Expand All @@ -1667,8 +1683,7 @@ Switch ($Option) {
Submit-Manifest
}

'Update' {
Read-WinGet-InstallerValues
'EditMetadata' {
Read-WinGet-InstallerManifest
New-Variable -Name 'PackageLocale' -Value 'en-US' -Scope 'Script' -Force
Read-WinGet-LocaleManifest
Expand Down Expand Up @@ -1758,4 +1773,4 @@ Class ReturnValue {
return "[$($this.Severity)] $($this.Title) - $($this.Message)`n"
}
}
}
}