Skip to content

Commit

Permalink
allow to customize the file header content (#5091)
Browse files Browse the repository at this point in the history
* allow to customize the file header content

* add some documents

* fix test failure
  • Loading branch information
ArcturusZhang authored Sep 27, 2024
1 parent 2ebc3ed commit 1e93f14
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ___
- [Change operation accessibility in bulk](#change-operation-accessibility-in-bulk)
- [Exclude models from namespace](#exclude-models-from-namespace)
- [Extending a model with additional constructors](#extending-a-model-with-additional-constructors)
- [Change the file license header](#change-the-file-license-header)
- [Management plane concepts and configurations](#management-plane-concepts-and-configurations)

<!-- /TOC -->
Expand Down Expand Up @@ -1788,6 +1789,16 @@ namespace Azure.Service.Models

</details>

### Change the file license header

<details>
Add the `--custom-header="// Your header content\n"` option to the autorest command, or add the following configuration in your `md` file for autorest
```yaml
custom-header: "// Your header content\n"
```
to change the file license headers in all generated files.
</details>
<details>
<summary>Repository-specific pipeline configuration</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/AutoRest.CSharp/Common/Input/AzureApiTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public override ValueExpression GetKeySampleExpression(string clientName)

public override ExtensibleSnippets ExtensibleSnippets { get; } = new AzureExtensibleSnippets();

public override string LicenseString => """
public override string LicenseString => Configuration.CustomHeader ?? """
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Expand Down
17 changes: 12 additions & 5 deletions src/AutoRest.CSharp/Common/Input/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static class Options
public const string EnableInternalRawData = "enable-internal-raw-data";
public const string HelperNamespace = "helper-namespace";
public const string DisableXmlDocs = "disable-xml-docs";
public const string CustomHeader = "custom-header";
}

public enum UnreferencedTypesHandlingOption
Expand Down Expand Up @@ -117,7 +118,8 @@ public static void Initialize(
bool generateSampleProject,
bool generateTestProject,
string? examplesDirectory,
string? helperNamespace)
string? helperNamespace,
string? customHeader)
{
_outputFolder = outputFolder;
_namespace = ns;
Expand Down Expand Up @@ -191,6 +193,7 @@ public static void Initialize(
GenerateSampleProject = DisableXmlDocs ? false : generateSampleProject; // turn off the samples if all xml docs are explicitly disabled
GenerateTestProject = generateTestProject;
ExamplesDirectory = examplesDirectory;
CustomHeader = customHeader;
_helperNamespace = helperNamespace ?? Namespace;
}

Expand Down Expand Up @@ -249,6 +252,8 @@ internal static (string AbsoluteProjectFolder, string RelativeProjectFolder) Par
private static string? _helperNamespace;
public static string HelperNamespace => _helperNamespace ?? throw new InvalidOperationException("Configuration has not been initialized");

public static string? CustomHeader { get; private set; }

public static bool DisableXmlDocs { get; private set; }

public static bool GenerateSampleProject { get; private set; }
Expand Down Expand Up @@ -396,7 +401,8 @@ public static void Initialize(IPluginCommunication autoRest, string defaultNames
generateTestProject: GetOptionBoolValue(autoRest, Options.GenerateTestProject),
examplesDirectory: null, // TODO -- what we put here?
helperNamespace: autoRest.GetValue<string?>(Options.HelperNamespace).GetAwaiter().GetResult(),
disableXmlDocs: GetOptionBoolValue(autoRest, Options.DisableXmlDocs)
disableXmlDocs: GetOptionBoolValue(autoRest, Options.DisableXmlDocs),
customHeader: autoRest.GetValue<string?>(Options.CustomHeader).GetAwaiter().GetResult()
);
}

Expand Down Expand Up @@ -569,12 +575,12 @@ internal static void LoadConfiguration(JsonElement root, string? projectPath, st
mgmtConfiguration: MgmtConfiguration.LoadConfiguration(root),
mgmtTestConfiguration: MgmtTestConfiguration.LoadConfiguration(root),
flavor: ReadStringOption(root, Options.Flavor),
disableXmlDocs: ReadOption(root, Options.DisableXmlDocs)
,
disableXmlDocs: ReadOption(root, Options.DisableXmlDocs),
generateSampleProject: ReadOption(root, Options.GenerateSampleProject),
generateTestProject: ReadOption(root, Options.GenerateTestProject),
examplesDirectory: ReadStringOption(root, Options.ExamplesDirectory),
helperNamespace: ReadStringOption(root, Options.HelperNamespace));
helperNamespace: ReadStringOption(root, Options.HelperNamespace),
customHeader: ReadStringOption(root, Options.CustomHeader));
}

internal static string SaveConfiguration()
Expand Down Expand Up @@ -644,6 +650,7 @@ private static void WriteConfiguration(Utf8JsonWriter writer)
WriteIfNotDefault(writer, Options.GenerateTestProject, GenerateTestProject);
WriteIfNotDefault(writer, Options.HelperNamespace, HelperNamespace);
WriteIfNotDefault(writer, Options.DisableXmlDocs, DisableXmlDocs);
WriteIfNotDefault(writer, Options.CustomHeader, CustomHeader);

writer.WriteEndObject();
}
Expand Down
3 changes: 1 addition & 2 deletions src/AutoRest.CSharp/Common/Input/SystemApiTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions;
using AutoRest.CSharp.Common.Output.Expressions.System;
using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;
using AutoRest.CSharp.Common.Output.Models.Types.HelperTypeProviders;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Generation.Writers;
using AutoRest.CSharp.Output.Models;
Expand Down Expand Up @@ -99,7 +98,7 @@ public override ValueExpression GetKeySampleExpression(string clientName)

public override ExtensibleSnippets ExtensibleSnippets { get; } = new SystemExtensibleSnippets();

public override string LicenseString => string.Empty;
public override string LicenseString => Configuration.CustomHeader ?? string.Empty;

public override string ResponseClassifierIsErrorResponseName => nameof(PipelineMessageClassifier.TryClassify);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public void Method(int? nullableInt, List<int> intList, List<int?> nullableIntLi
generateSampleProject: true,
generateTestProject: true,
examplesDirectory: null,
helperNamespace: "");
helperNamespace: "",
customHeader: null);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public void Setup()
generateSampleProject: true,
generateTestProject: true,
examplesDirectory: null,
helperNamespace: "");
helperNamespace: "",
customHeader: null);
}

private void TestPair(ResourceMatchType expected, string httpMethod, string resourcePathStr, string requestPathStr, bool isList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public void Initialize()
generateSampleProject: true,
generateTestProject: true,
examplesDirectory: null,
helperNamespace: "");
helperNamespace: "",
customHeader: null);
}


Expand Down

0 comments on commit 1e93f14

Please sign in to comment.