コード例 #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);
コード例 #2
0
ファイル: HostHelper.cs プロジェクト: wonsil/PSDocs
        /// <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());
        }