Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

[Enhancement] Function to associate file extensions with installed executables #146

Merged
merged 1 commit into from Sep 13, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/helpers/chocolateyInstaller.psm1
Expand Up @@ -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
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
21 changes: 21 additions & 0 deletions 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`'"
}
2 changes: 1 addition & 1 deletion tests/_Common.ps1
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/_FunctionRenameActuals.ps1
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/_Initialize-Variables.ps1
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions 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}
}