コード例 #1
0
        internal Document[] BuildDocument(TargetObject targetObject)
        {
            _Runspace.EnterTargetObject(targetObject);
            var result = new List <Document>();

            for (var c = 0; c < Context.Option.Output.Culture.Length; c++)
            {
                _Runspace.EnterCulture(Context.Option.Output.Culture[c]);
                for (var i = 0; i < _Builder.Length; i++)
                {
                    foreach (var instanceName in Context.InstanceNameBinder.GetInstanceName(_Builder[i].Name))
                    {
                        _Runspace.EnterDocument(instanceName);
                        try
                        {
                            var document = _Builder[i].Process(_Runspace, targetObject.Value);
                            if (document != null)
                            {
                                result.Add(document);
                            }
                        }
                        finally
                        {
                            _Runspace.ExitDocument();
                        }
                    }
                }
            }
            return(result.ToArray());
        }
コード例 #2
0
 private static bool IsAcceptedType(TargetObject targetObject)
 {
     return(targetObject.Value.BaseObject is string ||
            targetObject.Value.BaseObject is InputFileInfo ||
            targetObject.Value.BaseObject is FileInfo ||
            targetObject.Value.BaseObject is Uri);
 }
コード例 #3
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());
        }
コード例 #4
0
        public static IEnumerable <TargetObject> ConvertFromPowerShellData(TargetObject targetObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string or a file
            if (!IsAcceptedType(targetObject))
            {
                return new TargetObject[] { targetObject }
            }
            ;

            var data       = ReadAsString(targetObject.Value, out InputFileInfo sourceInfo);
            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()));
                }
            }
            return(VisitItems(result, sourceInfo, next));
        }
コード例 #5
0
        public void Enqueue(PSObject sourceObject)
        {
            if (sourceObject == null)
            {
                return;
            }

            var targetObject = new TargetObject(sourceObject);

            if (_Input == null)
            {
                _Queue.Enqueue(targetObject);
                return;
            }

            // Visit the object, which may change or expand the object
            var input = _Input(targetObject);

            if (input == null)
            {
                return;
            }

            foreach (var item in input)
            {
                _Queue.Enqueue(item);
            }
        }
コード例 #6
0
        private static void NoteSource(TargetObject value, InputFileInfo source)
        {
            if (value == null || source == null)
            {
                return;
            }

            value.SetSourceInfo(source);
        }
コード例 #7
0
        public static IEnumerable <TargetObject> ConvertFromJson(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 json  = ReadAsString(targetObject.Value, out InputFileInfo sourceInfo);
            var value = JsonConvert.DeserializeObject <PSObject[]>(json, new PSObjectArrayJsonConverter());

            return(VisitItems(value, sourceInfo, next));
        }
コード例 #8
0
        private static string GetPathExtension(TargetObject targetObject)
        {
            if (targetObject.Value.BaseObject is InputFileInfo inputFileInfo)
            {
                return(inputFileInfo.Extension);
            }

            if (targetObject.Value.BaseObject is FileInfo fileInfo)
            {
                return(fileInfo.Extension);
            }

            if (targetObject.Value.BaseObject is Uri uri)
            {
                return(Path.GetExtension(uri.OriginalString));
            }

            return(null);
        }
コード例 #9
0
 protected override void ProcessObject(TargetObject targetObject)
 {
     try
     {
         var doc = BuildDocument(targetObject);
         for (var i = 0; doc != null && i < doc.Length; i++)
         {
             var result = WriteDocument(doc[i]);
             if (result != null)
             {
                 Context.WriteOutput(result);
                 _Completed.Add(result);
             }
         }
     }
     finally
     {
         _Runspace.ExitTargetObject();
     }
 }
コード例 #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 });
        }
コード例 #11
0
        public static IEnumerable <TargetObject> ReadObjectPath(TargetObject targetObject, VisitTargetObject source, string objectPath, bool caseSensitive)
        {
            if (!ObjectHelper.GetField(bindingContext: null, targetObject: targetObject, name: objectPath, caseSensitive: caseSensitive, value: out object nestedObject))
            {
                return(EmptyArray);
            }

            var nestedType = nestedObject.GetType();

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

                return(result.ToArray());
            }
            else
            {
                return(new TargetObject[] { new TargetObject(PSObject.AsPSObject(nestedObject)) });
            }
        }
コード例 #12
0
 protected abstract void ProcessObject(TargetObject targetObject);
コード例 #13
0
 public static IEnumerable <TargetObject> PassThru(TargetObject targetObject)
 {
     yield return(targetObject);
 }
コード例 #14
0
 public bool TryDequeue(out TargetObject targetObject)
 {
     return(_Queue.TryDequeue(out targetObject));
 }