Skip to content

Commit

Permalink
Merge pull request #2861 from AdmiringWorm/new-path-helper
Browse files Browse the repository at this point in the history
(#2860) Add new PowerShell helper function for acquiring paths from private variables
  • Loading branch information
gep13 committed Oct 19, 2022
2 parents baac40f + dcf859e commit 83b6d99
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 9 deletions.
3 changes: 2 additions & 1 deletion GenerateDocs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $thisDirectory = (Split-Path -parent $MyInvocation.MyCommand.Definition);
$psModuleName = 'chocolateyInstaller'
$psModuleLocation = [System.IO.Path]::GetFullPath("$thisDirectory\src\chocolatey.resources\helpers\chocolateyInstaller.psm1")
$docsFolder = [System.IO.Path]::GetFullPath("$thisDirectory\docs\generated")
$chocoExe = [System.IO.Path]::GetFullPath("$thisDirectory\code_drop\chocolatey\console\choco.exe")
$chocoExe = [System.IO.Path]::GetFullPath("$thisDirectory\code_drop\temp\_PublishedApps\choco_merged\choco.exe")
$lineFeed = "`r`n"
$sourceLocation = 'https://github.com/chocolatey/choco/blob/master/'
$sourceCommands = $sourceLocation + 'src/chocolatey/infrastructure.app/commands'
Expand Down Expand Up @@ -80,6 +80,7 @@ These are the functions from above as one list.
* [Install-ChocolateyZipPackage](xref:install-chocolateyzippackage)
* [Install-ChocolateyPowershellCommand](xref:install-chocolateypowershellcommand)
* [Get-ChocolateyPath](xref:get-chocolateypath)
* [Get-ChocolateyWebFile](xref:get-chocolateywebfile)
* [Get-ChocolateyUnzip](xref:get-chocolateyunzip)
* [Install-ChocolateyPath](xref:install-chocolateypath) - when specifying user path
Expand Down
1 change: 1 addition & 0 deletions src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<EmbeddedResource Include="helpers\chocolateyInstaller.psm1" />
<EmbeddedResource Include="helpers\functions\Format-FileSize.ps1" />
<EmbeddedResource Include="helpers\functions\Get-CheckSumValid.ps1" />
<EmbeddedResource Include="helpers\functions\Get-ChocolateyPath.ps1" />
<EmbeddedResource Include="helpers\functions\Get-ChocolateyUnzip.ps1" />
<EmbeddedResource Include="helpers\functions\Get-ChocolateyWebFile.ps1" />
<EmbeddedResource Include="helpers\functions\Get-FtpFile.ps1" />
Expand Down
19 changes: 11 additions & 8 deletions src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,34 @@ $script:someCommands = @('-?','search','list','info','install','outdated','upgra
# ensure these all have a space to start, or they will cause issues
$allcommands = " --debug --verbose --trace --noop --help --accept-license --confirm --limit-output --no-progress --log-file='' --execution-timeout='' --cache-location='' --proxy='' --proxy-user='' --proxy-password='' --proxy-bypass-list='' --proxy-bypass-on-local --force --no-color --skip-compatibility-checks"
$proListOptions = " --audit --use-self-service"
$proInfoOptions = " --use-self-service"
$proInstallUpgradeOptions = " --install-directory='' --package-parameters-sensitive='' --max-download-rate='' --install-arguments-sensitive='' --skip-download-cache --use-download-cache --skip-virus-check --virus-check --virus-positives-minimum='' --deflate-package-size --no-deflate-package-size --deflate-nupkg-only --use-self-service"
$proUpgradeOptions = " --exclude-chocolatey-packages-during-upgrade-all --include-chocolatey-packages-during-upgrade-all --use-self-service"
$proNewOptions = " --file='' --build-package --file64='' --from-programs-and-features --use-original-location --keep-remote --url='' --url64='' --checksum='' --checksum64='' --checksumtype='' --pause-on-error --remove-architecture-from-name --include-architecture-in-name"
$proUninstallOptions = " --from-programs-and-features --use-self-service"
$proPinOptions = " --note='' --use-self-service"
$proOutdatedOptions = " --use-self-service"
$proPushOptions = " --use-self-service"

$commandOptions = @{
list = "--lo --id-only --pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user='' --password='' --local-only --prerelease --include-programs --page='' --page-size='' --order-by-popularity --download-cache-only --disable-package-repository-optimizations" + $proListOptions + $allcommands
search = "--pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user='' --password='' --local-only --prerelease --include-programs --page='' --page-size='' --order-by-popularity --download-cache-only --disable-package-repository-optimizations" + $allcommands
info = "--pre --lo --source='' --user='' --password='' --local-only --prerelease --disable-package-repository-optimizations" + $allcommands
install = "-y -whatif -? --pre --version= --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user='' --password='' --prerelease --forcex86 --not-silent --package-parameters='' --exit-when-reboot-detected --ignore-detected-reboot --allow-downgrade --force-dependencies --require-checksums --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts --allow-multiple-versions --ignore-checksums --allow-empty-checksums --allow-empty-checksums-secure --download-checksum='' --download-checksum-type='' --download-checksum-x64='' --download-checksum-type-x64='' --stop-on-first-package-failure --disable-package-repository-optimizations" + $proInstallUpgradeOptions + $allcommands
search = "--pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user='' --password='' --local-only --prerelease --include-programs --page='' --page-size='' --order-by-popularity --download-cache-only --disable-package-repository-optimizations" + $proListOptions + $allcommands
info = "--pre --lo --source='' --user='' --password='' --local-only --prerelease --disable-package-repository-optimizations" + $proInfoOptions + $allcommands
install = "-y -whatif -? --pre --version= --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user='' --password='' --prerelease --forcex86 --not-silent --package-parameters='' --exit-when-reboot-detected --ignore-detected-reboot --allow-downgrade --force-dependencies --require-checksums --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts --allow-multiple-versions --ignore-checksums --allow-empty-checksums --allow-empty-checksums-secure --download-checksum='' --download-checksum-type='' --download-checksum-x64='' --download-checksum-type-x64='' --stop-on-first-package-failure --disable-package-repository-optimizations --pin" + $proInstallUpgradeOptions + $allcommands
pin = "--name='' --version='' -?" + $proPinOptions + $allcommands
outdated = "-? --source='' --user='' --password='' --ignore-pinned --ignore-unfound --pre --prerelease --disable-package-repository-optimizations" + $allcommands
upgrade = "-y -whatif -? --pre --version='' --except='' --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user='' --password='' --prerelease --forcex86 --not-silent --package-parameters='' --exit-when-reboot-detected --ignore-detected-reboot --allow-downgrade --allow-multiple-versions --require-checksums --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts --fail-on-unfound --fail-on-not-installed --ignore-checksums --allow-empty-checksums --allow-empty-checksums-secure --download-checksum='' --download-checksum-type='' --download-checksum-x64='' --download-checksum-type-x64='' --exclude-prerelease --stop-on-first-package-failure --use-remembered-options --ignore-remembered-options --skip-when-not-installed --install-if-not-installed --disable-package-repository-optimizations" + $proInstallUpgradeOptions + $proUpgradeOptions + $allcommands
outdated = "-? --source='' --user='' --password='' --ignore-pinned --ignore-unfound --pre --prerelease --disable-package-repository-optimizations" + $proOutdatedOptions + $allcommands
upgrade = "-y -whatif -? --pre --version='' --except='' --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user='' --password='' --prerelease --forcex86 --not-silent --package-parameters='' --exit-when-reboot-detected --ignore-detected-reboot --allow-downgrade --allow-multiple-versions --require-checksums --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts --fail-on-unfound --fail-on-not-installed --ignore-checksums --allow-empty-checksums --allow-empty-checksums-secure --download-checksum='' --download-checksum-type='' --download-checksum-x64='' --download-checksum-type-x64='' --exclude-prerelease --stop-on-first-package-failure --use-remembered-options --ignore-remembered-options --skip-when-not-installed --install-if-not-installed --disable-package-repository-optimizations --pin" + $proInstallUpgradeOptions + $proUpgradeOptions + $allcommands
uninstall = "-y -whatif -? --force-dependencies --remove-dependencies --all-versions --source='windowsfeatures' --source='webpi' --version= --uninstall-arguments='' --override-arguments --not-silent --params='' --package-parameters='' --exit-when-reboot-detected --ignore-detected-reboot --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts --use-autouninstaller --skip-autouninstaller --fail-on-autouninstaller --ignore-autouninstaller-failure --stop-on-first-package-failure" + $proUninstallOptions + $allcommands
new = "--template-name='' --output-directory='' --automaticpackage --version='' --maintainer='' packageversion='' maintainername='' maintainerrepo='' installertype='' url='' url64='' silentargs='' --use-built-in-template -?" + $proNewOptions + $allcommands
pack = "--version='' --output-directory='' -?" + $allcommands
push = "--source='' --api-key='' --timeout='' -?" + $allcommands
push = "--source='' --api-key='' --timeout='' -?" + $proPushOptions + $allcommands
source = "--name='' --source='' --user='' --password='' --priority= --bypass-proxy --allow-self-service -?" + $allcommands
config = "--name='' --value='' -?" + $allcommands
feature = "--name='' -?" + $allcommands
apikey = "--source='' --api-key='' --remove -?" + $allcommands
download = "--internalize --internalize-all-urls --ignore-dependencies --installed-packages --ignore-unfound-packages --resources-location='' --download-location='' --outputdirectory='' --source='' --version='' --prerelease --user='' --password='' --cert='' --certpassword='' --append-use-original-location --recompile --disable-package-repository-optimizations -? --use-self-service" + $allcommands
sync = "--output-directory='' --id='' --package-id='' -?" + $allcommands
optimize = "--deflate-nupkg-only --id='' -?" + $allcommands
sync = "--output-directory='' --id='' --package-id='' -? --use-self-service" + $allcommands
optimize = "--deflate-nupkg-only --id='' -? --use-self-service" + $allcommands
export = "--include-version-numbers --output-file-path='' -?" + $allcommands
template = "--name=''" + $allcommands
}
Expand Down
83 changes: 83 additions & 0 deletions src/chocolatey.resources/helpers/functions/Get-ChocolateyPath.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright © 2022 Chocolatey Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function Get-ChocolateyPath {
<#
.SYNOPSIS
Retrieve the paths available to be used by maintainers of packages.
.DESCRIPTION
This function will attempt to retrieve the path according to the specified Path Type
to a valid location that can be used by maintainers in certain scenarios.
.NOTES
Available in 1.2.0+.
.INPUTS
None
.OUTPUTS
This function outputs the full path stored accordingly with specified path type.
If no path could be found, there is no output.
.PARAMETER pathType
The type of path that should be looked up.
Available values are:
- `PackagePath` - The path to the the package that is being installed. Typically `C:\ProgramData\chocolatey\lib\<PackageName>`
- `InstallPath` - The path to where Chocolatey is installed. Typically `C:\ProgramData\chocolatey`
.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.
.EXAMPLE
>
$path = Get-ChocolateyPath -PathType 'PackagePath'
#>
param(
[parameter(Mandatory=$true)]
[alias('type')] [string] $pathType
)

$result = $null

switch ($pathType) {
'PackagePath' {
if (Test-Path Env:\ChocolateyPackagePath) {
$result = "$env:ChocolateyPackagePath"
} elseif (Test-Path Env:\PackagePath) {
$result = "$env:PackagePath"
} else {
$installPath = Get-ChocolateyPath -pathType 'InstallPath'
$result = "$installPath\lib\$env:ChocolateyPackageName"
}
}
'InstallPath' {
if (Test-Path Env:\ChocolateyInstall) {
$result = "$env:ChocolateyInstall"
} elseif (Test-Path Env:\ProgramData) {
$result = "$env:ProgramData\chocolatey"
} else {
$result = "$env:SystemDrive\ProgramData\chocolatey"
}
}
Default {
throw "The path type $pathType is not a supported."
}
}

if ((Test-Path $result)) {
$result
}
}
20 changes: 20 additions & 0 deletions tests/chocolatey-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1574,4 +1574,24 @@ Describe "choco install" -Tag Chocolatey, InstallCommand {
$Output.String | Should -Not -Match "- $([regex]::Escape($_))"
}
}

Context "Installing package that makes use of new Get Chocolatey Path helper" {
BeforeAll {
Restore-ChocolateyInstallSnapshot
Enable-ChocolateySource -Name 'local'

$Output = Invoke-Choco install test-chocolateypath -y
}

It "Exits with Success (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Outputs message <_>" -ForEach @(
'Package Path in Install Script: <installPath>\lib\test-chocolateypath'
'Install Path in Install Script: <installPath>'
) {
$Output.Lines | Should -Contain "$($_ -replace '<installPath>',$env:ChocolateyInstall)" -Because $Output.String
}
}
}
25 changes: 25 additions & 0 deletions tests/chocolatey-tests/commands/choco-uninstall.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,29 @@ Describe "choco uninstall" -Tag Chocolatey, UninstallCommand {
$Output.Lines | Should -Contain "Chocolatey uninstalled 1/1 packages." -Because $Output.String
}
}

Context "Uninstalling package that makes use of new Get Chocolatey Path helper" {
BeforeAll {
Restore-ChocolateyInstallSnapshot

Enable-ChocolateySource -Name 'local'

$null = Invoke-Choco install test-chocolateypath -y

$Output = Invoke-Choco uninstall test-chocolateypath -y
}

It "Exits with Success (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Outputs message <_>" -ForEach @(
'Package Path in Before Modify Script: <installPath>\lib\test-chocolateypath'
'Install Path in Before Modify Script: <installPath>'
'Package Path in Uninstall Script: <installPath>\lib\test-chocolateypath'
'Install Path in Uninstall Script: <installPath>'
) {
$Output.Lines | Should -Contain ($_ -replace '<installPath>',$env:ChocolateyInstall) -Because $Output.String
}
}
}
Loading

0 comments on commit 83b6d99

Please sign in to comment.