Skip to content

Hongtu/new attribute #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Common/CustomAttributes/CmdletDeprecationAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Microsoft.WindowsAzure.Commands.Common.CustomAttributes
{
[Obsolete("CmdletDeprecationAttribute is deprecated. Please use CmdletDeprecationWithVersionAttribute instead to ensure that version information is included in the deprecation message.", false)]
[AttributeUsage(
AttributeTargets.Class,
AllowMultiple = true)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft 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.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.Common.Properties;
using System;

namespace Microsoft.WindowsAzure.Commands.Common.CustomAttributes
{
[AttributeUsage(
AttributeTargets.Class,
AllowMultiple = true)]
public class CmdletDeprecationWithVersionAttribute : GenericBreakingChangeAttribute
{
public string ReplacementCmdletName { get; set; }

public CmdletDeprecationWithVersionAttribute(string deprecateByVersion) :
base(string.Empty, deprecateByVersion)
{
}

public CmdletDeprecationWithVersionAttribute(string deprecateByVersion, string changeInEffectByDate) :
base(string.Empty, deprecateByVersion, changeInEffectByDate)
{
}

protected override string GetAttributeSpecificMessage()
{
if (string.IsNullOrWhiteSpace(ReplacementCmdletName))
{
return Resources.BreakingChangesAttributesCmdLetDeprecationMessageNoReplacement;
}
else
{
return string.Format(Resources.BreakingChangesAttributesCmdLetDeprecationMessageWithReplacement, ReplacementCmdletName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Microsoft.WindowsAzure.Commands.Common.CustomAttributes
{
[Obsolete("CmdletOutputBreakingChangeAttribute is deprecated. Please use CmdletOutputBreakingChangeWithVersionAttribute instead to ensure that version information is included in the breaking change message.", false)]
[AttributeUsage(
AttributeTargets.Class,
AllowMultiple = true)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft 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.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.Common.Properties;
using System;
using System.Text;

namespace Microsoft.WindowsAzure.Commands.Common.CustomAttributes
{
[AttributeUsage(
AttributeTargets.Class,
AllowMultiple = true)]
public class CmdletOutputBreakingChangeWithVersionAttribute : GenericBreakingChangeWithVersionAttribute
{
public Type DeprecatedCmdLetOutputType { get; }

//This is still a String instead of a Type as this
//might be undefined at the time of adding the attribute
public string ReplacementCmdletOutputTypeName { get; set; }

public string[] DeprecatedOutputProperties { get; set; }

public string[] NewOutputProperties { get; set; }


public CmdletOutputBreakingChangeWithVersionAttribute(Type deprecatedCmdletOutputTypeName, string deprecateByVersion) :
base(string.Empty, deprecateByVersion)
{
this.DeprecatedCmdLetOutputType = deprecatedCmdletOutputTypeName;
}

public CmdletOutputBreakingChangeWithVersionAttribute(Type deprecatedCmdletOutputTypeName, string deprecateByVersion, string changeInEfectByDate) :
base(string.Empty, deprecateByVersion, changeInEfectByDate)
{
this.DeprecatedCmdLetOutputType = deprecatedCmdletOutputTypeName;
}

protected override string GetAttributeSpecificMessage()
{
StringBuilder message = new StringBuilder();

//check for the deprecation scenario
if (string.IsNullOrWhiteSpace(ReplacementCmdletOutputTypeName) && NewOutputProperties == null && DeprecatedOutputProperties == null && string.IsNullOrWhiteSpace(ChangeDescription))
{
message.Append(string.Format(Resources.BreakingChangesAttributesCmdLetOutputTypeDeprecated, DeprecatedCmdLetOutputType.FullName));
}
else
{
if (!string.IsNullOrWhiteSpace(ReplacementCmdletOutputTypeName))
{
message.Append(string.Format(Resources.BreakingChangesAttributesCmdLetOutputChange1, DeprecatedCmdLetOutputType.FullName, ReplacementCmdletOutputTypeName));
}
else
{
message.Append(string.Format(Resources.BreakingChangesAttributesCmdLetOutputChange2, DeprecatedCmdLetOutputType.FullName));
}

if (DeprecatedOutputProperties != null && DeprecatedOutputProperties.Length > 0)
{
message.Append(Resources.BreakingChangesAttributesCmdLetOutputPropertiesRemoved);
foreach (string property in DeprecatedOutputProperties)
{
message.Append(" '" + property + "'");
}
}

if (NewOutputProperties != null && NewOutputProperties.Length > 0)
{
message.Append(Resources.BreakingChangesAttributesCmdLetOutputPropertiesAdded);
foreach (string property in NewOutputProperties)
{
message.Append(" '" + property + "'");
}
}
}
return message.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Microsoft.WindowsAzure.Commands.Common.CustomAttributes
{
[Obsolete("CmdletParameterBreakingChangeAttribute is deprecated. Please use CmdletParameterBreakingChangeWithVersionAttribute instead to ensure that version information is included in the breaking change message.", false)]
[AttributeUsage(
AttributeTargets.Property |
AttributeTargets.Field,
Expand Down Expand Up @@ -80,7 +81,7 @@ protected override string GetAttributeSpecificMessage()
}
}

//See if the type of the param is changing
//See if the type of the param is changing
if (OldParamaterType != null && !string.IsNullOrWhiteSpace(NewParameterTypeName))
{
message.Append(string.Format(Resources.BreakingChangeAttributeParameterTypeChange, OldParamaterType.FullName, NewParameterTypeName));
Expand All @@ -90,7 +91,7 @@ protected override string GetAttributeSpecificMessage()

/// <summary>
/// See if the bound parameters contain the current parameter, if they do
/// then the attribbute is applicable
/// then the attribute is applicable
/// If the invocationInfo is null we return true
/// </summary>
/// <param name="invocationInfo"></param>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft 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.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.Common.Properties;
using System;
using System.Linq;
using System.Text;
using System.Management.Automation;
namespace Microsoft.WindowsAzure.Commands.Common.CustomAttributes
{
[AttributeUsage(
AttributeTargets.Property |
AttributeTargets.Field,
AllowMultiple = true)]
public class CmdletParameterBreakingChangeWithVersionAttribute : GenericBreakingChangeWithVersionAttribute
{
public string NameOfParameterChanging { get; }

public string ReplaceMentCmdletParameterName { get; set; } = null;

public bool IsBecomingMandatory { get; set; } = false;

public Type OldParamaterType { get; set; }

public String NewParameterTypeName { get; set; }


public CmdletParameterBreakingChangeWithVersionAttribute(string nameOfParameterChanging, string deprecateByVersion) :
base(string.Empty, deprecateByVersion)
{
this.NameOfParameterChanging = nameOfParameterChanging;
}

public CmdletParameterBreakingChangeWithVersionAttribute(string nameOfParameterChanging, string deprecateByVersion, string changeInEffectByDate) :
base(string.Empty, deprecateByVersion, changeInEffectByDate)
{
this.NameOfParameterChanging = nameOfParameterChanging;
}

protected override string GetAttributeSpecificMessage()
{
StringBuilder message = new StringBuilder();
if (!string.IsNullOrWhiteSpace(ReplaceMentCmdletParameterName))
{
if (IsBecomingMandatory)
{
message.Append(string.Format(Resources.BreakingChangeAttributeParameterReplacedMandatory, NameOfParameterChanging, ReplaceMentCmdletParameterName));
}
else
{
message.Append(string.Format(Resources.BreakingChangeAttributeParameterReplaced, NameOfParameterChanging, ReplaceMentCmdletParameterName));
}
}
else
{
if (IsBecomingMandatory)
{
message.Append(string.Format(Resources.BreakingChangeAttributeParameterMandatoryNow, NameOfParameterChanging));
}
else
{
message.Append(string.Format(Resources.BreakingChangeAttributeParameterChanging, NameOfParameterChanging));
}
}

//See if the type of the param is changing
if (OldParamaterType != null && !string.IsNullOrWhiteSpace(NewParameterTypeName))
{
message.Append(string.Format(Resources.BreakingChangeAttributeParameterTypeChange, OldParamaterType.FullName, NewParameterTypeName));
}
return message.ToString();
}

/// <summary>
/// See if the bound parameters contain the current parameter, if they do
/// then the attribute is applicable
/// If the invocationInfo is null we return true
/// </summary>
/// <param name="invocationInfo"></param>
/// <returns>bool</returns>
public override bool IsApplicableToInvocation(InvocationInfo invocationInfo)
{
bool? applicable = invocationInfo == null ? true : invocationInfo.BoundParameters?.Keys?.Contains(this.NameOfParameterChanging);
return applicable.HasValue ? applicable.Value : false;
}
}
}
9 changes: 5 additions & 4 deletions src/Common/CustomAttributes/GenericBreakingChangeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace Microsoft.WindowsAzure.Commands.Common.CustomAttributes
{
[Obsolete("GenericBreakingChangeAttribute is deprecated. Please use GenericBreakingChangeWithVersionAttribute instead to ensure that version information is included in the breaking change message.", false)]
[AttributeUsage(
AttributeTargets.Class |
AttributeTargets.Field |
Expand All @@ -35,7 +36,7 @@ namespace Microsoft.WindowsAzure.Commands.Common.CustomAttributes
public class GenericBreakingChangeAttribute : System.Attribute
{
private string _message;
//A dexcription of what the change is about, non mandatory
//A description of what the change is about, non mandatory
public string ChangeDescription { get; set; } = null;

//The version the change is effective from, non mandatory
Expand Down Expand Up @@ -83,9 +84,9 @@ public DateTime getInEffectByDate()

/**
* This function returns the breaking change text for the attribute
* If the withCmdletName is true we return the message with the cmdlet name in it otherwse not
* If the withCmdletName is true we return the message with the cmdlet name in it otherwise not
*
* We get the cmdlet name from the passed in Type (it is expected to have the CMdlet attribute decorated on the class)
* We get the cmdlet name from the passed in Type (it is expected to have the Cmdlet attribute decorated on the class)
*/
public string GetBreakingChangeTextFromAttribute(Type type, bool withCmdletName)
{
Expand Down Expand Up @@ -128,7 +129,7 @@ public string GetBreakingChangeTextFromAttribute(Type type, bool withCmdletName)
* If the "withCmdletName" is specified, the message is printed out with the cmdlet name in it
* otherwise not
*
* We get the cmdlet name from the passed in Type (it is expected to have the CMdlet attribute decorated on the class)
* We get the cmdlet name from the passed in Type (it is expected to have the Cmdlet attribute decorated on the class)
* */
public void PrintCustomAttributeInfo(Type type, bool withCmdletName, Action<string> writeOutput)
{
Expand Down
Loading