Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add one-liner for installing cargo-binstall on Windows #1099

Merged
merged 3 commits into from
Jun 3, 2023
Merged

Add one-liner for installing cargo-binstall on Windows #1099

merged 3 commits into from
Jun 3, 2023

Conversation

supleed2
Copy link
Contributor

I saw the one-liner added for Unix systems (in #1073 / #1074) and thought it should be possible for Windows :)

The script should have the same behaviour as the Bash version, and the oneliner temporarily (scoped to the current terminal session) disables Powershell's Execution Policy.

The url used in the script is for after the PR is merged, use the following if you want to test:

Set-ExecutionPolicy Unrestricted -Scope Process && iex (iwr "https://raw.githubusercontent.com/supleed2/cargo-binstall/main/install-from-binstall-release.ps1").Content

@NobodyXu NobodyXu requested a review from passcod May 29, 2023 03:08
Copy link
Member

@NobodyXu NobodyXu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I have no knowledge of powershell, it seems to me that this script closely mirrors our unix script.

@passcod Can you review this please since you clearly know more about powershell than me?

install-from-binstall-release.ps1 Outdated Show resolved Hide resolved
install-from-binstall-release.ps1 Outdated Show resolved Hide resolved
Copy link
Member

@NobodyXu NobodyXu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, once @passcod approved this we can merge it

@NobodyXu
Copy link
Member

Thanks, I do have an aarch64 windows running in VM on M1 that can test this.
I would give it a go tomorrow

@NobodyXu
Copy link
Member

@supleed2 Executing this on windows powershell give the following error:

PS C:\Users\user> Set-ExecutionPolicy Unrestricted -Scope Process && iex (iwr "https://raw.githubusercontent.com/supleed2/cargo-binstall/main/install-from-binstall-release.ps1").Content
At line:1 char:49
+ Set-ExecutionPolicy Unrestricted -Scope Process && iex (iwr "https:// ...
+                                                 ~~
The token '&&' is not a valid statement separator in this version.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidEndOfLine

`&&` was added in Powershell 7
@supleed2
Copy link
Contributor Author

@NobodyXu Thanks for testing, it seems && and || were only added in Powershell 7, so I've swapped out the one-liner to use ; instead, which should work. Maybe you can try this:

Set-ExecutionPolicy Unrestricted -Scope Process; iex (iwr "https://raw.githubusercontent.com/supleed2/cargo-binstall/main/install-from-binstall-release.ps1").Content

@NobodyXu
Copy link
Member

Seems like it is installed to the wrong location:

PS C:\Users\nobodyxu> ls .cargo\bin


    Directory: C:\Users\nobodyxu\.cargo\bin


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         5/30/2023  12:50 AM        8594944 cargo-clippy.exe
-a----         5/30/2023  12:50 AM        8594944 cargo-fmt.exe
-a----         5/30/2023  12:50 AM        8594944 cargo-miri.exe
-a----         5/30/2023  12:50 AM        8594944 cargo.exe
-a----         5/30/2023  12:50 AM        8594944 clippy-driver.exe
-a----         5/30/2023  12:50 AM        8594944 rls.exe
-a----         5/30/2023  12:50 AM        8594944 rust-analyzer.exe
-a----         5/30/2023  12:50 AM        8594944 rust-gdb.exe
-a----         5/30/2023  12:50 AM        8594944 rust-gdbgui.exe
-a----         5/30/2023  12:50 AM        8594944 rust-lldb.exe
-a----         5/30/2023  12:50 AM        8594944 rustc.exe
-a----         5/30/2023  12:50 AM        8594944 rustdoc.exe
-a----         5/30/2023  12:50 AM        8594944 rustfmt.exe
-a----         5/30/2023  12:50 AM        8594944 rustup.exe

There is no cargo-binstall in $HOME\.cargo\bin.

@supleed2
Copy link
Contributor Author

I'm not sure what happened then, because https://github.com/supleed2/cargo-binstall/blob/f2ed7bfc043fb42740c5ae2b97e76dcb3bb5a3ee/install-from-binstall-release.ps1#L15 simply calls the executable the same way as the bash script. Maybe the elseif fails. Can you run (Get-ComputerInfo).CsSystemType for me?

Invoke-Expression "$tmpdir\cargo-binstall\cargo-binstall.exe -y --force cargo-binstall"

is equivalent to

eval "\tmp\cargo-binstall\cargobinstall -y --force cargo-binstall"

Invoke-Expression is needed because of the variable $tmpdir in the program path.

@NobodyXu
Copy link
Member

It gives me ARM64-based PC:

PS C:\Users\nobodyxu> (Get-ComputerInfo).CsSystemType
ARM64-based PC

@supleed2
Copy link
Contributor Author

Sorry, I just realised 😅 if the check failed, you would have seen the Unsupported Architecture message.
Did you get any other errors?
The powershell script does not control the install location, that is left to the default for cargo-binstall.exe -y --force cargo-binstall.

@NobodyXu
Copy link
Member

Did you get any other errors?

No.

The powershell script does not control the install location, that is left to the default for cargo-binstall.exe -y --force cargo-binstall.

I just realized that I haven't seen any output from cargo-binstall when using the one-liner

@supleed2
Copy link
Contributor Author

I haven't seen any output from cargo-binstall when using the one-liner

But it works if you download & extract the file manually and run .\cargo-binstall.exe -y --force cargo-binstall?

@NobodyXu
Copy link
Member

I will have a try later

@NobodyXu

This comment was marked as resolved.

@NobodyXu

This comment was marked as resolved.

@NobodyXu
Copy link
Member

I forgot to move the file out of the preview.
Once I copy it out into desktop, it can be run without any problem and generates output as expected @supleed2 :

PS > .\cargo-binstall.exe binstall cargo-watch
 INFO resolve: Resolving package: 'cargo-watch'
 WARN The package cargo-watch v8.4.0 will be downloaded from github.com
 INFO This will install the following binaries:
 INFO   - cargo-watch.exe (cargo-watch.exe -> C:\Users\nobodyxu\.cargo\bin\cargo-watch.exe)
Do you wish to continue? yes/[no]
? yes
 INFO Installing binaries...
 INFO Done in 10.688220209s

@NobodyXu
Copy link
Member

I tried this again and this time it works:

PS > Set-ExecutionPolicy Unrestricted -Scope Process; iex (iwr "https://raw.githubusercontent.com/supleed2/cargo-binstall/main/install-from-binstall-release.ps1").Content
DEBUG:    1+  >>>> Set-ExecutionPolicy Unrestricted -Scope Process; iex (iwr
"https://raw.githubusercontent.com/supleed2/cargo-binstall/main/install-from-binstall-release.ps1").Content
DEBUG:    1+ Set-ExecutionPolicy Unrestricted -Scope Process;  >>>> iex (iwr
"https://raw.githubusercontent.com/supleed2/cargo-binstall/main/install-from-binstall-release.ps1").Content
DEBUG:    1+  >>>> $ErrorActionPreference = "Stop"
DEBUG:    2+  >>>> Set-PSDebug -Trace 1
DEBUG:    3+  >>>> $tmpdir = $Env:TEMP
DEBUG:    4+  >>>> $base_url = "https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-"
DEBUG:    5+  >>>> $type = (Get-ComputerInfo).CsSystemType.ToLower()
DEBUG:    6+ if ( >>>> $type.StartsWith("x64")) {
DEBUG:    8+ } elseif ( >>>> $type.StartsWith("arm64")) {
DEBUG:    9+   >>>> $arch = "aarch64"
DEBUG:   14+  >>>> $url = "$base_url$arch-pc-windows-msvc.zip"
DEBUG:   15+  >>>> Invoke-WebRequest $url -OutFile $tmpdir\cargo-binstall.zip
DEBUG:   16+  >>>> Expand-Archive -Force $tmpdir\cargo-binstall.zip $tmpdir\cargo-binstall
DEBUG:  285+      >>>> {
DEBUG:  286+         >>>> $isVerbose = $psboundparameters.ContainsKey("Verbose")
DEBUG:  287+         >>>> $isConfirm = $psboundparameters.ContainsKey("Confirm")
DEBUG:  289+          >>>> $isDestinationPathProvided = $true
DEBUG:  290+         if( >>>> $DestinationPath -eq [string]::Empty)
DEBUG:  297+              >>>> $destinationPathExists = Test-Path -Path $DestinationPath -PathType Container
DEBUG:  298+             if( >>>> $destinationPathExists)
DEBUG:  318+                  >>>> $createdItem = New-Item -Path $DestinationPath -ItemType Directory
-Confirm:$isConfirm -Verbose:$isVerbose -ErrorAction Stop
DEBUG:  319+                 if( >>>> $createdItem -ne $null -and $createdItem.PSProvider.Name -ne "FileSystem")        DEBUG:  326+                  >>>> $resolvedDestinationPath = GetResolvedPathHelper $DestinationPath $true $PSCmdlet                                                                                                                            DEBUG:  424+  >>>> {                                                                                                    DEBUG:  433+      >>>> $resolvedPaths =@()                                                                              DEBUG:  436+     foreach($currentPath in  >>>> $path)                                                                   DEBUG:  436+     foreach( >>>> $currentPath in $path)                                                                   DEBUG:  440+             if( >>>> $isLiteralPath)                                                                       DEBUG:  442+                  >>>> $currentResolvedPaths = Resolve-Path -LiteralPath $currentPath -ErrorAction Stop     DEBUG:  457+         foreach($currentResolvedPath in  >>>> $currentResolvedPaths)                                       DEBUG:  457+         foreach( >>>> $currentResolvedPath in $currentResolvedPaths)                                       DEBUG:  459+              >>>> $resolvedPaths += $currentResolvedPath.ProviderPath                                      DEBUG:  457+         foreach( >>>> $currentResolvedPath in $currentResolvedPaths)                                       DEBUG:  436+     foreach( >>>> $currentPath in $path)                                                                   DEBUG:  463+      >>>> $resolvedPaths                                                                                   DEBUG:  464+  >>>> }                                                                                                    DEBUG:  330+          >>>> $isWhatIf = $psboundparameters.ContainsKey("WhatIf")                                         DEBUG:  331+         if( >>>> !$isWhatIf)                                                                               DEBUG:  333+              >>>> $preparingToExpandVerboseMessage = ($LocalizedData.PreparingToExpandVerboseMessage)      DEBUG:  334+              >>>> Write-Verbose $preparingToExpandVerboseMessage                                           DEBUG:  336+              >>>> $progressBarStatus = ($LocalizedData.ExpandProgressBarText -f $DestinationPath)          DEBUG:  337+              >>>> ProgressBarHelper "Expand-Archive" $progressBarStatus 0 100 100 1                        DEBUG: 1113+  >>>> {                                                                                                    DEBUG: 1124+     if( >>>> $currentEntryCount -gt 0 -and                                                                 DEBUG: 1129+          >>>> $entryDefaultWeight = $currentSegmentWeight/[double]$totalNumberofEntries                    DEBUG: 1131+          >>>> $percentComplete = $previousSegmentWeight + ($entryDefaultWeight * $currentEntryCount)       DEBUG: 1132+          >>>> Write-Progress -Activity $cmdletName -Status $status -PercentComplete $percentComplete       DEBUG: 1134+  >>>> }                                                                                                    DEBUG:  339+      >>>> }                                                                                                DEBUG:  341+      >>>> {                                                                                                DEBUG:  342+         switch( >>>> $PsCmdlet.ParameterSetName)                                                           DEBUG:  344+              >>>> "Path"                                                                                   DEBUG:  346+                  >>>> $resolvedSourcePaths = GetResolvedPathHelper $Path $false $PSCmdlet                  DEBUG:  424+  >>>> {                                                                                                    DEBUG:  433+      >>>> $resolvedPaths =@()                                                                              DEBUG:  436+     foreach($currentPath in  >>>> $path)                                                                   DEBUG:  436+     foreach( >>>> $currentPath in $path)                                                                   DEBUG:  440+             if( >>>> $isLiteralPath)                                                                       DEBUG:  446+                  >>>> $currentResolvedPaths = Resolve-Path -Path $currentPath -ErrorAction Stop            DEBUG:  457+         foreach($currentResolvedPath in  >>>> $currentResolvedPaths)                                       DEBUG:  457+         foreach( >>>> $currentResolvedPath in $currentResolvedPaths)                                       DEBUG:  459+              >>>> $resolvedPaths += $currentResolvedPath.ProviderPath                                      DEBUG:  457+         foreach( >>>> $currentResolvedPath in $currentResolvedPaths)                                       DEBUG:  436+     foreach( >>>> $currentPath in $path)                                                                   DEBUG:  463+      >>>> $resolvedPaths                                                                                   DEBUG:  464+  >>>> }                                                                                                    DEBUG:  348+                 if( >>>> $resolvedSourcePaths.Count -gt 1)                                                 DEBUG:  354+              >>>> "LiteralPath"                                                                            DEBUG:  342+         switch( >>>> $PsCmdlet.ParameterSetName)                                                           DEBUG:  366+          >>>> ValidateArchivePathHelper $resolvedSourcePaths                                               DEBUG:  884+  >>>> {                                                                                                    DEBUG:  890+     if( >>>> [System.IO.File]::Exists($archiveFile))                                                       DEBUG:  892+          >>>> $extension = [system.IO.Path]::GetExtension($archiveFile)                                    DEBUG:  895+         if( >>>> $extension -ne $zipFileExtension)                                                         DEBUG:  906+  >>>> }                                                                                                    DEBUG:  368+         if( >>>> $pscmdlet.ShouldProcess($resolvedSourcePaths))                                            DEBUG:  370+              >>>> $expandedItems = @()                                                                     DEBUG:  379+                  >>>> $isArchiveFileProcessingComplete = $false                                            DEBUG:  385+                 if( >>>> !$isDestinationPathProvided)                                                      DEBUG:  397+                  >>>> ExpandArchiveHelper $resolvedSourcePaths $resolvedDestinationPath                    ([ref]$expandedItems) $Force $isVerbose $isConfirm                                                                      DEBUG:  913+  >>>> {                                                                                                    DEBUG:  924+      >>>> Add-CompressionAssemblies                                                                        DEBUG:  466+ function Add-CompressionAssemblies  >>>> {                                                                 DEBUG:  468+     if ( >>>> $PSEdition -eq "Desktop")                                                                    DEBUG:  470+          >>>> Add-Type -AssemblyName System.IO.Compression                                                 DEBUG:  471+          >>>> Add-Type -AssemblyName System.IO.Compression.FileSystem                                      DEBUG:  473+  >>>> }                                                                                                    DEBUG:  930+          >>>> $archiveFileStreamArgs = @($archiveFile, [System.IO.FileMode]::Open,                         [System.IO.FileAccess]::Read)                                                                                           DEBUG:  931+          >>>> $archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList                 $archiveFileStreamArgs                                                                                                  DEBUG:  933+          >>>> $zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read,        $false)                                                                                                                 DEBUG:  934+          >>>> $zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList            $zipArchiveArgs                                                                                                         DEBUG:  936+         if( >>>> $zipArchive.Entries.Count -eq 0)                                                          DEBUG:  943+          >>>> $currentEntryCount = 0                                                                       DEBUG:  944+          >>>> $progressBarStatus = ($LocalizedData.ExpandProgressBarText -f $archiveFile)                  DEBUG:  949+         if ( >>>> -not $expandedDir.EndsWith([System.IO.Path]::DirectorySeparatorChar))                    DEBUG:  951+           >>>> $expandedDir += [System.IO.Path]::DirectorySeparatorChar                                    DEBUG:  955+         foreach($currentArchiveEntry in  >>>> $zipArchive.Entries)                                         DEBUG:  955+         foreach( >>>> $currentArchiveEntry in $zipArchive.Entries)                                         DEBUG:  957+              >>>> $currentArchiveEntryPath = Join-Path -Path $expandedDir -ChildPath                       $currentArchiveEntry.FullName                                                                                           DEBUG:  960+              >>>> $currentArchiveEntryPath =                                                               $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($currentArchiveEntryPath)                               DEBUG:  963+             if ( >>>> -not ($currentArchiveEntryPath.StartsWith($expandedDir,                              [System.StringComparison]::Ordinal)))                                                                                   DEBUG:  972+              >>>> $extension = [system.IO.Path]::GetExtension($currentArchiveEntryPath)                    DEBUG:  976+             if( >>>> $extension -eq [string]::Empty -and                                                   DEBUG: 1001+                      >>>> $currentArchiveEntryFileInfo = New-Object -TypeName System.IO.FileInfo           -ArgumentList $currentArchiveEntryPath                                                                                  DEBUG: 1002+                      >>>> $parentDirExists = Test-Path -LiteralPath                                        $currentArchiveEntryFileInfo.DirectoryName -PathType Container                                                          DEBUG: 1005+                     if( >>>> $parentDirExists -eq $false)                                                  DEBUG: 1021+                      >>>> $hasNonTerminatingError = $false                                                 DEBUG: 1025+                     if( >>>> $currentArchiveEntryFileInfo.Exists)                                          DEBUG: 1054+                     if( >>>> !$hasNonTerminatingError)                                                     DEBUG: 1056+                          >>>>                                                                              [System.IO.Compression.ZipFileExtensions]::ExtractToFile($currentArchiveEntry, $currentArchiveEntryPath, $false)        DEBUG: 1062+                          >>>> $expandedItems.Value += $currentArchiveEntryPath                             DEBUG: 1064+                          >>>> $addFiletoExpandedPathMessage = ($LocalizedData.CreateFileAtExpandedPath -f
$currentArchiveEntryPath)
DEBUG: 1065+                          >>>> Write-Verbose $addFiletoExpandedPathMessage
DEBUG: 1070+                     If( >>>> $null -ne $destStream)
DEBUG: 1075+                     If( >>>> $null -ne $srcStream)
DEBUG: 1082+              >>>> $currentEntryCount += 1
DEBUG: 1085+              >>>> $previousSegmentWeight = 0
DEBUG: 1086+              >>>> $currentSegmentWeight = 100
DEBUG: 1087+              >>>> ProgressBarHelper "Expand-Archive" $progressBarStatus $previousSegmentWeight
$currentSegmentWeight $zipArchive.Entries.Count  $currentEntryCount
DEBUG: 1113+  >>>> {
DEBUG: 1124+     if( >>>> $currentEntryCount -gt 0 -and
DEBUG: 1129+          >>>> $entryDefaultWeight = $currentSegmentWeight/[double]$totalNumberofEntries
DEBUG: 1131+          >>>> $percentComplete = $previousSegmentWeight + ($entryDefaultWeight * $currentEntryCount)
DEBUG: 1132+          >>>> Write-Progress -Activity $cmdletName -Status $status -PercentComplete $percentComplete
DEBUG: 1134+  >>>> }
DEBUG:  955+         foreach( >>>> $currentArchiveEntry in $zipArchive.Entries)
DEBUG: 1092+         If( >>>> $null -ne $zipArchive)
DEBUG: 1094+              >>>> $zipArchive.Dispose()
DEBUG: 1097+         If( >>>> $null -ne $archiveFileStream)
DEBUG: 1099+              >>>> $archiveFileStream.Dispose()
DEBUG: 1103+          >>>> Write-Progress -Activity "Expand-Archive" -Completed
DEBUG: 1105+  >>>> }
DEBUG:  399+                  >>>> $isArchiveFileProcessingComplete = $true
DEBUG:  405+                 if( >>>> $isArchiveFileProcessingComplete -eq $false)
DEBUG:  416+      >>>> }
DEBUG:   17+  >>>> Write-Host ""

DEBUG:   18+  >>>> Invoke-Expression "$tmpdir\cargo-binstall\cargo-binstall.exe -y --force cargo-binstall"
DEBUG:    1+  >>>> C:\Users\nobodyxu\AppData\Local\Temp\cargo-binstall\cargo-binstall.exe -y --force cargo-binstall
 INFO resolve: Resolving package: 'cargo-binstall'
 WARN The package cargo-binstall v0.23.1 will be downloaded from github.com
 INFO This will install the following binaries:
 INFO   - cargo-binstall.exe (cargo-binstall.exe -> C:\Users\nobodyxu\.cargo\bin\cargo-binstall.exe)
 INFO Installing binaries...
 INFO Done in 2.924972875s
DEBUG:   19+  >>>> Remove-Item -Force $tmpdir\cargo-binstall.zip
DEBUG:   20+  >>>> Remove-Item -Recurse -Force $tmpdir\cargo-binstall
DEBUG:   21+ if ( >>>> $Env:Path -split ";" -notcontains "$HOME\.cargo\bin") {
PS >

@NobodyXu
Copy link
Member

@passcod Can you review this please and run this on x64 windows, if you have time?
Thanks!

@NobodyXu
Copy link
Member

NobodyXu commented Jun 3, 2023

@passcod Pinging just in case you forgot this.

@passcod
Copy link
Member

passcod commented Jun 3, 2023

Don't have time at the moment sorry.

@NobodyXu
Copy link
Member

NobodyXu commented Jun 3, 2023

Don't have time at the moment sorry.

Thanks nonetheless, @passcod I think we can merge it as-is for now, and fix it if there is any issue reported later?
Given that it can run on my aarch64 windows VM, it likely can also run on x64 windows.

@NobodyXu NobodyXu added this pull request to the merge queue Jun 3, 2023
Merged via the queue into cargo-bins:main with commit a849db3 Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants