Esempio n. 1
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));
        }
 protected bool TryTranslatePSPath(string psPath, out string path, out bool?exists, out string errorMessage)
 {
     return(PSPathUtility.TryTranslatePSPath(SessionState, psPath, out path, out exists, out errorMessage));
 }