DougFinke

Untitled

Nov 11th, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-RoslynVisitMethods {
  2.    
  3.     Add-Type -Path "C:\Program Files\Reference Assemblies\Microsoft\Roslyn\v1.0\Roslyn.Compilers.CSharp.dll"
  4.  
  5.     [Roslyn.Compilers.CSharp.SyntaxVisitor].GetMethods("NonPublic,Instance") |
  6.         Where {$_.name -like 'Visit*'}        
  7. }
  8.  
  9. $code = @"
  10. using System;
  11. using System.Management.Automation;
  12. using Roslyn.Compilers.CSharp;
  13.  
  14. public class PSRoslynWalker : SyntaxWalker
  15. {
  16.    PSObject module;
  17.    public PSRoslynWalker(PSObject module) {        
  18.        this.module = module;
  19.    }
  20.  
  21. $(
  22. Get-RoslynVisitMethods |
  23.    ForEach {
  24.    @"
  25.    protected override void $($_.name) ($($_.GetParameters()[0].ParameterType.Name) node)
  26.     {
  27.         var method = module.Methods["$($_.name)"];
  28.         if (method != null)
  29.         {
  30.             method.Invoke(node);
  31.         }
  32.  
  33.         base.$($_.name)(node);
  34.     }
  35.  
  36. "@
  37.    }
  38. )
  39. }
  40. "@
  41.  
  42.  
  43. $r = "C:\Program Files\Reference Assemblies\Microsoft\Roslyn\v1.0\Roslyn.Compilers.dll",
  44.      "C:\Program Files\Reference Assemblies\Microsoft\Roslyn\v1.0\Roslyn.Compilers.CSharp.dll"
  45.  
  46. Add-Type -TypeDefinition $Code -ReferencedAssemblies $r
  47.  
  48. $m = New-Module -AsCustomObject {
  49.    
  50.     $script:classNames = @()
  51.  
  52.     function VisitClassDeclaration ($node) {        
  53.         $script:classNames +=  New-Object PSObject -Property @{
  54.             Name = $node.Identifier.Value
  55.         }
  56.     }
  57.  
  58.     function GetClassName {$script:classNames}
  59. }
  60.  
  61. cls
  62.  
  63. $targetDirectory = "C:\downloads\NuGet\nuget_47b7cea8ff2a"
  64. dir $targetDirectory -Recurse *.cs | % {
  65.     $fileName = $_.FullName
  66.     $source   = [IO.File]::ReadAllText($fileName)
  67.     $tree     = [Roslyn.Compilers.CSharp.SyntaxTree]::ParseCompilationUnit($source, "", $null, (New-Object System.Threading.CancellationToken) )
  68.     $obj      = New-Object PSRoslynWalker $m
  69.     $obj.Visit($tree.Root)
  70. }
  71.  
  72. $m.GetClassName() | Sort Name
  73.  
Advertisement
Add Comment
Please, Sign In to add comment