Пример #1
0
 Document IDocumentBuilder.Process(RunspaceContext context, PSObject sourceObject)
 {
     context.EnterBuilder(this);
     context.EnterSourceFile(_Block.Source);
     try
     {
         _Parent  = new Stack <SectionNode>();
         _Current = Document = new Document(context.InstanceName, context.Culture);
         Document.AddNodes(_Block.Body.Invoke());
         return(Document);
     }
     catch (Exception e)
     {
         context.WriteRuntimeException(_Block.SourcePath, e);
         return(null);
     }
     finally
     {
         Document = null;
         _Current = null;
         _Parent.Clear();
         _Parent = null;
         context.ExitBuilder();
     }
 }
Пример #2
0
        public static Rule[] GetRule(Source[] source, RunspaceContext context, bool includeDependencies)
        {
            var builder = new DependencyGraphBuilder <Rule>(includeDependencies: includeDependencies);

            builder.Include(items: ToRule(GetLanguageBlock(context, source), context), filter: (b) => Match(context, b));
            return(builder.GetItems());
        }
Пример #3
0
 internal InvokePipeline(PipelineContext context, Source[] source)
     : base(context, source)
 {
     _Runspace  = new RunspaceContext(Context);
     _Builder   = HostHelper.GetDocumentBuilder(_Runspace, Source);
     _Processor = new MarkdownProcessor();
 }
Пример #4
0
        /// <summary>
        /// Execute one or more PowerShell script files to get language blocks.
        /// </summary>
        /// <param name="sources"></param>
        /// <returns></returns>
        private static IEnumerable <ILanguageBlock> GetLanguageBlock(RunspaceContext context, Source[] sources)
        {
            var results = new Collection <ILanguageBlock>();
            var ps      = context.GetPowerShell();

            try
            {
                context.Writer.EnterScope("[Discovery.Rule]");
                PipelineContext.CurrentThread.ExecutionScope = ExecutionScope.Script;

                // Process scripts
                foreach (var source in sources)
                {
                    foreach (var file in source.File)
                    {
                        if (file.Type != RuleSourceType.Script)
                        {
                            continue;
                        }

                        ps.Commands.Clear();
                        context.VerboseRuleDiscovery(path: file.Path);
                        context.EnterSourceScope(source: file);

                        var scriptAst = System.Management.Automation.Language.Parser.ParseFile(file.Path, out Token[] tokens, out ParseError[] errors);
Пример #5
0
        public static DependencyGraph <RuleBlock> GetRuleBlockGraph(Source[] source, RunspaceContext context)
        {
            var builder = new DependencyGraphBuilder <RuleBlock>(includeDependencies: true);

            builder.Include(items: GetLanguageBlock(context, source).OfType <RuleBlock>(), filter: (b) => Match(context, b));
            return(builder.Build());
        }
Пример #6
0
        private static RuleHelpInfo GetHelpInfo(RunspaceContext context, string name)
        {
            if (string.IsNullOrEmpty(context.Source.File.HelpPath))
            {
                return(null);
            }

            var fileName = string.Concat(name, ".md");
            var culture  = context.Pipeline.Culture;

            for (var i = 0; i < culture.Length; i++)
            {
                var path = Path.Combine(context.Source.File.HelpPath, culture[i], fileName);
                if (!File.Exists(path))
                {
                    continue;
                }

                if (TryDocument(path, out RuleDocument document))
                {
                    return(new RuleHelpInfo(name: name, displayName: document.Name ?? name, moduleName: context.Source.File.ModuleName)
                    {
                        Synopsis = document.Synopsis?.Text,
                        Description = document.Description?.Text,
                        Recommendation = document.Recommendation?.Text ?? document.Synopsis?.Text,
                        Notes = document.Notes?.Text,
                        Links = GetLinks(document.Links),
                        Annotations = document.Annotations?.ToHashtable()
                    });
                }
            }
            return(null);
        }
        private static RuleHelpInfo GetHelpInfo(RunspaceContext context, string name)
        {
            if (string.IsNullOrEmpty(context.Source.File.HelpPath))
            {
                return(null);
            }

            var helpFileName = string.Concat(name, Markdown_Extension);
            var path         = context.GetLocalizedPath(helpFileName);

            if (path == null || !TryDocument(path, out RuleDocument document))
            {
                return(null);
            }

            return(new RuleHelpInfo(name: name, displayName: document.Name ?? name, moduleName: context.Source.File.ModuleName)
            {
                Synopsis = document.Synopsis?.Text,
                Description = document.Description?.Text,
                Recommendation = document.Recommendation?.Text ?? document.Synopsis?.Text,
                Notes = document.Notes?.Text,
                Links = GetLinks(document.Links),
                Annotations = document.Annotations?.ToHashtable()
            });
        }
Пример #8
0
        public void ReadBaseline()
        {
            var context  = new RunspaceContext(PipelineContext.New(GetOption(), null, null, new BaselineContext(), null), null);
            var baseline = HostHelper.GetBaseline(GetSource(), context).ToArray();

            Assert.NotNull(baseline);
            Assert.Equal("TestBaseline1", baseline[0].Name);
        }
Пример #9
0
        public void ReadModuleConfig()
        {
            var context       = new RunspaceContext(PipelineContext.New(GetOption(), null, null, new OptionContext(), null), null);
            var configuration = HostHelper.GetModuleConfig(GetSource(), context).ToArray();

            Assert.NotNull(configuration);
            Assert.Equal("Configuration1", configuration[0].Name);
        }
Пример #10
0
 internal InvokePipeline(PipelineContext context, Source[] source)
     : base(context, source)
 {
     _Runspace = new RunspaceContext(Context);
     HostHelper.ImportResource(Source, _Runspace);
     _Builder   = HostHelper.GetDocumentBuilder(_Runspace, Source);
     _Processor = new MarkdownProcessor();
     _Completed = new List <IDocumentResult>();
 }
Пример #11
0
        public override void Process(RunspaceContext context, IEnumerable input)
        {
            var culture    = context.Builder.Document.Culture;
            var rootedPath = PSDocumentOption.GetRootedPath(context.Pipeline.Option.Output.Path);
            var outputPath = !string.IsNullOrEmpty(culture) && context.Pipeline.Option.Output?.Culture?.Length > 1 ?
                             Path.Combine(rootedPath, culture) : rootedPath;

            context.DocumentContext.InstanceName = context.Builder.Document.Name;
            context.DocumentContext.OutputPath   = outputPath;
        }
        private PowerShell GetCondition(RunspaceContext context)
        {
            var result = context.GetPowerShell();

            result.AddCommand(new CmdletInfo(InvokeBlockCmdletName, typeof(InvokeRuleBlockCommand)));
            result.AddParameter(InvokeBlockCmdlet_TypeParameter, Type);
            result.AddParameter(InvokeBlockCmdlet_IfParameter, If);
            result.AddParameter(InvokeBlockCmdlet_BodyParameter, Body);
            return(result);
        }
Пример #13
0
        public void GetDocumentBuilder()
        {
            var source  = GetSource();
            var context = new RunspaceContext(new PipelineContext(GetOption(), null, null, null, null, null));

            HostHelper.ImportResource(source, context);
            var actual = HostHelper.GetDocumentBuilder(context, source);

            Assert.Equal(9, actual.Length);
        }
Пример #14
0
        public void ReadSelector()
        {
            var context  = new RunspaceContext(new PipelineContext(GetOption(), null, null, null, null, null));
            var selector = HostHelper.GetSelector(context, GetSource()).ToArray();

            Assert.NotNull(selector);
            Assert.Equal(31, selector.Length);

            Assert.Equal("BasicSelector", selector[0].Name);
            Assert.Equal("YamlAllOf", selector[4].Name);
        }
Пример #15
0
 /// <summary>
 /// Creates a clone of the given runspace through which another
 /// runspace was attached.  Sets the IsAttached property of the
 /// resulting RunspaceDetails object to true.
 /// </summary>
 /// <param name="runspaceDetails">
 /// The RunspaceDetails object which the new object based.
 /// </param>
 /// <param name="runspaceContext">
 /// The RunspaceContext of the runspace.
 /// </param>
 /// <param name="sessionDetails">
 /// The SessionDetails for the runspace.
 /// </param>
 /// <returns>
 /// A new RunspaceDetails instance for the attached runspace.
 /// </returns>
 public static RunspaceDetails CreateFromContext(
     RunspaceDetails runspaceDetails,
     RunspaceContext runspaceContext,
     SessionDetails sessionDetails)
 {
     return
         (new RunspaceDetails(
              runspaceDetails.Runspace,
              sessionDetails,
              runspaceDetails.PowerShellVersion,
              runspaceDetails.Location,
              runspaceContext,
              runspaceDetails.ConnectionString));
 }
Пример #16
0
        private static LanguageScriptBlock ConventionBlock(RunspaceContext context, ScriptBlock block)
        {
            if (block == null)
            {
                return(null);
            }

            // Create PS instance for execution
            var ps = context.NewPowerShell();

            ps.AddCommand(new CmdletInfo(InvokeCmdletName, typeof(InvokeConventionCommand)));
            ps.AddParameter(InvokeCmdlet_BodyParameter, block);
            return(new LanguageScriptBlock(ps));
        }
Пример #17
0
 /// <summary>
 /// Creates a new instance of the RunspaceDetails class.
 /// </summary>
 /// <param name="runspace">
 /// The runspace for which this instance contains details.
 /// </param>
 /// <param name="sessionDetails">
 /// The SessionDetails for the runspace.
 /// </param>
 /// <param name="powerShellVersion">
 /// The PowerShellVersionDetails of the runspace.
 /// </param>
 /// <param name="runspaceLocation">
 /// The RunspaceLocation of the runspace.
 /// </param>
 /// <param name="runspaceContext">
 /// The RunspaceContext of the runspace.
 /// </param>
 /// <param name="connectionString">
 /// The connection string of the runspace.
 /// </param>
 public RunspaceDetails(
     Runspace runspace,
     SessionDetails sessionDetails,
     PowerShellVersionDetails powerShellVersion,
     RunspaceLocation runspaceLocation,
     RunspaceContext runspaceContext,
     string connectionString)
 {
     this.Runspace          = runspace;
     this.SessionDetails    = sessionDetails;
     this.PowerShellVersion = powerShellVersion;
     this.Location          = runspaceLocation;
     this.Context           = runspaceContext;
     this.ConnectionString  = connectionString;
 }
Пример #18
0
 /// <summary>
 /// Creates a new RunspaceDetails object from a remote
 /// debugging session.
 /// </summary>
 /// <param name="runspaceDetails">
 /// The RunspaceDetails object which the new object based.
 /// </param>
 /// <param name="runspaceLocation">
 /// The RunspaceLocation of the runspace.
 /// </param>
 /// <param name="runspaceContext">
 /// The RunspaceContext of the runspace.
 /// </param>
 /// <param name="sessionDetails">
 /// The SessionDetails for the runspace.
 /// </param>
 /// <returns>
 /// A new RunspaceDetails instance for the attached runspace.
 /// </returns>
 public static RunspaceDetails CreateFromDebugger(
     RunspaceDetails runspaceDetails,
     RunspaceLocation runspaceLocation,
     RunspaceContext runspaceContext,
     SessionDetails sessionDetails)
 {
     // TODO: Get the PowerShellVersion correctly!
     return
         (new RunspaceDetails(
              runspaceDetails.Runspace,
              sessionDetails,
              runspaceDetails.PowerShellVersion,
              runspaceLocation,
              runspaceContext,
              runspaceDetails.ConnectionString));
 }
Пример #19
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _Processor = null;
                if (_Builder != null)
                {
                    for (var i = 0; i < _Builder.Length; i++)
                    {
                        _Builder[i].Dispose();
                    }

                    _Builder = null;
                }
                _Runspace.Dispose();
                _Runspace = null;
            }
            base.Dispose(disposing);
        }
Пример #20
0
        private void EndConventions(RunspaceContext context, IEnumerable results)
        {
            if (_Conventions == null || _Conventions.Length == 0)
            {
                return;
            }

            try
            {
                context.PushScope(RunspaceScope.ConventionEnd);
                for (var i = 0; i < _Conventions.Length; i++)
                {
                    _Conventions[i].End(context, results);
                }
            }
            finally
            {
                context.PopScope();
            }
        }
Пример #21
0
        private void ProcessConventions(RunspaceContext context, PSObject sourceObject)
        {
            if (_Conventions == null || _Conventions.Length == 0)
            {
                return;
            }

            try
            {
                context.PushScope(RunspaceScope.ConventionProcess);
                for (var i = 0; i < _Conventions.Length; i++)
                {
                    _Conventions[i].Process(context, new PSObject[] { sourceObject });
                }
            }
            finally
            {
                context.PopScope();
            }
        }
Пример #22
0
 internal GetPipeline(PipelineContext context, Source[] source)
     : base(context, source)
 {
     _Runspace = new RunspaceContext(Context);
 }
Пример #23
0
 internal GetPipeline(PipelineContext context, Source[] source)
     : base(context, source)
 {
     _Runspace = new RunspaceContext(Context);
     HostHelper.ImportResource(Source, _Runspace);
 }
Пример #24
0
 public static RuleHelpInfo[] GetRuleHelp(Source[] source, RunspaceContext context)
 {
     return(ToRuleHelp(GetLanguageBlock(context, source), context));
 }
Пример #25
0
 internal PSRule(RunspaceContext context)
 {
     _Context = context;
 }
Пример #26
0
 public static IEnumerable <Baseline> GetBaseline(Source[] source, RunspaceContext context)
 {
     return(ToBaseline(ReadYamlObjects(source, context), context));
 }
Пример #27
0
 public static void ImportResource(Source[] source, RunspaceContext context)
 {
     Import(ReadYamlObjects(source, context), context);
 }
 public override void Begin(RunspaceContext context, IEnumerable input)
 {
     InvokeConventionBlock(_Begin, input);
 }
 public override void End(RunspaceContext context, IEnumerable input)
 {
     InvokeConventionBlock(_End, input);
 }
 public override void Process(RunspaceContext context, IEnumerable input)
 {
     InvokeConventionBlock(_Process, input);
 }