Skip to content

Commit

Permalink
Update build script
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
  • Loading branch information
gabriel-samfira committed Aug 25, 2024
1 parent 4d1a970 commit 063c033
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/obj
src/bin
75 changes: 6 additions & 69 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,14 @@
#

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$source = [string](Get-Content -Raw (Join-Path $here "src/serializer.cs"))

dotnet build --configuration Release $here/src/

function Invoke-LoadInContext {
param(
[string]$assemblyPath,
[string]$loadContextName
)

$loadContext = [System.Runtime.Loader.AssemblyLoadContext]::New($loadContextName, $true)
$assemblies = $loadContext.LoadFromAssemblyPath($assemblyPath)
$destinations = @("netstandard2.1", "net47")

return @{ "yaml"= $assemblies }
}

function Invoke-LoadInGlobalContext {
param(
[string]$assemblyPath
)
$assemblies = [Reflection.Assembly]::LoadFrom($assemblyPath)
return @{ "yaml"= $assemblies }
}


function Invoke-LoadAssembly {
$libDir = Join-Path $here "lib"
$assemblies = @{
"core" = Join-Path $libDir "netstandard2.1\YamlDotNet.dll";
"net47" = Join-Path $libDir "net47\YamlDotNet.dll";
}

if ($PSVersionTable.Keys -contains "PSEdition") {
if ($PSVersionTable.PSEdition -eq "Core") {
return (Invoke-LoadInContext -assemblyPath $assemblies["core"] -loadContextName "powershellyaml")
} elseif ($PSVersionTable.PSVersion.Major -gt 5.1) {
return (Invoke-LoadInContext -assemblyPath $assemblies["net47"] -loadContextName "powershellyaml")
} else {
return (Invoke-LoadInGlobalContext $assemblies["net47"])
}
} else {
return (Invoke-LoadInGlobalContext $assemblies["net47"])
}
}

$assemblies = Invoke-LoadAssembly
$yamlDotNetAssembly = $assemblies["yaml"]


if (!([System.Management.Automation.PSTypeName]'StringQuotingEmitter').Type) {
$referenceList = @($yamlDotNetAssembly.Location,[Text.RegularExpressions.Regex].Assembly.Location, "System.Management.Automation.dll", "System.Collections.dll")
if ($PSVersionTable.PSEdition -eq "Core") {
$referenceList += [IO.Directory]::GetFiles([IO.Path]::Combine($PSHOME, 'ref'), 'netstandard.dll', [IO.SearchOption]::TopDirectoryOnly)
$destinations = @("lib/netstandard2.1")
$referenceList += 'System.Runtime.Numerics.dll'
} else {
$referenceList += 'System.Runtime.dll'
$destinations = @("lib/net47")
$referenceList += 'System.Numerics.dll'
}
}

$destinations = @("lib/netstandard2.1", "lib/net47")
foreach ($item in $destinations) {
$src = Join-Path $here "src" "bin" "Release" $item "serializer.dll"
$dst = Join-Path $here "lib" $item "StringQuotingEmitter.dll"

foreach ($target in $destinations) {
$targetPath = Join-Path $here $target
$file = Join-Path $targetPath "StringQuotingEmitter.dll"
if (!(Test-Path $file)) {
if ($PSVersionTable.PSEdition -eq "Core") {
Add-Type -TypeDefinition $source -ReferencedAssemblies $referenceList -Language CSharp -CompilerOptions "-nowarn:1701" -OutputAssembly $file
} else {
Add-Type -TypeDefinition $source -ReferencedAssemblies $referenceList -Language CSharp -OutputAssembly $file
}
}
Copy-Item -Force $src $dst
}
Binary file modified lib/net47/StringQuotingEmitter.dll
Binary file not shown.
Binary file modified lib/netstandard2.1/StringQuotingEmitter.dll
Binary file not shown.
6 changes: 1 addition & 5 deletions powershell-yaml.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,14 @@ function Convert-PSObjectToGenericObject {
}

$dataType = $data.GetType()
if ($data -isnot [System.Object]) {
return $data -as $dataType
}

if (([System.Collections.Specialized.OrderedDictionary].IsAssignableFrom($dataType))){
return Convert-OrderedHashtableToDictionary $data
} elseif (([System.Collections.IDictionary].IsAssignableFrom($dataType))){
return Convert-HashtableToDictionary $data
} elseif (([System.Collections.IList].IsAssignableFrom($dataType))) {
return Convert-ListToGenericList $data
}
return $data -as $dataType
return $data
}

function ConvertFrom-Yaml {
Expand Down
13 changes: 13 additions & 0 deletions src/serializer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;net47</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.5.1.ReferenceAssemblies" Version="1.0.0" />
<PackageReference Include="System.Management" Version="8.0.0" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
</ItemGroup>

</Project>

0 comments on commit 063c033

Please sign in to comment.