Skip to content

Commit

Permalink
Update snippet-generator (Azure#33665)
Browse files Browse the repository at this point in the history
Fixes Azure/azure-sdk-tools#1991 while making sure snippet-generator is
always updated. It wasn't before because `dotnet tool install` will not
upgrade an installed tool and the .NET SDK team decided to add `dotnet
tool update` in favor of adding a `--force` to `dotnet tool install`,
thus making installation and updating more complicated.

Instead, `dotnet tool restore` does both and is fast if the tool is
already installed. Additionally, running most (or any?) `dotnet`
commands restores the tool automatically.
  • Loading branch information
heaths authored Jan 25, 2023
1 parent cbb6ae7 commit 2fb2e19
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 45 deletions.
6 changes: 6 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"version": 1,
"isRoot": true,
"tools": {
"Azure.Sdk.Tools.SnippetGenerator": {
"version": "1.0.0-dev.20230124.1",
"commands": [
"snippet-generator"
]
},
"dotnet-reportgenerator-globaltool": {
"version": "4.8.0",
"commands": [
Expand Down
2 changes: 1 addition & 1 deletion eng/pipelines/templates/jobs/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ jobs:
inputs:
command: custom
custom: 'tool'
arguments: 'install --global --add-source "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json" --version "1.0.0-dev.20211119.1" "Azure.Sdk.Tools.SnippetGenerator"'
arguments: 'restore'
workingDirectory: '$(Agent.BuildDirectory)'

- task: PowerShell@2
Expand Down
38 changes: 9 additions & 29 deletions eng/scripts/Update-Snippets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,23 @@ param (
[string] $ServiceDirectory,

[Parameter()]
[switch] $StrictMode,

[Parameter()]
[string] $SnippetToolPath=''
[switch] $StrictMode = !(Test-Path Env:TF_BUILD)
)

$root = "$PSScriptRoot/../../sdk"

# special casing * here because single invocation of SnippetGenerator is much faster than
# running it per service directory
if ($ServiceDirectory -and ($ServiceDirectory -ne "*")) {
if ($ServiceDirectory -and ($ServiceDirectory -ne '*')) {
$root += '/' + $ServiceDirectory
}

if (-not (Test-Path Env:TF_BUILD))
{
$StrictMode = $true

dotnet tool install --global `
--add-source "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json" `
--version "1.0.0-dev.20211119.1" `
"Azure.Sdk.Tools.SnippetGenerator"
}
else
{
if ($Env:AGENT_OS -match "Windows.*")
{
$SnippetToolPath = "%USERPROFILE%\.dotnet\tools\"
}
else
{
$SnippetToolPath = "$HOME/.dotnet/tools/"
}
[string[]] $additionalArgs = @()
if ($StrictMode) {
$additionalArgs += '-sm'
}

if($StrictMode) {
Resolve-Path "$root" | %{ & "${SnippetToolPath}snippet-generator" -b "$_" -sm}
} else {
Resolve-Path "$root" | %{ & "${SnippetToolPath}snippet-generator" -b "$_" }
}
dotnet tool restore
Resolve-Path $root | ForEach-Object {
dotnet tool run snippet-generator -b "$_" $additionalArgs
}
1 change: 1 addition & 0 deletions sdk/template/.content/packageResource/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ You can familiarize yourself with different APIs using [Samples](https://github.
You can create a client and call the client's `<operation>` method.

```C# Snippet:Azure_Template_Scenario
Console.WriteLine("Hello, world!");
```

## Troubleshooting
Expand Down
7 changes: 2 additions & 5 deletions sdk/template/.content/samples/Sample1_HelloWorld.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ To use these samples, you'll first need to set up resources. See [getting starte

You can create a client and call the client's `<operation>` method

<!-- please refer to <SampleReadmeLink> to write sample readme file. -->
```C# Snippet:Azure_Template_Scenario
Console.WriteLine("Hello, world!");
```

To see the full example source files, see:
* [HelloWorld](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/template/Azure.Template/tests/Samples/Sample1_HelloWorld.cs))

<!-- please refer to <SampleReadmeLink> to write sample readme file. -->
7 changes: 2 additions & 5 deletions sdk/template/.content/samples/Sample1_HelloWorldAsync.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ To use these samples, you'll first need to set up resources. See [getting starte

You can create a client and call the client's `<operation>` method

<!-- please refer to <AsyncSampleReadmeLink> to write sample readme file. -->
```C# Snippet:Azure_Template_ScenarioAsync
Console.WriteLine("Hello, world!");
```

To see the full example source files, see:
* [HelloWorld](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/template/Azure.Template/tests/Samples/Sample1_HelloWorldAsync.cs))

<!-- please refer to <AsyncSampleReadmeLink> to write sample readme file. -->
11 changes: 8 additions & 3 deletions sdk/template/.content/tests/Samples/Sample1_HelloWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ namespace Azure.Template.Tests.Samples
public partial class TemplateSamples: SamplesBase<TemplateClientTestEnvironment>
{
/* please refer to SamplesLink to write samples. */
#region Snippet:Azure_Template_Scenario

#endregion
[Test]
[SyncOnly]
public void Scenario()
{
#region Snippet:Azure_Template_Scenario
Console.WriteLine("Hello, world!");
#endregion
}
}
}
11 changes: 9 additions & 2 deletions sdk/template/.content/tests/Samples/Sample1_HelloWorldAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ namespace Azure.Template.Tests.Samples
public partial class TemplateSamples: SamplesBase<TemplateClientTestEnvironment>
{
/* please refer to AsyncSamplesLink to write samples. */
#region Snippet:Azure_Template_ScenarioAsync
[Test]
[AsyncOnly]
public async Task ScenarioAsync()
{
#region Snippet:Azure_Template_ScenarioAsync
Console.WriteLine("Hello, world!");
#endregion

#endregion
await Task.Yield();
}
}
}

0 comments on commit 2fb2e19

Please sign in to comment.