Skip to content

Commit

Permalink
Update PSReadLine build to target netstandard2.0 (#4584)
Browse files Browse the repository at this point in the history
By retargeting PSReadLine to `netstandard2.0`, we can simplify the build a lot and produce the same assembly no matter building on Windows or non-Windows.

- Update PSReadLine build to target `netstandard2.0`, including the CI and release pipeline YAML files.
- Update the test to target `net472` and `net6.0`, so that we can run tests with both .NET Framework and .NET
- Rename `Microsoft.PowerShell.PSReadLine2.dll` to `Microsoft.PowerShell.PSReadLine.dll`
- Update and clean up the `README.md` with the up-to-date information
  • Loading branch information
daxian-dbw authored Feb 28, 2025
1 parent a272810 commit 49604e8
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 228 deletions.
18 changes: 1 addition & 17 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,7 @@ Additional references:

### Bootstrap, Build and Test

To build `PSReadLine` on Windows, Linux, or macOS,
you must have the following installed:

* .NET Core SDK 2.1.802 or [a newer version](https://www.microsoft.com/net/download)
* The PowerShell modules `InvokeBuild` and `platyPS`

The build script `build.ps1` can be used to bootstrap, build and test the project.

* Bootstrap: `./build.ps1 -Bootstrap`
* Build:
* Targeting .NET 4.6.2 (Windows only): `./build.ps1 -Configuration Debug -Framework net462`
* Targeting .NET Core: `./build.ps1 -Configuration Debug -Framework net6.0`
* Test:
* Targeting .NET 4.6.2 (Windows only): `./build.ps1 -Test -Configuration Debug -Framework net462`
* Targeting .NET Core: `./build.ps1 -Test -Configuration Debug -Framework net6.0`

After build, the produced artifacts can be found at `<your-local-repo-root>/bin/Debug`.
See the [Building](../README.md#building) section in README for details.

### Submitting Pull Request

Expand Down
4 changes: 2 additions & 2 deletions .pipelines/PSReadLine-Official.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ extends:
- pwsh: |
Write-Host "PS Version: $($($PSVersionTable.PSVersion))"
Set-Location -Path '$(repoRoot)'
.\build.ps1 -Configuration Release -Framework net462
.\build.ps1 -Configuration Release
displayName: Build
env:
# Set ob_restore_phase to run this step before '🔒 Setup Signing' step.
Expand Down Expand Up @@ -166,7 +166,7 @@ extends:
TargetFolder: $(ob_outputDirectory)

- pwsh: |
$versionInfo = Get-Item "$(signSrcPath)\Microsoft.PowerShell.PSReadLine2.dll" | ForEach-Object VersionInfo
$versionInfo = Get-Item "$(signSrcPath)\Microsoft.PowerShell.PSReadLine.dll" | ForEach-Object VersionInfo
$moduleVersion = $versionInfo.ProductVersion.Split('+')[0]
$vstsCommandString = "vso[task.setvariable variable=ob_sdl_sbom_packageversion]${moduleVersion}"
Expand Down
9 changes: 3 additions & 6 deletions MockPSConsole/MockPSConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
<OutputType>Exe</OutputType>
<RootNamespace>MockPSConsole</RootNamespace>
<AssemblyName>MockPSConsole</AssemblyName>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<FileAlignment>512</FileAlignment>
<ApplicationManifest>Program.manifest</ApplicationManifest>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<PackageReference Include="System.Xml.XDocument" version="4.3.0" />
<PackageReference Include="System.Data.DataSetExtensions" version="4.5.0" />
<PackageReference Include="Microsoft.CSharp" version="4.7.0" />
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<PackageReference Include="PowerShellStandard.Library" version="5.1.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.PowerShell.SDK" version="7.2.23" />
<PackageReference Include="Microsoft.PowerShell.SDK" version="7.2.24" />
</ItemGroup>

<ItemGroup>
Expand Down
75 changes: 27 additions & 48 deletions PSReadLine.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ param(
[ValidateSet("Debug", "Release")]
[string]$Configuration = (property Configuration Release),

[ValidateSet("net462", "net6.0")]
[string]$Framework,
[ValidateSet("net472", "net6.0")]
[string]$TestFramework,

[switch]$CheckHelpContent
)
Expand All @@ -30,71 +30,58 @@ Import-Module "$PSScriptRoot/tools/helper.psm1"
# Final bits to release go here
$targetDir = "bin/$Configuration/PSReadLine"

if (-not $Framework)
{
$Framework = if ($PSVersionTable.PSEdition -eq "Core") { "net6.0" } else { "net462" }
if (-not $TestFramework) {
$TestFramework = $IsWindows ? "net472" : "net6.0"
}

Write-Verbose "Building for '$Framework'" -Verbose

function ConvertTo-CRLF([string] $text) {
$text.Replace("`r`n","`n").Replace("`n","`r`n")
}

$polyFillerParams = @{
Inputs = { Get-ChildItem Polyfill/*.cs, Polyfill/Polyfill.csproj }
Outputs = "Polyfill/bin/$Configuration/$Framework/Microsoft.PowerShell.PSReadLine.Polyfiller.dll"
Outputs = "Polyfill/bin/$Configuration/netstandard2.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll"
}

$binaryModuleParams = @{
Inputs = { Get-ChildItem PSReadLine/*.cs, PSReadLine/PSReadLine.csproj, PSReadLine/PSReadLineResources.resx, Polyfill/*.cs, Polyfill/Polyfill.csproj }
Outputs = "PSReadLine/bin/$Configuration/$Framework/Microsoft.PowerShell.PSReadLine2.dll"
Outputs = "PSReadLine/bin/$Configuration/netstandard2.0/Microsoft.PowerShell.PSReadLine.dll"
}

$xUnitTestParams = @{
Inputs = { Get-ChildItem test/*.cs, test/*.json, test/PSReadLine.Tests.csproj }
Outputs = "test/bin/$Configuration/$Framework/PSReadLine.Tests.dll"
}

$mockPSConsoleParams = @{
Inputs = { Get-ChildItem MockPSConsole/*.cs, MockPSConsole/Program.manifest, MockPSConsole/MockPSConsole.csproj }
Outputs = "MockPSConsole/bin/$Configuration/$Framework/MockPSConsole.dll"
Outputs = "test/bin/$Configuration/$TestFramework/PSReadLine.Tests.dll"
}

<#
Synopsis: Build the Polyfiller assembly
#>
task BuildPolyfiller @polyFillerParams -If ($Framework -eq "net462") {
## Build both "net462" and "net6.0"
exec { dotnet publish -f "net462" -c $Configuration Polyfill }
exec { dotnet publish -f "net6.0" -c $Configuration Polyfill }
task BuildPolyfiller @polyFillerParams {
exec { dotnet publish -c $Configuration -f 'netstandard2.0' Polyfill }
exec { dotnet publish -c $Configuration -f 'net6.0' Polyfill }
}

<#
Synopsis: Build main binary module
#>
task BuildMainModule @binaryModuleParams {
exec { dotnet publish -f $Framework -c $Configuration PSReadLine }
exec { dotnet publish -c $Configuration PSReadLine\PSReadLine.csproj }
}

<#
Synopsis: Build xUnit tests
#>
task BuildXUnitTests @xUnitTestParams {
exec { dotnet publish -f $Framework -c $Configuration test }
}

<#
Synopsis: Build the mock powershell console.
#>
task BuildMockPSConsole @mockPSConsoleParams {
exec { dotnet publish -f $Framework -c $Configuration MockPSConsole }
exec { dotnet publish -f $TestFramework -c $Configuration test }
}

<#
Synopsis: Run the unit tests
#>
task RunTests BuildMainModule, BuildXUnitTests, { Start-TestRun -Configuration $Configuration -Framework $Framework }
task RunTests BuildMainModule, BuildXUnitTests, {
Write-Verbose "Run tests targeting '$TestFramework' ..."
Start-TestRun -Configuration $Configuration -Framework $TestFramework
}

<#
Synopsis: Check if the help content is in sync.
Expand Down Expand Up @@ -128,35 +115,27 @@ task LayoutModule BuildPolyfiller, BuildMainModule, {
Set-Content -Path (Join-Path $targetDir (Split-Path $file -Leaf)) -Value (ConvertTo-CRLF $content) -Force
}

if ($Framework -eq "net462") {
if (-not (Test-Path "$targetDir/net462")) {
New-Item "$targetDir/net462" -ItemType Directory -Force > $null
}
if (-not (Test-Path "$targetDir/net6plus")) {
New-Item "$targetDir/net6plus" -ItemType Directory -Force > $null
}

Copy-Item "Polyfill/bin/$Configuration/net462/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/net462" -Force
Copy-Item "Polyfill/bin/$Configuration/net6.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/net6plus" -Force
if (-not (Test-Path "$targetDir/netstd")) {
New-Item "$targetDir/netstd" -ItemType Directory -Force > $null
}
if (-not (Test-Path "$targetDir/net6plus")) {
New-Item "$targetDir/net6plus" -ItemType Directory -Force > $null
}

$binPath = "PSReadLine/bin/$Configuration/$Framework/publish"
Copy-Item $binPath/Microsoft.PowerShell.PSReadLine2.dll $targetDir
Copy-Item "Polyfill/bin/$Configuration/netstandard2.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/netstd" -Force
Copy-Item "Polyfill/bin/$Configuration/net6.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/net6plus" -Force

$binPath = "PSReadLine/bin/$Configuration/netstandard2.0/publish"
Copy-Item $binPath/Microsoft.PowerShell.PSReadLine.dll $targetDir
Copy-Item $binPath/Microsoft.PowerShell.Pager.dll $targetDir

if ($Configuration -eq 'Debug') {
Copy-Item $binPath/*.pdb $targetDir
}

if (Test-Path $binPath/System.Runtime.InteropServices.RuntimeInformation.dll) {
Copy-Item $binPath/System.Runtime.InteropServices.RuntimeInformation.dll $targetDir
} else {
Write-Warning "Build using $Framework is not sufficient to be downlevel compatible"
}

# Copy module manifest, but fix the version to match what we've specified in the binary module.
$moduleManifestContent = ConvertTo-CRLF (Get-Content -Path 'PSReadLine/PSReadLine.psd1' -Raw)
$versionInfo = (Get-ChildItem -Path $targetDir/Microsoft.PowerShell.PSReadLine2.dll).VersionInfo
$versionInfo = (Get-ChildItem -Path $targetDir/Microsoft.PowerShell.PSReadLine.dll).VersionInfo
$version = $versionInfo.FileVersion
$semVer = $versionInfo.ProductVersion

Expand Down
2 changes: 1 addition & 1 deletion PSReadLine/OnImportAndRemove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
}

string root = Path.GetDirectoryName(typeof(OnModuleImportAndRemove).Assembly.Location);
string subd = (Environment.Version.Major >= 6) ? "net6plus" : "net462";
string subd = (Environment.Version.Major >= 6) ? "net6plus" : "netstd";
string path = Path.Combine(root, subd, "Microsoft.PowerShell.PSReadLine.Polyfiller.dll");

return Assembly.LoadFrom(path);
Expand Down
17 changes: 5 additions & 12 deletions PSReadLine/PSReadLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,23 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.PowerShell.PSReadLine</RootNamespace>
<AssemblyName>Microsoft.PowerShell.PSReadLine2</AssemblyName>
<AssemblyName>Microsoft.PowerShell.PSReadLine</AssemblyName>
<NoWarn>$(NoWarn);CA1416</NoWarn>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0</FileVersion>
<InformationalVersion>2.4.0-beta0</InformationalVersion>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" version="5.1.0" />
<PackageReference Include="Microsoft.CSharp" version="4.7.0" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" />
<ProjectReference Include="..\Polyfill\Polyfill.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Management.Automation" Version="7.2.23" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.Pager" version="1.0.0" />
<ProjectReference Include="..\Polyfill\Polyfill.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion PSReadLine/PSReadLine.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
RootModule = 'PSReadLine.psm1'
NestedModules = @("Microsoft.PowerShell.PSReadLine2.dll")
NestedModules = @("Microsoft.PowerShell.PSReadLine.dll")
ModuleVersion = '2.4.0'
GUID = '5714753b-2afd-4492-a5fd-01d9e2cff8b5'
Author = 'Microsoft Corporation'
Expand Down
8 changes: 4 additions & 4 deletions Polyfill/Polyfill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
<PropertyGroup>
<AssemblyName>Microsoft.PowerShell.PSReadLine.Polyfiller</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Management.Automation" Version="7.2.23" />
<PackageReference Include="System.Management.Automation" Version="7.2.24" />
</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net462'">
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>$(DefineConstants);LEGACY</DefineConstants>
</PropertyGroup>

Expand Down
Loading

0 comments on commit 49604e8

Please sign in to comment.