Skip to content

Commit

Permalink
fix: Hung PowerShell script (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMichaelis authored Mar 27, 2024
1 parent ff1a0a4 commit 0956625
Showing 1 changed file with 36 additions and 13 deletions.
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

0 comments on commit 0956625

Please sign in to comment.