From 83ef7bdfb8b97c310070294aa33544a45ee83622 Mon Sep 17 00:00:00 2001 From: Konstantin Sharon Date: Sun, 21 Jul 2024 09:47:35 +0300 Subject: [PATCH 1/2] Use .NET 8.0 --- .github/workflows/ci.yml | 20 ++++++------ README.md | 50 +++++++++++++++--------------- build/00-prerequisites.ps1 | 4 +-- build/01-build.ps1 | 26 ++++++++-------- build/02-test.ps1 | 14 ++++----- global.json | 2 +- src/gsudo.Tests/gsudo.Tests.csproj | 4 +-- src/gsudo/gsudo.csproj | 18 +++++------ 8 files changed, 69 insertions(+), 69 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1c7c1bb2..6eb810ae 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,15 +28,15 @@ jobs: permissions: id-token: write contents: read - checks: write + checks: write steps: - uses: actions/setup-dotnet@v2 with: - dotnet-version: '7.0.x' - include-prerelease: true + dotnet-version: '8.0.x' + include-prerelease: true - uses: actions/checkout@v2 - with: - fetch-depth: 0 + with: + fetch-depth: 0 - name: Run Tests id: tests run: ./build/02-test.ps1 @@ -45,7 +45,7 @@ jobs: if: success() || failure() with: name: TestsResults (dotnet) - path: "**/TestResults*.trx" + path: "**/TestResults*.trx" reporter: dotnet-trx fail-on-error: true - name: Test Report PowerShell v5 @@ -70,10 +70,10 @@ jobs: steps: - uses: actions/setup-dotnet@v2 with: - dotnet-version: '7.0.x' - include-prerelease: true - - uses: actions/checkout@v2 - with: + dotnet-version: '8.0.x' + include-prerelease: true + - uses: actions/checkout@v2 + with: fetch-depth: 0 - name: Install dependencies run: choco install GitVersion.Portable --version 5.6.11 --confirm --no-progress; choco install il-repack --confirm --no-progress diff --git a/README.md b/README.md index 03e9072e..ae8d795b 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,12 @@ **gsudo** is a `sudo` equivalent for Windows, with a similar user-experience as the original Unix/Linux sudo. Allows you to run a command (or re-launch your current shell) with elevated permissions, in the current console window or a new one. - + Just [prepend `gsudo`](#usage) (or the `sudo` alias) to your command and it will run elevated. For PowerShell use this [syntax](#usage-from-powershell--powershell-core): `gsudo { ScriptBlock }` One UAC popup will appear each time. You can see less popups if you enable [gsudo cache](#credentials-cache). -It detects your current shell and elevates your command as native shell commands. (Supports `Cmd`, `PowerShell`, `WSL`, `git-bash`, `MinGW`, `Cygwin`, `Yori`, `Take Command`, `BusyBox` & `NuShell`.) +It detects your current shell and elevates your command as native shell commands. (Supports `Cmd`, `PowerShell`, `WSL`, `git-bash`, `MinGW`, `Cygwin`, `Yori`, `Take Command`, `BusyBox` & `NuShell`.) ## Table of contents @@ -52,7 +52,7 @@ It detects your current shell and elevates your command as native shell commands - Supports being used on scripts: - Outputs StdOut/StdErr can be piped or captured (e.g. `gsudo dir | findstr /c:"bytes free" > FreeSpace.txt`) and exit codes too (`%errorlevel%`). If `gsudo` fails to elevate, the exit code will be 999. - If `gsudo` is invoked from an already elevated console, it will just run the command (it won't fail). So, you don't have to worry if you run `gsudo` or a script that uses `gsudo` from an already elevated console. (The UAC popup will not appear, as no elevation is required) - + - `gsudo !!` elevates the last executed command. Works on CMD, Git-Bash, MinGW, Cygwin (and PowerShell with [gsudo module](#gsudomodule) only) ## Installation @@ -64,11 +64,11 @@ It detects your current shell and elevates your command as native shell commands ``` PowerShell PowerShell -Command "Set-ExecutionPolicy RemoteSigned -scope Process; [Net.ServicePointManager]::SecurityProtocol = 'Tls12'; iwr -useb https://raw.githubusercontent.com/gerardog/gsudo/master/installgsudo.ps1 | iex" ``` -- Portable: +- Portable: 1. Download `gsudo.portable.zip` from the [latest release](https://github.com/gerardog/gsudo/releases/latest) 2. Extract the folder corresponding to your processor architecture (x64, x86, or arm64) from the zip file to a suitable directory on your computer. 3. Optionally, add that directory to your system's PATH environment variable if it's not already accessible. - + **Please restart all your console windows** after installing to ensure that the `PATH` environment variable is refreshed. Note: `gsudo.exe` is portable. No windows service is required or system change is done, except adding it to the Path. @@ -149,10 +149,10 @@ To elevate a commands or script block: **Wrap it in {curly braces}**. # Pass arguments with -args $MyString = "Hello World" - gsudo { Write-Output $args[0] } -args $MyString + gsudo { Write-Output $args[0] } -args $MyString # Output is serialized as PSObjects with properties. - $services = gsudo { Get-Service 'WSearch', 'Winmgmt'} + $services = gsudo { Get-Service 'WSearch', 'Winmgmt'} Write-Output $services.DisplayName # Inputs too: Example elevated iteration of a list. @@ -167,18 +167,18 @@ Alternative syntaxes: # Pass values (not variables by reference) by prefixing `$using:`. I.E. $MyString = "Hello World" - Invoke-Gsudo { Write-Output $using:MyString } + Invoke-Gsudo { Write-Output $using:MyString } # Syntax: - Invoke-Gsudo [-ScriptBlock] - [[-ArgumentList] ] - [-InputObject ] - [-LoadProfile | -NoProfile] + Invoke-Gsudo [-ScriptBlock] + [[-ArgumentList] ] + [-InputObject ] + [-LoadProfile | -NoProfile] [-Credential ] ``` - PowerShell function. - - Performs auto serialization of inputs & outputs. + - Performs auto serialization of inputs & outputs. - You can prefix variables with the `$using:` scope modifier (like `$using:variableName`) and their serialized value is applied. - Use `-LoadProfile` or `-NoProfile` to override profile loading or not. - Use `-Credential` option for Run As User (same as `-u` but for `Get-Credentials`). @@ -187,16 +187,16 @@ Alternative syntaxes:
3. Manual string interpolation. (not recommended) - I don't recommend this approach as it is really hard to do proper escape all special characters. + I don't recommend this approach as it is really hard to do proper escape all special characters. ``` PowerShell Usage: gsudo 'string literal' # Variable substitutions example: - $file='C:\My Secret.txt'; + $file='C:\My Secret.txt'; $algorithm='md5'; $hash = gsudo "(Get-FileHash '$file' -Algorithm $algorithm).Hash" - # or + # or $hash = gsudo "(Get-FileHash ""$file"" -Algorithm $algorithm).Hash" ``` @@ -209,13 +209,13 @@ Alternative syntaxes: - Optional: Import module `gsudoModule.psd1` into your PowerShell Profile: - Adds syntax auto-complete to gsudo in PowerShell. Plus, it suggests the 3 previous commands, making your workflow smoother than ever! - Enables `gsudo !!` on Powershell, to elevate the last executed command. - - Adds Functions: + - Adds Functions: - `Test-IsGsudoCacheAvailable` Returns true if a gsudo cache is active (meaning elevation with no UAC is possible). - `Test-IsProcessElevated`: Returns true if the current process is elevated. - `Test-IsAdminMember`: Returns true if the current user is member of the `Local Administrators` groups, which means it can elevate with. - + ``` Powershell - # Add the following line to your $PROFILE + # Add the following line to your $PROFILE Import-Module "gsudoModule" # Or run: @@ -224,12 +224,12 @@ Alternative syntaxes: - 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: ![gsudo prompt](docs/static/img/gsudo-powershell-prompt.gif) - + To do so add this line to your profile (after importing `gsudoModule`): - + ``` powershell Set-Alias Prompt gsudoPrompt - ``` + ``` --- ### Usage from WSL (Windows Subsystem for Linux) @@ -240,7 +240,7 @@ On WSL bash, prepend `gsudo` to elevate **WSL commands** or `gsudo -d` for **CMD ``` bash # elevate default shell -PC:~$ gsudo +PC:~$ gsudo # run elevated WSL command PC:~$ gsudo mkdir /mnt/c/Windows/MyFolder @@ -304,9 +304,9 @@ In any case, you can stop all cache sessions with `gsudo -k`. When I created `gsudo`, there were other `sudo` packages on most Windows popular package managers such as `Chocolatey` and `Scoop`, so I had no other choice to pick another name. `gsudo` installers create an alias for `sudo`, so feel free to use `sudo` on your command line to invoke `gsudo`. -- Why did you migrated from `.Net Framework 4.6` to `.Net Core 7.0`? +- Why did you migrated from `.Net Framework 4.6` to `.Net 8.0`? - Starting from v1.4.0, it is built using `.Net 7.0` NativeAOT. It loads faster and uses less memory, and runs on machines without any .Net runtime installed. Prior versions ` ilmerge -> artifacts\net46-AnyCpu\" -} +ilrepack .\artifacts\net46-AnyCpu\unmerged\gsudo.exe .\artifacts\net46-AnyCpu\unmerged\*.dll /out:.\artifacts\net46-AnyCpu\gsudo.exe /target:exe /targetplatform:v4 /ndebug /wildcards || $(exit $LASTEXITCODE) + +if ($?) { + rm artifacts\net46-AnyCpu\unmerged -Recurse + echo "artifacts\net46-AnyCpu\unmerged -> ilmerge -> artifacts\net46-AnyCpu\" +} cp .\src\gsudo.Wrappers\* .\artifacts\x86 cp .\src\gsudo.Wrappers\* .\artifacts\x64 diff --git a/build/02-test.ps1 b/build/02-test.ps1 index 2847dd77..8e62d4eb 100644 --- a/build/02-test.ps1 +++ b/build/02-test.ps1 @@ -14,7 +14,7 @@ dotnet build .\src\gsudo.sln || $(exit $LASTEXITCODE) $originalPath = $env:path -$env:path=(Get-Item .\src\gsudo.Tests\bin\Debug\net7.0\).FullName+";" + [String]::Join(";", (($ENV:Path).Split(";") -notlike "*gsudo*" | % {$_ -replace "\\$" })) +$env:path=(Get-Item .\src\gsudo.Tests\bin\Debug\net8.0\).FullName+";" + [String]::Join(";", (($ENV:Path).Split(";") -notlike "*gsudo*" | % {$_ -replace "\\$" })) gsudo -k gsudo --debug cache on -p 0 -d 1 @@ -25,19 +25,19 @@ if (! $?) { $failure = $true } if ($failure) { exit 1 } # fail fast $script = { - $ProgressPreference = "SilentlyContinue"; + $ProgressPreference = "SilentlyContinue"; if ((Get-InstalledModule Pester -ErrorAction SilentlyContinue).Version -lt "5.0.0") { Install-Module Pester -Force -SkipPublisherCheck } - Import-Module Pester - + Import-Module Pester + $configuration = New-PesterConfiguration; $configuration.Run.Path = "src" $configuration.TestResult.Enabled = $true $configuration.TestResult.OutputPath = "TestResults_PS$($PSVersionTable.PSVersion.Major).xml" $configuration.TestResult.OutputFormat = "JUnitXml" # $configuration.Should.ErrorAction = 'Continue' -# $configuration.CodeCoverage.Enabled = $true - - Invoke-Pester -Configuration $configuration +# $configuration.CodeCoverage.Enabled = $true + + Invoke-Pester -Configuration $configuration } gsudo --debug cache on -p 0 -d 1 diff --git a/global.json b/global.json index f0858b83..f60c66db 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.0", + "version": "8.0.0", "rollForward": "latestFeature" } } \ No newline at end of file diff --git a/src/gsudo.Tests/gsudo.Tests.csproj b/src/gsudo.Tests/gsudo.Tests.csproj index f1eed44b..382c0f96 100644 --- a/src/gsudo.Tests/gsudo.Tests.csproj +++ b/src/gsudo.Tests/gsudo.Tests.csproj @@ -1,11 +1,11 @@  - net7.0 + net8.0 false Debug;Release - + diff --git a/src/gsudo/gsudo.csproj b/src/gsudo/gsudo.csproj index a8c08df3..423e1f33 100644 --- a/src/gsudo/gsudo.csproj +++ b/src/gsudo/gsudo.csproj @@ -1,7 +1,7 @@  Exe - net7.0;net46 + net8.0;net46 false StrongName.snk Gerardo Grignoli @@ -25,27 +25,27 @@ 1.0.0.0 1701;1702;CA1303;CA1707;CA1028;CA1001;CA1031;CA1416 - + False true - true + true Size DEBUG pdbonly - true + true - - + + - - + + @@ -57,7 +57,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + From 2a3eba7d80a1eb283c3d943d5bdb4fc370f7cd2d Mon Sep 17 00:00:00 2001 From: Konstantin Sharon Date: Sun, 21 Jul 2024 13:52:49 +0300 Subject: [PATCH 2/2] Update GH actions --- .github/workflows/ci.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6eb810ae..b2e2001b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,10 +30,9 @@ jobs: contents: read checks: write steps: - - uses: actions/setup-dotnet@v2 + - uses: actions/setup-dotnet@v4 with: dotnet-version: '8.0.x' - include-prerelease: true - uses: actions/checkout@v2 with: fetch-depth: 0 @@ -68,15 +67,14 @@ jobs: name: Build runs-on: windows-latest steps: - - uses: actions/setup-dotnet@v2 + - uses: actions/setup-dotnet@v4 with: dotnet-version: '8.0.x' - include-prerelease: true - uses: actions/checkout@v2 with: fetch-depth: 0 - name: Install dependencies - run: choco install GitVersion.Portable --version 5.6.11 --confirm --no-progress; choco install il-repack --confirm --no-progress + run: choco install GitVersion.Portable --version 5.12.0 --confirm --no-progress; choco install il-repack --confirm --no-progress - name: Update project version run: gitversion /l console /output buildserver /updateAssemblyInfo /verbosity minimal - name: Get project version