public override string GetHelpHtmlContent(ScriptIntellisenseItem item)
        {
            string cmdletName = CmdletName ?? item?.Value;

            if (string.IsNullOrWhiteSpace(cmdletName))
            {
                return(null);
            }

            using var runspace = PowerShellScriptEngine.CreateRunspace();
            using var ps       = Automation.PowerShell.Create();

            ps.Runspace = runspace;
            ps.AddCommand("get-help").AddParameter("Name", cmdletName).AddParameter("Full").AddCommand("out-string").AddParameter("Width", 10000);

            var commands = ps.Invoke();
            var command  = commands?.FirstOrDefault();
            var result   = Convert.ToString(command?.BaseObject);

            if (string.IsNullOrWhiteSpace(result))
            {
                return(null);
            }

            result = HtmlEncode(result);

            var reHeaders = new Regex(@"(?m)(^\w[\w \t]+)");

            result = reHeaders.Replace(result, "<b><u>$1</u></b>");

            var reParameters = new Regex(@"(?m)^(?:[ \t]*(\-\w+(?:\s*\[?&lt;.*?&gt;\]?)?))\s*<br>\s*$");

            result = reParameters.Replace(result, "<b>$1</b><br>\r\n");

            var reCommonParameters = new Regex(@"(?m)^[ \t]*(&lt;CommonParameters&gt;)\s*<br>\s*$");

            result = reCommonParameters.Replace(result, "<b>$1</b><br>\r\n");

            result = result.Replace(" ", "&nbsp;");

            return(result);
        }
        public override void ShowOnlineHelp(ScriptIntellisenseItem item)
        {
            string cmdletModule = HtmlEncode(CmdletModule);
            string cmdletName   = HtmlEncode(CmdletName ?? item?.Value);

            if (string.IsNullOrWhiteSpace(cmdletName))
            {
                XtraMessageBox.Show("Cmdlet name is empty.", "Cannot show help for cmdlet", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                using var runspace = PowerShellScriptEngine.CreateRunspace();
                using var ps       = Automation.PowerShell.Create();
                ps.Runspace        = runspace;
                ps.AddCommand("get-help").AddParameter("Name", cmdletName).AddParameter("Online");

                ps.Invoke();
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrWhiteSpace(CmdletModule))
                {
                    //Try to show on Microsoft site
                    string url = $"https://docs.microsoft.com/en-us/powershell/module/{cmdletModule}/{CmdletName}";
                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
                    {
                        FileName = url, UseShellExecute = true
                    });
                    return;
                }

                XtraMessageBox.Show(ex.Message, "Cannot show online help", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Пример #3
0
 public virtual void ShowOnlineHelp(ScriptIntellisenseItem item)
 {
 }
Пример #4
0
 public virtual string GetHelpHtmlContent(ScriptIntellisenseItem item) => null;
        public override string GetHelpHtmlContent(ScriptIntellisenseItem item)
        {
            var type = Utils.GetUnderlyingType(Type);

            if (type == null)
            {
                return(null);
            }

            var result = new StringBuilder();

            result.AppendLine($"<h1><b>{HtmlEncode(type.Name)}</b> ({HtmlEncode(type.FullName)})</h1><br>");

            var attrTypeDescription = type.GetCustomAttribute <DescriptionAttribute>(true);

            if (!string.IsNullOrWhiteSpace(attrTypeDescription?.Description))
            {
                result.AppendLine($"<i>{HtmlEncode(attrTypeDescription?.Description)}</i><br>");
            }

            result.AppendLine("<br>");

            if (IsStatic && type.IsEnum)
            {
                var names = Enum.GetNames(type);
                if (names == null || names.Length <= 0)
                {
                    return(result.ToString());
                }

                foreach (var name in names)
                {
                    result.AppendLine($"  -{HtmlEncode(name)}<br>");
                }

                return(result.ToString());
            }

            var bindingFlags = IsStatic ?
                               BindingFlags.Static | BindingFlags.Public :
                               BindingFlags.Instance | BindingFlags.Public;

            result.AppendLine("<h2>Properties:</h2><br>");
            var properties = type.GetProperties(bindingFlags);

            foreach (var property in properties)
            {
                if (property.MemberType != MemberTypes.Property || property.IsSpecialName)
                {
                    continue;
                }

                var attributeBrowsable = property.GetCustomAttribute <BrowsableAttribute>(true);
                if (!(attributeBrowsable?.Browsable ?? true))
                {
                    continue;
                }

                var typeName      = Utils.GetTypeName(property.PropertyType);
                var strParameters = new StringBuilder();
                var parameters    = property.GetIndexParameters();
                if (parameters.Length > 0)
                {
                    strParameters.Append(" [");
                    foreach (var parameter in parameters)
                    {
                        var paramTypeName = parameters[0].ParameterType;
                        if (strParameters.Length > 2)                           // "[ " was added initially
                        {
                            strParameters.Append(", ");
                        }
                        if (parameter.ParameterType.IsByRef)
                        {
                            strParameters.Append(parameter.IsOut ? "out " : "ref ");
                        }
                        strParameters.Append($"[{paramTypeName}] {parameter.Name}");
                    }
                    strParameters.Append(']');
                }

                var attributeDescription = property.GetCustomAttribute <DescriptionAttribute>(true);
                var description          = $"<b>{HtmlEncode(property.Name)}</b> [{HtmlEncode(typeName)}]<br>{HtmlEncode(attributeDescription?.Description)}<br><br>";

                result.AppendLine(description);
            }

            result.AppendLine("<h2>Methods:</h2><br>");
            var methods = type.GetMethods(bindingFlags | BindingFlags.InvokeMethod);

            foreach (var method in methods)
            {
                if (method.IsConstructor || method.MemberType != MemberTypes.Method || method.IsSpecialName)
                {
                    continue;
                }

                var attributeBrowsable = method.GetCustomAttribute <BrowsableAttribute>(true);
                if (!(attributeBrowsable?.Browsable ?? true))
                {
                    continue;
                }

                string typeName = "object";
                if (method.ReturnType != typeof(void))
                {
                    typeName = Utils.GetTypeName(method.ReturnType);
                }

                var parameters    = method.GetParameters();
                var strParameters = new StringBuilder();
                strParameters.Append(" (");

                foreach (var parameter in parameters)
                {
                    var paramTypeName = Utils.GetTypeName(parameter.ParameterType);
                    if (string.IsNullOrWhiteSpace(paramTypeName))
                    {
                        paramTypeName = "object";
                    }

                    if (strParameters.Length > 2)                       // " (" was added initially
                    {
                        strParameters.Append(", ");
                    }

                    if (parameter.ParameterType.IsByRef)
                    {
                        strParameters.Append(parameter.IsOut ? "out " : "ref ");
                    }
                    strParameters.Append($"[{paramTypeName}] {parameter.Name}");
                }

                strParameters.Append(')');

                var attributeDescription = method.GetCustomAttribute <DescriptionAttribute>(true);
                var description          = $"<b>{HtmlEncode(method.Name)}</b> [{HtmlEncode(typeName)}] {HtmlEncode(strParameters.ToString())}<br>{HtmlEncode(attributeDescription?.Description)}<br><br>";

                result.AppendLine(description);
            }

            return(result.ToString());