示例#1
0
        protected SourceValidationResult ValidateSource(string source)
        {
            // If source string is not specified, get the current active package source from the host.
            if (string.IsNullOrEmpty(source))
            {
                source = (string)GetPropertyValueFromHost(ActivePackageSourceKey);
            }

            // Look through all available sources (including those disabled) by matching source name and URL (or path).
            var matchingSource = GetMatchingSource(source);

            if (matchingSource != null)
            {
                return(SourceValidationResult.Valid(
                           source,
                           _sourceRepositoryProvider?.CreateRepository(matchingSource)));
            }

            // If we really can't find a source string, return an empty validation result.
            if (string.IsNullOrEmpty(source))
            {
                return(SourceValidationResult.None);
            }

            return(CheckSourceValidity(source));
        }
示例#2
0
        /// <summary>
        /// If a relative local URI is passed, it converts it into an absolute URI.
        /// If the local URI does not exist or it is neither http nor local type, then the source is rejected.
        /// If the URI is not relative then no action is taken.
        /// </summary>
        /// <param name="source">The source string specified by -Source switch.</param>
        /// <returns>The source validation result.</returns>
        private SourceValidationResult CheckSourceValidity(string inputSource)
        {
            // Convert file:// to a local path if needed, this noops for other types
            var source = UriUtility.GetLocalPath(inputSource);

            // Convert a relative local URI into an absolute URI
            var packageSource = new PackageSource(source);
            Uri sourceUri;

            if (Uri.TryCreate(source, UriKind.Relative, out sourceUri))
            {
                string outputPath;
                bool?  exists;
                string errorMessage;
                if (PSPathUtility.TryTranslatePSPath(SessionState, source, out outputPath, out exists, out errorMessage) &&
                    exists == true)
                {
                    source        = outputPath;
                    packageSource = new PackageSource(source);
                }
                else if (exists == false)
                {
                    return(SourceValidationResult.UnknownSource(source));
                }
            }
            else if (!packageSource.IsHttp)
            {
                // Throw and unknown source type error if the specified source is neither local nor http
                return(SourceValidationResult.UnknownSourceType(source));
            }

            // Check if the source is a valid HTTP URI.
            if (packageSource.IsHttp && packageSource.TrySourceAsUri == null)
            {
                return(SourceValidationResult.UnknownSource(source));
            }

            var sourceRepository = CreateRepositoryFromSource(source);

            return(SourceValidationResult.Valid(source, sourceRepository));
        }