Skip to content

Commit e73a4c7

Browse files
authored
Implement Support for One-liner Installs (#1957)
* Download Zip File as a Fallback in Install-IronPython.ps1 * Add Default Path to Install-IronPython.ps1 * Add iex DownloadString Invocation Example to Install-IronPython.ps1 * Add Install-IronPython.ps1 to README.md * Fix Destination Path Install-IronPython.ps1 * Add PowerShell Usage Examples to README.md * Catch API Errors in Install-IronPython.ps1 * Use `Scriptblock.Create` Instead of `iex` in PowerShell Example in README.md * Reverse $Path to Have no Default Value in Install-IronPython.ps1 Also fixes example text * Fix Search Paths in README PowerShell Example * Fix the One-Liner in the README * Improve Verbage in PowerShell Example in README.md * Make Download Path in Install-IronPython.ps1 Cross-Platform * Fix Example in Install-IronPython.ps1 * Use Different Install Path in README.md PowerShell Example * Improve PowerShell Example in README.md by Adding Pip, SQLite, and WPF * Use Different Example Install Path in Install-IronPython.ps1 Usage Text * Fix Search Paths in README.md PowerShell Example bumping to 3.4.2 will occur in a later commit * Fix and Improve Search Path Resolution in README.md PowerShell Example * Bump 3.4.0 to 3.4.2 in README.md * Add Link for PowerShell Usage in README.md * Improve Readability of One-Liner in README.md * Bump 3.4.0 to 3.4.2 in Install-IronPython.ps1 Also places web install into its own example block
1 parent 00fdda4 commit e73a4c7

File tree

2 files changed

+103
-12
lines changed

2 files changed

+103
-12
lines changed

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ IronPython 3 targets Python 3, including the re-organized standard library, Unic
1212
| **What?** | **Where?** |
1313
| --------: | :------------: |
1414
| **Windows/Linux/macOS Builds** | [![Build status](https://dotnet.visualstudio.com/IronLanguages/_apis/build/status/ironpython3)](https://dotnet.visualstudio.com/IronLanguages/_build/latest?definitionId=43) [![Github build status](https://github.com/IronLanguages/ironpython3/workflows/CI/badge.svg)](https://github.com/IronLanguages/ironpython3/actions?workflow=CI) |
15-
| **Downloads** | [![NuGet](https://img.shields.io/nuget/vpre/IronPython.svg)](https://www.nuget.org/packages/IronPython/3.4.0) [![Release](https://img.shields.io/github/release/IronLanguages/ironpython3.svg?include_prereleases)](https://github.com/IronLanguages/ironpython3/releases/latest)|
15+
| **Downloads** | [![NuGet](https://img.shields.io/nuget/vpre/IronPython.svg)](https://www.nuget.org/packages/IronPython/3.4.2) [![Release](https://img.shields.io/github/release/IronLanguages/ironpython3.svg?include_prereleases)](https://github.com/IronLanguages/ironpython3/releases/latest)|
1616
| **Help** | [![Gitter chat](https://badges.gitter.im/IronLanguages/ironpython.svg)](https://gitter.im/IronLanguages/ironpython) [![StackExchange](https://img.shields.io/badge/stack%20overflow-ironpython-informational?logo=stack-overflow&logoColor=white)](https://stackoverflow.com/questions/tagged/ironpython) |
1717

1818

1919
## Examples
2020

21+
To see how to use in PowerShell (either directly embedded or executable invocation), skip to [Installation](#Installation)
22+
2123
The following C# program:
2224

2325
```cs
@@ -87,7 +89,62 @@ See the [Package compatibility](https://github.com/IronLanguages/ironpython3/wik
8789

8890
## Installation
8991

90-
Binaries of IronPython 3 can be downloaded from the [release page](https://github.com/IronLanguages/ironpython3/releases/latest), available in various formats: `.msi`, `.zip`, `.deb`, `.pkg`. The IronPython package is also available on [NuGet](https://www.nuget.org/packages/IronPython/3.4.0). See the [installation document](https://github.com/IronLanguages/ironpython3/wiki/Installing) for detailed instructions on how to install a standalone IronPython interpreter on various operating systems and .NET frameworks.
92+
Binaries of IronPython 3 can be downloaded from the [release page](https://github.com/IronLanguages/ironpython3/releases/latest), available in various formats: `.msi`, `.zip`, `.deb`, `.pkg`. The IronPython package is also available on [NuGet](https://www.nuget.org/packages/IronPython/3.4.2). See the [installation document](https://github.com/IronLanguages/ironpython3/wiki/Installing) for detailed instructions on how to install a standalone IronPython interpreter on various operating systems and .NET frameworks.
93+
94+
### PowerShell
95+
96+
For usage in PowerShell, you can install using the Install-IronPython.ps1 within the aforementioned `.zip` file or by simply using this one-liner:
97+
98+
```pwsh
99+
& ([scriptblock]::Create((iwr `
100+
-Uri 'https://raw.githubusercontent.com/IronLanguages/ironpython3/main/eng/scripts/Install-IronPython.ps1').Content)) `
101+
-Path "~/ipyenv/v3.4.2"
102+
103+
# Optionally, ensure pip:
104+
# & "~/ipyenv/v3.4.2/ipy" -m ensurepip
105+
```
106+
107+
Once installed, you can start using IronPython directly in PowerShell!
108+
109+
To use the ipy shim, you can use:
110+
```pwsh
111+
& "~/ipyenv/v3.4.2/Enter-IronPythonEnvironment.ps1"
112+
113+
ipy -c "print('Hello from IronPython!')"
114+
```
115+
116+
... or to use IronPython embedded in PowerShell, you can use:
117+
```pwsh
118+
Import-Module "~/ipyenv/v3.4.2/IronPython.dll"
119+
120+
$engine = & {
121+
$engine = [IronPython.Hosting.Python]::CreateEngine()
122+
123+
# You need to add the correct paths, as IronPython will use PowerShell's installation path by default
124+
$paths = $engine.GetSearchPaths()
125+
$paths.Add("$(Resolve-Path "~/ipyenv/v3.4.2/lib")")
126+
$paths.Add("$(Resolve-Path "~/ipyenv/v3.4.2/lib/site-packages")")
127+
128+
# To use `wpf` and `sqlite3` you have to add the DLLs search path
129+
# - the [IronPython.SQLite] and [IronPython.WPF] powershell namespaces will become available on python import
130+
$paths.Add("$(Resolve-Path "~/ipyenv/v3.4.2/DLLs")")
131+
132+
# or if you prefer to have the powershell namespaces early, you can use:
133+
# - just note, you will have to initialize _sqlite3 (see further down the script)
134+
# Import-Module "~/ipyenv/v3.4.2/DLLs/IronPython.SQLite.dll"
135+
# Import-Module "~/ipyenv/v3.4.2/DLLs/IronPython.WPF.dll"
136+
137+
$engine.SetSearchPaths($paths)
138+
139+
# Then have fun!
140+
$engine.Execute("print('Hello from IronPython!')")
141+
142+
# Optionally, if you need to initialize _sqlite3:
143+
# $engine.Execute("import sqlite3")
144+
145+
return $engine
146+
}
147+
```
91148

92149
## Build
93150

eng/scripts/Install-IronPython.ps1

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,26 @@
1212
1313
.EXAMPLE
1414
15-
PS>Invoke-WebRequest -Uri https://github.com/IronLanguages/ironpython3/releases/download/v3.4.0/IronPython.3.4.0.zip -OutFile IronPython.3.4.0.zip
16-
PS>Expand-Archive -Path IronPython.3.4.0.zip -DestinationPath IronPython-unzipped
17-
PS>./IronPython-unzipped/scripts/Install-IronPython -Path ~/ipyenv/v3.4.0
15+
With a one-liner, install latest over the web:
1816
19-
The official binaries are downloaded from GitHub to the current directory, unzipped, and then the installation proceeds using the script from the unzipped directory. IronPython is installed into ~/ipyenv/v3.4.0.
17+
PS>& ([scriptblock]::Create((iwr 'https://raw.githubusercontent.com/IronLanguages/ironpython3/main/eng/scripts/Install-IronPython.ps1').Content)) -Path ~/ipyenv/v3.4.2
18+
19+
The official binaries are downloaded from GitHub to the current directory, unzipped, and then the installation proceeds using the script from the unzipped directory. IronPython is installed into ~/ipyenv/v3.4.2
20+
21+
.EXAMPLE
22+
23+
PS>Invoke-WebRequest -Uri https://github.com/IronLanguages/ironpython3/releases/download/v3.4.2/IronPython.3.4.2.zip -OutFile IronPython.3.4.2.zip
24+
PS>Expand-Archive -Path IronPython.3.4.2.zip -DestinationPath IronPython-unzipped
25+
PS>./IronPython-unzipped/scripts/Install-IronPython -Path ~/ipyenv/v3.4.2
26+
27+
The official binaries are downloaded from GitHub to the current directory, unzipped, and then the installation proceeds using the script from the unzipped directory. IronPython is installed into ~/ipyenv/v3.4.2
2028
2129
.EXAMPLE
2230
23-
PS>Invoke-WebRequest -Uri https://github.com/IronLanguages/ironpython3/releases/download/v3.4.0/IronPython.3.4.0.zip -OutFile IronPython.3.4.0.zip
24-
PS>Install-IronPython -Path ~/ipyenv/v3.4.0 -ZipFile IronPython.3.4.0.zip -Framework net462 -Force
31+
PS>Invoke-WebRequest -Uri https://github.com/IronLanguages/ironpython3/releases/download/v3.4.2/IronPython.3.4.2.zip -OutFile IronPython.3.4.2.zip
32+
PS>Install-IronPython -Path ~/ipyenv/v3.4.2 -ZipFile IronPython.3.4.2.zip -Framework net462 -Force
2533
26-
The official binaries are downloaded from GitHub to the current directory and then the installation proceeds using the downloaded zip file. IronPython is installed into ~/ipyenv/v3.4.0, overwriting any previous installation in that location. IronPython binaries running on .NET Framework 4.6.2 are used during the installation.
34+
The official binaries are downloaded from GitHub to the current directory and then the installation proceeds using the downloaded zip file. IronPython is installed into ~/ipyenv/v3.4.2, overwriting any previous installation in that location. IronPython binaries running on .NET Framework 4.6.2 are used during the installation.
2735
This example assumes that the installation script is in a directory on the search path ($env:PATH).
2836
2937
.EXAMPLE
@@ -54,6 +62,8 @@ Param(
5462

5563
$ErrorActionPreference = "Stop"
5664

65+
$downloaded = $false
66+
5767
if (-not $ZipFile) {
5868
# If zipfile path not given, try to locate it
5969
$splitPSScriptRoot = $PSScriptRoot -split "\$([IO.Path]::DirectorySeparatorChar)"
@@ -72,7 +82,23 @@ if (-not $ZipFile) {
7282
}
7383
$ZipFile = $zipFiles
7484
} else {
75-
Write-Error "Cannot locate implicit zip file. Provide path to the zip file using '-ZipFile <path>'."
85+
Try {
86+
$zipUrl = (Invoke-RestMethod "https://api.github.com/repos/IronLanguages/ironpython3/releases/latest").assets |
87+
Where-Object -Property name -Like "IronPython.3.*.zip" |
88+
Select-Object -ExpandProperty browser_download_url
89+
90+
$downloadPath = "$([System.IO.Path]::GetTempPath())$([guid]::NewGuid()).zip"
91+
Invoke-WebRequest -Uri $zipUrl -OutFile $downloadPath | Out-Null
92+
93+
if (-not (Test-Path -PathType Leaf $downloadPath)) {
94+
throw
95+
}
96+
97+
$downloaded = $true
98+
$ZipFile = $downloadPath
99+
} Catch {
100+
Write-Error "Cannot retrieve zip file implicitly. Check your network connection or provide a path to the zip file using '-ZipFile <path>'."
101+
}
76102
}
77103
} elseif (-not (Test-Path $ZipFile)) {
78104
Write-Error "ZipFile not found: $ZipFile"
@@ -85,16 +111,24 @@ if (Test-Path $Path) {
85111
Write-Error "Overwriting of multiple destinations not allowed: $Path"
86112
}
87113
Remove-Item -Path $Path -Force -Recurse
114+
New-Item $Path -ItemType Directory | Out-Null
88115
} else {
89-
Write-Error "Path already exists: $Path"
116+
Write-Warning "Path already exists: $(Resolve-Path $Path)"
90117
}
118+
} else {
119+
New-Item $Path -ItemType Directory | Out-Null
91120
}
92-
New-Item $Path -ItemType Directory | Out-Null
93121

94122
# Unzip archive
95123
if (-not $unzipDir) {
96124
$unzipDir = Join-Path $Path "unzipped"
97125
Expand-Archive -Path $ZipFile -DestinationPath $unzipDir
126+
127+
# Cleanup temporary files
128+
if ($downloaded) {
129+
Remove-Item -Path $ZipFile
130+
}
131+
98132
$unzipped = $true
99133
}
100134

0 commit comments

Comments
 (0)