Skip to content

Commit

Permalink
Automatically create complete output file structure (including sln fi…
Browse files Browse the repository at this point in the history
…le) when generating

 - Add support for parameter attributes (e.g. validation)
 - Add parameters for ID placeholders in the URL
  • Loading branch information
rohitramu committed Mar 24, 2018
1 parent 09f3f17 commit 2ceca4e
Show file tree
Hide file tree
Showing 31 changed files with 275 additions and 180 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
56 changes: 0 additions & 56 deletions src/Generated/PowerShellCmdlets/Generated/DeviceAppManagement.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class NodeToResourceConversionBehavior
/// </summary>
/// <param name="node">The ODCM node which represents the OData route</param>
/// <returns>The resource that was generated from the OData route.</returns>
public static Resource ConvertToResource(this OdcmNode node)
public static Resource ConvertToResource(this OdcmNode node, string pathPrefix = "")
{
if (node == null)
{
Expand All @@ -33,7 +33,7 @@ public static Resource ConvertToResource(this OdcmNode node)
ODataRoute oDataRoute = new ODataRoute(node);

// Create file system path
string fileSystemPath = oDataRoute.ToRelativeFilePathString();
string fileSystemPath = $"{pathPrefix.Trim('\\')}\\{oDataRoute.ToRelativeFilePathString()}";

// Create a resource
Resource resource = new Resource(fileSystemPath);
Expand Down Expand Up @@ -114,17 +114,19 @@ private static Cmdlet CreateGetCmdlet(this OdcmProperty property, ODataRoute oDa
string oDataRouteString = oDataRoute.ToODataRouteString();
if (property.IsEnumeration())
{
// Create ID parameter
string idParameterName = "id";
CmdletParameter parameter = new CmdletParameter(idParameterName, typeof(string));
parameter.Attributes.Add(CmdletParameterAttribute.ValidateNotNullOrEmpty);

// Create parameter set
CmdletParameterSet parameterSet = new CmdletParameterSet("Get");

// Add ID parameter
string idParameterName = "id";
parameterSet.Add(new CmdletParameter(idParameterName, typeof(string)));
parameterSet.Add(parameter);

// Add parameter set to cmdlet
result.ParameterSets.Add(parameterSet);

// Set the call URL to use this parameter
// Set the URL to use this parameter
result.CallUrl = $"{oDataRouteString}/{{{idParameterName} ?? string.Empty}}";

// Since the property is an enumeration, allow "search" as well as "get"
Expand All @@ -137,10 +139,7 @@ private static Cmdlet CreateGetCmdlet(this OdcmProperty property, ODataRoute oDa
}

// Add properties to represent the ID placeholders in the URL
foreach (string idParameterName in oDataRoute.IdParameters)
{
result.GetDefaultParameterSet().Add(new CmdletParameter(idParameterName, typeof(string)));
}
result.AddIdParameters(oDataRoute);

return result;
}
Expand All @@ -160,7 +159,17 @@ private static Cmdlet CreateDeleteCmdlet(this OdcmProperty property, ODataRoute
{
// Add ID parameter
string idParameterName = "id";
result.ParameterSets.DefaultParameterSet.Add(new CmdletParameter(idParameterName, typeof(string)));
CmdletParameter parameter = new CmdletParameter(idParameterName, typeof(string));
parameter.Attributes.Add(CmdletParameterAttribute.ValidateNotNullOrEmpty);

// Create parameter set
CmdletParameterSet parameterSet = new CmdletParameterSet("Get");
parameterSet.Add(parameter);

// Add parameter set to cmdlet
result.ParameterSets.Add(parameterSet);

// Set the URL to use this parameter
result.CallUrl = $"{oDataRouteString}/{{{idParameterName}}}";
}
else
Expand All @@ -169,12 +178,19 @@ private static Cmdlet CreateDeleteCmdlet(this OdcmProperty property, ODataRoute
}

// Add properties to represent the ID placeholders in the URL
result.AddIdParameters(oDataRoute);

return result;
}

private static void AddIdParameters(this Cmdlet cmdlet, ODataRoute oDataRoute)
{
foreach (string idParameterName in oDataRoute.IdParameters)
{
result.GetDefaultParameterSet().Add(new CmdletParameter(idParameterName, typeof(string)));
CmdletParameter idParameter = new CmdletParameter(idParameterName, typeof(string));
idParameter.Attributes.Add(CmdletParameterAttribute.ValidateNotNullOrEmpty);
cmdlet.GetDefaultParameterSet().Add(idParameter);
}

return result;
}

private static string GetCmdletName(this OdcmProperty property, ODataRoute oDataRoute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Microsoft.Graph.GraphODataPowerShellSDKWriter.Generator.Models
{
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a PowerShell cmdlet's parameter.
Expand All @@ -24,6 +25,8 @@ public class CmdletParameter
/// </summary>
public bool IsMandatory { get; }

public ICollection<CmdletParameterAttribute> Attributes { get; } = new List<CmdletParameterAttribute>();

/// <summary>
/// Creates a new cmdlet parameter.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.

namespace Microsoft.Graph.GraphODataPowerShellSDKWriter.Generator.Models
{
using System;
using System.Collections.Generic;

public class CmdletParameterAttribute
{
public static readonly CmdletParameterAttribute ValidateNotNull = new CmdletParameterAttribute("ValidateNotNull");
public static readonly CmdletParameterAttribute ValidateNotNullOrEmpty = new CmdletParameterAttribute("ValidateNotNullOrEmpty");

public string Name { get; }

public ICollection<string> Arguments { get; }

public CmdletParameterAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Parameter name cannot be null or whitespace", nameof(name));
}

this.Name = name;
this.Arguments = new List<string>();
}

public CmdletParameterAttribute(string name, ICollection<string> arguments)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Parameter name cannot be null or whitespace", nameof(name));
}

this.Name = name;
this.Arguments = arguments ?? throw new ArgumentNullException(nameof(arguments));
}

public override string ToString()
{
return $"[{this.Name}({string.Join(", ", this.Arguments)})]";
}
}
}
Loading

0 comments on commit 2ceca4e

Please sign in to comment.