Esempio n. 1
0
        public static IEnumerable <PSObject> ConvertFromYaml(PSObject sourceObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string, file or URI
            if (!IsAcceptedType(sourceObject: sourceObject))
            {
                return new PSObject[] { sourceObject }
            }
            ;

            var d = new DeserializerBuilder()
                    .IgnoreUnmatchedProperties()
                    .WithTypeConverter(new PSObjectYamlTypeConverter())
                    .WithNodeTypeResolver(new PSObjectYamlTypeResolver())
                    .Build();

            var reader = ReadAsReader(sourceObject, out PSSourceInfo source);
            var parser = new YamlDotNet.Core.Parser(reader);

            parser.Expect <StreamStart>();

            var result = new List <PSObject>();

            while (parser.Accept <DocumentStart>())
            {
                var item = d.Deserialize <PSObject>(parser: parser);

                if (item == null)
                {
                    continue;
                }

                NoteSource(item, source);
                var items = next(item);

                if (items == null)
                {
                    continue;
                }

                result.AddRange(items);
            }

            if (result.Count == 0)
            {
                return(null);
            }

            return(result.ToArray());
        }
Esempio n. 2
0
        public static IEnumerable <PSObject> ConvertFromJson(PSObject sourceObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string, file or URI
            if (!IsAcceptedType(sourceObject: sourceObject))
            {
                return new PSObject[] { sourceObject }
            }
            ;

            var json  = ReadAsString(sourceObject, out PSSourceInfo source);
            var value = JsonConvert.DeserializeObject <PSObject[]>(json, new PSObjectArrayJsonConverter());

            NoteSource(value, source);
            return(VisitItems(value, next));
        }
        public static IEnumerable <PSObject> ReadObjectPath(PSObject sourceObject, VisitTargetObject source, string objectPath, bool caseSensitive)
        {
            if (!ObjectHelper.GetField(bindingContext: null, targetObject: sourceObject, name: objectPath, caseSensitive: caseSensitive, value: out object nestedObject))
            {
                return(EmptyArray);
            }

            var nestedType = nestedObject.GetType();

            if (typeof(IEnumerable).IsAssignableFrom(nestedType))
            {
                var result = new List <PSObject>();
                foreach (var item in (nestedObject as IEnumerable))
                {
                    result.Add(PSObject.AsPSObject(item));
                }

                return(result.ToArray());
            }
            else
            {
                return(new PSObject[] { PSObject.AsPSObject(nestedObject) });
            }
        }
Esempio n. 4
0
        public static IEnumerable <PSObject> DetectInputFormat(PSObject sourceObject, VisitTargetObject next)
        {
            string pathExtension = null;

            if (sourceObject.BaseObject is FileInfo)
            {
                var fileInfo = sourceObject.BaseObject as FileInfo;
                pathExtension = fileInfo.Extension;
            }
            else if (sourceObject.BaseObject is Uri)
            {
                var uri = sourceObject.BaseObject as Uri;
                pathExtension = Path.GetExtension(uri.OriginalString);
            }

            // Handle JSON
            if (pathExtension == ".json")
            {
                return(ConvertFromJson(sourceObject: sourceObject, next: next));
            }
            // Handle YAML
            else if (pathExtension == ".yaml" || pathExtension == ".yml")
            {
                return(ConvertFromYaml(sourceObject: sourceObject, next: next));
            }
            // Handle Markdown
            else if (pathExtension == ".md")
            {
                return(ConvertFromMarkdown(sourceObject: sourceObject, next: next));
            }
            return(new PSObject[] { sourceObject });
        }
Esempio n. 5
0
        public static IEnumerable <PSObject> ConvertFromMarkdown(PSObject sourceObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string or a file
            if (!IsAcceptedType(sourceObject: sourceObject))
            {
                return new PSObject[] { sourceObject }
            }
            ;

            var markdown = ReadAsString(sourceObject, out PSSourceInfo source);
            var value    = MarkdownConvert.DeserializeObject(markdown);

            NoteSource(value, source);
            return(VisitItems(value, next));
        }
Esempio n. 6
0
 public PipelineReader(VisitTargetObject input, string[] inputPath)
 {
     _Input     = input;
     _InputPath = inputPath;
     _Queue     = new ConcurrentQueue <PSObject>();
 }
        public static IEnumerable <PSObject> DetectInputFormat(PSObject sourceObject, VisitTargetObject next)
        {
            var pathExtension = GetPathExtension(sourceObject);

            // Handle JSON
            if (pathExtension == ".json")
            {
                return(ConvertFromJson(sourceObject, next));
            }
            // Handle YAML
            else if (pathExtension == ".yaml" || pathExtension == ".yml")
            {
                return(ConvertFromYaml(sourceObject, next));
            }
            // Handle Markdown
            else if (pathExtension == ".md")
            {
                return(ConvertFromMarkdown(sourceObject, next));
            }
            // Handle PowerShell Data
            else if (pathExtension == ".psd1")
            {
                return(ConvertFromPowerShellData(sourceObject, next));
            }
            return(new PSObject[] { sourceObject });
        }
        public static IEnumerable <PSObject> ConvertFromPowerShellData(PSObject sourceObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string or a file
            if (!IsAcceptedType(sourceObject: sourceObject))
            {
                return new PSObject[] { sourceObject }
            }
            ;

            var data       = ReadAsString(sourceObject, out PSSourceInfo source);
            var ast        = System.Management.Automation.Language.Parser.ParseInput(data, out _, out _);
            var hashtables = ast.FindAll(item => item is System.Management.Automation.Language.HashtableAst, false);

            if (hashtables == null)
            {
                return(EmptyArray);
            }

            var result = new List <PSObject>();

            foreach (var hashtable in hashtables)
            {
                if (hashtable?.Parent?.Parent?.Parent?.Parent == ast)
                {
                    result.Add(PSObject.AsPSObject(hashtable.SafeGetValue()));
                }
            }
            var value = result.ToArray();

            NoteSource(value, source);
            return(VisitItems(value, next));
        }
Esempio n. 9
0
        public static IEnumerable <TargetObject> ConvertFromYaml(TargetObject targetObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string, file or URI
            if (!IsAcceptedType(targetObject))
            {
                return new TargetObject[] { targetObject }
            }
            ;

            var d = new DeserializerBuilder()
                    .IgnoreUnmatchedProperties()
                    .WithTypeConverter(new PSObjectYamlTypeConverter())
                    .WithNodeTypeResolver(new PSObjectYamlTypeResolver())
                    .Build();

            var reader = ReadAsReader(targetObject.Value, out InputFileInfo sourceInfo);
            var parser = new YamlDotNet.Core.Parser(reader);
            var result = new List <TargetObject>();

            parser.TryConsume <StreamStart>(out _);
            while (parser.Current is DocumentStart)
            {
                var item = d.Deserialize <PSObject>(parser: parser);

                if (item == null)
                {
                    continue;
                }

                result.AddRange(VisitItem(item, sourceInfo, next));
            }
            return(result.Count == 0 ? EmptyArray : result.ToArray());
        }
Esempio n. 10
0
        public static IEnumerable <TargetObject> DetectInputFormat(TargetObject targetObject, VisitTargetObject next)
        {
            var pathExtension = GetPathExtension(targetObject);

            // Handle JSON
            if (pathExtension == ".json" || pathExtension == ".jsonc")
            {
                return(ConvertFromJson(targetObject, next));
            }
            // Handle YAML
            else if (pathExtension == ".yaml" || pathExtension == ".yml")
            {
                return(ConvertFromYaml(targetObject, next));
            }
            // Handle PowerShell Data
            else if (pathExtension == ".psd1")
            {
                return(ConvertFromPowerShellData(targetObject, next));
            }
            return(new TargetObject[] { targetObject });
        }
Esempio n. 11
0
        private static IEnumerable <TargetObject> VisitItems(IEnumerable <PSObject> value, InputFileInfo sourceInfo, VisitTargetObject next)
        {
            if (value == null)
            {
                return(EmptyArray);
            }

            var result = new List <TargetObject>();

            foreach (var item in value)
            {
                result.AddRange(VisitItem(item, sourceInfo, next));
            }

            return(result.Count == 0 ? EmptyArray : result.ToArray());
        }
Esempio n. 12
0
        private static IEnumerable <TargetObject> VisitItem(PSObject value, InputFileInfo sourceInfo, VisitTargetObject next)
        {
            if (value == null)
            {
                return(EmptyArray);
            }

            var items = next(new TargetObject(value));

            if (items == null)
            {
                return(EmptyArray);
            }

            foreach (var i in items)
            {
                NoteSource(i, sourceInfo);
            }

            return(items);
        }
Esempio n. 13
0
 public PipelineStream(VisitTargetObject input, InputFileInfo[] inputPath)
 {
     _Input     = input;
     _InputPath = inputPath;
     _Queue     = new ConcurrentQueue <TargetObject>();
 }