Skip to content
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

fix: Hung PowerShell script for Listing 10.08 Test #763

Merged
merged 2 commits into from
Mar 27, 2024
Merged
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
49 changes: 36 additions & 13 deletions src/Chapter10/Listing10.08.MakingTypesAvailableExternally.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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')"}
Expand All @@ -38,28 +40,41 @@ 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"
Get-Content "$PSScriptRoot/$LibraryProjectName/GeoTypes.cs" # Display the listing
dotnet add "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" `
reference "$PSScriptRoot/$LibraryProjectName/$LibraryProjectName.csproj"
$codeListing = @"
namespace $ConsoleProgramProjectName
{
namespace $ConsoleProgramProjectName;
using $LibraryProjectName;
class HelloWorld
{
Expand All @@ -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 {
Expand Down
Loading