Skip to content

Commit ab7f910

Browse files
Fix build stuff (#54)
* Fix up a ton of build tech debt - Use a source generator for resgen - And for polyfills - Make the build script suck a bit less - Use directory build props * Meant to delete some commented code * Had a condition the wrong way
1 parent 0c691d0 commit ab7f910

File tree

9 files changed

+174
-346
lines changed

9 files changed

+174
-346
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Ignore Visual Studio temporary files, build results, and
22
## files generated by popular Visual Studio add-ons.
33

4+
src/ClassExplorer/Generated/
5+
46
# Project specific files
57
tools/dotnet
68
tools/opencover

ClassExplorer.build.ps1

Lines changed: 132 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,96 @@ $testModuleManifestSplat = @{
1818
}
1919

2020
$manifest = Test-ModuleManifest @testModuleManifestSplat
21-
22-
$script:Settings = @{
23-
Name = $moduleName
24-
Manifest = $manifest
25-
Version = $manifest.Version
26-
ShouldTest = $true
27-
}
28-
29-
$script:Folders = @{
30-
PowerShell = "$PSScriptRoot\module"
31-
CSharp = "$PSScriptRoot\src"
32-
Build = '{0}\src\{1}\bin\{2}' -f $PSScriptRoot, $moduleName, $Configuration
33-
Release = '{0}\Release\{1}\{2}' -f $PSScriptRoot, $moduleName, $manifest.Version
34-
Docs = "$PSScriptRoot\docs"
35-
Test = "$PSScriptRoot\test"
36-
Results = "$PSScriptRoot\testresults"
37-
}
38-
39-
$script:Discovery = @{
40-
HasDocs = Test-Path ('{0}\{1}\*.md' -f $Folders.Docs, $PSCulture)
41-
HasTests = Test-Path ('{0}\*.Tests.ps1' -f $Folders.Test)
42-
IsUnix = $PSVersionTable.PSEdition -eq "Core" -and -not $IsWindows
43-
}
21+
$moduleVersion = $manifest.Version
4422

4523
$tools = "$PSScriptRoot\tools"
4624
$script:GetDotNet = Get-Command $tools\GetDotNet.ps1
4725
$script:AssertModule = Get-Command $tools\AssertRequiredModule.ps1
4826
$script:GetOpenCover = Get-Command $tools\GetOpenCover.ps1
4927
$script:GenerateSignatureMarkdown = Get-Command $tools\GenerateSignatureMarkdown.ps1
5028

29+
function RemakeFolder {
30+
[CmdletBinding()]
31+
param(
32+
[ValidateNotNullOrEmpty()]
33+
[string] $LiteralPath
34+
)
35+
end {
36+
$ErrorActionPreference = 'Stop'
37+
if (Test-Path -LiteralPath $LiteralPath) {
38+
Remove-Item -LiteralPath $LiteralPath -Recurse
39+
}
40+
41+
$null = New-Item -ItemType Directory -Path $LiteralPath
42+
}
43+
}
44+
45+
function GetArtifactPath {
46+
[CmdletBinding()]
47+
param(
48+
[ValidateNotNullOrEmpty()]
49+
[string] $FileName,
50+
51+
[switch] $Legacy
52+
)
53+
end {
54+
$moduleName = $script:ModuleName
55+
$config = $script:Configuration
56+
$legacyTarget = $script:LegacyTarget
57+
$modernTarget = $script:ModernTarget
58+
59+
$target = $modernTarget
60+
if ($Legacy) {
61+
$target = $legacyTarget
62+
}
63+
64+
if (-not $FileName) {
65+
return "./artifacts/publish/$moduleName/${config}_${target}"
66+
}
67+
68+
return "./artifacts/publish/$moduleName/${config}_${target}/$FileName"
69+
}
70+
}
71+
72+
task GetProjectInfo {
73+
$script:ModernTarget = $null
74+
$script:LegacyTarget = $null
75+
if (Test-Path -LiteralPath ./Directory.Build.props) {
76+
$content = Get-Content -Raw -LiteralPath ./Directory.Build.props
77+
if ($content -match '<ModernTarget>(?<target>[^<]+)</ModernTarget>') {
78+
$script:ModernTarget = $matches['target']
79+
}
80+
81+
if ($content -match '<LegacyTarget>(?<target>[^<]+)</LegacyTarget>') {
82+
$script:LegacyTarget = $matches['target']
83+
}
84+
}
85+
86+
87+
$script:ModuleName = $ModuleName = 'ClassExplorer'
88+
$testModuleManifestSplat = @{
89+
ErrorAction = 'Ignore'
90+
WarningAction = 'Ignore'
91+
Path = "./module/$ModuleName.psd1"
92+
}
93+
94+
$manifest = Test-ModuleManifest @testModuleManifestSplat
95+
$script:ModuleVersion = $manifest.Version
96+
$script:_IsWindows = $true
97+
$runtimeInfoType = 'System.Runtime.InteropServices.RuntimeInformation' -as [type]
98+
try {
99+
if ($null -ne $runtimeInfoType) {
100+
$script:_IsWindows = $runtimeInfoType::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
101+
}
102+
} catch { }
103+
}
104+
51105
task AssertDotNet {
52-
$script:dotnet = & $GetDotNet -Unix:$Discovery.IsUnix
106+
$script:dotnet = & $GetDotNet -Unix:(-not $script:_IsWindows)
53107
}
54108

55109
task AssertOpenCover -If { $GenerateCodeCoverage.IsPresent } {
56-
if ($Discovery.IsUnix) {
110+
if (-not $script:_IsWindows) {
57111
Write-Warning 'Generating code coverage from .NET core is currently unsupported, disabling code coverage generation.'
58112
$script:GenerateCodeCoverage = $false
59113
return
@@ -63,66 +117,77 @@ task AssertOpenCover -If { $GenerateCodeCoverage.IsPresent } {
63117
}
64118

65119
task AssertRequiredModules {
66-
& $AssertModule Pester 5.3.0 -Force:$Force.IsPresent
67-
& $AssertModule InvokeBuild 5.8.4 -Force:$Force.IsPresent
120+
& $AssertModule Pester 5.7.1 -Force:$Force.IsPresent
121+
& $AssertModule InvokeBuild 5.14.22 -Force:$Force.IsPresent
68122
& $AssertModule platyPS 0.14.2 -Force:$Force.IsPresent
69-
& $AssertModule Yayaml 0.1.1 -Force:$Force.IsPresent
123+
& $AssertModule Yayaml 0.7.0 -Force:$Force.IsPresent
70124
}
71125

72126
task AssertDevDependencies -Jobs AssertDotNet, AssertOpenCover, AssertRequiredModules
73127

74128
task Clean {
75-
if ($PSScriptRoot -and (Test-Path $PSScriptRoot\Release)) {
76-
Remove-Item $PSScriptRoot\Release -Recurse
77-
}
78-
79-
$null = New-Item $Folders.Release -ItemType Directory
80-
if (Test-Path $Folders.Results) {
81-
Remove-Item $Folders.Results -Recurse
82-
}
83-
84-
$null = New-Item $Folders.Results -ItemType Directory
129+
RemakeFolder ./Release
130+
RemakeFolder ./testresults
85131
& $dotnet clean --verbosity quiet -nologo
86132
}
87133

88-
task BuildDocs -If { $Discovery.HasDocs } {
89-
$sourceDocs = "$PSScriptRoot\docs\$PSCulture"
90-
$releaseDocs = '{0}\{1}' -f $Folders.Release, $PSCulture
134+
task BuildDocs -If { Test-Path ./docs/$PSCulture/*.md } {
135+
$releaseDocs = "./Release/ClassExplorer/$moduleVersion"
136+
$null = New-Item $releaseDocs/$PSCulture -ItemType Directory -Force -ErrorAction Ignore
137+
$null = New-ExternalHelp -Path ./docs/$PSCulture -OutputPath $releaseDocs/$PSCulture
91138

92-
$null = New-Item $releaseDocs -ItemType Directory -Force -ErrorAction SilentlyContinue
93-
$null = New-ExternalHelp -Path $sourceDocs -OutputPath $releaseDocs
94-
95-
& $GenerateSignatureMarkdown.Source -AboutHelp $releaseDocs\about_Type_Signatures.help.txt
96-
& $GenerateSignatureMarkdown.Source $PSScriptRoot\docs\en-US\about_Type_Signatures.help.md
139+
& $GenerateSignatureMarkdown.Source -AboutHelp $releaseDocs/about_Type_Signatures.help.txt
140+
& $GenerateSignatureMarkdown.Source ./docs/en-US/about_Type_Signatures.help.md
97141
}
98142

99143
task BuildDll {
100-
if (-not $Discovery.IsUnix) {
101-
& $dotnet publish --configuration $Configuration --framework net471 --verbosity quiet -nologo
144+
if ($script:_IsWindows) {
145+
& $dotnet publish --configuration $Configuration --framework $script:LegacyTarget --verbosity quiet -nologo
102146
}
103-
& $dotnet publish --configuration $Configuration --framework netcoreapp3.1 --verbosity quiet -nologo
147+
148+
& $dotnet publish --configuration $Configuration --framework $script:ModernTarget --verbosity quiet -nologo
104149
}
105150

106151
task CopyToRelease {
107-
$powershellSource = '{0}\*' -f $Folders.PowerShell
108-
$release = $Folders.Release
109-
$releaseDesktopBin = "$release\bin\Desktop"
110-
$releaseCoreBin = "$release\bin\Core"
111-
$sourceDesktopBin = '{0}\net471\publish\*' -f $Folders.Build
112-
$sourceCoreBin = '{0}\netcoreapp3.1\publish\*' -f $Folders.Build
113-
Copy-Item -Path $powershellSource -Destination $release -Recurse -Force
152+
$version = $script:ModuleVersion
153+
$modern = $script:ModernTarget
154+
$legacy = $script:LegacyTarget
114155

115-
if (-not $Discovery.IsUnix) {
116-
$null = New-Item $releaseDesktopBin -Force -ItemType Directory
117-
Copy-Item -Path $sourceDesktopBin -Destination $releaseDesktopBin -Force
156+
$releasePath = "./Release/ClassExplorer/$version"
157+
if (-not (Test-Path -LiteralPath $releasePath)) {
158+
$null = New-Item $releasePath -ItemType Directory
118159
}
119160

120-
$null = New-Item $releaseCoreBin -Force -ItemType Directory
121-
Copy-Item -Path $sourceCoreBin -Destination $releaseCoreBin -Force
161+
Copy-Item -Path ./module/* -Destination $releasePath -Recurse -Force
162+
163+
if ($script:_IsWindows) {
164+
$null = New-Item $releasePath/bin/Legacy -Force -ItemType Directory
165+
$legacyFiles = (
166+
'ClassExplorer.dll',
167+
'ClassExplorer.pdb',
168+
'System.Buffers.dll',
169+
'System.Collections.Immutable.dll',
170+
'System.Memory.dll',
171+
'System.Numerics.Vectors.dll',
172+
'System.Runtime.CompilerServices.Unsafe.dll')
173+
174+
foreach ($file in $legacyFiles) {
175+
Copy-Item -Force -LiteralPath ./artifacts/publish/ClassExplorer/${Configuration}_$legacy/$file -Destination $releasePath/bin/Legacy
176+
}
177+
}
178+
179+
$null = New-Item $releasePath/bin/Modern -Force -ItemType Directory
180+
$modernFiles = (
181+
'ClassExplorer.dll',
182+
'ClassExplorer.pdb',
183+
'ClassExplorer.deps.json')
184+
foreach ($file in $modernFiles) {
185+
Copy-Item -Force -LiteralPath ./artifacts/publish/ClassExplorer/${Configuration}_$modern/$file -Destination $releasePath/bin/Modern
186+
}
122187
}
123188

124-
task DoTest -If { $Discovery.HasTests -and $Settings.ShouldTest } {
125-
if ($Discovery.IsUnix) {
189+
task DoTest -If { Test-Path ./test/*.ps1 } {
190+
if (-not $script:_IsWindows) {
126191
$scriptString = '
127192
$projectPath = "{0}"
128193
Invoke-Pester "$projectPath" -OutputFormat NUnitXml -OutputFile "$projectPath\testresults\pester.xml"
@@ -141,16 +206,16 @@ task DoTest -If { $Discovery.HasTests -and $Settings.ShouldTest } {
141206
$scriptString))
142207

143208
$powershellCommand = 'powershell'
144-
if ($Discovery.IsUnix) {
209+
if ($PSVersionTable.PSVersion.Major -gt 5) {
145210
$powershellCommand = 'pwsh'
146211
}
147212

148-
$powershell = (Get-Command $powershellCommand).Source
213+
$powershell = (Get-Command -CommandType Application $powershellCommand).Source
149214

150215
if ($GenerateCodeCoverage.IsPresent) {
151216
# OpenCover needs full pdb's. I'm very open to suggestions for streamlining this...
152217
# & $dotnet clean
153-
& $dotnet publish --configuration $Configuration --framework net471 --verbosity quiet -nologo /p:DebugType=Full
218+
& $dotnet publish --configuration $Configuration --framework $script:LegacyTarget --verbosity quiet --nologo /p:DebugType=Full
154219

155220
$moduleName = $Settings.Name
156221
$release = '{0}\bin\Desktop\{1}' -f $Folders.Release, $moduleName
@@ -212,7 +277,7 @@ task DoPublish {
212277
Publish-Module -Name $Folders.Release -NuGetApiKey $apiKey -Force:$Force.IsPresent
213278
}
214279

215-
task Build -Jobs AssertDevDependencies, Clean, BuildDll, CopyToRelease, BuildDocs
280+
task Build -Jobs GetProjectInfo, AssertDevDependencies, Clean, BuildDll, CopyToRelease, BuildDocs
216281

217282
task Test -Jobs Build, DoTest
218283

Directory.Build.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project>
2+
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
3+
<PropertyGroup>
4+
5+
<ModernTarget>net8.0</ModernTarget>
6+
<LegacyTarget>net471</LegacyTarget>
7+
<ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath>
8+
9+
</PropertyGroup>
10+
</Project>

module/ClassExplorer.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
if (-not $PSVersionTable.PSEdition -or $PSVersionTable.PSEdition -eq 'Desktop') {
2-
Import-Module "$PSScriptRoot/bin/Desktop/ClassExplorer.dll"
2+
Import-Module "$PSScriptRoot/bin/Legacy/ClassExplorer.dll"
33
} else {
4-
Import-Module "$PSScriptRoot/bin/Core/ClassExplorer.dll"
4+
Import-Module "$PSScriptRoot/bin/Modern/ClassExplorer.dll"
55
}
66

77
if (-not $env:CLASS_EXPLORER_TRUE_CHARACTER) {
Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,52 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net471</TargetFrameworks>
5-
<!-- <TargetFrameworks>net471;netcoreapp3.1</TargetFrameworks> -->
4+
<TargetFrameworks>$(ModernTarget);$(LegacyTarget)</TargetFrameworks>
5+
<!-- <TargetFrameworks>$(LegacyTarget);$(ModernTarget)</TargetFrameworks> -->
66
<LangVersion>preview</LangVersion>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<Nullable>enable</Nullable>
9-
<!-- <AssemblyName>ClassExplorer2</AssemblyName> -->
9+
<ResXGenerator_NullForgivingOperators>true</ResXGenerator_NullForgivingOperators>
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="Aigamo.ResXGenerator" Version="4.2.0">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
<PackageReference Include="PolySharp" Version="1.15.0">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
1322
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" PrivateAssets="all" />
14-
<PackageReference Include="System.Memory" Version="4.5.4" />
1523
</ItemGroup>
1624

17-
<ItemGroup Condition=" '$(TargetFramework)' == 'net471' ">
25+
<ItemGroup Condition=" '$(TargetFramework)' == '$(LegacyTarget)' ">
1826
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
27+
<PackageReference Include="System.Memory" Version="4.5.4" />
1928
</ItemGroup>
2029

2130
<ItemGroup>
22-
<!--
23-
omnisharp-vscode doesn't seem to pick this up without this.
24-
For now we'll ignore the compiler warning
25-
-->
26-
<Compile Include="$(IntermediateOutputPath)\SR.Designer.cs" />
2731
<EmbeddedResource Update="SR.resx">
28-
<Generator>ResXFileCodeGenerator</Generator>
29-
<StronglyTypedFileName>$(IntermediateOutputPath)\SR.Designer.cs</StronglyTypedFileName>
30-
<LastGenOutput>SR.Designer.cs</LastGenOutput>
32+
<PartialClass>true</PartialClass>
33+
<GenerateCode>true</GenerateCode>
34+
<GenerateResource>false</GenerateResource>
3135
<StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
3236
<StronglyTypedNamespace>ClassExplorer</StronglyTypedNamespace>
3337
<StronglyTypedClassName>SR</StronglyTypedClassName>
3438
</EmbeddedResource>
3539
</ItemGroup>
3640

37-
<Target Name="FixResGen" BeforeTargets="AfterResGen">
38-
<Exec ConsoleToMsBuild="true" Command="pwsh -NoLogo -NoProfile -NonInteractive -Command &quot;Set-Content '$(IntermediateOutputPath)\SR.Designer.cs' ((Get-Content '$(IntermediateOutputPath)\SR.Designer.cs' -Raw) -replace '(?m)^(\s*internal )class', '$1partial class')&quot;" />
39-
</Target>
41+
<PropertyGroup>
42+
<!-- Create files for source generated code -->
43+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
44+
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
45+
</PropertyGroup>
46+
47+
<ItemGroup>
48+
<!-- Exclude the output of source generators from the compilation -->
49+
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
50+
</ItemGroup>
4051

4152
</Project>

src/ClassExplorer/IsExternalInit.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)