Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net focused 73rd Devclub.lv

90 views

Published on

The Roslyn C# gives developers access to the C# language parsing and generation capabilities. This can be leveraged in many ways by building tools that fully understand the code and are capable of generating new code with full comprehension of your codebase. In this talk I will walk you through the basic concepts of the Roslyn and language parsing, building a small tool using these principles and packaging it as a .NET Core tool for easy distribution and usage via the .NET Core CLI

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net focused 73rd Devclub.lv

  1. 1. Code generation with Roslyn APIs
  2. 2. Arthur Tabatchnic Working at Innoforce & KeelDocs Hacking away at compilers, tools and languages
  3. 3. Structure ● History ● Compiler pipeline ● Compiler APIs ● Demos
  4. 4. A little history
  5. 5. The .NET Compiler Platform
  6. 6. The .NET Compiler Platform Roslyn
  7. 7. Compiler pipeline Parsing Binding Emitting
  8. 8. Parsing
  9. 9. Parsing Tokenizer Parser Symbol building
  10. 10. Tokenizer turns text into tokens namespace MyNamespace { class Class1 { } } Keyword.Namespace, “MyNamespace”, LBRACE, Keyword.Class, “Class1”, LBRACE, RBRACE, RBRACE
  11. 11. Tokenizer turns text into tokens namespace MyNamespace { class Class1 { } } Keyword.Namespace, SPACE “MyNamespace”, NEWLINE, LBRACE, NEWLINE, TAB, Keyword.Class, SPACE “Class1”, NEWLINE TAB, LBRACE, NEWLINE TAB, RBRACE, NEWLINE RBRACE, EOF
  12. 12. Parsing Tokenizer Parser Symbol building
  13. 13. Parser turns tokens into a syntax tree Keyword.Namespace, “MyNamespace”, LBRACE, Keyword.Class, “Class1”, LBRACE, RBRACE, RBRACE Syntax nodes, syntax tokens and trivia
  14. 14. Parsing Tokenizer Parser Symbol building
  15. 15. Symbol building creates a semantic model Symbols know: ● Where they are declared ● What namespace they belong to ● Is it abstract? Sealed? ● Does it hide a base method? ● What symbol does it return?
  16. 16. Parsing Tokenizer Parser Symbol building
  17. 17. Compiler pipeline Parsing Binding Emitting
  18. 18. Binding
  19. 19. Binding maps symbols to source code “MyNamespace.Class1 is declared in RoslynDemo.csproj in file Class1.cs on Line 3 Column 11”
  20. 20. Compiler pipeline Parsing Binding Emitting
  21. 21. Emitting
  22. 22. Emitting turns compiler info into IL namespace MyNamespace { class Class1 { } } MyProject.dll
  23. 23. Compiler pipeline Parsing Binding Emitting
  24. 24. Ok, now what?
  25. 25. Roslyn APIs!
  26. 26. Compiler pipeline Parsing Binding Emitting
  27. 27. Compiler pipeline Syntax tree API Binding Emitting
  28. 28. Compiler pipeline Syntax tree API & Symbol API Binding Emitting
  29. 29. Compiler pipeline Syntax tree API & Symbol API Binding & Flow Analysis API Emitting
  30. 30. Compiler pipeline Syntax tree API & Symbol API Binding & Flow Analysis API Emit API
  31. 31. Ok, now what?
  32. 32. Build things!
  33. 33. But what things?
  34. 34. Syntax tree API ● Outlining ● Colorizing ● Automatic formatting
  35. 35. Symbol API ● Object browser ● Navigate to
  36. 36. Binding & Flow Analysis API ● Go To Definition ● Extract method ● Rename ● Find All References ● Autocompletion ● Signature assistance ● So much more!
  37. 37. Emit API ● Edit and Continue
  38. 38. Diagnostic APIs ● Errors, warnings, information ● Custom user warnings
  39. 39. Scripting APIs ● Execute code interactively ● Build up an execution context
  40. 40. Workspace APIs ● Work over entire solutions seamlessly in a single model
  41. 41. Let’s build something already!
  42. 42. Demo 1: Syntax analysis using System; using System.Collections; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }
  43. 43. Demo 1: Syntax analysis We have: ● Turned code into a syntax tree dynamically ● Explored manually the content of the tree ● Queried the tree using LINQ
  44. 44. Demo 2: Semantic analysis using System; using System.Collections; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }
  45. 45. Demo 2: Semantic analysis We have: ● Turned code into a syntax tree dynamically ● Built a semantic model of our code ● Queried the semantic model about: ○ Our code ○ Dependencies
  46. 46. Demo 3: Basic syntax transformation using System; using System.Collections; -> using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }
  47. 47. Demo 3: Basic syntax transformation We have: ● Created a new syntax node from scratch ● Manually found the target to replace in the syntax tree ● Made a new tree, replacing the target node with our new node
  48. 48. Demo 4: Advanced syntax transformation This: MyType myVariable = new MyType(); To this: var myVariable = new MyType(); In the scope of a project!
  49. 49. Demo 4: Advanced syntax transformation We have: ● Read code from .cs files ● Dynamically identified the nodes to replace ● Built up new nodes, keeping the original semantics intact ● Replaced the target nodes in the syntax tree ● Turned our syntax tree into .cs files, replacing the old ones!
  50. 50. Distribution
  51. 51. Step 1: add NuGet data <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.2</TargetFramework> <PackAsTool>true</PackAsTool> <ToolCommandName>MyNewTool</ToolCommandName> <PackageOutputPath>./nupkg</PackageOutputPath> </PropertyGroup> </Project>
  52. 52. Step 2: make the package > dotnet pack
  53. 53. Step 3: install the package > dotnet tool install --global MyNewTool
  54. 54. Questions? Sources available at github.com/faso/RoslynTalkDemos

×