Exemplo n.º 1
0
        /// <summary>
        /// Check if the default parameter set has changed, and if so, make sure
        /// that the parameters are the same
        /// </summary>
        /// <param name="oldCmdlet">The cmdlet metadata from the old (serialized) assembly.</param>
        /// <param name="newCmdlet">The cmdlet metadata from the new assembly.</param>
        /// <param name="issueLogger">ReportLogger that will keep track of issues found.</param>
        private void CheckDefaultParameterName(
            CmdletMetadata oldCmdlet,
            CmdletMetadata newCmdlet,
            ReportLogger <BreakingChangeIssue> issueLogger)
        {
            // If the default parameter name hasn't changed, or if there wasn't a
            // default parameter set defined before, return
            if (oldCmdlet.DefaultParameterSetName.Equals(newCmdlet.DefaultParameterSetName) ||
                oldCmdlet.DefaultParameterSetName.Equals("__AllParameterSets"))
            {
                return;
            }

            // Get the metadata for the old default parameter set
            ParameterSetMetadata oldDefaultParameterSet = oldCmdlet.ParameterSets
                                                          .FirstOrDefault(p => p.Name.Equals(oldCmdlet.DefaultParameterSetName, StringComparison.OrdinalIgnoreCase));
            // Get the metadata for the new default parameter set
            ParameterSetMetadata newDefaultParameterSet = newCmdlet.ParameterSets
                                                          .FirstOrDefault(p => p.Name.Equals(newCmdlet.DefaultParameterSetName, StringComparison.OrdinalIgnoreCase));

            if (oldDefaultParameterSet == null || newDefaultParameterSet == null)
            {
                return;
            }
            // This dictionary will map a parameter name and aliases to the corresponding Parameter object
            Dictionary <string, Parameter> parameterDictionary = new Dictionary <string, Parameter>();

            // Add each parameter name and aliases to the dictionary
            foreach (var newParameter in newDefaultParameterSet.Parameters)
            {
                parameterDictionary.Add(newParameter.ParameterMetadata.Name, newParameter);

                foreach (var alias in newParameter.ParameterMetadata.AliasList)
                {
                    parameterDictionary.Add(alias, newParameter);
                }
            }

            // For each parameter in the old default parameter set, check if it
            // exists in the new default parameter set
            foreach (var oldParameter in oldDefaultParameterSet.Parameters)
            {
                // If the parameter cannot be found, log an issue
                if (!parameterDictionary.ContainsKey(oldParameter.ParameterMetadata.Name))
                {
                    issueLogger?.LogBreakingChangeIssue(
                        cmdlet: oldCmdlet,
                        severity: 0,
                        problemId: ProblemIds.BreakingChangeProblemId.ChangeDefaultParameter,
                        description: string.Format(Resources.ChangeDefaultParameterDescription,
                                                   oldCmdlet.DefaultParameterSetName, oldCmdlet.Name),
                        remediation: string.Format(Resources.ChangeDefaultParameterRemediation,
                                                   oldCmdlet.Name, oldCmdlet.DefaultParameterSetName));
                }
            }
        }
        /// <summary>
        /// Get cmdlets from the given assembly
        /// </summary>
        /// <param name="assmeblyPath"></param>
        /// <returns></returns>
        public ModuleMetadata GetModuleMetadata(string assemblyPath)
        {
            List <CmdletBreakingChangeMetadata> results = new List <CmdletBreakingChangeMetadata>();

            ModuleMetadata = new ModuleMetadata();

            try
            {
                var assembly = Assembly.LoadFrom(assemblyPath);
                foreach (var type in assembly.GetCmdletTypes())
                {
                    var cmdlet     = type.GetAttribute <CmdletAttribute>();
                    var outputs    = type.GetAttributes <OutputTypeAttribute>();
                    var parameters = type.GetParameters();

                    var cmdletMetadata = new CmdletBreakingChangeMetadata
                    {
                        VerbName                = cmdlet.VerbName,
                        NounName                = cmdlet.NounName,
                        ConfirmImpact           = cmdlet.ConfirmImpact,
                        SupportsPaging          = cmdlet.SupportsPaging,
                        SupportsShouldProcess   = cmdlet.SupportsShouldProcess,
                        ClassName               = type.FullName,
                        DefaultParameterSetName = cmdlet.DefaultParameterSetName ?? "__AllParameterSets"
                    };

                    if (type.HasAttribute <AliasAttribute>())
                    {
                        var aliases = type.GetAttributes <AliasAttribute>();
                        cmdletMetadata.AliasList.AddRange(
                            aliases.SelectMany(a => a.AliasNames));
                    }

                    foreach (var output in outputs)
                    {
                        foreach (var outputType in output.Type)
                        {
                            var outputMetadata = new OutputMetadata
                            {
                                Type = outputType.Type
                            };
                            outputMetadata.ParameterSets.AddRange(output.ParameterSetName);
                            cmdletMetadata.OutputTypes.Add(outputMetadata);
                        }
                    }

                    List <Parameter> globalParameters = new List <Parameter>();

                    foreach (var parameter in parameters)
                    {
                        var parameterData = new ParameterMetadata
                        {
                            Type = parameter.PropertyType,
                            Name = parameter.Name
                        };

                        if (parameter.HasAttribute <AliasAttribute>())
                        {
                            var aliases = parameter.GetAttributes <AliasAttribute>();
                            parameterData.AliasList.AddRange(
                                aliases.SelectMany(a => a.AliasNames));
                        }

                        if (parameter.HasAttribute <ValidateSetAttribute>())
                        {
                            var validateSet = parameter.GetAttribute <ValidateSetAttribute>();
                            parameterData.ValidateSet.AddRange(validateSet.ValidValues);
                        }

                        if (parameter.HasAttribute <ValidateRangeAttribute>())
                        {
                            var validateRange = parameter.GetAttribute <ValidateRangeAttribute>();
                            parameterData.ValidateRangeMin = Convert.ToInt64(validateRange.MinRange);
                            parameterData.ValidateRangeMax = Convert.ToInt64(validateRange.MaxRange);
                        }

                        parameterData.ValidateNotNullOrEmpty = parameter.HasAttribute <ValidateNotNullOrEmptyAttribute>();

                        cmdletMetadata.Parameters.Add(parameterData);

                        foreach (var parameterSet in parameter.GetAttributes <ParameterAttribute>())
                        {
                            var parameterSetMetadata = cmdletMetadata.ParameterSets.FirstOrDefault(s => s.Name.Equals(parameterSet.ParameterSetName));

                            if (parameterSetMetadata == null)
                            {
                                parameterSetMetadata = new ParameterSetMetadata()
                                {
                                    Name = parameterSet.ParameterSetName ?? "__AllParameterSets"
                                };
                            }

                            Parameter param = new Parameter
                            {
                                ParameterMetadata = parameterData,
                                Mandatory         = parameterSet.Mandatory,
                                Position          = parameterSet.Position,
                                ValueFromPipeline = parameterSet.ValueFromPipeline,
                                ValueFromPipelineByPropertyName = parameterSet.ValueFromPipelineByPropertyName
                            };

                            if (parameterSet.ParameterSetName.Equals("__AllParameterSets"))
                            {
                                globalParameters.Add(param);
                            }

                            parameterSetMetadata.Parameters.Add(param);

                            if (parameterSetMetadata.Parameters.Count == 1)
                            {
                                cmdletMetadata.ParameterSets.Add(parameterSetMetadata);
                            }
                        }
                    }

                    foreach (var parameterSet in cmdletMetadata.ParameterSets)
                    {
                        if (parameterSet.Name.Equals("__AllParameterSets"))
                        {
                            continue;
                        }

                        foreach (var parameter in globalParameters)
                        {
                            var param = parameterSet.Parameters.FirstOrDefault(p => p.ParameterMetadata.Name.Equals(parameter.ParameterMetadata.Name));
                            if (param == null)
                            {
                                parameterSet.Parameters.Add(parameter);
                            }
                        }
                    }

                    if (cmdletMetadata.ParameterSets.Count == 0)
                    {
                        ParameterSetMetadata defaultSet = new ParameterSetMetadata()
                        {
                            Name = cmdletMetadata.DefaultParameterSetName
                        };
                        cmdletMetadata.ParameterSets.Add(defaultSet);
                    }

                    results.Add(cmdletMetadata);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            ModuleMetadata.Cmdlets = results;
            return(ModuleMetadata);
        }