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

(#89) Remove cpack shim for choco pack #2551

Merged
merged 2 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions nuget/chocolatey/tools/chocolateysetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,77 @@ param (
}
}

function Remove-ShimWithAuthenticodeSignature {
param (
[string] $filePath
)
if (!(Test-Path $filePath)) {
return
}
AdmiringWorm marked this conversation as resolved.
Show resolved Hide resolved

$signature = Get-AuthenticodeSignature $filePath -ErrorAction SilentlyContinue

if (!$signature -or !$signature.SignerCertificate) {
Write-ChocolateyWarning "Shim found in $filePath, but was not signed. Ignoring removal..."
return
}

$possibleSignatures = @(
'RealDimensions Software, LLC'
'Chocolatey Software, Inc\.'
)

$possibleSignatures | ForEach-Object {
if ($signature.SignerCertificate.Subject -match "$_") {
Write-Output "Removing shim $filePath"
$null = Remove-Item "$filePath"

if (Test-Path "$filePath.ignore") {
$null = Remove-Item "$filePath.ignore"
}

if (Test-Path "$filePath.old") {
$null = Remove-Item "$filePath.old"
}
}
}

# This means the file was found, however did not get removed as it contained a authenticode signature that
# is not ours.
if (Test-Path $filePath) {
Write-ChocolateyWarning "Shim found in $filePath, but did not match our signature. Ignoring removal..."
return
}
}

function Remove-UnsupportedShimFiles {
param([string[]]$Paths)

$shims = @('cpack.exe')

$Paths | ForEach-Object {
$path = $_
$shims | ForEach-Object { Join-Path $path $_ } | Where-Object { Test-Path $_ } | ForEach-Object {
$path = $_
Write-Debug "Removing shim from '$path'."

try {
Remove-ShimWithAuthenticodeSignature -filePath $path
}
catch {
Write-ChocolateyWarning "Unable to remove '$path'. Please remove the file manually."
}
}
}
}

function Initialize-Chocolatey {
<#
.DESCRIPTION
This will initialize the Chocolatey tool by
a) setting up the "chocolateyPath" (the location where all chocolatey nuget packages will be installed)
b) Installs chocolatey into the "chocolateyPath"
c) Instals .net 4.0 if needed
c) Installs .net 4.0 if needed
d) Adds Chocolatey to the PATH environment variable so you have access to the choco commands.
.PARAMETER ChocolateyPath
Allows you to override the default path of (C:\ProgramData\chocolatey\) by specifying a directory chocolatey will install nuget packages.
Expand Down Expand Up @@ -107,6 +171,12 @@ Creating Chocolatey folders if they do not already exist.
Create-DirectoryIfNotExists $chocolateyExePath
Create-DirectoryIfNotExists $chocolateyLibPath

$possibleShimPaths = @(
Join-Path "$chocolateyPath" "redirects"
Join-Path "$thisScriptFolder" "chocolateyInstall\redirects"
)
Remove-UnsupportedShimFiles -Paths $possibleShimPaths

Install-ChocolateyFiles $chocolateyPath
Ensure-ChocolateyLibFiles $chocolateyLibPath

Expand Down Expand Up @@ -142,6 +212,8 @@ You may need to shut down and restart powershell and/or consoles
if (-not $allowInsecureRootInstall) {
Remove-OldChocolateyInstall $defaultChocolateyPathOld
}

Remove-UnsupportedShimFiles -Paths $chocolateyExePath
}

function Set-ChocolateyInstallFolder {
Expand Down Expand Up @@ -315,7 +387,7 @@ param(
$from = "$chocolateyPathOld\bin"
$to = "$chocolateyPath\bin"
# TODO: This exclusion list needs to be updated once shims are removed
$exclude = @("choco.exe", "chocolatey.exe", "cinst.exe", "clist.exe", "cpack.exe", "cpush.exe", "cuninst.exe", "cup.exe", "cver.exe", "RefreshEnv.cmd")
$exclude = @("choco.exe", "chocolatey.exe", "cinst.exe", "clist.exe", "cpush.exe", "cuninst.exe", "cup.exe", "cver.exe", "RefreshEnv.cmd")
Get-ChildItem -Path $from -recurse -Exclude $exclude |
% {
Write-Debug "Copying $_ `n to $to"
Expand Down
2 changes: 0 additions & 2 deletions src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
<EmbeddedResource Include="redirects\chocolatey.exe.ignore" />
<EmbeddedResource Include="redirects\cinst.exe.ignore" />
<EmbeddedResource Include="redirects\clist.exe.ignore" />
<EmbeddedResource Include="redirects\cpack.exe.ignore" />
<EmbeddedResource Include="redirects\cpush.exe.ignore" />
<EmbeddedResource Include="redirects\cuninst.exe.ignore" />
<EmbeddedResource Include="redirects\cup.exe.ignore" />
Expand All @@ -102,7 +101,6 @@
<EmbeddedResource Include="redirects\chocolatey.exe" />
<EmbeddedResource Include="redirects\cinst.exe" />
<EmbeddedResource Include="redirects\clist.exe" />
<EmbeddedResource Include="redirects\cpack.exe" />
<EmbeddedResource Include="redirects\cpush.exe" />
<EmbeddedResource Include="redirects\cuninst.exe" />
<EmbeddedResource Include="redirects\cup.exe" />
Expand Down
Binary file removed src/chocolatey.resources/redirects/cpack.exe
Binary file not shown.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public virtual void help_message(ChocolateyConfiguration configuration)
{
this.Log().Info(ChocolateyLoggers.Important, "Pack Command");
this.Log().Info(@"
Chocolatey will attempt to package a nuspec into a compiled nupkg. Some
may prefer to use `cpack` as a shortcut for `choco pack`.
Chocolatey will attempt to package a nuspec into a compiled nupkg.

NOTE: 100% compatible with older chocolatey client (0.9.8.32 and below)
with options and switches. In most cases you can still pass options
Expand All @@ -92,15 +91,11 @@ the command reference (`choco -?`).
NOTE: You can pass arbitrary property value pairs through to nuspecs.
These will replace variables formatted as `$property$` with the value passed.

NOTE: `cpack` has been deprecated as it has a name collision with CMake. Please
use `choco pack` instead. The shortcut will be removed in v1.

");

"chocolatey".Log().Info(ChocolateyLoggers.Important, "Usage");
"chocolatey".Log().Info(@"
choco pack [<path to nuspec>] [<options/switches>] [<property=value>]
cpack [<path to nuspec>] [<options/switches>] (DEPRECATED)
");

"chocolatey".Log().Info(ChocolateyLoggers.Important, "Examples");
Expand Down