Skip to content

Commit

Permalink
generate assemblyinfo.cs for new module (#1376)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
VeryEarly committed Sep 19, 2024
1 parent a317aa5 commit f925a81
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
2 changes: 2 additions & 0 deletions powershell/generators/gitignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ tools
custom/*.psm1
custom/autogen-model-cmdlets
test/*-TestResults.xml
license.txt
/*.ps1
/*.psd1
/*.ps1xml
/*.psm1
/*.snk
Expand Down
11 changes: 11 additions & 0 deletions powershell/internal/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -340,6 +346,11 @@ export class Project extends codeDomProject {
const afterBuildTasksArgsDictionary: Dictionary<string> = await this.state.getValue<Dictionary<string>>('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 = <Array<string>>(
values(
Expand Down
3 changes: 3 additions & 0 deletions powershell/plugins/powershell-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export async function powershellV2(service: Host | TspHost, state?: ModelState<P
await project.state.protectFiles(project.examplesFolder);
await project.state.protectFiles(project.resourcesFolder);
await project.state.protectFiles(project.uxFolder);
if (project.azure) {
await project.state.protectFiles(project.assemblyInfoFolder);
}

// wait for all the generation to be done
await copyRequiredFiles(project);
Expand Down
8 changes: 7 additions & 1 deletion powershell/resources/assets/build-module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,17 @@ if (Test-Path (Join-Path $PSScriptRoot 'generate-portal-ux.ps1'))
. (Join-Path $PSScriptRoot 'generate-portal-ux.ps1')
}

$assemblyInfoPath = Join-Path $PSScriptRoot 'Properties' 'AssemblyInfo.cs'
if (-not (Test-Path $assemblyInfoPath) -And [System.Convert]::ToBoolean('${$project.azure}')) {
Write-Host -ForegroundColor Green 'Creating assembly info...'
New-AssemblyInfo
}

if (-not $DisableAfterBuildTasks){
$afterBuildTasksPath = Join-Path $PSScriptRoot '${$project.afterBuildTasksPath}'
$afterBuildTasksArgs = ConvertFrom-Json '${$project.afterBuildTasksArgs}' -AsHashtable
if(Test-Path -Path $afterBuildTasksPath -PathType leaf){
Write-Host -ForegroundColor Green 'Running after build tasks...'
Write-Host -ForegroundColor Green 'Executing after build tasks...'
. $afterBuildTasksPath @afterBuildTasksArgs
}
}
Expand Down
1 change: 1 addition & 0 deletions powershell/resources/built-time-cmdlets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="psruntime\BuildTime\Cmdlets\ExportExampleStub.cs" />
<Compile Include="psruntime\BuildTime\Cmdlets\GetModuleGuid.cs" />
<Compile Include="psruntime\BuildTime\Cmdlets\ExportPsd1.cs" />
<Compile Include="psruntime\BuildTime\Cmdlets\NewAssemblyInfo.cs" />
<Compile Include="psruntime\BuildTime\Cmdlets\GetCommonParameter.cs" />
<Compile Include="psruntime\BuildTime\Models\PsHelpMarkdownOutputs.cs" />
<Compile Include="psruntime\BuildTime\Models\PsMarkdownTypes.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}

0 comments on commit f925a81

Please sign in to comment.