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

Add Snippet Generator as a tool to be published, update dotnet.yml to… #1834

Merged
merged 7 commits into from Jul 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 10 additions & 14 deletions .azure-pipelines/dotnet.yml
Expand Up @@ -11,7 +11,8 @@ trigger:
- hotfix/*
paths:
include:
- src/dotnet/Azure.ClientSdk.Analyzers
- dotnet.proj
- src/dotnet/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think /* is doing what you expect as devops doesn't support wildcards in the paths.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. Will src/dotnet work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I believe it should work without the *


pr:
branches:
Expand All @@ -22,7 +23,8 @@ pr:
- hotfix/*
paths:
include:
- src/dotnet/Azure.ClientSdk.Analyzers
- dotnet.proj
- src/dotnet/*

resources:
repositories:
Expand All @@ -32,8 +34,9 @@ resources:
ref: refs/tags/azure-sdk-build-tools_20210603.1

variables:
DotNetCoreVersion: '3.0.100'
NugetSecurityAnalysisWarningLevel: 'none'
ProjectFile: 'dotnet.proj'
VersioningProps: '/p:OfficialBuildId=$(Build.BuildNumber)'

stages:
- stage: 'Build_and_Test'
Expand All @@ -44,11 +47,6 @@ stages:
vmImage: 'vs2017-win2016'

steps:
- task: DotNetCoreInstaller@0
displayName: 'Use .NET Core sdk $(DotNetCoreVersion)'
inputs:
version: '$(DotNetCoreVersion)'

- script: 'dotnet pack $(ProjectFile) -o $(Build.ArtifactStagingDirectory) -warnaserror $(VersioningProps)'
displayName: 'Build and Package'
env:
Expand All @@ -68,11 +66,6 @@ stages:
vmImage: 'vs2017-win2016'

steps:
- task: DotNetCoreInstaller@0
displayName: 'Use .NET Core sdk $(DotNetCoreVersion)'
inputs:
version: '$(DotNetCoreVersion)'

- task: DotNetCoreCLI@2
displayName: 'Build & Test'
env:
Expand All @@ -93,5 +86,8 @@ stages:
testResultsFormat: 'VSTest'
mergeTestResults: true

- ${{ if ne(variables['System.TeamProject'], 'Public') }}:
- ${{ if and(ne(variables['System.TeamProject'], 'Public'), ne(variables['Build.Reason'], 'PullRequest'))}}:
- template: pipelines/stages/net-release-to-feed.yml@azure-sdk-build-tools
parameters:
# azure-sdk-tools feed at https://dev.azure.com/azure-sdk/public/_packaging?_a=feed&feed=azure-sdk-tools
DevOpsFeedId: 29ec6040-b234-4e31-b139-33dc4287b756/5a74d21c-c79b-44a9-afb3-39fed340c1f3
3 changes: 1 addition & 2 deletions dotnet.proj
@@ -1,6 +1,5 @@
<Project Sdk="Microsoft.Build.Traversal">
<ItemGroup>
<ProjectReference
Include="src\dotnet\Azure.ClientSdk.Analyzers\**\*.csproj"/>
<ProjectReference Include="src\dotnet\Azure.ClientSdk.Analyzers\**\*.csproj" />
</ItemGroup>
</Project>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>net5.0</TargetFrameworks>
<PreserveCompilationContext>true</PreserveCompilationContext>
<LangVersion>8</LangVersion>
</PropertyGroup>
Expand Down
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we intend to change this to net5?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praveen is fixing this up with another PR.

<BuildOutputTargetFolder>analyzers/dotnet/cs/</BuildOutputTargetFolder>
<VersionPrefix>0.1.1</VersionPrefix>
<LangVersion>8</LangVersion>
Expand Down
66 changes: 66 additions & 0 deletions src/dotnet/SnippetGenerator/CSharpProcessor.cs
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SnippetGenerator
{
public class CSharpProcessor
{
private static readonly string _snippetFormat = "{3} <code snippet=\"{0}\" language=\"csharp\">{1}{2} </code>";
private static readonly string _snippetExampleFormat = "{3} <example snippet=\"{0}\">{1}{3} <code language=\"csharp\">{1}{2} </code>{1}{3} </example>";

private static readonly Regex _snippetRegex = new Regex("^(?<indent>\\s*)\\/{3}\\s*<code snippet=\"(?<name>[\\w:]+)\"[^>]*?>.*?\\s*<\\/code>",
RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

private static readonly Regex _snippetExampleRegex = new Regex("^(?<indent>\\s*)\\/{3}\\s*<example snippet=\"(?<name>[\\w:]+)\">.*?\\s*<\\/example>",
RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

public static async ValueTask<string> ProcessAsync(string markdown, Func<string, ValueTask<string>> snippetProvider)
{
async ValueTask<string> CodeTagFormatter(Match match)
{
return await BuildResult(snippetProvider, match, _snippetFormat);
}

async ValueTask<string> ExampleTagFormatter(Match match)
{
return await BuildResult(snippetProvider, match, _snippetExampleFormat);
}

string result = await _snippetRegex.ReplaceAsync(markdown, CodeTagFormatter);
return result != markdown ? result : await _snippetExampleRegex.ReplaceAsync(markdown, ExampleTagFormatter);
}

private static async ValueTask<string> BuildResult(Func<string, ValueTask<string>> snippetProvider, Match match, string format)
{
var name = match.Groups["name"].Value;
var prefix = match.Groups["indent"].Value + "///";

var snippetText = await snippetProvider(name);

var builder = new StringBuilder();
foreach (var line in snippetText.Split(Environment.NewLine))
{
builder.Append(prefix);
if (!string.IsNullOrWhiteSpace(line))
{
builder.Append(" ");
}

builder.AppendLine(SecurityElement.Escape(line));
}

if (builder.Length > 0)
{
builder.Length -= Environment.NewLine.Length;
}

return string.Format(format, name, Environment.NewLine, builder, prefix);
}
}
}