public override bool GenerateDynamicParameters()
        {
            var packageProvider = SelectProviders(ProviderName).ReEnumerable();

            // if more than one provider is selected, this will never work
            if (packageProvider.Count() != 1)
            {
                return(false);
            }

            // if the provider is selected, we can get package metadata keys from the provider
            foreach (var md in packageProvider.First().GetDynamicOptions(OptionCategory.Source, this))
            {
                if (DynamicParameterDictionary.ContainsKey(md.Name))
                {
                    // for now, we're just going to mark the existing parameter as also used by the second provider to specify it.
                    (DynamicParameterDictionary[md.Name] as CustomRuntimeDefinedParameter).Options.Add(md);
                }
                else
                {
                    DynamicParameterDictionary.Add(md.Name, new CustomRuntimeDefinedParameter(md));
                }
            }
            return(true);
        }
Exemple #2
0
        protected override void GenerateCmdletSpecificParameters(Dictionary <string, object> unboundArguments)
        {
            if (!IsInvocation)
            {
                var providerNames  = PackageManagementService.AllProviderNames;
                var whatsOnCmdline = GetDynamicParameterValue <string[]>("ProviderName");
                if (whatsOnCmdline != null)
                {
                    providerNames = providerNames.Concat(whatsOnCmdline).Distinct();
                }

                DynamicParameterDictionary.AddOrSet("ProviderName", new RuntimeDefinedParameter("ProviderName", typeof(string[]), new Collection <Attribute> {
                    new ParameterAttribute {
                        ValueFromPipelineByPropertyName = true,
                        ParameterSetName = Constants.ParameterSets.PackageBySearchSet
                    },
                    new AliasAttribute("Provider"),
                    new ValidateSetAttribute(providerNames.ToArray())
                }));
            }
            else
            {
                DynamicParameterDictionary.AddOrSet("ProviderName", new RuntimeDefinedParameter("ProviderName", typeof(string[]), new Collection <Attribute> {
                    new ParameterAttribute {
                        ValueFromPipelineByPropertyName = true,
                        ParameterSetName = Constants.ParameterSets.PackageBySearchSet
                    },
                    new AliasAttribute("Provider")
                }));
            }
        }
Exemple #3
0
        public object GetDynamicParameters()
        {
            // CompletionCompleters.
            // CommandCompletion.
            if (DynamicParameterDictionary.IsNullOrEmpty())
            {
                if (IsOverridden(Constants.GenerateDynamicParametersMethod))
                {
                    AsyncRun(GenerateDynamicParameters);
                }
            }

            return(DynamicParameterDictionary);
        }
Exemple #4
0
        public override bool GenerateDynamicParameters()
        {
            // if the provider (or source) is selected, we can get package metadata keys from the provider
            //var providers = SelectedProviders.ToArray();


            if (_reentrancyLock.WaitOne(0))
            {
                // we're in here already.
                // this happens because we're asking for the parameters below, and it creates a new instance to get them.
                // we don't want dynamic parameters for that call, so let's get out.
                return(true);
            }
            _reentrancyLock.Set();

            try {
                foreach (var md in SelectedProviders.SelectMany(provider => _optionCategories.SelectMany(category => provider.GetDynamicOptions(category, this))))
                {
                    // check if the dynamic parameter is a static parameter first.
                    // this can happen if we make a common dynamic parameter into a proper static one
                    // and a provider didn't know that yet.

                    if (MyInvocation.MyCommand.Parameters.ContainsKey(md.Name))
                    {
                        // don't add it.
                        continue;
                    }

                    // foreach (var md in _optionCategories.SelectMany(category => providers.SelectMany(provider => provider.GetDynamicOptions(category, this)))) {
                    if (DynamicParameterDictionary.ContainsKey(md.Name))
                    {
                        // todo: if the dynamic parameters from two providers aren't compatible, then what?

                        // for now, we're just going to mark the existing parameter as also used by the second provider to specify it.
                        (DynamicParameterDictionary[md.Name] as CustomRuntimeDefinedParameter).Options.Add(md);
                    }
                    else
                    {
                        DynamicParameterDictionary.Add(md.Name, new CustomRuntimeDefinedParameter(md));
                    }
                }
            } finally {
                _reentrancyLock.Reset();
            }

            return(true);
        }
Exemple #5
0
        protected bool GenerateCommonDynamicParameters()
        {
            if (IsInvocation)
            {
                // only generate these parameters if there is an actual call happening.
                // this prevents get-help from showing them.
                DynamicParameterDictionary.Add("Timeout", new RuntimeDefinedParameter("Timeout", typeof(int), new Collection <Attribute> {
                    new ParameterAttribute()
                }));

                DynamicParameterDictionary.Add("Responsiveness", new RuntimeDefinedParameter("Responsiveness", typeof(int), new Collection <Attribute> {
                    new ParameterAttribute()
                }));

                DynamicParameterDictionary.Add("MessageResolver", new RuntimeDefinedParameter("MessageResolver", typeof(GetMessageString), new Collection <Attribute> {
                    new ParameterAttribute()
                }));
            }
            return(true);
        }
Exemple #6
0
        protected override void GenerateCmdletSpecificParameters(Dictionary <string, object> unboundArguments)
        {
#if DEEP_DEBUG
            Console.WriteLine("»» Entering GCSP ");
#endif
            if (!IsInvocation)
            {
#if DEEP_DEBUG
                Console.WriteLine("»»» Does not appear to be Invocation (locked:{0})", IsReentrantLocked);
#endif
                var providerNames  = PackageManagementService.AllProviderNames;
                var whatsOnCmdline = GetDynamicParameterValue <string[]>("ProviderName");
                if (whatsOnCmdline != null)
                {
                    providerNames = providerNames.Concat(whatsOnCmdline).Distinct();
                }

                DynamicParameterDictionary.AddOrSet("ProviderName", new RuntimeDefinedParameter("ProviderName", typeof(string[]), new Collection <Attribute> {
                    new ParameterAttribute {
                        ValueFromPipelineByPropertyName = true,
                        ParameterSetName = Constants.ParameterSets.PackageBySearchSet
                    },
                    new AliasAttribute("Provider"),
                    new ValidateSetAttribute(providerNames.ToArray())
                }));
            }
            else
            {
#if DEEP_DEBUG
                Console.WriteLine("»»» Does appear to be Invocation (locked:{0})", IsReentrantLocked);
#endif
                DynamicParameterDictionary.AddOrSet("ProviderName", new RuntimeDefinedParameter("ProviderName", typeof(string[]), new Collection <Attribute> {
                    new ParameterAttribute {
                        ValueFromPipelineByPropertyName = true,
                        ParameterSetName = Constants.ParameterSets.PackageBySearchSet
                    },
                    new AliasAttribute("Provider")
                }));
            }
        }
        public override bool GenerateDynamicParameters()
        {
            var packageProvider = PackageManagementService.SelectProviders(ProviderName, this).FirstOrDefault();

            if (packageProvider == null)
            {
                return(false);
            }

            // if the provider is selected, we can get package metadata keys from the provider
            foreach (var md in packageProvider.GetDynamicOptions(OptionCategory.Source, this))
            {
                if (DynamicParameterDictionary.ContainsKey(md.Name))
                {
                    // for now, we're just going to mark the existing parameter as also used by the second provider to specify it.
                    (DynamicParameterDictionary[md.Name] as CustomRuntimeDefinedParameter).Options.Add(md);
                }
                else
                {
                    DynamicParameterDictionary.Add(md.Name, new CustomRuntimeDefinedParameter(md));
                }
            }
            return(true);
        }
Exemple #8
0
        public override bool GenerateDynamicParameters()
        {
            var thisIsFirstObject = false;

            try {
                if (!IsReentrantLocked)
                {
                    // we're not locked at this point. Let's turn on the lock.
                    IsReentrantLocked = true;
                    thisIsFirstObject = true;

                    try {
                        // do all the work that we need to during the lock
                        // this includes:
                        //      one-time-per-call work
                        //      any access to MyInvocation.MyCommand.*
                        //      modifying parameter validation sets
                        //

                        if (MyInvocation != null && MyInvocation.MyCommand != null && MyInvocation.MyCommand.Parameters != null)
                        {
                            GetType().AddOrSet(MyInvocation.MyCommand.Parameters, "MyInvocation.MyCommand.Parameters");
                        }
#if DEEP_DEBUG
                        else
                        {
                            if (MyInvocation == null)
                            {
                                Console.WriteLine("»»» Attempt to get parameters MyInvocation == NULL");
                            }
                            else
                            {
                                if (MyInvocation.MyCommand == null)
                                {
                                    Console.WriteLine("»»» Attempt to get parameters MyCommand == NULL");
                                }
                                else
                                {
                                    Console.WriteLine("»»» Attempt to get parameters Parameters == NULL");
                                }
                            }
                        }
#endif


                        // the second time, it will generate all the parameters, including the dynamic ones.
                        // (not that we currently need it, but if you do, you gotta do it here!)
                        // var all_parameters = MyInvocation.MyCommand.Parameters;

                        // ask for the unbound arguments.
                        var unbound = UnboundArguments;

                        if (unbound.ContainsKey("ProviderName"))
                        {
                            var pName = unbound["ProviderName"];
                            if (pName != null)
                            {
                                ProviderName = pName as string[] ?? new[] { pName.ToString() };
                            }
                        }
                        else if (unbound.ContainsKey("Provider"))
                        {
                            var pName = unbound["Provider"];
                            if (pName != null)
                            {
                                ProviderName = pName as string[] ?? new[] { pName.ToString() };
                            }
                        }

                        // we've now got a copy of the arguments that aren't bound
                        // and we can potentially narrow the provider selection based
                        // on arguments the user specified.

                        if (IsCanceled)
                        {
#if DEEP_DEBUG
                            Console.WriteLine("»»» Cancelled before we got finished doing dynamic parameters");
#endif
                            // this happens if there is a serious failure early in the cmdlet
                            // i.e. - if the SelectedProviders comes back empty (due to agressive filtering)

                            // in this case, we just want to provide a catch-all for remaining arguments so that we can make
                            // output the error that we really want to (that the user specified conditions that filtered them all out)

                            DynamicParameterDictionary.Add("RemainingArguments", new RuntimeDefinedParameter("RemainingArguments", typeof(object), new Collection <Attribute> {
                                new ParameterAttribute()
                                {
                                    ValueFromRemainingArguments = true
                                },
                            }));
                        }

                        // at this point, we're actually calling to have the dynamic parameters generated
                        // that are expected to be used.
                        return(ActualGenerateDynamicParameters(unbound));
                    } finally {
                        IsReentrantLocked = false;
                    }
                }

                // otherwise just call the AGDP because we're in a reentrant call.
                // and this might be needed if the original call had some strange need
                // to know what the parameters that it's about to generate would be.
                // Yeah, you heard me.
                return(ActualGenerateDynamicParameters(null));
            } finally
            {
                if (thisIsFirstObject)
                {
                    // clean up our once-per-call data.
                    GetType().Remove <PackageProvider[]>("CachedSelectedProviders");
                    GetType().Remove <Dictionary <string, ParameterMetadata> >("MyInvocation.MyCommand.Parameters");
                    GetType().Remove <DynamicOption[]>("CachedDynamicOptions");
                }
            }
            // return true;
        }
Exemple #9
0
        protected bool ActualGenerateDynamicParameters(Dictionary <string, object> unboundArguments)
        {
            if (CachedStaticParameters == null)
            {
                // we're in the second call, we're just looking to find out what the static parameters actually are.
                // we're gonna just skip generating the dynamic parameters on this call.
                return(true);
            }

            try {
                unboundArguments = unboundArguments ?? new Dictionary <string, object>();

                // if there are unbound arguments that are owned by a provider, we can narrow the rest of the
                // arguments to just ones that are connected with that provider
                var dynamicOptions = CachedDynamicOptions;

                var keys = unboundArguments.Keys.ToArray();
                if (keys.Length > 0)
                {
                    var acceptableProviders = CachedDynamicOptions.Where(option => keys.ContainsAnyOfIgnoreCase(option.Name)).Select(option => option.ProviderName).Distinct().ToArray();
                    if (acceptableProviders.Length > 0)
                    {
                        dynamicOptions = dynamicOptions.Where(option => acceptableProviders.Contains(option.ProviderName)).ToArray();
                    }
                }
                // generate the common parameters for our cmdlets (timeout, messagehandler, etc)
                GenerateCommonDynamicParameters();

                // generate parameters that are specific to the cmdlet being implemented.
                GenerateCmdletSpecificParameters(unboundArguments);

                var staticParameters = GetType().Get <Dictionary <string, ParameterMetadata> >("MyInvocation.MyCommand.Parameters");

                foreach (var md in dynamicOptions)
                {
                    if (DynamicParameterDictionary.ContainsKey(md.Name))
                    {
                        // todo: if the dynamic parameters from two providers aren't compatible, then what?

                        // for now, we're just going to mark the existing parameter as also used by the second provider to specify it.
                        var crdp = DynamicParameterDictionary[md.Name] as CustomRuntimeDefinedParameter;

                        if (crdp == null)
                        {
                            // the package provider is trying to overwrite a parameter that is already dynamically defined by the BaseCmdlet.
                            // just ignore it.
                            continue;
                        }

                        if (IsInvocation)
                        {
                            // this is during an actual execution
                            crdp.Options.Add(md);
                        }
                        else
                        {
                            // this is for metadata sake. (get-help, etc)
                            crdp.IncludeInParameterSet(md, IsInvocation, ParameterSets);
                        }
                    }
                    else
                    {
                        // check if the dynamic parameter is a static parameter first.

                        // this can happen if we make a common dynamic parameter into a proper static one
                        // and a provider didn't know that yet.

                        if (staticParameters != null && staticParameters.ContainsKey(md.Name))
                        {
                            // don't add it.
                            continue;
                        }

                        DynamicParameterDictionary.Add(md.Name, new CustomRuntimeDefinedParameter(md, IsInvocation, ParameterSets));
                    }
                }
            } catch (Exception e) {
                e.Dump();
            }
            return(true);
        }
Exemple #10
0
        public override bool GenerateDynamicParameters()
        {
            if (_reentrancyLock.WaitOne(0))
            {
                // we're in here already.
                // this happens because we're asking for the parameters below, and it creates a new instance to get them.
                // we don't want dynamic parameters for that call, so let's get out.
                return(true);
            }
            _reentrancyLock.Set();

            // generate the common parameters for our cmdlets (timeout, messagehandler, etc)
            GenerateCommonDynamicParameters();

            var providers = SelectProviders(ProviderName).ReEnumerable();

            // if more than one provider is selected, this will never work
            if (providers.Count() != 1)
            {
                return(false);
            }

            var provider = providers.First();

            try {
                // if the provider is selected, we can get package metadata keys from the provider
                foreach (var md in provider.GetDynamicOptions(OptionCategory.Source, this))
                {
                    if (MyInvocation.MyCommand.Parameters.ContainsKey(md.Name))
                    {
                        // don't add it.
                        continue;
                    }

                    if (DynamicParameterDictionary.ContainsKey(md.Name))
                    {
                        // for now, we're just going to mark the existing parameter as also used by the second provider to specify it.
                        var crdp = DynamicParameterDictionary[md.Name] as CustomRuntimeDefinedParameter;
                        if (crdp == null)
                        {
                            // the provider is trying to overwrite a parameter that is already dynamically defined by the BaseCmdlet.
                            // just ignore it.
                            continue;
                        }

                        if (IsInvocation)
                        {
                            crdp.Options.Add(md);
                        }
                        else
                        {
                            crdp.IncludeInParameterSet(md, IsInvocation, ParameterSets);
                        }
                    }
                    else
                    {
                        DynamicParameterDictionary.Add(md.Name, new CustomRuntimeDefinedParameter(md, IsInvocation, ParameterSets));
                    }
                }
            }
            finally {
                _reentrancyLock.Reset();
            }
            return(true);
        }