Updated to support multiple frameworks pulled from Directory.Build.props
MODIFY
src/Chapter10/Listing10.13.MakingTypesAvailableExternally.ps1
src/Chapter10/Listing10.13.MakingTypesAvailableExternally.ps1
param(
[int]$traceLevel
)
-if('traceLevel' -notin $PSBoundParameters.Keys) {
- $traceLevel = Read-Host -Prompt @"
- Specifiy the trace level:
- - 0: Turn script tracing off.
- - 1: Trace script lines as they run.
- - 2: Trace script lines, variable assignments, function calls, and scripts.
-"@
-}
+[string]$LibraryProjectName = 'GeoCoordinates'
+[string]$ConsoleProgramProjectName = 'GeoCoordinateProgram'
+[xml]$directoryBuildPropsXml = ([xml](Get-Content "$PSScriptRoot/../../Directory.Build.props"))
+[string]$langVersion = $directoryBuildPropsXml.Project.PropertyGroup.LangVersion
+[string[]]$frameworks = $directoryBuildPropsXml.Project.PropertyGroup.TargetFrameworks -split ';'
+[string]$SutCSFile = split-path -leaf $MyInvocation.MyCommand.Definition
+[string]$SutCSFile = "$PSScriptRoot/$([IO.Path]::GetFileNameWithoutExtension($SutCSFile)).cs"
+if(-not (Test-Path $SutCSFile)) { throw "Unable to find the file with the type to export ('$SutCSFile')"}
-$LibraryProjectName = 'GeoCoordinates'
-$ConsoleProgramProjectName = 'GeoCoordinateProgram'
+function RunProject {
+ param(
+ $Framework
+ )
try {
- Get-Item "$PSScriptRoot\$LibraryProjectName","$PSScriptRoot\$ConsoleProgramProjectName" -ErrorAction Ignore | Remove-Item -Recurse
+ Get-Item "$PSScriptRoot/$LibraryProjectName","$PSScriptRoot/$ConsoleProgramProjectName" -ErrorAction Ignore | Remove-Item -Recurse
Set-PSDebug -Trace $traceLevel
- dotnet new Console --output "$ConsoleProgramProjectName"
- dotnet new ClassLib --langVersion '8.0' --output "$LibraryProjectName"
- Remove-Item "$PSScriptRoot\$LibraryProjectName\class1.cs"
- $SutCSFile = split-path -leaf $MyInvocation.MyCommand.Definition
- $SutCSFile = "$PSScriptRoot\$([IO.Path]::GetFileNameWithoutExtension($SutCSFile)).cs"
- if(-not (Test-Path $SutCSFile)) { throw "Unable to fine the file with the type to export ('$SutCSFile')"}
- #New-Item -ItemType Directory "$PSScriptRoot\$LibraryProjectName"
+
+ if([string]::IsNullOrEmpty($langVersion))
+ {
+ throw "LangVersion not set."
+ }
+ dotnet new Console --langVersion $langVersion --output "$ConsoleProgramProjectName" --framework $Framework
+ dotnet new ClassLib --langVersion $langVersion --output "$LibraryProjectName" --framework $Framework
+ Remove-Item "$PSScriptRoot/$LibraryProjectName/class1.cs"
+
+ #New-Item -ItemType Directory "$PSScriptRoot/$LibraryProjectName"
$codeListing = @('namespace GeoCoordinates') + (
Get-Content $SutCSFile |
Select-Object -Skip 1)
- $codeListing > "$PSScriptRoot\$LibraryProjectName\GeoTypes.cs"
- Get-Content "$PSScriptRoot\$LibraryProjectName\GeoTypes.cs" # Display the listing
- dotnet add "$PSScriptRoot\$ConsoleProgramProjectName\$ConsoleProgramProjectName.csproj" reference "$PSScriptRoot\$LibraryProjectName\$LibraryProjectName.csproj"
+ $codeListing > "$PSScriptRoot/$LibraryProjectName/GeoTypes.cs"
+ Get-Content "$PSScriptRoot/$LibraryProjectName/GeoTypes.cs" # Display the listing
+ dotnet add "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" reference "$PSScriptRoot/$LibraryProjectName/$LibraryProjectName.csproj"
$codeListing = @"
namespace $ConsoleProgramProjectName
{
@@ -48,12 +53,28 @@ namespace $ConsoleProgramProjectName
}
}
"@
- $codeListing > "$PSScriptRoot\$ConsoleProgramProjectName\Program.cs"
- Get-Content "$PSScriptRoot\$ConsoleProgramProjectName\Program.cs" # Display the listing
+ $codeListing > "$PSScriptRoot/$ConsoleProgramProjectName/Program.cs"
+ Get-Content "$PSScriptRoot/$ConsoleProgramProjectName/Program.cs" # Display the listing
- dotnet run -p "$PSScriptRoot\$ConsoleProgramProjectName\$ConsoleProgramProjectName.csproj"
+ dotnet build "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" -property:TargetFrameworks=$Framework
+ dotnet run --project "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" --no-build --framework $Framework
}
finally {
Set-PSDebug -Off
}
+}
+
+if('traceLevel' -notin $PSBoundParameters.Keys) {
+ $traceLevel = Read-Host -Prompt @"
+ Specifiy the trace level:
+ - 0: Turn script tracing off.
+ - 1: Trace script lines as they run.
+ - 2: Trace script lines, variable assignments, function calls, and scripts.
+
+"@
+}
+
+$frameworks | Foreach-Object{
+ RunProject $_
+}

Updated to support multiple frameworks pulled from Directory.Build.props