Skip to content

Commit ef88f7b

Browse files
authored
Merge pull request #5666 from vladimir-shcherbakov/iss#4516
Tool to generate format.ps1xml
2 parents 9a25b3a + 4c03d97 commit ef88f7b

31 files changed

+1757
-9
lines changed

TestMappings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,8 @@
203203
],
204204
"tools/BuildPackagesTask/": [
205205
".\\tools\\BuildPackagesTask\\Microsoft.Azure.Build.Tasks.Test\\bin\\Debug\\Microsoft.Azure.Build.Tasks.Test.dll"
206+
],
207+
"tools/RepoTasks/": [
208+
".\\tools\\RepoTasks\\RepoTasks.Cmdlets.Tests\\bin\\Debug\\RepoTasks.Cmdlets.Tests.dll"
206209
]
207210
}

build.proj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171

7272
<StaticAnalysis Include=".\tools\StaticAnalysis\StaticAnalysis.sln" />
7373
<LocalBuildTasks Include="$(LibraryToolsFolder)\BuildPackagesTask\Microsoft.Azure.Build.Tasks.sln" />
74+
<LocalBuildTasks Include="$(LibraryToolsFolder)\RepoTasks\RepoTasks.sln" />
7475
</ItemGroup>
7576

7677
<!-- Tasks -->
@@ -148,6 +149,7 @@
148149
<NuGetRestoreConfigSwitch>-ConfigFile &quot;$(NuGetRestoreConfigFile)&quot;</NuGetRestoreConfigSwitch>
149150
<NuGetRestorePackageSetting>-PackagesDirectory $(LibraryNugetPackageFolder)</NuGetRestorePackageSetting>
150151
<NuGetLocalBuildTaskPackages>-PackagesDirectory $(LibraryToolsFolder)\BuildPackagesTask\packages</NuGetLocalBuildTaskPackages>
152+
<NuGetRepoTasksPackages>-PackagesDirectory $(LibraryToolsFolder)\RepoTasks\packages</NuGetRepoTasksPackages>
151153

152154
<PowerShellCommand Condition=" '$(PowerShellCommand)' == '' ">C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellCommand>
153155
<PowerShellCoreCommand Condition=" '$(PowerShellCoreCommand)' == '' ">pwsh</PowerShellCoreCommand>
@@ -182,6 +184,11 @@
182184
<!-- Restore packages for local build tasks-->
183185
<Exec Command="$(NuGetCommand) restore %(LocalBuildTasks.FullPath) $(NuGetRestoreConfigSwitch) $(NuGetLocalBuildTaskPackages)"
184186
ContinueOnError="false" />
187+
<!-- Restore packages for repo tasks tasks-->
188+
<Exec Command="$(NuGetCommand) restore %(LocalBuildTasks.FullPath) $(NuGetRestoreConfigSwitch) $(NuGetRepoTasksPackages)"
189+
ContinueOnError="false" />
190+
<Exec Command="$(NuGetCommand) restore %(SetupSln.FullPath) $(NuGetRestoreConfigSwitch) $(NuGetRestorePackageSetting)"
191+
ContinueOnError="false" />
185192

186193
<!--Restore the xunit runner needed to run unit tests-->
187194
<Exec Command="$(NuGetCommand) restore $(MSBuildProjectDirectory)\packages.config -PackagesDirectory $(MSBuildProjectDirectory)\packages" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright Microsoft Corporation
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
// Unless required by applicable law or agreed to in writing, software
7+
// distributed under the License is distributed on an "AS IS" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
11+
// ----------------------------------------------------------------------------------
12+
13+
using System;
14+
15+
namespace Microsoft.WindowsAzure.Commands.Common.Attributes
16+
{
17+
[Flags]
18+
public enum ViewControl
19+
{
20+
None = 0,
21+
Table,
22+
List,
23+
All = Table | List,
24+
}
25+
26+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
27+
public sealed class Ps1XmlAttribute : Attribute
28+
{
29+
public string Label { get; set; }
30+
31+
public ViewControl Target { get; set; } = ViewControl.Table;
32+
}
33+
}

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<Reference Include="System.Xml" />
112112
</ItemGroup>
113113
<ItemGroup>
114+
<Compile Include="Attributes\Ps1XmlAttribute.cs" />
114115
<Compile Include="LongRunningJobCancelledException.cs" />
115116
<Compile Include="ShouldMethodInvoker.cs" />
116117
<Compile Include="AzureLongRunningJob.cs" />

src/ResourceManager/Common/Commands.Common.Authentication.ResourceManager/Models/PSAzureSubscription.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Management.Automation;
20+
using Microsoft.WindowsAzure.Commands.Common.Attributes;
2021

2122
namespace Microsoft.Azure.Commands.Profile.Models
2223
{
@@ -86,19 +87,16 @@ public PSAzureSubscription(PSObject other)
8687
this.PopulateExtensions(other);
8788
}
8889

89-
/// <summary>
90-
/// The subscription id.
91-
/// </summary>
90+
/// <inheritdoc />
91+
[Ps1Xml(Label = "Subscription Id", Target = ViewControl.Table)]
9292
public string Id { get; set; }
9393

94-
/// <summary>
95-
/// The name of the subscription.
96-
/// </summary>
94+
/// <inheritdoc />
95+
[Ps1Xml(Label = "Subscription Name", Target = ViewControl.Table)]
9796
public string Name { get; set; }
9897

99-
/// <summary>
100-
/// Gets or sets subscription State
101-
/// </summary>
98+
/// <inheritdoc />
99+
[Ps1Xml]
102100
public string State { get; set; }
103101

104102
/// <summary>
@@ -109,6 +107,7 @@ public PSAzureSubscription(PSObject other)
109107
/// <summary>
110108
/// The tenant home for the subscription.
111109
/// </summary>
110+
[Ps1Xml(Label = "Tenant Id", Target = ViewControl.Table)]
112111
public string TenantId
113112
{
114113
get

tools/RepoTasks/NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<config>
4+
<add key="repositorypath" value="..\..\src\packages" />
5+
</config>
6+
</configuration>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("RepoTasks.Attributes")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("HP Inc.")]
12+
[assembly: AssemblyProduct("RepoTasks.Attributes")]
13+
[assembly: AssemblyCopyright("Copyright © HP Inc. 2018")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("8fa4a3ff-cb2c-48d9-9b44-a6e01c75fc2a")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// ----------------------------------------------------------------------------------
2+
// Copyright Microsoft Corporation
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ----------------------------------------------------------------------------------
13+
14+
namespace RepoTasks.Cmdlets.Tests
15+
{
16+
using CmdletsForTest;
17+
using System;
18+
using System.Collections.Generic;
19+
using RemoteWorker;
20+
using Xunit;
21+
using System.IO;
22+
23+
public class AppDomainWorkerShould : IDisposable
24+
{
25+
private readonly AppDomain _domain;
26+
private readonly IReflectionWorker _reflectionWorker;
27+
private const string ExpectedAssemblyName = "RepoTasks.CmdletsForTest";
28+
29+
// Setup
30+
public AppDomainWorkerShould()
31+
{
32+
var setup = new AppDomainSetup
33+
{
34+
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
35+
};
36+
37+
_domain = AppDomain.CreateDomain("AppDomainIsolation: " + Guid.NewGuid(), null, setup);
38+
var type = typeof(AppDomainWorker);
39+
Assert.NotNull(type.FullName);
40+
41+
_reflectionWorker = (IReflectionWorker)_domain.CreateInstanceFromAndUnwrap(type.Assembly.Location, type.FullName);
42+
}
43+
44+
// TearDown
45+
public void Dispose()
46+
{
47+
if (_domain != null) AppDomain.Unload(_domain);
48+
}
49+
50+
[Fact]
51+
[Trait(Category.AcceptanceType, Category.CheckIn)]
52+
public void ReturnCmdletTypesAndAssemblyNameWithCmdlets()
53+
{
54+
var cmdlets = new[] {"Test-Dummy"};
55+
ReturnCmdletTypesAndAssemblyName(cmdlets);
56+
}
57+
58+
[Fact]
59+
[Trait(Category.AcceptanceType, Category.CheckIn)]
60+
public void ReturnCmdletTypesAndAssemblyNameWithoutCmdlets()
61+
{
62+
ReturnCmdletTypesAndAssemblyName(null);
63+
}
64+
65+
[Fact]
66+
[Trait(Category.AcceptanceType, Category.CheckIn)]
67+
public void ReturnCmdletTypesAndAssemblyNameWithEmptyCmdlets()
68+
{
69+
var cmdlets = new string[] {};
70+
ReturnCmdletTypesAndAssemblyName(cmdlets);
71+
}
72+
73+
[Fact]
74+
[Trait(Category.AcceptanceType, Category.CheckIn)]
75+
public void BuildConfiguration()
76+
{
77+
var cmdlets = new[] { "Test-Dummy" };
78+
var types = ReturnCmdletTypesAndAssemblyName(cmdlets);
79+
var conf = new AppDomainWorker().BuildXmlConfiguration(types);
80+
Assert.Equal(4, conf.ViewDefinitions.Views.Length);
81+
82+
var view0 = conf.ViewDefinitions.Views[0];
83+
Assert.Equal($"{ExpectedAssemblyName}.Models.PsDummyOutput1", view0?.ViewSelectedBy?.TypeName);
84+
85+
var expecterProps1 = new[] { "RequestId", "StatusCode", "Id", "Name", "Type" };
86+
var columnItems = view0?.TableControl?.TableRowEntries[0]?.TableColumnItems;
87+
Assert.NotNull(columnItems);
88+
Assert.Equal(expecterProps1.Length, columnItems.Length);
89+
foreach (var expectedProp in expecterProps1)
90+
{
91+
Assert.Contains(columnItems, ci => ci.PropertyName == expectedProp);
92+
}
93+
}
94+
95+
[Fact]
96+
[Trait(Category.AcceptanceType, Category.CheckIn)]
97+
public void DoWorkRemotelyAndReturnFilname()
98+
{
99+
var assemblyFileName = Path.GetFileName(typeof(TestDummyCommand).Assembly.Location);
100+
var assemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, assemblyFileName);
101+
var cmdlets = new[] { "Test-Dummy" };
102+
var filename =_reflectionWorker.BuildFormatPs1Xml(assemblyPath, cmdlets, false);
103+
Assert.NotNull(filename);
104+
Assert.Equal($"{ExpectedAssemblyName}.generated.format.ps1xml", filename);
105+
}
106+
107+
private static IList<Type> ReturnCmdletTypesAndAssemblyName(string[] cmdlets)
108+
{
109+
var assemblyPath = typeof(TestDummyCommand).Assembly.Location;
110+
string assembyName;
111+
var types = AppDomainWorker.GetCmdletTypes(assemblyPath, cmdlets, out assembyName);
112+
Assert.Equal($"{ExpectedAssemblyName}", assembyName);
113+
Assert.True(types.Count >= 1);
114+
Assert.Contains(types, t => t.FullName == $"{ExpectedAssemblyName}.TestDummyCommand");
115+
if (cmdlets != null && cmdlets?.Length == 1)
116+
{
117+
Assert.DoesNotContain(types, t => t.FullName == $"{ExpectedAssemblyName}.TestDummyTwoCommand");
118+
}
119+
120+
return types;
121+
}
122+
}
123+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RepoTasks.Cmdlets.Tests
2+
{
3+
class Category
4+
{
5+
public const string AcceptanceType = "AcceptanceType";
6+
public const string CheckIn = "CheckIn";
7+
}
8+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
@{
2+
3+
# Script module or binary module file associated with this manifest.
4+
RootModule = ''
5+
6+
# Version number of this module.
7+
ModuleVersion = '3.4.0'
8+
9+
# ID used to uniquely identify this module
10+
GUID = '0a83c907-1ffb-4d87-a492-c65ac7d7ed37'
11+
12+
# Author of this module
13+
Author = 'Microsoft Corporation'
14+
15+
# Company or vendor of this module
16+
CompanyName = 'Microsoft Corporation'
17+
18+
# Copyright statement for this module
19+
Copyright = 'Microsoft Corporation. All rights reserved.'
20+
21+
# Description of the functionality provided by this module
22+
Description = 'Test'
23+
24+
# Minimum version of the Windows PowerShell engine required by this module
25+
PowerShellVersion = '3.0'
26+
27+
# Name of the Windows PowerShell host required by this module
28+
# PowerShellHostName = ''
29+
30+
# Minimum version of the Windows PowerShell host required by this module
31+
# PowerShellHostVersion = ''
32+
33+
# Minimum version of Microsoft .NET Framework required by this module
34+
DotNetFrameworkVersion = '4.5.2'
35+
36+
# Minimum version of the common language runtime (CLR) required by this module
37+
CLRVersion = '4.0'
38+
39+
# Processor architecture (None, X86, Amd64) required by this module
40+
# ProcessorArchitecture = ''
41+
42+
# Modules that must be imported into the global environment prior to importing this module
43+
RequiredModules = @()
44+
45+
# Assemblies that must be loaded prior to importing this module
46+
# RequiredAssemblies = @()
47+
48+
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
49+
# ScriptsToProcess = @()
50+
51+
# Type files (.ps1xml) to be loaded when importing this module
52+
#TypesToProcess = 'Dummy.Types.ps1xml'
53+
54+
# Format files (.ps1xml) to be loaded when importing this module
55+
#FormatsToProcess = ''
56+
57+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
58+
NestedModules = @('.\RepoTasks.CmdletsForTest.dll')
59+
60+
# Functions to export from this module
61+
# FunctionsToExport = @()
62+
63+
# Cmdlets to export from this module
64+
CmdletsToExport = '*'
65+
66+
# Variables to export from this module
67+
# VariablesToExport = @()
68+
69+
# Aliases to export from this module
70+
AliasesToExport = ''
71+
72+
# DSC resources to export from this module
73+
# DscResourcesToExport = @()
74+
75+
# List of all modules packaged with this module
76+
# ModuleList = @()
77+
78+
# List of all files packaged with this module
79+
# FileList = @()
80+
81+
# HelpInfo URI of this module
82+
# HelpInfoURI = ''
83+
84+
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
85+
# DefaultCommandPrefix = ''
86+
87+
}

0 commit comments

Comments
 (0)