From c1c008e8db031a307cc25f5804716be3cb0814eb Mon Sep 17 00:00:00 2001 From: Matt Wrock Date: Mon, 3 Sep 2012 11:40:56 -0700 Subject: [PATCH] Added function to associate file extensions with installed executables --- src/helpers/chocolateyInstaller.psm1 | 2 +- .../Install-ChocolateyFileAssociation.ps1 | 21 +++++++++++++++++++ tests/_Common.ps1 | 2 +- tests/_FunctionRenameActuals.ps1 | 1 + tests/_Initialize-Variables.ps1 | 5 ++++- .../Install-ChocolateyFileAssociation.ps1 | 12 +++++++++++ 6 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/helpers/functions/Install-ChocolateyFileAssociation.ps1 create mode 100644 tests/helpers/Install-ChocolateyFileAssociation.ps1 diff --git a/src/helpers/chocolateyInstaller.psm1 b/src/helpers/chocolateyInstaller.psm1 index 5dbf0c4..0164ccb 100644 --- a/src/helpers/chocolateyInstaller.psm1 +++ b/src/helpers/chocolateyInstaller.psm1 @@ -9,4 +9,4 @@ Resolve-Path $helpersPath\functions\*.ps1 | ? { -not ($_.ProviderPath.Contains(".Tests.")) } | % { . $_.ProviderPath } -Export-ModuleMember -Function Start-ChocolateyProcessAsAdmin, Install-ChocolateyPackage, Uninstall-ChocolateyPackage, Install-ChocolateyZipPackage, Install-ChocolateyPowershellCommand, Get-ChocolateyWebFile, Install-ChocolateyInstallPackage, Get-ChocolateyUnzip, Write-ChocolateySuccess, Write-ChocolateyFailure, Install-ChocolateyPath, Install-ChocolateyDesktopLink, Write-Host, Write-Error \ No newline at end of file +Export-ModuleMember -Function Start-ChocolateyProcessAsAdmin, Install-ChocolateyPackage, Uninstall-ChocolateyPackage, Install-ChocolateyZipPackage, Install-ChocolateyPowershellCommand, Get-ChocolateyWebFile, Install-ChocolateyInstallPackage, Get-ChocolateyUnzip, Write-ChocolateySuccess, Write-ChocolateyFailure, Install-ChocolateyPath, Install-ChocolateyDesktopLink, Write-Host, Write-Error, Install-ChocolateyFileAssociation \ No newline at end of file diff --git a/src/helpers/functions/Install-ChocolateyFileAssociation.ps1 b/src/helpers/functions/Install-ChocolateyFileAssociation.ps1 new file mode 100644 index 0000000..f96611b --- /dev/null +++ b/src/helpers/functions/Install-ChocolateyFileAssociation.ps1 @@ -0,0 +1,21 @@ +function Install-ChocolateyFileAssociation { +param( + [string] $extension, + [string] $executable +) + Write-Debug "Running 'Install-ChocolateyFileAssociation' associating $extension with `'$executable`'"; + if(-not(Test-Path $executable)){ + $errorMessage = "`'$executable`' does not exist, not able to create association" + Write-Error $errorMessage + throw $errorMessage + } + $extension=$extension.trim() + if(-not($extension.StartsWith("."))) { + $extension = ".$extension" + } + $fileType = Split-Path $executable -leaf + $fileType = $fileType.Replace(" ","_") + $elevated = "cmd /c 'assoc $extension=$fileType';cmd /c 'ftype $fileType=\`"$executable\`" \`"%1\`" \`"%*\`"'" + Start-ChocolateyProcessAsAdmin $elevated + Write-Host "`'$extension`' has been associated with `'$executable`'" +} \ No newline at end of file diff --git a/tests/_Common.ps1 b/tests/_Common.ps1 index 5ce8bf1..c337fe0 100644 --- a/tests/_Common.ps1 +++ b/tests/_Common.ps1 @@ -6,7 +6,7 @@ $setup = Join-Path $here '_Setup.ps1' $initializeVariables = Join-Path $here '_Initialize-Variables.ps1' $installModule = Join-Path (Join-Path $src 'helpers') 'chocolateyInstaller.psm1' -Import-Module $installModule -Function Start-ChocolateyProcessAsAdmin, Install-ChocolateyPackage, Install-ChocolateyZipPackage, Install-ChocolateyPowershellCommand, Get-ChocolateyWebFile, Install-ChocolateyInstallPackage, Get-ChocolateyUnzip, Write-ChocolateySuccess, Write-ChocolateyFailure, Install-ChocolateyPath, Install-ChocolateyDesktopLink +Import-Module $installModule -Function Start-ChocolateyProcessAsAdmin, Install-ChocolateyPackage, Install-ChocolateyZipPackage, Install-ChocolateyPowershellCommand, Get-ChocolateyWebFile, Install-ChocolateyInstallPackage, Get-ChocolateyUnzip, Write-ChocolateySuccess, Write-ChocolateyFailure, Install-ChocolateyPath, Install-ChocolateyDesktopLink, Install-ChocolateyFileAssociation Import-Module $script Import-Module $functionRenames diff --git a/tests/_FunctionRenameActuals.ps1 b/tests/_FunctionRenameActuals.ps1 index 55894cd..8f52b18 100644 --- a/tests/_FunctionRenameActuals.ps1 +++ b/tests/_FunctionRenameActuals.ps1 @@ -32,6 +32,7 @@ rename-item function:Get-ChocolateyUnzip Get-ChocolateyUnzip-Actual rename-item function:Get-ChocolateyWebFile Get-ChocolateyWebFile-Actual #rename-item function:Get-WebFile Get-WebFile-Actual rename-item function:Install-ChocolateyDesktopLink Install-ChocolateyDesktopLink-Actual +rename-item function:Install-ChocolateyFileAssociation Install-ChocolateyFileAssociation-Actual rename-item function:Install-ChocolateyInstallPackage Install-ChocolateyInstallPackage-Actual rename-item function:Install-ChocolateyPackage Install-ChocolateyPackage-Actual rename-item function:Install-ChocolateyPath Install-ChocolateyPath-Actual diff --git a/tests/_Initialize-Variables.ps1 b/tests/_Initialize-Variables.ps1 index 3f46272..6ec9513 100644 --- a/tests/_Initialize-Variables.ps1 +++ b/tests/_Initialize-Variables.ps1 @@ -18,7 +18,8 @@ $script:path = '' $script:configValue = '' $script:action = '' - + $script:extension = '' + # function calls $script:chocolatey_install_was_called = $false $script:chocolatey_installall_was_called = $false @@ -52,6 +53,7 @@ $script:get_chocolateywebfile_was_called = $false $script:get_webfile_was_called = $false $script:install_chocolateydesktoplink_was_called = $false + $script:install_ChocolateyFileAssociation_was_called = $false $script:install_chocolateyinstallpackage_was_called = $false $script:install_chocolateypackage_was_called = $false $script:install_chocolateypath_was_called = $false @@ -136,6 +138,7 @@ $script:exec_get_chocolateywebfile_actual = $false $script:exec_get_webfile_actual = $false $script:exec_install_chocolateydesktoplink_actual = $false + $script:exec_install_chocolateyfileassociation_actual = $false $script:exec_install_chocolateyinstallpackage_actual = $false $script:exec_install_chocolateypackage_actual = $false $script:exec_install_chocolateypath_actual = $false diff --git a/tests/helpers/Install-ChocolateyFileAssociation.ps1 b/tests/helpers/Install-ChocolateyFileAssociation.ps1 new file mode 100644 index 0000000..8f1dd43 --- /dev/null +++ b/tests/helpers/Install-ChocolateyFileAssociation.ps1 @@ -0,0 +1,12 @@ +function Install-ChocolateyFileAssociation { +param( + [string] $extension, + [string] $executable +) + + $script:install_ChocolateyFileAssociation_was_called = $true + $script:extension = $extension + $script:executable = $executable + + if ($script:exec_install_ChocolateyFileAssociation_actual) { Install-ChocolateyFileAssociation-Actual @PSBoundParameters} +} \ No newline at end of file