pulumi/pkg/codegen/dotnet/templates.go

216 lines
7.6 KiB
Go
Raw Normal View History

// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
sdk/go: Remove 'nolint' directives from package docs Go treats comments that match the following regex as directives. //[a-z0-9]+:[a-z0-9] Comments that are directives don't show in an entity's documentation. https://github.com/golang/go/commit/5a550b695117f07a4f2454039a4871250cd3ed09#diff-f56160fd9fcea272966a8a1d692ad9f49206fdd8dbcbfe384865a98cd9bc2749R165 Our code has `//nolint` directives that now show in the API Reference. This is because these directives are in one of the following forms, which don't get this special treatment. // nolint:foo //nolint: foo This change fixes all such directives found by the regex: `// nolint|//nolint: `. See bottom of commit for command used for the fix. Verification: Here's the output of `go doc` on some entities before and after this change. Before ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi | head -n8 package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" nolint: lll, interfacer nolint: lll, interfacer const EnvOrganization = "PULUMI_ORGANIZATION" ... var ErrPlugins = errors.New("pulumi: plugins requested") ``` After ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi | head -n8 package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" const EnvOrganization = "PULUMI_ORGANIZATION" ... var ErrPlugins = errors.New("pulumi: plugins requested") func BoolRef(v bool) *bool func Float64Ref(v float64) *float64 func IntRef(v int) *int func IsSecret(o Output) bool ``` Before ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi URN_ package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" func URN_(o string) ResourceOption URN_ is an optional URN of a previously-registered resource of this type to read from the engine. nolint: revive ``` After: ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi URN_ package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" func URN_(o string) ResourceOption URN_ is an optional URN of a previously-registered resource of this type to read from the engine. ``` Note that golangci-lint offers a 'nolintlint' linter that finds such miuses of nolint, but it also finds other issues so I've deferred that to a follow up PR. Resolves #11785 Related: https://github.com/golangci/golangci-lint/issues/892 [git-generate] FILES=$(mktemp) rg -l '// nolint|//nolint: ' | tee "$FILES" | xargs perl -p -i -e ' s|// nolint|//nolint|g; s|//nolint: |//nolint:|g; ' rg '.go$' < "$FILES" | xargs gofmt -w -s
2023-01-06 00:07:45 +00:00
//nolint:lll
package dotnet
import (
[codegen/dotnet] Fixes to version.txt (#6398) We currently include `version.txt` in the package via `<Content Include="version.txt">`. This places the file in the NuGet package in two locations: the `content` directory and `contentFiles`. We want the file to be in `content` because this is where the Pulumi .NET Language host looks for the file when determining a program's required plugins. However, having the file in `contentFiles` is problematic. For packages that reference other Pulumi resource packages (e.g. for multi-lang components), referenced `contentFiles` are included in the package by default. This means another package's `version.txt` will be included and used over the current project's `version.txt`. For example, if a multi-lang component package `Pulumi.Xyz` references `Pulumi.Aws`, the `version.txt` from `Pulumi.Aws` will be used in `Pulumi.Xyz` package rather than the intended `version.txt` for `Pulumi.Xyz`. To address this, stop including `version.txt` in `contentFiles`. We do this by changing the `<Content Include="version.txt" />` to `<None Include="version.txt" Pack="True" PackagePath="content" />` in the project file. This ensures the file will still be included in the package in the `content` dir (where the Pulumi .NET language host expects to find it), but doesn't include the file in `contentFiles` which would cause it to be used in other packages that reference the package. Further, when generating `<PackageReference>`s for packages that start with "Pulumi.", include an additional `ExcludeAssets="contentFiles"` attribute to prevent the package's `contentFiles` from being included.
2021-02-22 16:33:16 +00:00
"strings"
"text/template"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)
sdk/go: Remove 'nolint' directives from package docs Go treats comments that match the following regex as directives. //[a-z0-9]+:[a-z0-9] Comments that are directives don't show in an entity's documentation. https://github.com/golang/go/commit/5a550b695117f07a4f2454039a4871250cd3ed09#diff-f56160fd9fcea272966a8a1d692ad9f49206fdd8dbcbfe384865a98cd9bc2749R165 Our code has `//nolint` directives that now show in the API Reference. This is because these directives are in one of the following forms, which don't get this special treatment. // nolint:foo //nolint: foo This change fixes all such directives found by the regex: `// nolint|//nolint: `. See bottom of commit for command used for the fix. Verification: Here's the output of `go doc` on some entities before and after this change. Before ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi | head -n8 package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" nolint: lll, interfacer nolint: lll, interfacer const EnvOrganization = "PULUMI_ORGANIZATION" ... var ErrPlugins = errors.New("pulumi: plugins requested") ``` After ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi | head -n8 package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" const EnvOrganization = "PULUMI_ORGANIZATION" ... var ErrPlugins = errors.New("pulumi: plugins requested") func BoolRef(v bool) *bool func Float64Ref(v float64) *float64 func IntRef(v int) *int func IsSecret(o Output) bool ``` Before ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi URN_ package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" func URN_(o string) ResourceOption URN_ is an optional URN of a previously-registered resource of this type to read from the engine. nolint: revive ``` After: ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi URN_ package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" func URN_(o string) ResourceOption URN_ is an optional URN of a previously-registered resource of this type to read from the engine. ``` Note that golangci-lint offers a 'nolintlint' linter that finds such miuses of nolint, but it also finds other issues so I've deferred that to a follow up PR. Resolves #11785 Related: https://github.com/golangci/golangci-lint/issues/892 [git-generate] FILES=$(mktemp) rg -l '// nolint|//nolint: ' | tee "$FILES" | xargs perl -p -i -e ' s|// nolint|//nolint|g; s|//nolint: |//nolint:|g; ' rg '.go$' < "$FILES" | xargs gofmt -w -s
2023-01-06 00:07:45 +00:00
//nolint:lll
const csharpUtilitiesTemplateText = `// *** WARNING: this file was generated by {{.Tool}}. ***
// *** Do not edit by hand unless you're certain you know what you are doing! ***
namespace {{.Namespace}}
{
static class {{.ClassName}}
{
public static string? GetEnv(params string[] names)
{
foreach (var n in names)
{
var value = global::System.Environment.GetEnvironmentVariable(n);
if (value != null)
{
return value;
}
}
return null;
}
static string[] trueValues = { "1", "t", "T", "true", "TRUE", "True" };
static string[] falseValues = { "0", "f", "F", "false", "FALSE", "False" };
public static bool? GetEnvBoolean(params string[] names)
{
var s = GetEnv(names);
if (s != null)
{
if (global::System.Array.IndexOf(trueValues, s) != -1)
{
return true;
}
if (global::System.Array.IndexOf(falseValues, s) != -1)
{
return false;
}
}
return null;
}
public static int? GetEnvInt32(params string[] names) => int.TryParse(GetEnv(names), out int v) ? (int?)v : null;
public static double? GetEnvDouble(params string[] names) => double.TryParse(GetEnv(names), out double v) ? (double?)v : null;
[global::System.Obsolete("Please use WithDefaults instead")]
public static global::Pulumi.InvokeOptions WithVersion(this global::Pulumi.InvokeOptions? options)
{
var dst = options ?? new global::Pulumi.InvokeOptions{};
dst.Version = options?.Version ?? Version;
return dst;
}
public static global::Pulumi.InvokeOptions WithDefaults(this global::Pulumi.InvokeOptions? src)
{
var dst = src ?? new global::Pulumi.InvokeOptions{};
dst.Version = src?.Version ?? Version;{{if ne .PluginDownloadURL "" }}
dst.PluginDownloadURL = src?.PluginDownloadURL ?? "{{.PluginDownloadURL}}";{{end}}
return dst;
}
private readonly static string version;
public static string Version => version;
static {{.ClassName}}()
{
var assembly = global::System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof({{.ClassName}})).Assembly;
using var stream = assembly.GetManifestResourceStream("{{.Namespace}}.version.txt");
using var reader = new global::System.IO.StreamReader(stream ?? throw new global::System.NotSupportedException("Missing embedded version.txt file"));
version = reader.ReadToEnd().Trim();
var parts = version.Split("\n");
if (parts.Length == 2)
{
// The first part is the provider name.
version = parts[1].Trim();
}
}
}
internal sealed class {{.Name}}ResourceTypeAttribute : global::Pulumi.ResourceTypeAttribute
{
public {{.Name}}ResourceTypeAttribute(string type) : base(type, {{.ClassName}}.Version)
{
}
}
}
`
var csharpUtilitiesTemplate = template.Must(template.New("CSharpUtilities").Parse(csharpUtilitiesTemplateText))
type csharpUtilitiesTemplateContext struct {
Name string
Namespace string
ClassName string
Tool string
PluginDownloadURL string
}
// TODO(pdg): parameterize package name
const csharpProjectFileTemplateText = `<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>{{or .Package.Publisher "Pulumi Corp."}}</Authors>
<Company>{{or .Package.Publisher "Pulumi Corp."}}</Company>
<Description>{{.Package.Description}}</Description>
<PackageLicenseExpression>{{.Package.License}}</PackageLicenseExpression>
<PackageProjectUrl>{{.Package.Homepage}}</PackageProjectUrl>
<RepositoryUrl>{{.Package.Repository}}</RepositoryUrl>
<PackageIcon>logo.png</PackageIcon>
{{- if .Version }}
<Version>{{.Version}}</Version>
{{- end }}
2023-03-02 16:34:28 +00:00
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
</PropertyGroup>
2021-04-29 02:24:43 +00:00
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
{{ if .RestoreSources }}
<PropertyGroup>
<RestoreSources>{{.RestoreSources}};$(RestoreSources)</RestoreSources>
</PropertyGroup>
{{ end }}
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="version.txt" />
[codegen/dotnet] Fixes to version.txt (#6398) We currently include `version.txt` in the package via `<Content Include="version.txt">`. This places the file in the NuGet package in two locations: the `content` directory and `contentFiles`. We want the file to be in `content` because this is where the Pulumi .NET Language host looks for the file when determining a program's required plugins. However, having the file in `contentFiles` is problematic. For packages that reference other Pulumi resource packages (e.g. for multi-lang components), referenced `contentFiles` are included in the package by default. This means another package's `version.txt` will be included and used over the current project's `version.txt`. For example, if a multi-lang component package `Pulumi.Xyz` references `Pulumi.Aws`, the `version.txt` from `Pulumi.Aws` will be used in `Pulumi.Xyz` package rather than the intended `version.txt` for `Pulumi.Xyz`. To address this, stop including `version.txt` in `contentFiles`. We do this by changing the `<Content Include="version.txt" />` to `<None Include="version.txt" Pack="True" PackagePath="content" />` in the project file. This ensures the file will still be included in the package in the `content` dir (where the Pulumi .NET language host expects to find it), but doesn't include the file in `contentFiles` which would cause it to be used in other packages that reference the package. Further, when generating `<PackageReference>`s for packages that start with "Pulumi.", include an additional `ExcludeAssets="contentFiles"` attribute to prevent the package's `contentFiles` from being included.
2021-02-22 16:33:16 +00:00
<None Include="version.txt" Pack="True" PackagePath="content" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="pulumi-plugin.json" />
<None Include="pulumi-plugin.json" Pack="True" PackagePath="content" />
</ItemGroup>
<ItemGroup>
{{- range $package, $version := .PackageReferences}}
[codegen/dotnet] Fixes to version.txt (#6398) We currently include `version.txt` in the package via `<Content Include="version.txt">`. This places the file in the NuGet package in two locations: the `content` directory and `contentFiles`. We want the file to be in `content` because this is where the Pulumi .NET Language host looks for the file when determining a program's required plugins. However, having the file in `contentFiles` is problematic. For packages that reference other Pulumi resource packages (e.g. for multi-lang components), referenced `contentFiles` are included in the package by default. This means another package's `version.txt` will be included and used over the current project's `version.txt`. For example, if a multi-lang component package `Pulumi.Xyz` references `Pulumi.Aws`, the `version.txt` from `Pulumi.Aws` will be used in `Pulumi.Xyz` package rather than the intended `version.txt` for `Pulumi.Xyz`. To address this, stop including `version.txt` in `contentFiles`. We do this by changing the `<Content Include="version.txt" />` to `<None Include="version.txt" Pack="True" PackagePath="content" />` in the project file. This ensures the file will still be included in the package in the `content` dir (where the Pulumi .NET language host expects to find it), but doesn't include the file in `contentFiles` which would cause it to be used in other packages that reference the package. Further, when generating `<PackageReference>`s for packages that start with "Pulumi.", include an additional `ExcludeAssets="contentFiles"` attribute to prevent the package's `contentFiles` from being included.
2021-02-22 16:33:16 +00:00
<PackageReference Include="{{$package}}" Version="{{$version}}"{{if ispulumipkg $package}} ExcludeAssets="contentFiles"{{end}} />
{{- end}}
</ItemGroup>
<ItemGroup>
{{- range $projdir := .ProjectReferences}}
<ProjectReference Include="{{$projdir}}" />
{{- end}}
</ItemGroup>
<ItemGroup>
<None Include="logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
`
[codegen/dotnet] Fixes to version.txt (#6398) We currently include `version.txt` in the package via `<Content Include="version.txt">`. This places the file in the NuGet package in two locations: the `content` directory and `contentFiles`. We want the file to be in `content` because this is where the Pulumi .NET Language host looks for the file when determining a program's required plugins. However, having the file in `contentFiles` is problematic. For packages that reference other Pulumi resource packages (e.g. for multi-lang components), referenced `contentFiles` are included in the package by default. This means another package's `version.txt` will be included and used over the current project's `version.txt`. For example, if a multi-lang component package `Pulumi.Xyz` references `Pulumi.Aws`, the `version.txt` from `Pulumi.Aws` will be used in `Pulumi.Xyz` package rather than the intended `version.txt` for `Pulumi.Xyz`. To address this, stop including `version.txt` in `contentFiles`. We do this by changing the `<Content Include="version.txt" />` to `<None Include="version.txt" Pack="True" PackagePath="content" />` in the project file. This ensures the file will still be included in the package in the `content` dir (where the Pulumi .NET language host expects to find it), but doesn't include the file in `contentFiles` which would cause it to be used in other packages that reference the package. Further, when generating `<PackageReference>`s for packages that start with "Pulumi.", include an additional `ExcludeAssets="contentFiles"` attribute to prevent the package's `contentFiles` from being included.
2021-02-22 16:33:16 +00:00
var csharpProjectFileTemplate = template.Must(template.New("CSharpProject").Funcs(template.FuncMap{
// ispulumipkg is used in the template to conditionally emit `ExcludeAssets="contentFiles"`
// for `<PackageReference>`s that start with "Pulumi.", to prevent the references's contentFiles
// from being included in this project's package. Otherwise, if a reference has version.txt
// in its contentFiles, and we don't exclude contentFiles for the reference, the reference's
// version.txt will be used over this project's version.txt.
"ispulumipkg": func(s string) bool {
return strings.HasPrefix(s, "Pulumi.")
},
}).Parse(csharpProjectFileTemplateText))
type csharpProjectFileTemplateContext struct {
XMLDoc string
Package *schema.Package
PackageReferences map[string]string
ProjectReferences []string
Version string
RestoreSources string
}