diff --git a/src/Chapter10/Listing10.08.MakingTypesAvailableExternally.ps1 b/src/Chapter10/Listing10.08.MakingTypesAvailableExternally.ps1 index 5aa17e19..1dea9d63 100644 --- a/src/Chapter10/Listing10.08.MakingTypesAvailableExternally.ps1 +++ b/src/Chapter10/Listing10.08.MakingTypesAvailableExternally.ps1 @@ -13,12 +13,14 @@ if('traceLevel' -notin $PSBoundParameters.Keys) { "@ } -[string]$LibraryProjectName = 'GeoCoordinates.testing' -[string]$ConsoleProgramProjectName = 'GeoCoordinateProgram.testing' +[string]$LibraryProjectName = 'GeoCoordinates' +[string]$ConsoleProgramProjectName = 'GeoCoordinate.testing' + +# Path to the file containing the TargetFrameworks element +[string]$TargetFrameworksPropsFile = "$PSScriptRoot/../../Common.props" [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')"} @@ -38,19 +40,33 @@ try { } # Specifying langVersion as dotnet new appears to ignore the Directory.Build.props file. - dotnet new Console --langVersion $langVersion --output "$ConsoleProgramProjectName" + dotnet new console --langVersion $langVersion --output "$ConsoleProgramProjectName" $projectFilePath = "$PSScriptRoot/$ConsoleProgramProjectName/$($ConsoleProgramProjectName).csproj" $updatedContent =(Get-Content $projectFilePath) | ? { $_ -notlike '*TargetFramework*'} Set-Content -Value $updatedContent -Path $projectFilePath - dotnet new ClassLib --langVersion $langVersion --output "$LibraryProjectName" + # Add reference to Common.props + [xml]$projectFileContent = Get-Content -Path $projectFilePath + $importElement = $projectFileContent.CreateElement("Import", $projectFileContent.DocumentElement.NamespaceURI) + $importElement.SetAttribute("Project", $TargetFrameworksPropsFile) + $projectFileContent.Project.InsertAfter($importElement, $projectFileContent.Project.FirstChild) + $projectFileContent.Save($projectFilePath) + + dotnet new classlib --langVersion $langVersion --output "$LibraryProjectName" $projectFilePath = "$PSScriptRoot/$LibraryProjectName/$($LibraryProjectName).csproj" $updatedContent =(Get-Content $projectFilePath) | ? { $_ -notlike '*TargetFramework*'} Set-Content -Value $updatedContent -Path $projectFilePath Remove-Item "$PSScriptRoot/$LibraryProjectName/class1.cs" - + + # Add reference to Common.props + [xml]$projectFileContent = Get-Content -Path $projectFilePath + $importElement = $projectFileContent.CreateElement("Import", $projectFileContent.DocumentElement.NamespaceURI) + $importElement.SetAttribute("Project", $TargetFrameworksPropsFile) + $projectFileContent.Project.InsertAfter($importElement, $projectFileContent.Project.FirstChild) + $projectFileContent.Save($projectFilePath) + #New-Item -ItemType Directory "$PSScriptRoot/$LibraryProjectName" - $codeListing = @('namespace GeoCoordinates') + ( + $codeListing = @('namespace GeoCoordinates;') + ( Get-Content $SutCSFile | Select-Object -Skip 1) $codeListing > "$PSScriptRoot/$LibraryProjectName/GeoTypes.cs" @@ -58,8 +74,7 @@ try { dotnet add "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" ` reference "$PSScriptRoot/$LibraryProjectName/$LibraryProjectName.csproj" $codeListing = @" -namespace $ConsoleProgramProjectName -{ +namespace $ConsoleProgramProjectName; using $LibraryProjectName; class HelloWorld { @@ -72,13 +87,21 @@ namespace $ConsoleProgramProjectName ); } } -} "@ $codeListing > "$PSScriptRoot/$ConsoleProgramProjectName/Program.cs" Get-Content "$PSScriptRoot/$ConsoleProgramProjectName/Program.cs" # Display the listing - - dotnet build "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" - dotnet run --project "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" --no-build + +# Load the file containing the TargetFrameworks element as an XML document +[xml]$propsFile = Get-Content -Path $TargetFrameworksPropsFile + +# Extract the TargetFrameworks element value +$targetFrameworks = $propsFile.Project.PropertyGroup.TargetFrameworks -split ';' + +# Iterate over the target frameworks and execute commands for each +foreach ($framework in $targetFrameworks) { + dotnet build "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" --framework $framework + dotnet run --project "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" --no-build --framework $framework +} } finally {