watkipet
watkipet

Reputation: 1059

How should I use the tools path in my chocolatey package to let people choose an installation directory?

I'm creating a Chocolatey package that just unzips a file. I'd like to give the user a choice of where to unzip it. Others have tried to do the same thing, and the answer was to use an environment variable.

It looks like there's an environment variable just for this purpose: ChocolateyToolsLocation

Here's my chocolateyinstall.ps1:

$ErrorActionPreference = 'Stop'; # stop on all errors
$toolsDir   = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
$installationDir   = "$(Get-ToolsLocation)"
$fileLocation = Join-Path $toolsDir 'MyApp.zip'

$packageArgs = @{
  packageName   = $env:ChocolateyPackageName
  unzipLocation = $installationDir
  file          = $fileLocation

  softwareName  = 'myapp*' #part or all of the Display Name as you see it in Programs and Features. It should be enough to be unique
}

Get-ChocolateyUnzip $fileLocation $installationDir

So there are two concepts here:

Do I have that right? The user should change the "Tools Location" if he or she wishes to change the installation directory, but "Tools Dir" is always where Chcolatey unpacks the package in its lib dir?

Upvotes: 0

Views: 1426

Answers (1)

TheCakeIsNaOH
TheCakeIsNaOH

Reputation: 61

The $toolsDir is a local variable inside your chocolateyInstall.ps1 defined as "$(Split-Path -parent $MyInvocation.MyCommand.Definition)". This sets it to the directory the chocolateyInstall.ps1 is located in. So if the chocolateyInstall.ps1 is located in a folder (like tools) inside the package, then it is not the location that the Chocolatey package is unpacked to, it is subfolder of that location (for example C:\ProgramData\chocolatey\lib\curl vs C:\ProgramData\chocolatey\lib\curl\tools).

The $env:ChocolateyToolsLocation is an environment variable that can be set by the user to any folder they want. It defaults to C:\tools, but can be set or modified by a user outside of Chocolatey by using any of the normal methods that an environment variable can be set with. It should not be used directly inside a chocolateyInstall.ps1, instead the Get-ToolsLocation helper should always be used.

The $toolsDir and $env:ChocolateyToolsLocation are completely separate things. They do have similar names for legacy reasons, and both are locations too which archives can be extracted.

If you want the end user to be able to control where the software archive is extracted too, then using a subfolder undre Get-ToolsLocation is a good way to go.

Upvotes: 2

Related Questions