Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support #if SNIPPET in snippets #20391

Merged
merged 3 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ When run, the code regions in the format below (where `<snippetName>` is the nam
//some sample code
string snippet = "some snippet code";

// Lines prefixed with the below comment format will be ignored by the snippet updater.
/*@@*/ string ignored = "this code will not appear in the snippet markdown";

// Lines prefixed with the below comment format will appear in the snippet markdown, but will remain comments in the C#` code.
// Note: these comments should only be used for non-critical code as it will not be compiled or refactored as the code changes.
//@@ snippet = "value that would never pass a test but looks good in a sample!";
// The snippet updater defines the SNIPPET directive while parsing. You can use #if SNIPPET to filter lines in or out of the snippet.
#if SNIPPET
snippet = "value that would never pass a test but looks good in a sample!";
#else
string ignored = "this code will not appear in the snippet markdown";
#endif

#endregion
```
Expand Down
41 changes: 40 additions & 1 deletion eng/SnippetGenerator/DirectoryProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class DirectoryProcessor
RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.CultureInvariant);

private UTF8Encoding _utf8EncodingWithoutBOM;
private static string[] _snippetPreprocessorSymbols = new [] {"SNIPPET"};

public DirectoryProcessor(string directory)
{
Expand Down Expand Up @@ -208,7 +209,7 @@ private List<Snippet> GetSnippetsInDirectory(string baseDirectory)
{
var syntaxTree = CSharpSyntaxTree.ParseText(
File.ReadAllText(file),
new CSharpParseOptions(LanguageVersion.Preview),
new CSharpParseOptions(LanguageVersion.Preview, preprocessorSymbols: _snippetPreprocessorSymbols),
path: file);

list.AddRange(GetAllSnippets(syntaxTree));
Expand All @@ -225,6 +226,9 @@ private List<Snippet> GetSnippetsInDirectory(string baseDirectory)
private Snippet[] GetAllSnippets(SyntaxTree syntaxTree)
{
var snippets = new List<Snippet>();
var newRoot = PreprocessorDirectiveRemover.Instance.Visit(syntaxTree.GetRoot());
syntaxTree = syntaxTree.WithRootAndOptions(newRoot, syntaxTree.Options);

var directiveWalker = new DirectiveWalker();
directiveWalker.Visit(syntaxTree.GetRoot());

Expand All @@ -251,6 +255,41 @@ private Snippet[] GetAllSnippets(SyntaxTree syntaxTree)
return snippets.ToArray();
}

class PreprocessorDirectiveRemover : CSharpSyntaxRewriter
{
public static PreprocessorDirectiveRemover Instance = new PreprocessorDirectiveRemover();
private PreprocessorDirectiveRemover() : base(visitIntoStructuredTrivia: true)
{
}

public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
{
if (trivia.Kind() == SyntaxKind.DisabledTextTrivia)
return SyntaxFactory.Whitespace("");

return base.VisitTrivia(trivia);
}

public override SyntaxNode VisitIfDirectiveTrivia(IfDirectiveTriviaSyntax node)
{
return null;
}

public override SyntaxNode VisitElifDirectiveTrivia(ElifDirectiveTriviaSyntax node)
{
return null;
}

public override SyntaxNode VisitElseDirectiveTrivia(ElseDirectiveTriviaSyntax node)
{
return null;
}

public override SyntaxNode VisitEndIfDirectiveTrivia(EndIfDirectiveTriviaSyntax node)
{
return null;
}
}
class DirectiveWalker : CSharpSyntaxWalker
{
private Stack<RegionDirectiveTriviaSyntax> _regions = new Stack<RegionDirectiveTriviaSyntax>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public void TryTimeoutConfiguration()
Assert.That(options, Is.Not.Null);
}

#if NETCOREAPP3_1 || NET5
#if NETCOREAPP3_1 || NET5 || SNIPPET
/// <summary>
/// Performs basic smoke test validation of the contained snippet.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void TryTimeoutConfiguration()
Assert.That(options, Is.Not.Null);
}

#if NETCOREAPP3_1 || NET5
#if NETCOREAPP3_1 || NET5 || SNIPPET
/// <summary>
/// Performs basic smoke test validation of the contained snippet.
/// </summary>
Expand Down
28 changes: 20 additions & 8 deletions sdk/monitor/Azure.Monitory.Query/tests/LogsClientSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public async Task QueryLogsAsTablePrintAll()
#region Snippet:QueryLogsPrintTable

LogsClient client = new LogsClient(new DefaultAzureCredential());
/*@@*/string workspaceId = TestEnvironment.WorkspaceId;
//@@string workspaceId = "<workspace_id>";
#if SNIPPET
string workspaceId = "<workspace_id>";
#else
string workspaceId = TestEnvironment.WorkspaceId;
#endif
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated");

LogsQueryResultTable table = response.Value.PrimaryTable;
Expand Down Expand Up @@ -74,8 +77,11 @@ public async Task QueryLogsAsPrimitive()
#region Snippet:QueryLogsAsPrimitive

LogsClient client = new LogsClient(new DefaultAzureCredential());
/*@@*/string workspaceId = TestEnvironment.WorkspaceId;
//@@string workspaceId = "<workspace_id>";
#if SNIPPET
string workspaceId = "<workspace_id>";
#else
pakrym marked this conversation as resolved.
Show resolved Hide resolved
string workspaceId = TestEnvironment.WorkspaceId;
#endif

// Query TOP 10 resource groups by event count
Response<IReadOnlyList<string>> response = await client.QueryAsync<string>(workspaceId,
Expand All @@ -95,8 +101,11 @@ public async Task QueryLogsAsModels()
#region Snippet:QueryLogsAsModels

LogsClient client = new LogsClient(new DefaultAzureCredential());
/*@@*/string workspaceId = TestEnvironment.WorkspaceId;
//@@string workspaceId = "<workspace_id>";
#if SNIPPET
string workspaceId = "<workspace_id>";
#else
string workspaceId = TestEnvironment.WorkspaceId;
#endif

// Query TOP 10 resource groups by event count
Response<IReadOnlyList<MyLogEntryModel>> response = await client.QueryAsync<MyLogEntryModel>(workspaceId,
Expand All @@ -116,8 +125,11 @@ public async Task BatchQuery()
#region Snippet:BatchQuery

LogsClient client = new LogsClient(new DefaultAzureCredential());
/*@@*/string workspaceId = TestEnvironment.WorkspaceId;
//@@string workspaceId = "<workspace_id>";
#if SNIPPET
string workspaceId = "<workspace_id>";
#else
string workspaceId = TestEnvironment.WorkspaceId;
#endif

// Query TOP 10 resource groups by event count
// And total event count
Expand Down