Exemplo n.º 1
0
 internal ScriptAnalysis(string path)
 {
     ModuleIntrinsics.Tracer.WriteLine("Analyzing path: " + path, new object[0]);
     string input = File.ReadAllText(path);
     Token[] tokens = null;
     ParseError[] errors = null;
     Ast ast = Parser.ParseInput(input, out tokens, out errors);
     ExportVisitor astVisitor = new ExportVisitor();
     ast.Visit(astVisitor);
     this.DiscoveredExports = astVisitor.DiscoveredExports;
     this.DiscoveredAliases = new Dictionary<string, string>();
     this.DiscoveredModules = astVisitor.DiscoveredModules;
     this.DiscoveredCommandFilters = astVisitor.DiscoveredCommandFilters;
     if (this.DiscoveredCommandFilters.Count == 0)
     {
         this.DiscoveredCommandFilters.Add("*");
     }
     else
     {
         List<WildcardPattern> patterns = new List<WildcardPattern>();
         foreach (string str2 in this.DiscoveredCommandFilters)
         {
             patterns.Add(new WildcardPattern(str2));
         }
         foreach (string str3 in astVisitor.DiscoveredAliases.Keys)
         {
             if (SessionStateUtilities.MatchesAnyWildcardPattern(str3, patterns, false))
             {
                 this.DiscoveredAliases[str3] = astVisitor.DiscoveredAliases[str3];
             }
         }
     }
     this.AddsSelfToPath = astVisitor.AddsSelfToPath;
 }
Exemplo n.º 2
0
        internal ScriptAnalysis(string path)
        {
            ModuleIntrinsics.Tracer.WriteLine("Analyzing path: " + path, new object[0]);
            string input = File.ReadAllText(path);

            Token[]       tokens     = null;
            ParseError[]  errors     = null;
            Ast           ast        = Parser.ParseInput(input, out tokens, out errors);
            ExportVisitor astVisitor = new ExportVisitor();

            ast.Visit(astVisitor);
            this.DiscoveredExports        = astVisitor.DiscoveredExports;
            this.DiscoveredAliases        = new Dictionary <string, string>();
            this.DiscoveredModules        = astVisitor.DiscoveredModules;
            this.DiscoveredCommandFilters = astVisitor.DiscoveredCommandFilters;
            if (this.DiscoveredCommandFilters.Count == 0)
            {
                this.DiscoveredCommandFilters.Add("*");
            }
            else
            {
                List <WildcardPattern> patterns = new List <WildcardPattern>();
                foreach (string str2 in this.DiscoveredCommandFilters)
                {
                    patterns.Add(new WildcardPattern(str2));
                }
                foreach (string str3 in astVisitor.DiscoveredAliases.Keys)
                {
                    if (SessionStateUtilities.MatchesAnyWildcardPattern(str3, patterns, false))
                    {
                        this.DiscoveredAliases[str3] = astVisitor.DiscoveredAliases[str3];
                    }
                }
            }
            this.AddsSelfToPath = astVisitor.AddsSelfToPath;
        }
Exemplo n.º 3
0
        internal static ScriptAnalysis Analyze(string path, ExecutionContext context)
        {
            ModuleIntrinsics.Tracer.WriteLine("Analyzing path: {0}", path);

            try
            {
                if (Utils.PathIsUnc(path) && (context.CurrentCommandProcessor.CommandRuntime != null))
                {
                    ProgressRecord analysisProgress = new ProgressRecord(0,
                                                                         Modules.ScriptAnalysisPreparing,
                                                                         string.Format(CultureInfo.InvariantCulture, Modules.ScriptAnalysisModule, path));
                    analysisProgress.RecordType = ProgressRecordType.Processing;

                    // Write the progress using a static source ID so that all
                    // analysis messages get single-threaded in the progress pane (rather than nesting).
                    context.CurrentCommandProcessor.CommandRuntime.WriteProgress(typeof(ScriptAnalysis).FullName.GetHashCode(), analysisProgress);
                }
            }
            catch (InvalidOperationException)
            {
                // This may be called when we are not allowed to write progress,
                // So eat the invalid operation
            }

            string scriptContent = ReadScript(path);

            ParseError[] errors;
            var          moduleAst = (new Parser()).Parse(path, scriptContent, null, out errors, ParseMode.ModuleAnalysis);

            // Don't bother analyzing if there are syntax errors (we don't do semantic analysis which would
            // detect other errors that we also might choose to ignore, but it's slower.)
            if (errors.Length > 0)
            {
                return(null);
            }

            ExportVisitor exportVisitor = new ExportVisitor(forCompletion: false);

            moduleAst.Visit(exportVisitor);

            var result = new ScriptAnalysis
            {
                DiscoveredClasses        = exportVisitor.DiscoveredClasses,
                DiscoveredExports        = exportVisitor.DiscoveredExports,
                DiscoveredAliases        = new Dictionary <string, string>(),
                DiscoveredModules        = exportVisitor.DiscoveredModules,
                DiscoveredCommandFilters = exportVisitor.DiscoveredCommandFilters,
                AddsSelfToPath           = exportVisitor.AddsSelfToPath
            };

            if (result.DiscoveredCommandFilters.Count == 0)
            {
                result.DiscoveredCommandFilters.Add("*");
            }
            else
            {
                // Post-process aliases, as they are not exported by default
                List <WildcardPattern> patterns = new List <WildcardPattern>();
                foreach (string discoveredCommandFilter in result.DiscoveredCommandFilters)
                {
                    patterns.Add(WildcardPattern.Get(discoveredCommandFilter, WildcardOptions.IgnoreCase));
                }

                foreach (var pair in exportVisitor.DiscoveredAliases)
                {
                    string discoveredAlias = pair.Key;
                    if (SessionStateUtilities.MatchesAnyWildcardPattern(discoveredAlias, patterns, defaultValue: false))
                    {
                        result.DiscoveredAliases[discoveredAlias] = pair.Value;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 4
0
 private CommandProcessorBase PrepareFromAst(ExecutionContext context, out string resolvedCommandName)
 {
     string str;
     FunctionDefinitionAst ast2;
     ExportVisitor astVisitor = new ExportVisitor();
     Ast parent = this._commandAst;
     while (parent.Parent != null)
     {
         parent = parent.Parent;
     }
     parent.Visit(astVisitor);
     resolvedCommandName = this._commandAst.GetCommandName();
     CommandProcessorBase base2 = null;
     int num = 0;
     while (astVisitor.DiscoveredAliases.TryGetValue(resolvedCommandName, out str))
     {
         num++;
         if (num > 5)
         {
             break;
         }
         resolvedCommandName = str;
     }
     if (astVisitor.DiscoveredFunctions.TryGetValue(resolvedCommandName, out ast2))
     {
         ScriptBlock scriptblock = ast2.IsWorkflow ? CreateFakeScriptBlockForWorkflow(ast2) : new ScriptBlock(ast2, ast2.IsFilter);
         base2 = CommandDiscovery.CreateCommandProcessorForScript(scriptblock, context, true, context.EngineSessionState);
     }
     return base2;
 }
Exemplo n.º 5
0
        internal static ScriptAnalysis Analyze(string path, ExecutionContext context)
        {
            ModuleIntrinsics.Tracer.WriteLine("Analyzing path: {0}", path);

            try
            {
                if (Utils.PathIsUnc(path) && (context.CurrentCommandProcessor.CommandRuntime != null))
                {
                    ProgressRecord analysisProgress = new ProgressRecord(0,
                        Modules.ScriptAnalysisPreparing,
                        String.Format(CultureInfo.InvariantCulture, Modules.ScriptAnalysisModule, path));
                    analysisProgress.RecordType = ProgressRecordType.Processing;

                    // Write the progress using a static source ID so that all
                    // analysis messages get single-threaded in the progress pane (rather than nesting).
                    context.CurrentCommandProcessor.CommandRuntime.WriteProgress(typeof(ScriptAnalysis).FullName.GetHashCode(), analysisProgress);
                }
            }
            catch (InvalidOperationException)
            {
                // This may be called when we are not allowed to write progress,
                // So eat the invalid operation
            }

            string scriptContent = ReadScript(path);

            ParseError[] errors;
            var moduleAst = (new Parser()).Parse(path, scriptContent, null, out errors, ParseMode.ModuleAnalysis);

            // Don't bother analyzing if there are syntax errors (we don't do semantic analysis which would
            // detect other errors that we also might choose to ignore, but it's slower.)
            if (errors.Length > 0)
                return null;

            ExportVisitor exportVisitor = new ExportVisitor(forCompletion: false);
            moduleAst.Visit(exportVisitor);

            var result = new ScriptAnalysis
            {
                DiscoveredClasses = exportVisitor.DiscoveredClasses,
                DiscoveredExports = exportVisitor.DiscoveredExports,
                DiscoveredAliases = new Dictionary<string, string>(),
                DiscoveredModules = exportVisitor.DiscoveredModules,
                DiscoveredCommandFilters = exportVisitor.DiscoveredCommandFilters,
                AddsSelfToPath = exportVisitor.AddsSelfToPath
            };

            if (result.DiscoveredCommandFilters.Count == 0)
            {
                result.DiscoveredCommandFilters.Add("*");
            }
            else
            {
                // Post-process aliases, as they are not exported by default
                List<WildcardPattern> patterns = new List<WildcardPattern>();
                foreach (string discoveredCommandFilter in result.DiscoveredCommandFilters)
                {
                    patterns.Add(WildcardPattern.Get(discoveredCommandFilter, WildcardOptions.IgnoreCase));
                }

                foreach (var pair in exportVisitor.DiscoveredAliases)
                {
                    string discoveredAlias = pair.Key;
                    if (SessionStateUtilities.MatchesAnyWildcardPattern(discoveredAlias, patterns, defaultValue: false))
                    {
                        result.DiscoveredAliases[discoveredAlias] = pair.Value;
                    }
                }
            }

            return result;
        }