Skip to content

Commit

Permalink
Add settings file and script documentation (#38)
Browse files Browse the repository at this point in the history
* Do not default installer locale

* Add a script settings file with documentation

* Add setting to suppress quick update warning

* Remove accidental file inclusion
  • Loading branch information
Trenly authored Sep 21, 2021
1 parent 89cdb23 commit 81fe7f2
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 44 deletions.
122 changes: 78 additions & 44 deletions Tools/YamlCreate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ try {
exit 1
}

# Check for settings directory and create it if none exists
$script:SettingsPath = Join-Path $env:LOCALAPPDATA -ChildPath 'YamlCreate'
if (!(Test-Path $script:SettingsPath)) { New-Item -ItemType 'Directory' -Force -Path $script:SettingsPath | Out-Null }
# Check for settings file and create it if none exists
$script:SettingsPath = $(Join-Path $script:SettingsPath -ChildPath 'Settings.yaml')
if (!(Test-Path $script:SettingsPath)) { '# See https://github.com/microsoft/winget-pkgs/tree/master/doc/tools/YamlCreate.md for a list of available settings' > $script:SettingsPath }
# Load settings from file
$ScriptSettings = ConvertFrom-Yaml -Yaml ($(Get-Content -Path $script:SettingsPath -Encoding UTF8) -join "`n")

filter TrimString {
$_.Trim()
}
Expand Down Expand Up @@ -300,16 +309,24 @@ Function Read-WinGet-InstallerValues {
$InstallerUrl = Request-Installer-Url

# Get or request Installer Sha256
$_menu = @{
entries = @('[Y] Yes'; '*[N] No'; '[M] Manually Enter SHA256')
Prompt = 'Do you want to save the files to the Temp folder?'
DefaultString = 'N'
}
switch ( KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString']) {
'Y' { $script:SaveOption = '0' }
'N' { $script:SaveOption = '1' }
'M' { $script:SaveOption = '2' }
default { $script:SaveOption = '1' }
# Check the settings to see if we need to display this menu
switch ($ScriptSettings.SaveToTemporaryFolder) {
'true' { $script:SaveOption = '0' }
'false' { $script:SaveOption = '1' }
'manual' { $script:SaveOption = '2' }
default {
$_menu = @{
entries = @('[Y] Yes'; '*[N] No'; '[M] Manually Enter SHA256')
Prompt = 'Do you want to save the files to the Temp folder?'
DefaultString = 'N'
}
switch ( KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString']) {
'Y' { $script:SaveOption = '0' }
'N' { $script:SaveOption = '1' }
'M' { $script:SaveOption = '2' }
default { $script:SaveOption = '1' }
}
}
}

# If user did not select manual entry for Sha256, download file and calculate hash
Expand Down Expand Up @@ -538,13 +555,14 @@ Function Read-WinGet-InstallerValues {
Write-Host -ForegroundColor 'Yellow' -Object '[Optional] Enter the installer locale. For example: en-US, en-CA'
Write-Host -ForegroundColor 'Blue' -Object 'https://docs.microsoft.com/openspecs/office_standards/ms-oe376/6c085406-a698-4e12-9d4d-c3b0ee3dbc4a'
$InstallerLocale = Read-Host -Prompt 'InstallerLocale' | TrimString
if (String.Validate $InstallerLocale -IsNull) { $InstallerLocale = 'en-US' }
# If user defined a default locale, add it
if ((String.Validate $InstallerLocale -IsNull) -and (String.Validate -not $ScriptSettings.DefaultInstallerLocale -IsNull)) { $InstallerLocale = $ScriptSettings.DefaultInstallerLocale }

if (String.Validate $InstallerLocale -MaxLength $Patterns.InstallerLocaleMaxLength -MatchPattern $Patterns.PackageLocale -NotNull) {
if (String.Validate $InstallerLocale -MaxLength $Patterns.InstallerLocaleMaxLength -MatchPattern $Patterns.PackageLocale -AllowNull) {
$script:_returnValue = [ReturnValue]::Success()
} else {
if (String.Validate -not $InstallerLocale -MaxLength $Patterns.InstallerLocaleMaxLength -NotNull) {
$script:_returnValue = [ReturnValue]::LengthError(1, $Patterns.InstallerLocaleMaxLength)
if (String.Validate -not $InstallerLocale -MaxLength $Patterns.InstallerLocaleMaxLength -AllowNull) {
$script:_returnValue = [ReturnValue]::LengthError(0, $Patterns.InstallerLocaleMaxLength)
} elseif (String.Validate -not $InstallerLocale -MatchPattern $Patterns.PackageLocale) {
$script:_returnValue = [ReturnValue]::PatternError()
} else {
Expand Down Expand Up @@ -691,16 +709,24 @@ Function Read-WinGet-InstallerValues-Minimal {
$_NewInstaller['InstallerUrl'] = $NewInstallerUrl

# Get or request Installer Sha256
$_menu = @{
entries = @('[Y] Yes'; '*[N] No'; '[M] Manually Enter SHA256')
Prompt = 'Do you want to save the files to the Temp folder?'
DefaultString = 'N'
}
switch ( KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString']) {
'Y' { $script:SaveOption = '0' }
'N' { $script:SaveOption = '1' }
'M' { $script:SaveOption = '2' }
default { $script:SaveOption = '1' }
# Check the settings to see if we need to display this menu
switch ($ScriptSettings.SaveToTemporaryFolder) {
'true' { $script:SaveOption = '0' }
'false' { $script:SaveOption = '1' }
'manual' { $script:SaveOption = '2' }
default {
$_menu = @{
entries = @('[Y] Yes'; '*[N] No'; '[M] Manually Enter SHA256')
Prompt = 'Do you want to save the files to the Temp folder?'
DefaultString = 'N'
}
switch ( KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString']) {
'Y' { $script:SaveOption = '0' }
'N' { $script:SaveOption = '1' }
'M' { $script:SaveOption = '2' }
default { $script:SaveOption = '1' }
}
}
}

# If user did not select manual entry for Sha256, download file and calculate hash
Expand Down Expand Up @@ -1223,12 +1249,17 @@ Function Enter-PR-Parameters {
$_showMenu = $true
switch -Wildcard ( $_line ) {
'*CLA*' {
$_menu = @{
Prompt = 'Have you signed the Contributor License Agreement (CLA)?'
Entries = @('[Y] Yes'; '*[N] No')
HelpText = 'Reference Link: https://cla.opensource.microsoft.com/microsoft/winget-pkgs'
HelpTextColor = ''
DefaultString = 'N'
if ($ScriptSettings.SignedCLA -eq 'true') {
$PrBodyContentReply += @($_line.Replace('[ ]', '[X]'))
$_showMenu = $false
} else {
$_menu = @{
Prompt = 'Have you signed the Contributor License Agreement (CLA)?'
Entries = @('[Y] Yes'; '*[N] No')
HelpText = 'Reference Link: https://cla.opensource.microsoft.com/microsoft/winget-pkgs'
HelpTextColor = ''
DefaultString = 'N'
}
}
}

Expand Down Expand Up @@ -1645,7 +1676,7 @@ switch ($Keys[$keyInfo.Key]) {
}

# Confirm the user undertands the implications of using the quick update mode
if ($script:Option -eq 'QuickUpdateVersion') {
if (($script:Option -eq 'QuickUpdateVersion') -and ($ScriptSettings.SuppressQuickUpdateWarning -ne 'true')) {
$_menu = @{
entries = @('[Y] Continue with Quick Update'; '[N] Use Full Update Experience'; '*[Q] Exit Script')
Prompt = 'Quick Updates only allow for changes to the existing Installer URLs, Sha256 Values, and Product Codes. Are you sure you want to continue?'
Expand Down Expand Up @@ -1866,7 +1897,6 @@ Switch ($script:Option) {
'NewLocale' {
Read-WinGet-LocaleManifest
Write-WinGet-LocaleManifest-Yaml
if (Get-Command 'winget.exe' -ErrorAction SilentlyContinue) { winget validate $AppFolder }
}

'RemoveManifest' {
Expand All @@ -1891,17 +1921,22 @@ if ($script:Option -ne 'RemoveManifest') {

# If the user has sandbox enabled, request to test the manifest in the sandbox
if (Get-Command 'WindowsSandbox.exe' -ErrorAction SilentlyContinue) {
$_menu = @{
entries = @('*[Y] Yes'; '[N] No')
Prompt = '[Recommended] Do you want to test your Manifest in Windows Sandbox?'
DefaultString = 'Y'
}
switch ( KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString']) {
'Y' { $script:SandboxTest = '0' }
'N' { $script:SandboxTest = '1' }
default { $script:SandboxTest = '0' }
# Check the settings to see if we need to display this menu
if ($ScriptSettings.AlwaysTestManifests -eq 'true') {
$script:SandboxTest = '0'
} else {
$_menu = @{
entries = @('*[Y] Yes'; '[N] No')
Prompt = '[Recommended] Do you want to test your Manifest in Windows Sandbox?'
DefaultString = 'Y'
}
switch ( KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString']) {
'Y' { $script:SandboxTest = '0' }
'N' { $script:SandboxTest = '1' }
default { $script:SandboxTest = '0' }
}
Write-Host
}
Write-Host
if ($script:SandboxTest -eq '0') {
if (Test-Path -Path "$PSScriptRoot\SandboxTest.ps1") {
$SandboxScriptPath = (Resolve-Path "$PSScriptRoot\SandboxTest.ps1").Path
Expand All @@ -1911,12 +1946,11 @@ if ($script:Option -ne 'RemoveManifest') {
Write-Host -ForegroundColor 'Green' -Object 'SandboxTest.ps1 not found, input path'
$SandboxScriptPath = Read-Host -Prompt 'SandboxTest.ps1' | TrimString
}
& $SandboxScriptPath -Manifest $AppFolder
}
& $SandboxScriptPath -Manifest $AppFolder
}
}
}

# If the user has git installed, request to automatically submit the PR
if (Get-Command 'git.exe' -ErrorAction SilentlyContinue) {
$_menu = @{
Expand Down
43 changes: 43 additions & 0 deletions doc/tools/YamlCreate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Using YamlCreate.ps1
Using the YamlCreate is easy. First, [create a fork](https://docs.github.com/get-started/quickstart/fork-a-repo) of this repository and then [clone it](https://docs.github.com/repositories/creating-and-managing-repositories/cloning-a-repository) to your computer. Once the repository has finished cloning, open file explorer and navigate to the folder that the repository was cloned into. Inside this folder, you should be able to navigate to the `Tools` folder, which contains the script. Right click on "YamlCreate" and select "Run with PowerShell". If it is your first time running the script, you may see a message that it is installing some additional packages - [NuGet](https://docs.microsoft.com/nuget/), and [powershell-yaml] (https://www.powershellgallery.com/packages/powershell-yaml/0.4.2)([See it on GitHub](https://github.com/cloudbase/powershell-yaml)). These are required for the script to run.

Once the script begins, it will walk you through a series of prompts to create manifests. Enter the requested information for each of the prompts and once all the prompts are completed, the manifest will be generated! If you have Windows Sandbox enabled and the GitHub CLI installed, you can even automatically test and submit your manifest instead of having to do it manually.

# Optional Software
## Windows Package Manager
Because the script is meant for creating manifests for the Windows Package Manager, we highly recommend you install it. This allows the script to automatically validate the manifests that are generated and also provides you the ability to easily install other optional software packages. Instructions for [installing the package manager](https://github.com/microsoft/winget-cli#installing-the-client) can be found over on the [winget-cli repository](https://github.com/microsoft/winget-cli).

## Windows Sandbox
The Windows Sandbox is an optional feature within windows that allows you to run a virtual environment to test software. The [SandboxTest.ps1](/Tools/SandboxTest.ps1) script integrates with the Windows Sandbox to allow manifests to be tested without needing to install the software onto your machine. The script can automatically run the same validation as long as the Windows Sandbox is enabled. To enable the sandbox, open PowerShell and run the command `Enable-WindowsOptionalFeature -FeatureName "Containers-DisposableClientVM" -All -Online`. Once the command completes, restart your computer to finish the installation.

## Git
The script can automatically create commits and branches within your fork of the repo. In order to do this, you need to have Git installed. The easiest way to do this is to use winget! Open up PowerShell and install Git using `winget install Git`. This will allow the script to push the new manifest to your fork of the repository and provide you with a link to submit your pull request.

## GitHub CLI
If you have Git installed, you can also create the pull request for your manifest directly from the script. You will have to install the GitHub CLI and authorize it with your GitHub Account. Again, winget makes installing the CLI easy. Open up PowerShell and install the GitHub CLI using `winget install GitHub.CLI`. Once the CLI is installed, close, then re-open PowerShell. To log in and authorize the CLI, run the command `gh auth login`. Follow the prompts that appear to finish the authorization.

# YamlCreate Settings
YamlCreate offers a few settings to customize your manifest creation experience. The settings file is found in your local appdata folder under `YamlCreate`. It is empty by default, but you can copy the sample below which describes what all of the available options are; or, you can enter just specific keys as you see fit.
```yaml
# This setting allows you to skip the prompt to test your manifest in windows sandbox
# If this value is set to true, all manifests will be tested by default
AlwaysTestManifests: false

# This setting allows you to set a default installer locale
# Any value defined here will be set as the installer locale if one is not entered
DefaultInstallerLocale: en-US

# This setting allows you to define a default action for whether or not to save files to the temprorary folder
# true - Always saves files to the temporary folder
# false - Always removes the files from the temporary folder after secript excecution
# manual - Never downloads any files. All InstallerSha256 values must be entered manually
SaveToTemporaryFolder: false

# This setting allows you to set a default value for whether or not you have signed the Microsoft CLA
# If this value is set to true, all automatic PR's will be marked as having the CLA signed
SignedCLA: false

# This setting allows you to skip the prompt to confirm you want to use quick update mode
# If this value is set to true, the Quick Update Warning will be skipped
SuppressQuickUpdateWarning: false
```

0 comments on commit 81fe7f2

Please sign in to comment.