/// <summary>
 ///     Releases resources used by the <see cref="T:Microsoft.Perks.Async.Collections.BlockingCollection{T}" /> instance.
 /// </summary>
 /// <param name="isDisposing">Whether being disposed explicitly (true) or due to a finalizer (false).</param>
 protected override void Dispose(bool isDisposing)
 {
     if (!_isDisposed)
     {
         CompleteAdding();
         _blockingEnumerable = null;
         base.Dispose(isDisposing);
         _isDisposed = true;
     }
 }
Example #2
0
        protected IEnumerable <PackageProvider> FilterProvidersUsingDynamicParameters(MutableEnumerable <PackageProvider> providers, bool didUserSpecifyProviders, bool didUserSpecifySources)
        {
            var excluded = new Dictionary <string, IEnumerable <string> >();

            var setparameters = DynamicParameterDictionary.Values.OfType <CustomRuntimeDefinedParameter>().Where(each => each.IsSet).ReEnumerable();

            var matchedProviders = (setparameters.Any() ? providers.Where(p => setparameters.All(each => each.Options.Any(opt => opt.ProviderName == p.ProviderName))) : providers).ReEnumerable();

            foreach (var provider in matchedProviders)
            {
                // if a 'required' parameter is not filled in, the provider should not be returned.
                // we'll collect these for warnings at the end of the filter.
                var missingRequiredParameters = DynamicParameterDictionary.Values.OfType <CustomRuntimeDefinedParameter>().Where(each => !each.IsSet && each.IsRequiredForProvider(provider.ProviderName)).ReEnumerable();
                if (!missingRequiredParameters.Any())
                {
                    yield return(provider);
                }
                else
                {
                    // remember these so we can warn later.
                    excluded.Add(provider.ProviderName, missingRequiredParameters.Select(each => each.Name).ReEnumerable());
                }
            }

            /* TODO: provide errors in the case where everything got filtered out. Or maybe warnings?
             *
             * var mismatchedProviders = (setparameters.Any() ? providers.Where(each => !matchedProviders.Contains(each)).Where(p => setparameters.Any(each => each.Options.Any(opt => opt.ProviderName == p.ProviderName))) : Enumerable.Empty<PackageProvider>()).ReEnumerable();
             *
             * if (!found) {
             *  // we didn't find anything that matched
             *  // they specified dynamic parameters that implicitly select providers
             *  // that don't fit with the providers and sources that they initially asked for.
             *
             *  if (didUserSpecifyProviders || didUserSpecifySources) {
             *
             *      if (IsInvocation) {
             *          QueueHeldMessage(() => Error(Errors.ExcludedProvidersDueToMissingRequiredParameter, excluded.Keys, userSpecifiedSources.JoinWithComma()));
             *      }
             *
             *      yield break;
             *
             *  }
             *
             *  if (didUserSpecifySources) {
             *      // user gave sources which implied some providers but the dynamic parameters implied different providers
             *      if (IsInvocation) {
             *          // error
             *      }
             *      // return empty set
             *      return result;
             *  }
             *
             *  // well, this is silly.
             *  // if the user didn't specify a source or a provider
             *  // but the FilterProvidersUsingDynamicParameters came back empty
             *  // that means that they user specified parameters from two conflicting providers
             *  // and they forced each other out!
             *
             *  if (IsInvocation) {
             *      // error
             *
             *  }
             *
             * }
             */
            // these warnings only show for providers that would have otherwise be selected.
            // if not for the missing requrired parameter.
            foreach (var mp in excluded.OrderBy(each => each.Key))
            {
                Verbose(Constants.Messages.SkippedProviderMissingRequiredOption, mp.Key, mp.Value);
            }
        }
 /// <summary>
 ///     Provides a blocking AND consuming <see cref="T:System.Collections.Generics.IEnumerable{T}" /> for items in the
 ///     collection.
 ///     Iterating thru the collection will remove the items from the collection.
 /// </summary>
 /// <remarks>
 ///     This will remove items as they are returned, and iterating on the collection will block until the collection is
 ///     marked as complete,
 ///     or the cancellation token is cancelled.
 ///     (Think of this as a "Give me what you have" operation.)
 /// </remarks>
 /// <param name="cancellationToken">
 ///     A cancellation token that can be used to stop consuming items and yielding them.
 ///     Upon cancellation, items remaining in the collection will not be removed.
 /// </param>
 /// <returns>
 ///     An <see cref="T:System.Collections.Generics.IEnumerable{T}" /> that removes and returns items from the
 ///     collection.
 /// </returns>
 public IEnumerable <T> GetBlockingEnumerable(CancellationToken cancellationToken) =>
 _blockingEnumerable ?? (_blockingEnumerable = SafeGetBlockingEnumerable(cancellationToken).ReEnumerable());
Example #4
0
        private IEnumerable<PackageProvider> FilterProvidersUsingDynamicParameters(MutableEnumerable<PackageProvider> providers, IEnumerable<PackageSource> userSpecifiedRegisteredSources,  bool didUserSpecifyProviders, bool didUserSpecifySources) {
            var excluded = new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase);

            var setparameters = DynamicParameterDictionary.Values.OfType<CustomRuntimeDefinedParameter>().Where(each => each.IsSet).ReEnumerable();

            var matchedProviders = (setparameters.Any() ? providers.Where(p => setparameters.All(each => each.Options.Any(opt => opt.ProviderName == p.ProviderName))) : providers).ReEnumerable();

            foreach (var provider in matchedProviders) {
                // if a 'required' parameter is not filled in, the provider should not be returned.
                // we'll collect these for warnings at the end of the filter.
                var missingRequiredParameters = DynamicParameterDictionary.Values.OfType<CustomRuntimeDefinedParameter>().Where(each => !each.IsSet && each.IsRequiredForProvider(provider.ProviderName)).ReEnumerable();
                if (!missingRequiredParameters.Any()) {
                    yield return provider;
                } else {
                    Collection<string> missingOptions = new Collection<string>();
                    foreach (var missingRequiredParameter in missingRequiredParameters)
                    {
                        foreach (var option in missingRequiredParameter.Options)
                        {
                    // remember these so we can warn later.
                            missingOptions.Add(option.Name);
                        }
                    }
                    excluded.Add(provider.ProviderName, missingOptions);
                }
            }

            /* TODO: provide errors in the case where everything got filtered out. Or maybe warnings?
             *
            var mismatchedProviders = (setparameters.Any() ? providers.Where(each => !matchedProviders.Contains(each)).Where(p => setparameters.Any(each => each.Options.Any(opt => opt.ProviderName == p.ProviderName))) : Enumerable.Empty<PackageProvider>()).ReEnumerable();

            if (!found) {
                // we didn't find anything that matched
                // they specified dynamic parameters that implicitly select providers
                // that don't fit with the providers and sources that they initially asked for.

                if (didUserSpecifyProviders || didUserSpecifySources) {

                    if (IsInvocation) {
                        QueueHeldMessage(() => Error(Errors.ExcludedProvidersDueToMissingRequiredParameter, excluded.Keys, userSpecifiedSources.JoinWithComma()));
                    }

                    yield break;

                }

                if (didUserSpecifySources) {
                    // user gave sources which implied some providers but the dynamic parameters implied different providers
                    if (IsInvocation) {
                        // error
                    }
                    // return empty set
                    return result;
                }

                // well, this is silly.
                // if the user didn't specify a source or a provider
                // but the FilterProvidersUsingDynamicParameters came back empty
                // that means that they user specified parameters from two conflicting providers
                // and they forced each other out!

                if (IsInvocation) {
                    // error

                }

            }
            */

            if (ProviderName != null && ProviderName.Any()) {
                foreach (var providerName in ProviderName) {
                    if (excluded.ContainsKey(providerName)) {
                        Error(Constants.Errors.SpecifiedProviderMissingRequiredOption, providerName, excluded[providerName].JoinWithComma());
                    }
                }
            }

            // these warnings only show for providers that would have otherwise be selected.
            // if not for the missing required parameter.
            foreach (var mp in excluded.OrderBy(each => each.Key)) {
                string optionsValue = mp.Value.JoinWithComma();

                if (userSpecifiedRegisteredSources.Any())
                {
                    var mp1 = mp;

                    //Check if the provider with missing dynamic parameters has been registered with the source provided by a user
                    var sources = userSpecifiedRegisteredSources.Where(source => source.ProviderName != null && source.ProviderName.EqualsIgnoreCase(mp1.Key));

                    if (didUserSpecifySources && sources.Any())
                    {
                        //Error out if the provider associated with the -source matches
                        Error(Constants.Errors.SpecifiedProviderMissingRequiredOption, mp.Key, optionsValue);
                    }
                }

                Verbose(Constants.Messages.SkippedProviderMissingRequiredOption, mp.Key, optionsValue);
            }
        }