Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Get-WpfAssemblies
- {
- $names = 'PresentationCore', 'PresentationFramework', 'System.Xaml', 'WindowsBase'
- $programFiles = ${env:ProgramFiles(x86)}
- $base = $programFiles, 'Reference Assemblies', 'Microsoft', 'Framework', '.NETFramework' -Join '\'
- $latest = gci $base | % FullName | sort -Descending | select -Index 0
- return $names | % `
- {
- Join-Path $latest "$_.dll"
- }
- }
- function Get-UwpAssembly
- {
- $name = 'Windows.Foundation.UniversalApiContract'
- $programFiles = ${env:ProgramFiles(x86)}
- $directory = $programFiles, 'Windows Kits', '10', 'References', $name -Join '\'
- $latest = gci $directory | % FullName | sort -Descending | select -Index 0
- return Join-Path $latest "$name.winmd"
- }
- function Load-Assembly($assembly)
- {
- $assemblyClass = [Reflection.Assembly]
- $winmdClass = [Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMetadata]
- $domain = [AppDomain]::CurrentDomain
- # Since desktop .NET can't work with winmd files,
- # we have to use the reflection-only APIs and preload
- # all the dependencies manually.
- $appDomainHandler =
- {
- Param($sender, $e)
- $assemblyClass::ReflectionOnlyLoad($e.Name)
- }
- $winmdHandler =
- {
- Param($sender, $e)
- [string[]] $empty = @()
- $path = $winmdClass::ResolveNamespace($e.NamespaceName, $empty) | select -Index 0
- $e.ResolvedAssemblies.Add($assemblyClass::ReflectionOnlyLoadFrom($path))
- }
- # Hook up the handlers
- $domain.add_ReflectionOnlyAssemblyResolve($appDomainHandler)
- $winmdClass::add_ReflectionOnlyNamespaceResolve($winmdHandler)
- # Load it!
- $assemblyObject = $assemblyClass::ReflectionOnlyLoadFrom($assembly)
- # Deregister the handlers
- $domain.remove_ReflectionOnlyAssemblyResolve($appDomainHandler)
- $winmdClass::remove_ReflectionOnlyNamespaceResolve($winmdHandler)
- return $assemblyObject
- }
- function TryGet-Assembly($assembly, [ref] $cached)
- {
- $domain = [AppDomain]::CurrentDomain
- foreach ($reference in $domain.GetAssemblies())
- {
- # GetName() has a type of AssemblyName,
- # so we need to use GetName().Name to get a string
- $name = $reference.GetName().Name
- if ($name -eq $assembly)
- {
- $cached = $reference
- return $true
- }
- }
- return $false
- }
- function Get-Namespaces($assembly)
- {
- # Load the assembly, or retrieve it if it's cached
- $name = [IO.Path]::GetFileNameWithoutExtension($assembly)
- if (-not (TryGet-Assembly $name $assemblyObject))
- {
- $assemblyObject = Load-Assembly $assembly
- }
- # Do the actual work
- $types = $assemblyObject.GetTypes()
- return $types | ? IsPublic | select Namespace -Unique
- }
- function Filter-Namespaces($namespaces)
- {
- return $namespaces
- }
- cd $PSScriptRoot
- $assemblies = @()
- $assemblies += Get-WpfAssemblies
- $assemblies += Get-UwpAssembly
- $template = gc 'Template.cs'
- $namespaces = $assemblies | % `
- {
- % { Get-Namespaces $_ }
- }
- # Filter out the namespaces we don't need
- $namespaces = Filter-Namespaces $namespaces
- foreach ($namespace in $namespaces)
- {
- $contents = $template -Replace '$NAMESPACE', $namespace
- $file = "src/bin/$template.cs"
- # echo $contents > $file
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement