Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. function Get-WpfAssemblies
  2. {
  3. $names = 'PresentationCore', 'PresentationFramework', 'System.Xaml', 'WindowsBase'
  4. $programFiles = ${env:ProgramFiles(x86)}
  5. $base = $programFiles, 'Reference Assemblies', 'Microsoft', 'Framework', '.NETFramework' -Join '\'
  6. $latest = gci $base | % FullName | sort -Descending | select -Index 0
  7. return $names | % `
  8. {
  9. Join-Path $latest "$_.dll"
  10. }
  11. }
  12.  
  13. function Get-UwpAssembly
  14. {
  15. $name = 'Windows.Foundation.UniversalApiContract'
  16. $programFiles = ${env:ProgramFiles(x86)}
  17. $directory = $programFiles, 'Windows Kits', '10', 'References', $name -Join '\'
  18. $latest = gci $directory | % FullName | sort -Descending | select -Index 0
  19. return Join-Path $latest "$name.winmd"
  20. }
  21.  
  22.  
  23. function Load-Assembly($assembly)
  24. {
  25. $assemblyClass = [Reflection.Assembly]
  26. $winmdClass = [Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMetadata]
  27. $domain = [AppDomain]::CurrentDomain
  28.  
  29. # Since desktop .NET can't work with winmd files,
  30. # we have to use the reflection-only APIs and preload
  31. # all the dependencies manually.
  32. $appDomainHandler =
  33. {
  34. Param($sender, $e)
  35. $assemblyClass::ReflectionOnlyLoad($e.Name)
  36. }
  37.  
  38. $winmdHandler =
  39. {
  40. Param($sender, $e)
  41. [string[]] $empty = @()
  42. $path = $winmdClass::ResolveNamespace($e.NamespaceName, $empty) | select -Index 0
  43. $e.ResolvedAssemblies.Add($assemblyClass::ReflectionOnlyLoadFrom($path))
  44. }
  45.  
  46. # Hook up the handlers
  47. $domain.add_ReflectionOnlyAssemblyResolve($appDomainHandler)
  48. $winmdClass::add_ReflectionOnlyNamespaceResolve($winmdHandler)
  49.  
  50. # Load it!
  51. $assemblyObject = $assemblyClass::ReflectionOnlyLoadFrom($assembly)
  52.  
  53. # Deregister the handlers
  54. $domain.remove_ReflectionOnlyAssemblyResolve($appDomainHandler)
  55. $winmdClass::remove_ReflectionOnlyNamespaceResolve($winmdHandler)
  56.  
  57. return $assemblyObject
  58. }
  59.  
  60. function TryGet-Assembly($assembly, [ref] $cached)
  61. {
  62. $domain = [AppDomain]::CurrentDomain
  63.  
  64. foreach ($reference in $domain.GetAssemblies())
  65. {
  66. # GetName() has a type of AssemblyName,
  67. # so we need to use GetName().Name to get a string
  68. $name = $reference.GetName().Name
  69. if ($name -eq $assembly)
  70. {
  71. $cached = $reference
  72. return $true
  73. }
  74. }
  75.  
  76. return $false
  77. }
  78.  
  79. function Get-Namespaces($assembly)
  80. {
  81. # Load the assembly, or retrieve it if it's cached
  82. $name = [IO.Path]::GetFileNameWithoutExtension($assembly)
  83. if (-not (TryGet-Assembly $name $assemblyObject))
  84. {
  85. $assemblyObject = Load-Assembly $assembly
  86. }
  87.  
  88. # Do the actual work
  89. $types = $assemblyObject.GetTypes()
  90. return $types | ? IsPublic | select Namespace -Unique
  91. }
  92.  
  93. function Filter-Namespaces($namespaces)
  94. {
  95. return $namespaces
  96. }
  97.  
  98. cd $PSScriptRoot
  99.  
  100. $assemblies = @()
  101. $assemblies += Get-WpfAssemblies
  102. $assemblies += Get-UwpAssembly
  103.  
  104. $template = gc 'Template.cs'
  105. $namespaces = $assemblies | % `
  106. {
  107. % { Get-Namespaces $_ }
  108. }
  109.  
  110. # Filter out the namespaces we don't need
  111. $namespaces = Filter-Namespaces $namespaces
  112.  
  113. foreach ($namespace in $namespaces)
  114. {
  115. $contents = $template -Replace '$NAMESPACE', $namespace
  116. $file = "src/bin/$template.cs"
  117. # echo $contents > $file
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement