Skip to content

Commit

Permalink
Merge pull request #275 from gerardog/Fix.PwshInstaller
Browse files Browse the repository at this point in the history
PowerShell Module Improvements
  • Loading branch information
gerardog authored Jul 17, 2023
2 parents 530d77b + 5dbf0b6 commit 6662797
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ Alternative syntaxes:

``` Powershell
# Add the following line to your $PROFILE
Import-Module "$env:ProgramFiles\gsudo\Current\gsudoModule.psd1"
Import-Module "gsudoModule"
# Or run:
Get-Command gsudoModule.psd1 | % { Write-Output "`nImport-Module `"$($_.Source)`"" | Add-Content $PROFILE }
Write-Output "`nImport-Module `"gsudoModule`"" | Add-Content $PROFILE
```

- If you haven't already customized your PowerShell prompt (for example by installing Oh-My-Posh), you can easily add a red `#` indicating that the current process is elevated:
Expand Down
15 changes: 15 additions & 0 deletions build/01-build.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
pushd $PSScriptRoot\..

if ($env:version) {
"- Getting version from env:version"
$version = $env:version
$version_MajorMinorPatch=$env:version_MajorMinorPatch
} else {
"- Getting version using GitVersion"
$env:version = $version = $(gitversion /showvariable LegacySemVer)
$env:version_MajorMinorPatch = $version_MajorMinorPatch=$(gitversion /showvariable MajorMinorPatch)
}
"- Using version number v$version / v$version_MajorMinorPatch"

"-- Cleaning bin & obj folders"
Get-Item ".\src\gsudo\bin\", ".\src\gsudo\obj\" -ErrorAction Ignore | Remove-Item -Recurse -Force
"-- Building net7.0 win-arm64"
Expand All @@ -24,4 +36,7 @@ cp .\src\gsudo.Wrappers\* .\artifacts\x64
cp .\src\gsudo.Wrappers\* .\artifacts\arm64
cp .\src\gsudo.Wrappers\* .\artifacts\net46-AnyCpu

# Set Module version number.
Get-ChildItem .\artifacts\ -Filter gsudoModule.psd1 -Recurse | % { (Get-Content $_) -replace """0.1""", """$version_MajorMinorPatch""" | Set-Content $_.FullName }

popd
13 changes: 9 additions & 4 deletions build/Chocolatey/gsudo/tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Install-ChocolateyPath -PathToInstall $SymLinkDir -PathType 'Machine'

cmd /c mklink "$TargetDir\sudo.exe" "$TargetDir\gsudo.exe" 2>$null

# Copy gsudoModule to "$env:ProgramFiles\PowerShell\Modules\gsudoModule"
$PSModulesTargetDir = "$env:ProgramFiles\PowerShell\Modules\gsudoModule"
md $PSModulesTargetDir -ErrorAction SilentlyContinue
copy "$bin\*.ps*" $TargetDir -Exclude *.ignore -Force

$OldCurrentDir = Get-Item $SymLinkDir -ErrorAction ignore
if ($OldCurrentDir)
{
Expand All @@ -43,13 +48,13 @@ cmd /c mklink /d "$SymLinkDir" "$TargetDir\"
Write-Output "gsudo successfully installed. Please restart your consoles to use gsudo.`n"

if (Get-Module gsudoModule) {
"Please restart PowerShell to update PowerShell gsudo Module."
"Please restart all your PowerShell consoles to update PowerShell gsudo Module."
} else {
& {
"PowerShell users: To use enhanced gsudo and Invoke-Gsudo cmdlet, add the following line to your `$PROFILE"
" Import-Module '$SymLinkDir\gsudoModule.psd1'"
"PowerShell users: Add auto-complete to gsudo by adding the following line to your `$PROFILE"
" Import-Module 'gsudoModule'"
"Or run: "
" Write-Output `"``nImport-Module '$SymLinkDir\gsudoModule.psd1'`" | Add-Content `$PROFILE"
" Write-Output `"``nImport-Module 'gsudoModule'`" | Add-Content `$PROFILE"

}
}
4 changes: 2 additions & 2 deletions docs/docs/usage/powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ gsudo -d dir C:\
Add the following line to your $PROFILE (replace with full path)

``` powershell
Import-Module 'C:\FullPathTo\gsudoModule.psd1'
Import-Module 'gsudoModule.psd1'
# Or let the following line do it for you run:
Get-Command gsudoModule.psd1 | % { Write-Output "`nImport-Module `"$($_.Source)`"" | Add-Content $PROFILE }
Write-Output "`nImport-Module `"gsudoModule`"" | Add-Content $PROFILE
```

:::tip
Expand Down
7 changes: 4 additions & 3 deletions installgsudo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ if ($process.ExitCode -ne 0)
}
else
{
New-Item -Type Directory ($PROFILE | Split-Path) -ErrorAction SilentlyContinue

Write-Output "gsudo installed succesfully!"
Write-Output "Please restart your consoles to use gsudo!`n"

"PowerShell users: To use enhanced gsudo and Invoke-Gsudo cmdlet, add the following line to your `$PROFILE"
" Import-Module '${Env:ProgramFiles}\gsudo\Current\gsudoModule.psd1'"
" Import-Module 'gsudoModule'"
"Or run: "
" New-Item -Type Directory (`$PROFILE | Split-Path) -ErrorAction Ignore"
" Write-Output `"``nImport-Module '${Env:ProgramFiles}\gsudo\Current\gsudoModule.psd1'`" | Add-Content `$PROFILE"
" Write-Output `"``nImport-Module 'gsudoModule'`" | Add-Content `$PROFILE"

Remove-Item $fileName
}
Expand Down
27 changes: 14 additions & 13 deletions src/gsudo.Installer/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<Directory Id="INSTALLFOLDER" Name="gsudo">
<Directory Id="VERSIONFOLDER" Name="$(env.version)"/>
</Directory>
<Directory Id="POWERSHELLFOLDER" Name="PowerShell">
<Directory Id="MODULESDIR" Name="Modules" >
<Directory Id="GSUDOMODULEDIR" Name="gsudoModule" />
</Directory>
</Directory>
</StandardDirectory>
</Fragment>

Expand All @@ -60,10 +65,8 @@
<?error Platform $(sys.BUILDARCH) is not supported?>
<?endif?>

<ComponentRef Id="GSudoBash" />
<ComponentRef Id="GSudoPowerShell" />
<ComponentRef Id="GSudoExtensions" />
<ComponentRef Id="GSudoModule" />
<ComponentRef Id="InvokeGSudo" />
</ComponentGroup>
</Fragment>

Expand Down Expand Up @@ -94,20 +97,17 @@
<Environment Id="SET_ENV" Action="set" Name="PATH" Part="last" Permanent="no" System="yes" Value="[INSTALLFOLDER]Current" />
</Component>

<Component Id="GSudoBash" Directory="VERSIONFOLDER">
<Component Id="GSudoExtensions" Directory="VERSIONFOLDER" Guid="923f225a-75cd-4fca-ad48-a4161187f7a5">
<File Id="GSudoBash" Name="gsudo" Source="..\..\artifacts\x64\gsudo" />
</Component>

<Component Id="GSudoPowerShell" Directory="VERSIONFOLDER">
<File Id="GSudoModuleDef" Name="gsudoModule.psd1" Source="..\..\artifacts\x64\gsudoModule.psd1" />
</Component>

<Component Id="GSudoModule" Directory="VERSIONFOLDER">
<File Id="GSudoModule" Name="gsudoModule.psm1" Source="..\..\artifacts\x64\gsudoModule.psm1" />
<File Id="InvokeGSudo" Name="invoke-gsudo.ps1" Source="..\..\artifacts\x64\invoke-gsudo.ps1" />
</Component>

<Component Id="InvokeGSudo" Directory="VERSIONFOLDER">
<File Id="InvokeGSudo" Name="invoke-gsudo.ps1" Source="..\..\artifacts\x64\invoke-gsudo.ps1" />
<Component Id="GSudoModule" Directory="GSUDOMODULEDIR" Guid="923f225a-75cd-4fca-ad48-a4161187f7a6">
<File Id="GSudoModulePsd1" Name="gsudoModule.psd1" Source="..\..\artifacts\x64\gsudoModule.psd1" />
<File Id="GSudoModulePsm1" Name="gsudoModule.psm1" Source="..\..\artifacts\x64\gsudoModule.psm1" />
<File Id="GSudoModuleFn" Name="invoke-gsudo.ps1" Source="..\..\artifacts\x64\invoke-gsudo.ps1" />
</Component>
</Fragment>

Expand All @@ -119,4 +119,5 @@
<CustomAction Id="RemoveInstallFolder" Directory="INSTALLFOLDER" ExeCommand="cmd /c RD /S /Q &quot;[INSTALLFOLDER]&quot;" Execute="deferred" Return="ignore" Impersonate="no" />
</Fragment>

</Wix>
</Wix>

3 changes: 1 addition & 2 deletions src/gsudo/Commands/BangBangCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public Task<int> Execute()
{
if (ShellHelper.InvokingShell.In (Shell.PowerShell, Shell.PowerShellCore, Shell.PowerShellCore623BuggedGlobalInstall))
{
var module = Path.Combine(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), "gsudoModule.psd1");
throw new ApplicationException($"To use `gsudo !!` from powershell, run or add the following line to your `$PROFILE:\n\n Import-Module '{ module }'");
throw new ApplicationException($"To use `gsudo !!` from PowerShell, run or add the following line to your PowerShell $PROFILE:\n\n Import-Module 'gsudoModule'");
}

var caller = Process.GetCurrentProcess().GetShellProcess().MainModule.ModuleName;
Expand Down

0 comments on commit 6662797

Please sign in to comment.