choco icon indicating copy to clipboard operation
choco copied to clipboard

Profile command - add tab completion/refreshenv to PowerShell

Open ferventcoder opened this issue 9 years ago • 8 comments

This is a split off of #833.

ferventcoder avatar Aug 06 '16 20:08 ferventcoder

I'm trying to understand if refreshenv for powershell has been removed as a feature. If not, what is the correct way to load it? A few weeks ago you had to reload your profile, but it seems the profile is no longer updated.

masaeedu avatar Aug 16 '16 16:08 masaeedu

@masaeedu it's still there as a feature, you just may need to manually add it. We introduced #833 to address the issues of creating a profile in many automation scenarios.

To add it manually for now when you install and there isn't a profile found or the profile file is missing.

$profileInstall = @'
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
    Import-Module "$ChocolateyProfile"
}
'@

$profileFile = "$profile"
$chocoProfileSearch = '$ChocolateyProfile'
if(Select-String -Path $profileFile -Pattern $chocoProfileSearch -Quiet -SimpleMatch) {
    Write-Debug "Chocolatey profile is already installed."
    return
}

$profileInstall | Out-File $profileFile -Append -Encoding (Get-FileEncoding $profileFile)

ferventcoder avatar Aug 16 '16 16:08 ferventcoder

So just clarifying - this issue is about adding a new command to choco.exe that will add the Chocolatey bits to your PowerShell profile. This is for the scenario when you didn't have a $profile file when you first installed Chocolatey, and so the installer skipped that step.

flcdrg avatar Sep 20 '17 00:09 flcdrg

@flcdrg yes, that's the idea.

ferventcoder avatar Sep 20 '17 06:09 ferventcoder

Get-FileEncoding doesn't appear to be a standard module. Where can I get this?

dasjestyr avatar Mar 27 '18 17:03 dasjestyr

It's defined in https://github.com/chocolatey/choco/blob/master/nuget/chocolatey/tools/chocolateysetup.psm1#L551

flcdrg avatar Mar 27 '18 22:03 flcdrg

Hey Chocolatey Team, I was fussing around with Powershell 7 Preview and added a function to my $PROFILE, actually related to Chocolatey. I tried running it this morning and found my function didn't work.

In troubleshooting I found that my $profile was modified with a Chocolatey Profile script. First off I think I have done a disservice to Choco as I didn't realize there was tab completion until today. When researching this I have been missing this since 2016, Yikes! So something happened to my function and in its place the tab completion script. Not a big deal just wanted to add a comment about it here.

Anyhow it is awesome to have this feature.

ctmcisco avatar Nov 20 '19 15:11 ctmcisco

here's tweaked Get-FileEncoding that works for pwsh 7.1.4

function Get-FileEncoding($Path) {
     $bytes = [byte[]]((Get-Content $Path -AsByteStream -Raw -ReadCount 4)[0])

     if(!$bytes) { return 'utf8' }

     switch -regex ('{0:x2}{1:x2}{2:x2}{3:x2}' -f $bytes[0],$bytes[1],$bytes[2],$bytes[3]) {
         '^efbbbf'   { return 'utf8' }
         '^2b2f76'   { return 'utf7' }
         '^fffe'     { return 'unicode' }
         '^feff'     { return 'bigendianunicode' }
         '^0000feff' { return 'utf32' }
         default     { return 'ascii' }
     }
}

Beej126 avatar Aug 27 '21 02:08 Beej126