Skip to content

release 5.1.1 - which adds an assembly for net452 #60

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

Merged
merged 11 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Images/CopyLocal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 36 additions & 3 deletions PowerShellStandard.psm1
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
function Start-Build {
param ( [switch]$CoreOnly )
$versions = 3,5
$srcBase = Join-Path $PsScriptRoot src
foreach ( $version in $versions ) {
try {
$srcDir = Join-Path $srcBase $version
Push-Location $srcDir
dotnet restore
dotnet build --configuration Release
if ( $CoreOnly ) {
dotnet build --configuration Release --framework netstandard2.0
}
else {
dotnet build --configuration Release
}
}
finally {
Pop-Location
Expand Down Expand Up @@ -49,13 +55,40 @@ function Start-Clean {
}

function Invoke-Test {
param ( [switch]$CoreOnly )
# first, run the package tests and validate that the signing.xml file is correct
try {
$testBase = Join-Path $PsScriptRoot test
Push-Location $testBase
Invoke-Pester -Path ./Build.Tests.ps1
}
finally {
Pop-Location
}
$versions = 3,5
foreach ( $version in $versions ) {
try {
$testBase = Join-Path $PsScriptRoot "test/${version}"
Push-Location $testBase
dotnet build --configuration Release
Invoke-Pester
foreach ( $framework in "core","full" ) {
if ( $CoreOnly -and $framework -eq "full" ) {
continue
}
try {
Push-Location $framework
if ( $CoreOnly ) {
dotnet build --configuration Release --framework netstandard2.0
Invoke-Pester
}
else {
dotnet build --configuration Release
Invoke-Pester
}
}
finally {
pop-location
}
}
}
finally {
Pop-Location
Expand Down
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,92 @@ To remove all build artifacts (except for the .nuget files in the root of the re
```powershell
./build.ps1 -Clean
```

## How to use the PowerShell Standard Library

### Via Visual Studio

Using the PowerShell Standard Library within Visual Studio is as simple as installing the package into your solution and building the project

Create a project, and in the Package Manager Console (Tools -> Nuget Package Manager -> Package Manager Console) type:

```powershell
install-package -id PowerShellStandard.Library
```

This will add `System.Management.Automation` to your project references.
After this, you must set `Copy Local` to False in the Reference Properties pane.

![Copy Local = False](Images/CopyLocal.png)

This will keep the PowerShell Standard Library from being copied into your release/publish directories.
This is because the PowerShell Standard Library is a _reference_ assembly and doesn't actually contain any implementations and should never be distributed with your module.
You may now create and build your module in the usual way.

Once your module is built, you can see the portability of your module very easily, if you have both Windows PowerShell and PowerShell core on your system.
The following demonstrates the portability of using PowerShell Standard.

The following shows the same assembly being used by both Windows PowerShell, PowerShell Core, and via Docker; PowerShell Core on Linux.

```powershell
PS> powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Loading personal and system profiles took 714ms.
PS> gci

Directory: C:\users\jimtru\documents\visual studio 2017\Projects\ClassLibrary2\ClassLibrary2\bin\Debug

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/7/2019 10:54 AM 4608 ClassLibrary2.dll
-a---- 3/7/2019 10:54 AM 15872 ClassLibrary2.pdb

PS> import-module .\ClassLibrary2.dll
PS> "joe","jane" | Invoke-Demo
Hello 'joe'
Hello 'jane'
PS> exit

PS> pwsh-preview
PowerShell 6.1.0-rc.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS> import-module .\ClassLibrary2.dll
PS> "joe","jane" | Invoke-Demo
Hello 'joe'
Hello 'jane'
PS> exit

PS> $m = "C:\Users\jimtru\DOCUME~1\VIBB41~1\Projects\CLASSL~2\CLASSL~1\bin\Debug"
PS> docker run --rm -it -v "${m}:/module" mcr.microsoft.com/powershell:preview
PowerShell 6.2.0-rc.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /> hostname
add7d6ed4818

PS /> select-string pretty /etc/os-release
etc/os-release:5:PRETTY_NAME="Ubuntu 18.04.2 LTS"

PS /> gci /module
Directory: /module
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 3/7/19 6:54 PM 4608 ClassLibrary2.dll
------ 3/7/19 6:54 PM 15872 ClassLibrary2.pdbPS /> import-module /module/ClassLibrary2.dll

PS /> Import-Module /module/ClassLibrary2.dll
PS /> "joe","jane" | Invoke-Demo
Hello 'joe'
Hello 'jane'
```

And that's how you can build a single module used by Windows PowerShell, PowerShell Core on Windows and Linux!
22 changes: 17 additions & 5 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env pwsh
param (
[Parameter()][switch]$Clean,
[Parameter()][switch]$Test,
[Parameter()][switch]$Pack
[Parameter(HelpMessage="Remove earlier built versions")][switch]$Clean,
[Parameter(HelpMessage="Run the tests")][switch]$Test,
[Parameter(HelpMessage="Create a .nupkg")][switch]$Pack,
[Parameter(HelpMessage="Build only for netstandard2.0")][switch]$CoreOnly
)

import-module $PSScriptRoot/PowerShellStandard.psm1 -force
Expand All @@ -12,14 +13,25 @@ if ( $Clean ) {
return
}

# It would be great if there were targeting frameworks for net452
# on non-Windows platforms, but for now, if you're linux or macOS
# we'll build only core
if ( $IsLinux -or $IsMacOS ) {
$CoreOnly = $true
}

if ( $Pack ) {
if ( $CoreOnly ) {
Write-Warning "Must build both netstandard2.0 and net452 to build package"
throw "Build on a Windows system with netstandard and net452 target platforms"
}
Export-NuGetPackage
}
else {
Start-Build
Start-Build -CoreOnly:$CoreOnly
}

if ( $Test ) {
Invoke-Test
Invoke-Test -CoreOnly:$CoreOnly
}

4 changes: 2 additions & 2 deletions src/3/System.Management.Automation-lib.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net451</TargetFrameworks>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\signing\visualstudiopublic.snk</AssemblyOriginatorKeyFile>
<AssemblyName>System.Management.Automation</AssemblyName>
Expand All @@ -10,7 +10,7 @@
<DefineConstants>RUNTIME_SERIALIZATION</DefineConstants>
<NuspecFile>./PowershellStandard.Library.nuspec</NuspecFile>
</PropertyGroup>
<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'" >
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="4.5.0" />
<PackageReference Include="System.Security.AccessControl" Version="4.5.0" />
<PackageReference Include="System.Security.Permissions" Version="4.5.0" />
Expand Down
10 changes: 7 additions & 3 deletions src/5/PowerShellStandard.Library.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>PowerShellStandard.Library</id>
<version>5.1.0-RC1</version>
<version>5.1.1</version>
<title>PowerShellStandard.Library</title>
<authors>Microsoft</authors>
<owners>Microsoft,PowerShellTeam</owners>
<projectUrl>https://github.com/PowerShell/PowerShellStandard</projectUrl>
Expand All @@ -11,13 +12,16 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Contains the reference assemblies for PowerShell Standard 5</description>
<copyright>© Microsoft Corporation. All rights reserved.</copyright>
<tags>PowerShell, reference, netstandard2, netstandard2.0</tags>
<tags>PowerShell, reference, net452, netstandard2, netstandard2.0</tags>
<references>
<reference file="System.Management.Automation.dll" />
<reference file="System.Management.Automation.dll" >
<private>False</private>
</reference>
</references>
</metadata>
<files>
<file src="obj/Release/netstandard2.0/System.Management.Automation.dll" target="lib/netstandard2.0" />
<file src="obj/Release/net452/System.Management.Automation.dll" target="lib/net452" />
</files>
</package>

6 changes: 3 additions & 3 deletions src/5/System.Management.Automation-lib.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\signing\visualstudiopublic.snk</AssemblyOriginatorKeyFile>
<AssemblyName>System.Management.Automation</AssemblyName>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>5.1.0</FileVersion>
<FileVersion>5.1.1</FileVersion>
<DelaySign>True</DelaySign>
<DefineConstants>RUNTIME_SERIALIZATION</DefineConstants>
<NuspecFile>./PowershellStandard.Library.nuspec</NuspecFile>
</PropertyGroup>
<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'" >
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="4.5.0" />
<PackageReference Include="System.Security.AccessControl" Version="4.5.0" />
<PackageReference Include="System.Security.Permissions" Version="4.5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Microsoft.PowerShell.Standard.Module.Template</id>
<version>0.1.3</version>
<version>0.1.4</version>
<title>PowerShell Standard module</title>
<authors>Microsoft</authors>
<owners>Microsoft,PowerShellTeam</owners>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0-RC1">
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1">
<PrivateAssets>All</PrivateAssets>
</PackageReference>

Expand Down
6 changes: 3 additions & 3 deletions src/dotnetTemplate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ PowerShell Standard Module (C#)
Author: Microsoft Corporation
Options:
-v|--powershell-standard-version
5.1.0-preview-03 - PowerShell Standard 5.1
5.1.1 - PowerShell Standard 5.1.1
3.0.0-preview-02 - PowerShell Standard 3.0
Default: 5.1.0-preview-03
Default: 5.1.1

--no-restore If specified, skips the automatic restore of the project on create.
bool - Optional
Expand Down Expand Up @@ -132,4 +132,4 @@ This template will create:

* `*.csproj` - a project file that uses the same name as the folder it was created in
* `TestSampleCmdletCommand.cs` - an example PowerShell cmdlet class
* `obj` - a folder generated by the restore
* `obj` - a folder generated by the restore
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/3/PSStandard.csproj → test/3/core/PSStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../src/3/System.Management.Automation-lib.csproj" />
<ProjectReference Include="../../../src/3/System.Management.Automation-lib.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions test/3/full/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Management.Automation;

namespace PSStandard
{
[Cmdlet("get","thing")]
public class Class1 : PSCmdlet
{
[Parameter()]
[Credential()]
public PSCredential Credential { get; set; }

protected override void EndProcessing() {
WriteObject("Success!");
}
}
}
16 changes: 16 additions & 0 deletions test/3/full/PSS3.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Describe "PowerShell Standard 3" {
BeforeAll {
$cmdletAssembly = "bin/Release/net452/Demo.Cmdlet.dll"
$assemblyPath = Join-Path "$PSScriptRoot" $cmdletAssembly
$PSBin = (Get-Process -id $PID).MainModule.FileName
}
It "Can build a reference assembly" {
dotnet restore
dotnet build --configuration Release
$assemblyPath | Should Exist
}
It "Can execute the compiled cmdlet" {
$result = & $PSBin -c "import-module $assemblyPath; Get-Thing"
$result | Should Be "success!"
}
}
12 changes: 12 additions & 0 deletions test/3/full/PSStandard.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<AssemblyName>Demo.Cmdlet</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../../src/3/System.Management.Automation-lib.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/5/PSStandard.csproj → test/5/core/PSStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../src/5/System.Management.Automation-lib.csproj" />
<ProjectReference Include="../../../src/5/System.Management.Automation-lib.csproj" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions test/5/full/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace PSStandard
{
[Cmdlet("get","thing")]
public class Class1 : PSCmdlet
{
[Parameter()]
[Credential()]
public PSCredential Credential { get; set; }

[Parameter()]
[ValidateSet("a","b","c")]
public string p1 { get; set; }

protected override void BeginProcessing() {
WriteVerbose(Runspace.DefaultRunspace.Name);
PSObject p = new PSObject(DateTime.Now);
WriteVerbose(p.Properties["DateTime"].ToString());
}
protected override void EndProcessing() {
WriteObject("Success!");
}
}
}
Loading