Skip to content

Commit 6eee86d

Browse files
committed
Inlined missing snippets
1 parent 6302ff8 commit 6eee86d

File tree

6 files changed

+6
-80
lines changed

6 files changed

+6
-80
lines changed

docs/guide/compilation/assembly-generator.md

+3-15
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@ the `LamarCompiler.AssemblyGenerator` class in the LamarCompiler library.
1111

1212
Let's say that you have a simple interface in your system like this:
1313

14-
<!-- snippet: sample_IOperation -->
15-
<a id='snippet-sample_ioperation'></a>
1614
```cs
1715
public interface IOperation
1816
{
1917
int Calculate(int one, int two);
2018
}
2119
```
22-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Codegen.cs#L9-L14' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_ioperation' title='Start of snippet'>anchor</a></sup>
23-
<!-- endSnippet -->
2420

2521
Next, let's use `AssemblyGenerator` to compile code with a custom implementation of `IOperation` that we've generated
2622
in code:
2723

28-
<!-- snippet: sample_using-AssemblyGenerator -->
29-
<a id='snippet-sample_using-assemblygenerator'></a>
3024
```cs
3125
var generator = new AssemblyGenerator();
3226

@@ -64,22 +58,18 @@ var operation = (IOperation)Activator.CreateInstance(type);
6458
// Use our new type
6559
var result = operation.Calculate(1, 2);
6660
```
67-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Codegen.cs#L29-L66' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-assemblygenerator' title='Start of snippet'>anchor</a></sup>
68-
<!-- endSnippet -->
6961

7062
There's only a couple things going on in the code above:
7163

7264
1. I added an assembly reference for the .Net assembly that holds the `IOperation` interface
73-
1. I passed a string to the `GenerateCode()` method, which successfully compiles my code and hands me back a .Net [Assembly](https://msdn.microsoft.com/en-us/library/system.reflection.assembly(v=vs.110).aspx) object
74-
1. Load the newly generated type from the new Assembly
75-
1. Use the new `IOperation`
65+
2. I passed a string to the `GenerateCode()` method, which successfully compiles my code and hands me back a .Net [Assembly](https://msdn.microsoft.com/en-us/library/system.reflection.assembly(v=vs.110).aspx) object
66+
3. Load the newly generated type from the new Assembly
67+
4. Use the new `IOperation`
7668

7769
If you're not perfectly keen on doing brute force string manipulation to generate your code, you can
7870
also use Lamar's built in [ISourceWriter](/guide/compilation/source-writer) to generate some of the code for you with
7971
all its code generation utilities:
8072

81-
<!-- snippet: sample_using-AssemblyGenerator-with-source-writer -->
82-
<a id='snippet-sample_using-assemblygenerator-with-source-writer'></a>
8373
```cs
8474
var generator = new AssemblyGenerator();
8575

@@ -108,5 +98,3 @@ var operation = (IOperation)Activator.CreateInstance(type);
10898

10999
var result = operation.Calculate(1, 2);
110100
```
111-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Codegen.cs#L73-L103' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-assemblygenerator-with-source-writer' title='Start of snippet'>anchor</a></sup>
112-
<!-- endSnippet -->

docs/guide/compilation/frames/extension-methods.md

-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ determine how a type should be written *in code* with these extension methods in
99

1010
The functionality of `NameInCode()` is demonstrated below with some of its xUnit tests:
1111

12-
<!-- snippet: sample_get-the-type-name-in-code -->
13-
<a id='snippet-sample_get-the-type-name-in-code'></a>
1412
```cs
1513
[Theory]
1614
[InlineData(typeof(void), "void")]
@@ -29,13 +27,9 @@ public void alias_name_of_task(Type type, string name)
2927
type.NameInCode().ShouldBe(name);
3028
}
3129
```
32-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Codegen/ReflectionExtensionsTests.cs#L70-L87' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_get-the-type-name-in-code' title='Start of snippet'>anchor</a></sup>
33-
<!-- endSnippet -->
3430

3531
Likewise, `FullNameInCode()` is shown below:
3632

37-
<!-- snippet: sample_get-the-full-type-name-in-code -->
38-
<a id='snippet-sample_get-the-full-type-name-in-code'></a>
3933
```cs
4034
[Theory]
4135
[InlineData(typeof(void), "void")]
@@ -53,5 +47,3 @@ public void alias_full_name_of_task(Type type, string name)
5347
type.FullNameInCode().ShouldBe(name);
5448
}
5549
```
56-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Codegen/ReflectionExtensionsTests.cs#L89-L105' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_get-the-full-type-name-in-code' title='Start of snippet'>anchor</a></sup>
57-
<!-- endSnippet -->

docs/guide/compilation/frames/frame.md

-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ To build a custom frame, you first need to create a new class that subclasses `F
1313
The one thing you absolutely have to do when you create a new `Frame` class is to override the `GenerateCode()` method. Take this example
1414
from Lamar itself for a frame that just injects a comment into the generated code:
1515

16-
<!-- snippet: sample_CommentFrame -->
17-
<a id='snippet-sample_commentframe'></a>
1816
```cs
1917
public class CommentFrame : SyncFrame
2018
{
@@ -35,8 +33,6 @@ public class CommentFrame : SyncFrame
3533
}
3634
}
3735
```
38-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCodeGeneration/Frames/Frame.cs#L16-L35' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_commentframe' title='Start of snippet'>anchor</a></sup>
39-
<!-- endSnippet -->
4036

4137
A couple things to note about the `GenerateCode()` method:
4238

docs/guide/compilation/frames/index.md

+3-23
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,15 @@ The various types represent:
1919

2020
Alright then, let's make this concrete. Let's say that we want to generate and use dynamic instances of this interface:
2121

22-
<!-- snippet: sample_ISaySomething -->
23-
<a id='snippet-sample_isaysomething'></a>
2422
```cs
2523
public interface ISaySomething
2624
{
2725
void Speak();
2826
}
2927
```
30-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Frames.cs#L67-L72' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_isaysomething' title='Start of snippet'>anchor</a></sup>
31-
<!-- endSnippet -->
3228

3329
Moreover, I want a version of `ISaySomething` that will call the following method and write the current time to the console:
3430

35-
<!-- snippet: sample_NowSpeaker -->
36-
<a id='snippet-sample_nowspeaker'></a>
3731
```cs
3832
public static class NowSpeaker
3933
{
@@ -43,15 +37,11 @@ public static class NowSpeaker
4337
}
4438
}
4539
```
46-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Frames.cs#L57-L65' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_nowspeaker' title='Start of snippet'>anchor</a></sup>
47-
<!-- endSnippet -->
4840

4941
Our dynamic class for ISaySomething will need to pass the current time to the now parameter of that method. To help out here, there's some built in helpers in Lamar specifically to write in the right code to get the current time to a variable of DateTime or DateTimeOffset that is named "now."
5042

5143
To skip ahead a little bit, let's generate a new class and object with the following code:
5244

53-
<!-- snippet: sample_write-new-method -->
54-
<a id='snippet-sample_write-new-method'></a>
5545
```cs
5646
// Configures the code generation rules
5747
// and policies
@@ -79,8 +69,6 @@ method.Frames.Add(@call);
7969
// Compile the new code!
8070
assembly.CompileAll();
8171
```
82-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Frames.cs#L23-L49' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_write-new-method' title='Start of snippet'>anchor</a></sup>
83-
<!-- endSnippet -->
8472

8573
After all that, if we interrogate the source code for the generated type above (type.SourceCode), we'd see this ugly generated code:
8674

@@ -106,8 +94,6 @@ Some notes about the generated code:
10694

10795
So now let's look at how Lamar was able to add the code to pass along DateTime.UtcNow. First off, let's look at the code that just writes out the date variable:
10896

109-
<!-- snippet: sample_NowFetchFrame -->
110-
<a id='snippet-sample_nowfetchframe'></a>
11197
```cs
11298
public class NowFetchFrame : SyncFrame
11399
{
@@ -127,22 +113,18 @@ public class NowFetchFrame : SyncFrame
127113
}
128114
}
129115
```
130-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCodeGeneration/Model/NowTimeVariableSource.cs#L31-L49' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_nowfetchframe' title='Start of snippet'>anchor</a></sup>
131-
<!-- endSnippet -->
132116

133117
In the frame above, you'll see that the `GenerateCode()` method writes its code into the source, then immediately turns around and tells the next Frame - if there is one - to generated its code. As the last step to write out the new source code, Lamar:
134118

135119
1. Goes through an effort to find any missing frames and variables
136-
1. Sorts them with a topological sort (what frames depend on what other frames or variables, what variables are used or created by what frames)
137-
1. Organizes the frames into a single linked list
138-
1. Calls `GenerateCode()` on the first frame
120+
2. Sorts them with a topological sort (what frames depend on what other frames or variables, what variables are used or created by what frames)
121+
3. Organizes the frames into a single linked list
122+
4. Calls `GenerateCode()` on the first frame
139123

140124
In the generated method up above, the call to `NowSpeaker.Speak(now)` depends on the now variable which is in turn created by the `NowFetchFrame`, and that's enough information for Lamar to order things and generate the final code.
141125

142126
Lastly, we had to use a custom `IVariableSource` to teach Lamar how to resolve the now variable. That code looks like this:
143127

144-
<!-- snippet: sample_NowTimeVariableSource -->
145-
<a id='snippet-sample_nowtimevariablesource'></a>
146128
```cs
147129
public class NowTimeVariableSource : IVariableSource
148130
{
@@ -167,8 +149,6 @@ public class NowTimeVariableSource : IVariableSource
167149
}
168150
}
169151
```
170-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCodeGeneration/Model/NowTimeVariableSource.cs#L6-L29' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_nowtimevariablesource' title='Start of snippet'>anchor</a></sup>
171-
<!-- endSnippet -->
172152

173153
Out of the box, the Lamar + [Jasper](https://jasperfx.github.io) combination uses variable sources for:
174154

docs/guide/compilation/frames/injected-fields.md

-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ As an example, let's take the `WhatTimeIsIt` generated type from the [frames mod
1111
this time generate the class with the assumption that the "now" time is injected into the generated type's constructor
1212
like this:
1313

14-
<!-- snippet: sample_using-injected-field -->
15-
<a id='snippet-sample_using-injected-field'></a>
1614
```cs
1715
var assembly = GeneratedAssembly.Empty();
1816
var type = assembly.AddType("WhatTimeIsIt", typeof(ISaySomething));
@@ -30,8 +28,6 @@ method.Frames.Add(@call);
3028

3129
assembly.CompileAll();
3230
```
33-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/InjectedFieldUsage.cs#L22-L38' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-injected-field' title='Start of snippet'>anchor</a></sup>
34-
<!-- endSnippet -->
3531

3632
At runtime as Lamar tries to write the code for a new generated type, it will seek out any or all `InjectedField` variables
3733
used within any of the methods and use those to generate a constructor function. The generated code for the dynamic type

docs/guide/compilation/frames/variables.md

-26
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ The `Variable` class in LamarCodeGeneration models the CLR type and usage of a v
44

55
Here are some samples of creating variable objects with a variable type and name:
66

7-
<!-- snippet: sample_create-a-variable -->
8-
<a id='snippet-sample_create-a-variable'></a>
97
```cs
108
// Create a connection for the type SqlConnection
119
// with the name "conn"
@@ -19,21 +17,15 @@ var conn2 = new Variable(typeof(SqlConnection), "conn2");
1917
var conn3 = Variable.For<SqlConnection>();
2018
conn3.Usage.ShouldBe("sqlConnection");
2119
```
22-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Variables.cs#L40-L52' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_create-a-variable' title='Start of snippet'>anchor</a></sup>
23-
<!-- endSnippet -->
2420

2521
## Default Naming
2622

2723
If you do not give a name for a variable, Lamar will derive a default variable name from the type like this:
2824

29-
<!-- snippet: sample_default-variable-name-usage -->
30-
<a id='snippet-sample_default-variable-name-usage'></a>
3125
```cs
3226
var widget = Variable.For<IWidget>();
3327
widget.Usage.ShouldBe("widget");
3428
```
35-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Variables.cs#L26-L29' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_default-variable-name-usage' title='Start of snippet'>anchor</a></sup>
36-
<!-- endSnippet -->
3729

3830
The best way to understand the full rules for deriving the default variable names is to just peek at the
3931
[unit tests within the Lamar codebase](https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Codegen/VariableTests.cs).
@@ -43,8 +35,6 @@ The best way to understand the full rules for deriving the default variable name
4335
If a variable is created by a [Frame](/guide/compilation/frames/frame), you really want to mark that relationship by
4436
either passing the creating frame into the constructor function like this:
4537

46-
<!-- snippet: sample_NowFetchFrame -->
47-
<a id='snippet-sample_nowfetchframe'></a>
4838
```cs
4939
public class NowFetchFrame : SyncFrame
5040
{
@@ -64,46 +54,30 @@ public class NowFetchFrame : SyncFrame
6454
}
6555
}
6656
```
67-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCodeGeneration/Model/NowTimeVariableSource.cs#L31-L49' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_nowfetchframe' title='Start of snippet'>anchor</a></sup>
68-
<!-- endSnippet -->
6957

7058
## Overriding Variable Usage or Type
7159

7260
Do this sparingly, but you can override the name or usage and type of a previously built variable like this:
7361

74-
<!-- snippet: sample_override-variable-usage-and-type -->
75-
<a id='snippet-sample_override-variable-usage-and-type'></a>
7662
```cs
7763
var service = new Variable(typeof(IService), "service");
7864
service.OverrideName("myService");
7965
service.OverrideType(typeof(WhateverService));
8066
```
81-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Variables.cs#L33-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_override-variable-usage-and-type' title='Start of snippet'>anchor</a></sup>
82-
<!-- endSnippet -->
83-
8467
## Derived Variables
8568

8669
Variables don't have to mean literal C# variables in the generated code. They can be derived values like this example:
8770

88-
<!-- snippet: sample_derived-variable -->
89-
<a id='snippet-sample_derived-variable'></a>
9071
```cs
9172
var now = new Variable(typeof(DateTime), $"{typeof(DateTime).FullName}.{nameof(DateTime.Now)}");
9273
```
93-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Variables.cs#L22-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_derived-variable' title='Start of snippet'>anchor</a></sup>
94-
<!-- endSnippet -->
95-
9674
## Dependencies to Other Variables
9775

9876
For the sake of frame ordering, you might need to give Lamar a hint that your variable depends on another variable. Here's
9977
an example of doing that with the `HttpResponse` type from ASP.Net Core:
10078

101-
<!-- snippet: sample_variable-dependencies -->
102-
<a id='snippet-sample_variable-dependencies'></a>
10379
```cs
10480
var context = Variable.For<HttpContext>();
10581
var response = new Variable(typeof(HttpResponse), $"{context.Usage}.{nameof(HttpContext.Response)}");
10682
response.Dependencies.Add(context);
10783
```
108-
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Variables.cs#L56-L60' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_variable-dependencies' title='Start of snippet'>anchor</a></sup>
109-
<!-- endSnippet -->

0 commit comments

Comments
 (0)