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 1 commit
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
42 changes: 41 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,9 +226,13 @@ 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());


pakrym marked this conversation as resolved.
Show resolved Hide resolved
foreach (var region in directiveWalker.Regions)
{
var leadingTrivia = region.Item1.EndOfDirectiveToken.LeadingTrivia;
Expand All @@ -251,6 +256,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
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