示例#1
0
        /// <summary>
        /// Create suggestion with scriptblock rule and scriptblock suggestion with arguments.
        /// </summary>
        private static Hashtable NewSuggestion(int id, string category, SuggestionMatchType matchType, ScriptBlock rule, ScriptBlock suggestion, object[] suggestionArgs, bool enabled)
        {
            Hashtable result = NewSuggestion(id, category, matchType, rule, suggestion, enabled);

            result.Add("SuggestionArgs", suggestionArgs);

            return(result);
        }
示例#2
0
        private static Hashtable NewSuggestion(int id, string category, SuggestionMatchType matchType, string rule, string suggestion, bool enabled)
        {
            Hashtable hashtable = new Hashtable(StringComparer.CurrentCultureIgnoreCase);

            hashtable["Id"]         = id;
            hashtable["Category"]   = category;
            hashtable["MatchType"]  = matchType;
            hashtable["Rule"]       = rule;
            hashtable["Suggestion"] = suggestion;
            hashtable["Enabled"]    = enabled;
            return(hashtable);
        }
示例#3
0
        /// <summary>
        /// Create suggestion with scriptblock rule and suggestion.
        /// </summary>
        private static Hashtable NewSuggestion(int id, string category, SuggestionMatchType matchType, ScriptBlock rule, ScriptBlock suggestion, bool enabled)
        {
            Hashtable result = new Hashtable(StringComparer.CurrentCultureIgnoreCase);

            result["Id"]         = id;
            result["Category"]   = category;
            result["MatchType"]  = matchType;
            result["Rule"]       = rule;
            result["Suggestion"] = suggestion;
            result["Enabled"]    = enabled;

            return(result);
        }
示例#4
0
 private static Hashtable NewSuggestion(
     int id,
     string category,
     SuggestionMatchType matchType,
     ScriptBlock rule,
     ScriptBlock suggestion,
     bool enabled)
 {
     return(new Hashtable((IEqualityComparer)StringComparer.CurrentCultureIgnoreCase)
     {
         [(object)"Id"] = (object)id,
         [(object)"Category"] = (object)category,
         [(object)"MatchType"] = (object)matchType,
         [(object)"Rule"] = (object)rule,
         [(object)"Suggestion"] = (object)suggestion,
         [(object)"Enabled"] = (object)enabled
     });
 }
示例#5
0
        internal static ArrayList GetSuggestion(HistoryInfo lastHistory, Object lastError, ArrayList errorList)
        {
            ArrayList returnSuggestions = new ArrayList();

            PSModuleInfo invocationModule = new PSModuleInfo(true);

            invocationModule.SessionState.PSVariable.Set("lastHistory", lastHistory);
            invocationModule.SessionState.PSVariable.Set("lastError", lastError);

            int initialErrorCount = 0;

            // Go through all of the suggestions
            foreach (Hashtable suggestion in s_suggestions)
            {
                initialErrorCount = errorList.Count;

                // Make sure the rule is enabled
                if (!LanguagePrimitives.IsTrue(suggestion["Enabled"]))
                {
                    continue;
                }

                SuggestionMatchType matchType = (SuggestionMatchType)LanguagePrimitives.ConvertTo(
                    suggestion["MatchType"],
                    typeof(SuggestionMatchType),
                    CultureInfo.InvariantCulture);

                // If this is a dynamic match, evaluate the ScriptBlock
                if (matchType == SuggestionMatchType.Dynamic)
                {
                    object result = null;

                    ScriptBlock evaluator = suggestion["Rule"] as ScriptBlock;
                    if (evaluator == null)
                    {
                        suggestion["Enabled"] = false;

                        throw new ArgumentException(
                                  SuggestionStrings.RuleMustBeScriptBlock, "Rule");
                    }

                    try
                    {
                        result = invocationModule.Invoke(evaluator, null);
                    }
                    catch (Exception)
                    {
                        // Catch-all OK. This is a third-party call-out.
                        suggestion["Enabled"] = false;
                        continue;
                    }

                    // If it returned results, evaluate its suggestion
                    if (LanguagePrimitives.IsTrue(result))
                    {
                        string suggestionText = GetSuggestionText(suggestion["Suggestion"], (object[])suggestion["SuggestionArgs"], invocationModule);

                        if (!String.IsNullOrEmpty(suggestionText))
                        {
                            string returnString = String.Format(
                                CultureInfo.CurrentCulture,
                                "Suggestion [{0},{1}]: {2}",
                                (int)suggestion["Id"],
                                (string)suggestion["Category"],
                                suggestionText);

                            returnSuggestions.Add(returnString);
                        }
                    }
                }
                else
                {
                    string matchText = String.Empty;

                    // Otherwise, this is a Regex match against the
                    // command or error
                    if (matchType == SuggestionMatchType.Command)
                    {
                        matchText = lastHistory.CommandLine;
                    }
                    else if (matchType == SuggestionMatchType.Error)
                    {
                        if (lastError != null)
                        {
                            Exception lastException = lastError as Exception;
                            if (lastException != null)
                            {
                                matchText = lastException.Message;
                            }
                            else
                            {
                                matchText = lastError.ToString();
                            }
                        }
                    }
                    else
                    {
                        suggestion["Enabled"] = false;

                        throw new ArgumentException(
                                  SuggestionStrings.InvalidMatchType,
                                  "MatchType");
                    }

                    // If the text matches, evaluate the suggestion
                    if (Regex.IsMatch(matchText, (string)suggestion["Rule"], RegexOptions.IgnoreCase))
                    {
                        string suggestionText = GetSuggestionText(suggestion["Suggestion"], (object[])suggestion["SuggestionArgs"], invocationModule);

                        if (!String.IsNullOrEmpty(suggestionText))
                        {
                            string returnString = String.Format(
                                CultureInfo.CurrentCulture,
                                "Suggestion [{0},{1}]: {2}",
                                (int)suggestion["Id"],
                                (string)suggestion["Category"],
                                suggestionText);

                            returnSuggestions.Add(returnString);
                        }
                    }
                }

                // If the rule generated an error, disable it
                if (errorList.Count != initialErrorCount)
                {
                    suggestion["Enabled"] = false;
                }
            }

            return(returnSuggestions);
        }
示例#6
0
        internal static ArrayList GetSuggestion(HistoryInfo lastHistory, object lastError, ArrayList errorList)
        {
            ArrayList    list             = new ArrayList();
            PSModuleInfo invocationModule = new PSModuleInfo(true);

            invocationModule.SessionState.PSVariable.Set("lastHistory", lastHistory);
            invocationModule.SessionState.PSVariable.Set("lastError", lastError);
            int count = 0;

            foreach (Hashtable hashtable in suggestions)
            {
                count = errorList.Count;
                if (LanguagePrimitives.IsTrue(hashtable["Enabled"]))
                {
                    SuggestionMatchType type = (SuggestionMatchType)LanguagePrimitives.ConvertTo(hashtable["MatchType"], typeof(SuggestionMatchType), CultureInfo.InvariantCulture);
                    if (type == SuggestionMatchType.Dynamic)
                    {
                        object      obj2 = null;
                        ScriptBlock sb   = hashtable["Rule"] as ScriptBlock;
                        if (sb == null)
                        {
                            hashtable["Enabled"] = false;
                            throw new ArgumentException(SuggestionStrings.RuleMustBeScriptBlock, "Rule");
                        }
                        try
                        {
                            obj2 = invocationModule.Invoke(sb, null);
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            hashtable["Enabled"] = false;
                            continue;
                        }
                        if (LanguagePrimitives.IsTrue(obj2))
                        {
                            string suggestionText = GetSuggestionText(hashtable["Suggestion"], invocationModule);
                            if (!string.IsNullOrEmpty(suggestionText))
                            {
                                string str2 = string.Format(Thread.CurrentThread.CurrentCulture, "Suggestion [{0},{1}]: {2}", new object[] { (int)hashtable["Id"], (string)hashtable["Category"], suggestionText });
                                list.Add(str2);
                            }
                        }
                    }
                    else
                    {
                        string input = string.Empty;
                        switch (type)
                        {
                        case SuggestionMatchType.Command:
                            input = lastHistory.CommandLine;
                            break;

                        case SuggestionMatchType.Error:
                            if (lastError != null)
                            {
                                Exception exception2 = lastError as Exception;
                                if (exception2 != null)
                                {
                                    input = exception2.Message;
                                }
                                else
                                {
                                    input = lastError.ToString();
                                }
                            }
                            break;

                        default:
                            hashtable["Enabled"] = false;
                            throw new ArgumentException(SuggestionStrings.InvalidMatchType, "MatchType");
                        }
                        if (Regex.IsMatch(input, (string)hashtable["Rule"], RegexOptions.IgnoreCase))
                        {
                            string str4 = GetSuggestionText(hashtable["Suggestion"], invocationModule);
                            if (!string.IsNullOrEmpty(str4))
                            {
                                string str5 = string.Format(Thread.CurrentThread.CurrentCulture, "Suggestion [{0},{1}]: {2}", new object[] { (int)hashtable["Id"], (string)hashtable["Category"], str4 });
                                list.Add(str5);
                            }
                        }
                    }
                    if (errorList.Count != count)
                    {
                        hashtable["Enabled"] = false;
                    }
                }
            }
            return(list);
        }
示例#7
0
        internal static ArrayList GetSuggestion(
            HistoryInfo lastHistory,
            object lastError,
            ArrayList errorList)
        {
            ArrayList    arrayList        = new ArrayList();
            PSModuleInfo invocationModule = new PSModuleInfo(true);

            invocationModule.SessionState.PSVariable.Set(nameof(lastHistory), (object)lastHistory);
            invocationModule.SessionState.PSVariable.Set(nameof(lastError), lastError);
            foreach (Hashtable suggestion in HostUtilities.suggestions)
            {
                int count = errorList.Count;
                if (LanguagePrimitives.IsTrue(suggestion[(object)"Enabled"]))
                {
                    SuggestionMatchType suggestionMatchType = (SuggestionMatchType)LanguagePrimitives.ConvertTo(suggestion[(object)"MatchType"], typeof(SuggestionMatchType), (IFormatProvider)CultureInfo.InvariantCulture);
                    if (suggestionMatchType == SuggestionMatchType.Dynamic)
                    {
                        if (!(suggestion[(object)"Rule"] is ScriptBlock sb))
                        {
                            suggestion[(object)"Enabled"] = (object)false;
                            throw new ArgumentException(ResourceManagerCache.GetResourceString("SuggestionStrings", "RuleMustBeScriptBlock"), "Rule");
                        }
                        object obj;
                        try
                        {
                            obj = invocationModule.Invoke(sb, (object[])null);
                        }
                        catch (Exception ex)
                        {
                            CommandProcessorBase.CheckForSevereException(ex);
                            suggestion[(object)"Enabled"] = (object)false;
                            continue;
                        }
                        if (LanguagePrimitives.IsTrue(obj))
                        {
                            string suggestionText = HostUtilities.GetSuggestionText(suggestion[(object)"Suggestion"], invocationModule);
                            if (!string.IsNullOrEmpty(suggestionText))
                            {
                                string str = string.Format((IFormatProvider)Thread.CurrentThread.CurrentCulture, "Suggestion [{0},{1}]: {2}", (object)(int)suggestion[(object)"Id"], (object)(string)suggestion[(object)"Category"], (object)suggestionText);
                                arrayList.Add((object)str);
                            }
                        }
                    }
                    else
                    {
                        string input = string.Empty;
                        if (suggestionMatchType == SuggestionMatchType.Command)
                        {
                            input = lastHistory.CommandLine;
                        }
                        else if (suggestionMatchType == SuggestionMatchType.Error)
                        {
                            if (lastError != null)
                            {
                                input = !(lastError is Exception exception) ? lastError.ToString() : exception.Message;
                            }
                        }
                        else
                        {
                            suggestion[(object)"Enabled"] = (object)false;
                            throw new ArgumentException(ResourceManagerCache.GetResourceString("SuggestionStrings", "InvalidMatchType"), "MatchType");
                        }
                        if (Regex.IsMatch(input, (string)suggestion[(object)"Rule"], RegexOptions.IgnoreCase))
                        {
                            string suggestionText = HostUtilities.GetSuggestionText(suggestion[(object)"Suggestion"], invocationModule);
                            if (!string.IsNullOrEmpty(suggestionText))
                            {
                                string str = string.Format((IFormatProvider)Thread.CurrentThread.CurrentCulture, "Suggestion [{0},{1}]: {2}", (object)(int)suggestion[(object)"Id"], (object)(string)suggestion[(object)"Category"], (object)suggestionText);
                                arrayList.Add((object)str);
                            }
                        }
                    }
                    if (errorList.Count != count)
                    {
                        suggestion[(object)"Enabled"] = (object)false;
                    }
                }
            }
            return(arrayList);
        }
示例#8
0
        /// <summary>
        /// Create suggestion with scriptblock rule and scriptblock suggestion with arguments.
        /// </summary>
        private static Hashtable NewSuggestion(int id, string category, SuggestionMatchType matchType, ScriptBlock rule, ScriptBlock suggestion, object[] suggestionArgs, bool enabled)
        {
            Hashtable result = NewSuggestion(id, category, matchType, rule, suggestion, enabled);
            result.Add("SuggestionArgs", suggestionArgs);

            return result;
        }
示例#9
0
        /// <summary>
        /// Create suggestion with scriptblock rule and suggestion.
        /// </summary>
        private static Hashtable NewSuggestion(int id, string category, SuggestionMatchType matchType, ScriptBlock rule, ScriptBlock suggestion, bool enabled)
        {
            Hashtable result = new Hashtable(StringComparer.CurrentCultureIgnoreCase);

            result["Id"] = id;
            result["Category"] = category;
            result["MatchType"] = matchType;
            result["Rule"] = rule;
            result["Suggestion"] = suggestion;
            result["Enabled"] = enabled;

            return result;
        }
示例#10
0
 private static Hashtable NewSuggestion(int id, string category, SuggestionMatchType matchType, string rule, string suggestion, bool enabled)
 {
     Hashtable hashtable = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
     hashtable["Id"] = id;
     hashtable["Category"] = category;
     hashtable["MatchType"] = matchType;
     hashtable["Rule"] = rule;
     hashtable["Suggestion"] = suggestion;
     hashtable["Enabled"] = enabled;
     return hashtable;
 }