private static object Solidify(PSObject pso)
 {
     var solidifier = new PSObjectSolidifier();
     var o = solidifier.AsConcreteType(pso);
     Assert.NotNull(o);
     return o;
 }
Exemple #2
0
        private static object Solidify(PSObject pso)
        {
            var solidifier = new PSObjectSolidifier();
            var o          = solidifier.AsConcreteType(pso);

            Assert.NotNull(o);
            return(o);
        }
Exemple #3
0
        private IEnumerable <DynamicMemberDescriptor> CreateByItemMemberDescriptors(DynamicMemberSpecification spec, PSObject ps)
        {
            if (null == spec.AcrossSpecifier)
            {
                return(new DynamicMemberDescriptor[] { });
            }
            var list = new List <DynamicMemberDescriptor>();
            var acrossAccessorName = spec.AcrossSpecifier.Name;

            ps.Properties.Add(spec.AcrossSpecifier);
            var    sps   = new PSObjectSolidifier().AsConcreteType(ps) as SolidPSObjectBase;
            object oitem = sps.GetPropValue <object>(acrossAccessorName);

            ps.Properties.Remove(acrossAccessorName);

            var items = oitem as ICollection;

            if (null == items)
            {
                items = new object[] { oitem };
            }

            if (null == spec.IndexSpecifier)
            {
                for (int i = 0; i < items.Count; ++i)
                {
                    var byItemScript = String.Format("$this.'{1}' | select-object -index '{0}'", i, acrossAccessorName);
                    var prop         = new PSScriptProperty(
                        acrossAccessorName + i,
                        System.Management.Automation.ScriptBlock.Create(byItemScript));
                    var scale = (from sd in spec.ScaleDescriptors
                                 where sd.Key.IsMatch(prop.Name)
                                 select sd.Value).FirstOrDefault();
                    list.Add(new DynamicMemberDescriptor(prop, scale, "Index " + i));
                }
            }
            else
            {
                foreach (var item in items)
                {
                    var pso          = item.ToPSObject();
                    var tempProperty = new PSScriptProperty(
                        "_" + Guid.NewGuid().ToString("N"),
                        System.Management.Automation.ScriptBlock.Create(
                            "$this | " + spec.IndexSpecifier.GetterScript));
                    pso.Properties.Add(tempProperty);
                    var specValue = pso.Properties[tempProperty.Name].Value.ToPSObject();
                    pso.Properties.Remove(tempProperty.Name);
                    var name = specValue.Properties["Name"].Value;

                    var byItemScript =
                        String.Format("$this.'{1}' | {2} | where {{ $_.Name -eq '{0}' }} | select -expand Group",
                                      name, acrossAccessorName, spec.IndexSpecifier.GetterScript);
                    var prop = new PSScriptProperty(
                        acrossAccessorName + "_" + name,
                        System.Management.Automation.ScriptBlock.Create(byItemScript));
                    var scale = (from sd in spec.ScaleDescriptors
                                 where sd.Key.IsMatch(prop.Name)
                                 select sd.Value).FirstOrDefault();
                    list.Add(new DynamicMemberDescriptor(prop, scale, name));
                }
            }
            return(list);
        }
        private IEnumerable<DynamicMemberDescriptor> CreateByItemMemberDescriptors(DynamicMemberSpecification spec, PSObject ps)
        {
            if (null == spec.AcrossSpecifier)
            {
                return new DynamicMemberDescriptor[]{ };
            }
            var list = new List<DynamicMemberDescriptor>();
            var acrossAccessorName = spec.AcrossSpecifier.Name;
            ps.Properties.Add(spec.AcrossSpecifier);
            var sps = new PSObjectSolidifier().AsConcreteType(ps) as SolidPSObjectBase;
            object oitem = sps.GetPropValue<object>(acrossAccessorName);
            ps.Properties.Remove(acrossAccessorName);

            var items = oitem as ICollection;
            if (null == items)
            {
                items = new object[] { oitem };
            }

            if (null == spec.IndexSpecifier)
            {

                for (int i = 0; i < items.Count; ++i)
                {
                    var byItemScript = String.Format("$this.'{1}' | select-object -index '{0}'", i, acrossAccessorName);
                    var prop = new PSScriptProperty(
                        acrossAccessorName + i,
                        System.Management.Automation.ScriptBlock.Create(byItemScript));
                    var scale = ( from sd in spec.ScaleDescriptors
                                where sd.Key.IsMatch(prop.Name)
                                select sd.Value ).FirstOrDefault();
                    list.Add( new DynamicMemberDescriptor( prop, scale, "Index " + i ));
                }
            }
            else
            {
                foreach (var item in items)
                {
                    var pso = item.ToPSObject();
                    var tempProperty = new PSScriptProperty(
                        "_" + Guid.NewGuid().ToString("N"),
                        System.Management.Automation.ScriptBlock.Create(
                            "$this | " + spec.IndexSpecifier.GetterScript));
                    pso.Properties.Add(tempProperty);
                    var specValue = pso.Properties[tempProperty.Name].Value.ToPSObject();
                    pso.Properties.Remove(tempProperty.Name);
                    var name = specValue.Properties["Name"].Value;

                    var byItemScript =
                        String.Format("$this.'{1}' | {2} | where {{ $_.Name -eq '{0}' }} | select -expand Group",
                                      name, acrossAccessorName, spec.IndexSpecifier.GetterScript);
                    var prop = new PSScriptProperty(
                                 acrossAccessorName + "_" + name,
                                 System.Management.Automation.ScriptBlock.Create(byItemScript));
                    var scale = (from sd in spec.ScaleDescriptors
                                 where sd.Key.IsMatch(prop.Name)
                                 select sd.Value).FirstOrDefault();
                    list.Add( new DynamicMemberDescriptor( prop, scale, name));
                }
            }
            return list;
        }