From f925a81c6b2c58d601739f083efc167a0af3d034 Mon Sep 17 00:00:00 2001 From: Yabo Hu Date: Thu, 19 Sep 2024 14:24:43 +0800 Subject: [PATCH] generate assemblyinfo.cs for new module (#1376) * ignore *.psd1 * ignore assemblyinfo under Properties * add cmdlet newassemblyinfo.cs to create assemblyinfo * protect assemblyinfo.cs * fix * fix * fix * only generate assemblyinfo if is azure --- powershell/generators/gitignore.ts | 2 + powershell/internal/project.ts | 11 ++++ powershell/plugins/powershell-v2.ts | 3 + powershell/resources/assets/build-module.ps1 | 8 ++- .../resources/built-time-cmdlets.csproj | 1 + .../psruntime/BuildTime/Cmdlets/ExportPsd1.cs | 3 +- .../BuildTime/Cmdlets/NewAssemblyInfo.cs | 64 +++++++++++++++++++ 7 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 powershell/resources/psruntime/BuildTime/Cmdlets/NewAssemblyInfo.cs diff --git a/powershell/generators/gitignore.ts b/powershell/generators/gitignore.ts index d4dd530ff85..783cae2547a 100644 --- a/powershell/generators/gitignore.ts +++ b/powershell/generators/gitignore.ts @@ -17,7 +17,9 @@ tools custom/*.psm1 custom/autogen-model-cmdlets test/*-TestResults.xml +license.txt /*.ps1 +/*.psd1 /*.ps1xml /*.psm1 /*.snk diff --git a/powershell/internal/project.ts b/powershell/internal/project.ts index 1748c67d107..2bafb2af86f 100644 --- a/powershell/internal/project.ts +++ b/powershell/internal/project.ts @@ -142,6 +142,7 @@ export class Project extends codeDomProject { public uxFolder!: string; public serviceName!: string; public moduleName!: string; + public title!: string; public rootModuleName!: string; public csproj!: string; public nuspec!: string; @@ -151,6 +152,10 @@ export class Project extends codeDomProject { public readme!: string; public afterBuildTasksPath!: string; public afterBuildTasksArgs!: string; + public assemblyInfoFolder!: string; + public assemblyCompany!: string; + public assemblyProduct!: string; + public assemblyCopyright!: string; public dllName!: string; public dll!: string; public psd1!: string; @@ -284,6 +289,7 @@ export class Project extends codeDomProject { this.serviceName = this.model.language.default.serviceName; this.subjectPrefix = this.model.language.default.subjectPrefix; this.moduleName = await this.state.getValue('module-name'); + this.title = await this.state.getValue('title'); this.rootModuleName = await this.state.getValue('root-module-name', ''); this.dllName = await this.state.getValue('dll-name'); // Azure PowerShell data plane configuration @@ -340,6 +346,11 @@ export class Project extends codeDomProject { const afterBuildTasksArgsDictionary: Dictionary = await this.state.getValue>('after-build-tasks-args', {}); this.afterBuildTasksArgs = JSON.stringify(afterBuildTasksArgsDictionary); + this.assemblyInfoFolder = await this.state.getValue('assemblyInfo-folder', ''); + this.assemblyCompany = await this.state.getValue('assembly-company', ''); + this.assemblyProduct = await this.state.getValue('assembly-product', ''); + this.assemblyCopyright = await this.state.getValue('assembly-copyright', ''); + // excluded properties in table view const excludedList = >( values( diff --git a/powershell/plugins/powershell-v2.ts b/powershell/plugins/powershell-v2.ts index a68a95a7229..8d3da9fac1a 100644 --- a/powershell/plugins/powershell-v2.ts +++ b/powershell/plugins/powershell-v2.ts @@ -126,6 +126,9 @@ export async function powershellV2(service: Host | TspHost, state?: ModelState

+ diff --git a/powershell/resources/psruntime/BuildTime/Cmdlets/ExportPsd1.cs b/powershell/resources/psruntime/BuildTime/Cmdlets/ExportPsd1.cs index b3833ffb925..744765b600f 100644 --- a/powershell/resources/psruntime/BuildTime/Cmdlets/ExportPsd1.cs +++ b/powershell/resources/psruntime/BuildTime/Cmdlets/ExportPsd1.cs @@ -149,7 +149,8 @@ protected override void ProcessRecord() else { var aliasesList = functionInfos.SelectMany(fi => fi.ScriptBlock.Attributes).ToAliasNames().ToPsList(); - if (!String.IsNullOrEmpty(aliasesList)) { + if (!String.IsNullOrEmpty(aliasesList)) + { sb.AppendLine($@"{Indent}AliasesToExport = {aliasesList}"); } } diff --git a/powershell/resources/psruntime/BuildTime/Cmdlets/NewAssemblyInfo.cs b/powershell/resources/psruntime/BuildTime/Cmdlets/NewAssemblyInfo.cs new file mode 100644 index 00000000000..1c65c130b43 --- /dev/null +++ b/powershell/resources/psruntime/BuildTime/Cmdlets/NewAssemblyInfo.cs @@ -0,0 +1,64 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +using System; +using System.IO; +using System.Linq; +using System.Management.Automation; +using System.Text; + +namespace Microsoft.Rest.ClientRuntime.PowerShell +{ + [Cmdlet("New", "AssemblyInfo")] + [DoNotExport] + public class NewAssemblyInfo : PSCmdlet + { + private readonly string assemblyInfoPath = Path.Combine("${$project.baseFolder}", "Properties", "AssemblyInfo.cs"); + private const string assemblyName = "${$project.title}"; + private const string assemblyVersion = "${$project.moduleVersion}"; + private const string assemblyCompanyName = "${$project.assemblyCompany}"; + private const string assemblyProduct = "${$project.assemblyProduct}"; + private const string assemblyCopyright = "${$project.assemblyCopyright}"; + protected override void ProcessRecord() + { + try + { + if (File.Exists(assemblyInfoPath)) + { + return; + } + StringBuilder sb = new StringBuilder(); + sb.AppendLine(@" +# ---------------------------------------------------------------------------------- +${$project.pwshCommentHeaderForCsharp} +# ---------------------------------------------------------------------------------- +"); + sb.Append($"{Environment.NewLine}"); + sb.AppendLine("using System;"); + sb.AppendLine("using System.Reflection;"); + sb.AppendLine("using System.Runtime.InteropServices;"); + sb.Append($"{Environment.NewLine}"); + sb.AppendLine($"[assembly: AssemblyTitle(\"Microsoft Azure Powershell - {assemblyName}\")]"); + sb.AppendLine($"[assembly: AssemblyCompany(\"{assemblyCompanyName}\")]"); + sb.AppendLine($"[assembly: AssemblyProduct(\"{assemblyProduct}\")]"); + sb.AppendLine($"[assembly: AssemblyCopyright(\"{assemblyCopyright}\")]"); + sb.Append($"{Environment.NewLine}"); + sb.AppendLine("[assembly: ComVisible(false)]"); + sb.AppendLine("[assembly: CLSCompliant(false)]"); + sb.AppendLine($"[assembly: Guid(\"{Guid.NewGuid()}\")]"); + sb.AppendLine($"[assembly: AssemblyVersion(\"{assemblyVersion}\")]"); + sb.Append($"[assembly: AssemblyFileVersion(\"{assemblyVersion}\")]"); + + FileInfo assemblyInfo = new FileInfo(assemblyInfoPath); + assemblyInfo.Directory.Create(); + File.WriteAllText(assemblyInfo.FullName, sb.ToString()); + } + catch (Exception ee) + { + Console.WriteLine($"${ee.GetType().Name}/{ee.StackTrace}"); + throw ee; + } + } + } +} \ No newline at end of file