chocolatey-community / chocolatey-extensions

Apache License 2.0
2 stars 4 forks source link

(chocolatey-core.extension) add helper to set install location #16

Open TheCakeIsNaOH opened 8 months ago

TheCakeIsNaOH commented 8 months ago

Sometimes, it may be desirable to set the installation directory manually in an install script. This can be done by setting $env:ChocolateyPackageInstallLocation, however, it would be good to have a helper that abstracts the functionality. It should not set the variable if it is already set, unless force is passed.

This is so the install directory can be set for installers that do not set an explicit install directory in the registry, and for packages that have a complex install script that requires manual install directory setting. See https://github.com/orgs/chocolatey-community/discussions/2254

AdmiringWorm commented 8 months ago

My thoughts around this is to introduce two new cmdlets with a similar name to the Get-AppInstallLocation that is already implemented.

  1. Add the cmdlet Set-AppInstallLocation, which should have the responsibilities of:
    • Should have the parameters -LiteralPath and -Force. Literal Path should be mandatory (could potentially have -Path as an alternative that allows globbing, but not sure how useful that is)
    • It should verify whether the environment variable is already set or not, and only override the environment variable if it isn't set (both null, and an empty value) unless -Force is also used.
    • It should verify that the path specified exists, if it doesn't exist, it should throw an exception.
    • It should have an output value of the path that was set.
  2. Add the cmdlet Update-AppInstallLocation, which should have the responsibilities of:
    • Should have the parameters -AppNamePattern and -Force where AppNamePattern is mandatory.
    • It should call Get-AppInstallLocation to get the correct install location.
    • It should throw an exception if the installation path could not be found.
    • It should verify whether the environment variable is already set or not, and only override the environment variable if it isn't set (both null, and an empty value) unless -Force is also used.
    • It should have an output value of the path that was set.

Alternatively to the second cmdlet, we could also just have the first one but make LiteralPath/Path and AppNamePattern be mandatory in different sets.