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

Add option to include install-args in the choco export command #2503

Open
jimbrig opened this issue Dec 29, 2021 · 7 comments · May be fixed by #2506
Open

Add option to include install-args in the choco export command #2503

jimbrig opened this issue Dec 29, 2021 · 7 comments · May be fixed by #2506
Assignees
Milestone

Comments

@jimbrig
Copy link

jimbrig commented Dec 29, 2021

It would be nice to have a flag similar to the --include-version-numbers flag for the choco export command but for including the installation arguments of a given package (assuming the user has the configuration useRememberedArgumentsForUpgrades enabled).

Consider the following scenario where I install Git with various installation arguments applied:

cinst -y git.install --installargs "'/COMPONENTS=icons,assoc,assoc_sh,autoupdate,windowsterminal,scalar'" --params "/GitAndUnixToolsOnPath /NoAutoCrlf /WindowsTerminal /NoShellIntegration /NoGuiHereIntegration /NoShellHereIntegration /WindowsTerminalProfile /DefaultBranchName:main /Editor:VisualStudioCode"

then run choco export -o="choco-packages.config", however the output XML only contains:

<package id="git.install"/>

whereas it could contain something like so:

<package id="git.install" arguments=" --install-arguments=&quot;&apos;/COMPONENTS=icons,assoc,assoc_sh,autoupdate,windowsterminal,scalar&apos;&quot; --package-parameters=&quot;&apos;/GitAndUnixToolsOnPath /NoAutoCrlf /WindowsTerminal /NoShellIntegration /NoGuiHereIntegration /NoShellHereIntegration /WindowsTerminalProfile /DefaultBranchName:main /Editor:VisualStudioCode&apos;&quot;" />

This way, when I run cinst -y choco-packages.config it would apply the initial install-args and params for Git.

Proposed Solution: add --include-install-args flag to the choco export command.

@jimbrig
Copy link
Author

jimbrig commented Dec 29, 2021

I investigated a little further and found that installation arguments are stored under the path:
$env:ChocolateyInstall\.chocolatey\<package>.<version>\.arguments
(again, assuming the user has the config option useRememberedArgumentsForUpgrades enabled).

Therefore, to implement the proposed enhancement above to the export command, you could gather package specific installation arguments using some helper functions like these:

Add-Type -AssemblyName System.Security
$entropyBytes = [System.Text.UTF8Encoding]::UTF8.GetBytes("Chocolatey")

Function Unprotect-Arguments {
    param([string]$data)
    $encryptedByteArray = [System.Convert]::FromBase64String($data)
    $decryptedByteArray = [System.Security.Cryptography.ProtectedData]::Unprotect(
        $encryptedByteArray,
        $entropyBytes,
        [System.Security.Cryptography.DataProtectionScope]::LocalMachine
    )
    return [System.Text.UTF8Encoding]::UTF8.GetString($decryptedByteArray)
}

Function Read-Arguments {
    param([string]$packageName)
    $directory = Get-ChildItem $env:ChocolateyInstall\.chocolatey -Directory -Filter "$packageName*" | `
        Where-Object { $_.Name -match ("$packageName" + "\.[\d\.]+") } | Select-Object -Last 1
    If (!($directory)) { return }
    $argsFile = Join-Path $directory.fullname ".arguments"
    If (Test-Path $argsFile) {
        $argsData = Get-Content $argsFile
        #Implicitly return result from Unprotect-Arguments
        Unprotect-Arguments -data $argsData
    }
}

Function Get-LocalPackageInfo {
    & choco list -lo -r -y
}

Function Get-PackagesConfigBody($SaveArguments = $false) {
    Get-LocalPackageInfo | ForEach-Object {
        $packageName = $($_.SubString(0, $_.IndexOf("|")))
        $line = '   <package id="' + $packageName + '" '

        If ($SaveArguments) {
            $line = $line + 'arguments="' + [System.Security.SecurityElement]::Escape($(Read-Arguments $packageName)) + '" '
        }
    
        $line = $line + '/>'
        $line

    }
}

Function Write-PackagesConfig($OutputFile, $SaveArguments = $false) {
    $header = "<?xml version=`"1.0`" encoding=`"utf-8`"?>`n<packages>"
    $footer = "</packages>"
    $body = Get-PackagesConfigBody -SaveArguments $SaveArguments
    Write-Output $header $body $footer | Out-File $OutputFile -Encoding ASCII
}

and choco export -o="choco-packages.config" --include-install-args would utilize something like:

Write-PackagesConfig -OutputFile "choco-packages.config" -SaveArguments $true

Resulting in XML like this:

<?xml version="1.0" encoding="utf-8"?>
<packages>
   <package id="gh" arguments=" --cache-location=&quot;&apos;C:\Users\jimmy\AppData\Local\Temp\chocolatey&apos;&quot;" />
   <package id="git.install" arguments=" --cache-location=&quot;&apos;C:\Users\jimmy\AppData\Local\Temp\chocolatey&apos;&quot; --install-arguments=&quot;&apos;/COMPONENTS=icons,assoc,assoc_sh,autoupdate,windowsterminal,scalar&apos;&quot; --package-parameters=&quot;&apos;/GitAndUnixToolsOnPath /NoAutoCrlf /WindowsTerminal /NoShellIntegration /NoGuiHereIntegration /NoShellHereIntegration /WindowsTerminalProfile /DefaultBranchName:main /Editor:VisualStudioCode&apos;&quot;" />
</packages>

I tested this suite of functions locally and it worked exactly as mentioned above. Only potentially undesired aspect of the result is the --cache-location being included from the reading of the encrypted .chocolatey/<package>.<version>/.arguments file.

Here's the script I used (as .txt due to GitHub limitations):

@TheCakeIsNaOH
Copy link
Member

Thanks for adding this issue, I had thought that there already was one posted for this feature request.

This is definitely something that is planned to be added at some point, but it would be in the C# code, not in the PowerShell helper functions. See #1825 for where it would go in.

Also, I discussed this with @gep13, and it is likely that the other install argument would be added with individual elements, instead of via a single arguments element. This would require that #886 be completed first, and makes changes so the code for parsing CLI arguments can be reused for saved arguments.

TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 5, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 5, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
@TheCakeIsNaOH TheCakeIsNaOH linked a pull request Jan 5, 2022 that will close this issue
7 tasks
@Cyber1000
Copy link

Will this add the state of pinned-packages like stated here?
#1825 (comment)

Exactly. So if I pinned VS Code on one machine, it would end up pinned on another machine.

Wasn't quite sure if I should open a new issue, but would find it quite useful to export this with choco export

@TheCakeIsNaOH
Copy link
Member

TheCakeIsNaOH commented Feb 22, 2022

Wasn't quite sure if I should open a new issue,

@Cyber1000 In this case, a new issue would be appropriate. The information about whether a package is pinned or not is stored separately from the saved arguments, and there is not currently a way to pin a package during installation (although I do have a PR open to add that).

TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Feb 25, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Feb 25, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 6, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 6, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 6, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 6, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 9, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 9, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 9, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 9, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 21, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 21, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 21, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Mar 21, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jun 27, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jun 27, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jun 27, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jun 27, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
@TheCakeIsNaOH TheCakeIsNaOH self-assigned this Jul 30, 2022
@gep13 gep13 added this to the 1.2.0 milestone Aug 11, 2022
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Aug 11, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Aug 11, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Aug 12, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
@pauby pauby removed this from the 1.2.0 milestone Aug 25, 2022
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Sep 23, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Sep 23, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. This reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the saved arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Sep 23, 2022
Instead of exporting via building an xml document manually, this
creates a PackagesConfigFilePackageSetting and serializes that to
create the xml document that is saved. This allows for usage of the
same class as is used to read in packages.config files to export
those files.

The reason the "bool *Specified" members are added to the
PackagesConfigFilePackageSetting class is so if an element is not set
during export, it will not show up at all in the resulting serialized
packages.config file.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Sep 23, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Sep 23, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. It reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the remembered arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Dec 22, 2022
Instead of exporting via building an xml document manually, this
creates a PackagesConfigFilePackageSetting and serializes that to
create the xml document that is saved. This allows for usage of the
same class as is used to read in packages.config files to export
those files.

The reason the "bool *Specified" members are added to the
PackagesConfigFilePackageSetting class is so if an element is not set
during export, it will not show up at all in the resulting serialized
packages.config file.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Dec 22, 2022
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Dec 22, 2022
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. It reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the remembered arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 7, 2023
Instead of exporting via building an xml document manually, this
creates a PackagesConfigFilePackageSetting and serializes that to
create the xml document that is saved. This allows for usage of the
same class as is used to read in packages.config files to export
those files.

The reason the "bool *Specified" members are added to the
PackagesConfigFilePackageSetting class is so if an element is not set
during export, it will not show up at all in the resulting serialized
packages.config file.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 7, 2023
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 7, 2023
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. It reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the remembered arguments.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 13, 2023
Instead of exporting via building an xml document manually, this
creates a PackagesConfigFilePackageSetting and serializes that to
create the xml document that is saved. This allows for usage of the
same class as is used to read in packages.config files to export
those files.

The reason the "bool *Specified" members are added to the
PackagesConfigFilePackageSetting class is so if an element is not set
during export, it will not show up at all in the resulting serialized
packages.config file.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 13, 2023
Changes the set_package_config_for_upgrade method in the nuget service
to be public so it can be accessed for purposes of parsing the saved
arguments so they can be exported.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 13, 2023
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. It reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the remembered arguments.
@gep13 gep13 added this to the 2.3.0 milestone Dec 14, 2023
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 10, 2024
Instead of exporting via building an xml document manually, this
creates a PackagesConfigFilePackageSetting and serializes that to
create the xml document that is saved. This allows for usage of the
same class as is used to read in packages.config files to export
those files.

The reason the various "Specified" members are added to the
PackagesConfigFilePackageSetting class is so if an element is not set
during export, it will not show up at all in the resulting serialized
packages.config file.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Jan 10, 2024
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. It reuses the
GetPackageConfigFromRememberedArguments method in the NugetService to
read and parse the remembered arguments.
@bn-c
Copy link

bn-c commented Apr 18, 2024

👀 Any ETA on this feature getting merged?

@pauby
Copy link
Member

pauby commented Apr 18, 2024

We hope to be bringing this in shortly.

TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Apr 28, 2024
Instead of exporting via building an xml document manually, this
creates a PackagesConfigFilePackageSetting and serializes that to
create the xml document that is saved. This allows for usage of the
same class as is used to read in packages.config files to export
those files.

The reason the various "Specified" members are added to the
PackagesConfigFilePackageSetting class is so if an element is not set
during export, it will not show up at all in the resulting serialized
packages.config file.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Apr 28, 2024
This adds the --include-remembered-arguments option which is used to
export any remembered arguments. It reuses the
GetPackageConfigFromRememberedArguments method in the NugetService to
read and parse the remembered arguments.
@gep13 gep13 modified the milestones: 2.3.0, 2.4.0 May 7, 2024
@gep13
Copy link
Member

gep13 commented May 7, 2024

Based on the changes in the associated PR's for this issue, I am going to have to bump this issue from the 2.3.0 release, and instead look at this again in the 2.4.0.

The suggested changes to the INuGetService mean that this would essentially be a breaking change, that would need a corresponding change in the Chocolatey Licensed Extension, which we haven't planned to do at the minute.

Instead of holding up the release of 2.3.0, we will look to do the work in this issue in a later release.

Apologies for any inconvenience that this causes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants