Esempio n. 1
0
        /// <summary>
        /// Execute one or more PowerShell script files to get language blocks.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sources"></param>
        /// <returns></returns>
        private static ILanguageBlock[] GetLanguageBlock(RunspaceContext context, Source[] sources)
        {
            var results = new List <ILanguageBlock>();
            var ps      = context.NewPowerShell();

            context.PushScope(RunspaceScope.Source);
            try
            {
                // Process each source
                foreach (var source in sources)
                {
                    // Process search file per source
                    foreach (var file in source.File)
                    {
                        if (file.Type != SourceType.Script)
                        {
                            continue;
                        }

                        ps.Commands.Clear();
                        if (!context.EnterSourceFile(file))
                        {
                            throw new FileNotFoundException(PSDocsResources.ScriptNotFound, file.Path);
                        }

                        var scriptAst = System.Management.Automation.Language.Parser.ParseFile(file.Path, out Token[] tokens, out ParseError[] errors);
Esempio n. 2
0
        /// <summary>
        /// Convert language blocks to a document builder.
        /// </summary>
        private static IDocumentBuilder[] ToDocumentBuilder(ILanguageBlock[] blocks, RunspaceContext context)
        {
            // Index rules by RuleId
            var results = new Dictionary <string, IDocumentBuilder>(StringComparer.OrdinalIgnoreCase);

            try
            {
                foreach (var block in blocks.OfType <ScriptDocumentBlock>())
                {
                    // Ignore rule blocks that don't match
                    if (!Match(context, block))
                    {
                        continue;
                    }
                    if (!results.ContainsKey(block.Id))
                    {
                        //results[block.RuleId] = block.Info;
                        results[block.Id] = new ScriptDocumentBuilder(block);
                    }
                }
            }
            finally
            {
                context.ExitSourceFile();
            }
            return(results.Values.ToArray());
        }
Esempio n. 3
0
        /// <summary>
        /// Executes get document builders from Document script blocks.
        /// </summary>
        internal static IDocumentBuilder[] GetDocumentBuilder(RunspaceContext context, Source[] source)
        {
            context.PushScope(RunspaceScope.Source);
            var blocks      = GetLanguageBlock(context, source);
            var documents   = ToDocument(blocks, context);
            var conventions = GetConventions(blocks, context);

            return(ToDocumentBuilder(documents, conventions));
        }
Esempio n. 4
0
 public RunspaceContext(PipelineContext pipeline)
 {
     CurrentThread       = this;
     _Scope              = new Stack <RunspaceScope>();
     Pipeline            = pipeline;
     _Runspace           = GetRunspace();
     _LocalizedDataCache = new Dictionary <string, Hashtable>();
     ExpressionCache     = new Dictionary <string, object>();
 }
Esempio n. 5
0
        internal PowerShell NewPowerShell()
        {
            CurrentThread = this;
            var runspace = GetRunspace();
            var ps       = PowerShell.Create();

            ps.Runspace = runspace;
            EnableLogging(ps);
            return(ps);
        }
Esempio n. 6
0
        /// <summary>
        /// Execute one or more PowerShell script files to get language blocks.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sources"></param>
        /// <returns></returns>
        private static ILanguageBlock[] GetLanguageBlock(RunspaceContext context, Source[] sources)
        {
            var results = new List <ILanguageBlock>();
            var ps      = context.NewPowerShell();

            try
            {
                // Process each source
                foreach (var source in sources)
                {
                    // Process search file per source
                    foreach (var file in source.File)
                    {
                        ps.Commands.Clear();
                        if (!context.EnterSourceFile(file))
                        {
                            throw new FileNotFoundException(PSDocsResources.ScriptNotFound, file.Path);
                        }

                        try
                        {
                            // Invoke script
                            ps.AddScript(string.Concat("& '", file.Path, "'"), true);
                            var invokeResults = ps.Invoke();

                            // Discovery has errors so skip this file
                            if (ps.HadErrors)
                            {
                                continue;
                            }

                            foreach (var ir in invokeResults)
                            {
                                if (ir.BaseObject is ScriptDocumentBlock block)
                                {
                                    results.Add(block);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            context.WriteRuntimeException(sourceFile: file.Path, inner: e);
                        }
                    }
                }
            }
            finally
            {
                context.ExitSourceFile();
                ps.Runspace = null;
                ps.Dispose();
            }
            return(results.ToArray());
        }
Esempio n. 7
0
 private void Dispose(bool disposing)
 {
     if (!_Disposed)
     {
         if (disposing)
         {
             if (Builder != null)
             {
                 Builder.Dispose();
                 Builder = null;
             }
             if (_Runspace != null)
             {
                 _Runspace.Dispose();
                 _Runspace = null;
             }
             CurrentThread = null;
         }
         _Disposed = true;
     }
 }
Esempio n. 8
0
 internal static IDocumentDefinition[] GetDocumentBlock(RunspaceContext runspace, Source[] source)
 {
     return(ToDocumentBlock(GetLanguageBlock(runspace, source), runspace));
 }
Esempio n. 9
0
 /// <summary>
 /// Executes get document builders from Document script blocks.
 /// </summary>
 internal static IDocumentBuilder[] GetDocumentBuilder(RunspaceContext runspace, Source[] source)
 {
     return(ToDocumentBuilder(ToDocumentBlock(GetLanguageBlock(runspace, source), runspace)));
 }
Esempio n. 10
0
 private static bool Match(RunspaceContext context, ScriptDocumentBlock block)
 {
     return(context.Pipeline.Filter.Match(block.Name, block.Tag));
 }
Esempio n. 11
0
 internal PSDocsDocument(RunspaceContext context)
     : base(context)
 {
 }
Esempio n. 12
0
 internal ScopedItem(RunspaceContext context)
 {
     _Context = context;
 }
Esempio n. 13
0
 /// <summary>
 /// Read YAML objects and return selectors.
 /// </summary>
 internal static IEnumerable <SelectorV1> GetSelector(RunspaceContext context, Source[] source)
 {
     return(ToSelectorV1(ReadYamlObjects(source, context), context));
 }
Esempio n. 14
0
 internal static void ImportResource(Source[] source, RunspaceContext context)
 {
     Import(ReadYamlObjects(source, context), context);
 }
Esempio n. 15
0
 internal static IDocumentDefinition[] GetDocumentBlock(RunspaceContext context, Source[] source)
 {
     return(ToDocument(GetLanguageBlock(context, source), context));
 }
Esempio n. 16
0
 public void EnterBuilder(ScriptDocumentBuilder builder)
 {
     CurrentThread = this;
     Builder       = builder;
     Pipeline.Option.SwitchScope(builder.Module);
 }
Esempio n. 17
0
 public void EnterBuilder(ScriptDocumentBuilder builder)
 {
     CurrentThread = this;
     Builder       = builder;
 }
Esempio n. 18
0
 internal PSDocs(RunspaceContext context)
 {
     _Context = context;
 }
Esempio n. 19
0
 internal LocalizedData(RunspaceContext context)
 {
     _Context = context;
 }
Esempio n. 20
0
 internal LocalizedDataVariable(RunspaceContext context)
     : this()
 {
     _Value = new LocalizedData(context);
 }
Esempio n. 21
0
 internal PSDocsSource(RunspaceContext context)
     : base(context)
 {
 }
Esempio n. 22
0
 internal DocumentContext(RunspaceContext runspaceContext)
 {
     Culture  = runspaceContext?.Culture;
     Metadata = new OrderedDictionary();
     Data     = new Hashtable();
 }
Esempio n. 23
0
        /// <summary>
        /// Execute one or more PowerShell script files to get language blocks.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sources"></param>
        /// <returns></returns>
        private static ILanguageBlock[] GetLanguageBlock(RunspaceContext context, Source[] sources)
        {
            var results = new Collection <ILanguageBlock>();
            var ps      = context.NewPowerShell();

            try
            {
                //context.EnterScope("[Discovery.Rule]");

                // Process scripts

                foreach (var source in sources)
                {
                    foreach (var file in source.File)
                    {
                        ps.Commands.Clear();

                        if (!context.TrySourceFile(file))
                        {
                            throw new FileNotFoundException(PSDocsResources.ScriptNotFound, file.Path);
                        }

                        //context.
                        //PipelineContext.CurrentThread.Source = source;
                        //PipelineContext.CurrentThread.VerboseRuleDiscovery(path: source.Path);
                        //PipelineContext.CurrentThread.UseSource(source: source);

                        try
                        {
                            // Invoke script
                            ps.AddScript(string.Concat("& '", file.Path, "'"), true);
                            var invokeResults = ps.Invoke();

                            if (ps.HadErrors)
                            {
                                // Discovery has errors so skip this file
                                continue;
                            }

                            foreach (var ir in invokeResults)
                            {
                                if (ir.BaseObject is ScriptDocumentBlock block)
                                {
                                    results.Add(block);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            context.WriteRuntimeException(sourceFile: file.Path, inner: e);
                        }
                    }
                }
            }
            finally
            {
                //context.ExitScope();
                context.ExitSourceFile();
                //PipelineContext.CurrentThread.Source = null;
                ps.Runspace = null;
                ps.Dispose();
            }
            return(results.ToArray());
        }
Esempio n. 24
0
 internal Configuration(RunspaceContext context)
 {
     _Context = context;
 }
Esempio n. 25
0
 internal PSDocs(RunspaceContext context)
     : base(context)
 {
 }