コード例 #1
0
        /// <summary>
        /// This is called when the user is adding (or updating) a package source
        ///
        /// If this PROVIDER doesn't support user-defined package sources, remove this method.
        /// </summary>
        /// <param name="name">The name of the package source. If this parameter is null or empty the PROVIDER should use the location as the name (if the PROVIDER actually stores names of package sources)</param>
        /// <param name="location">The location (ie, directory, URL, etc) of the package source. If this is null or empty, the PROVIDER should use the name as the location (if valid)</param>
        /// <param name="trusted">A boolean indicating that the user trusts this package source. Packages returned from this source should be marked as 'trusted'</param>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param>
        public void AddPackageSource(string name, string location, bool trusted, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (!ValidatePackageManagementVersion(request))
            {
                return;
            }

            try {
                request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "AddPackageSource - ProviderName = '{0}', name='{1}', location='{2}', trusted='{3}'", PackageProviderName, name, location, trusted);

                // Error out if a user does not provide package source Name
                if (string.IsNullOrWhiteSpace(name))
                {
                    request.WriteError(ErrorCategory.InvalidArgument, Constants.Parameters.Name, Constants.Messages.MissingRequiredParameter, Constants.Parameters.Name);
                    return;
                }

                if (string.IsNullOrWhiteSpace(location))
                {
                    request.WriteError(ErrorCategory.InvalidArgument, Constants.Parameters.Location, Constants.Messages.MissingRequiredParameter, Constants.Parameters.Location);
                    return;
                }

                // Set-PackageSource will update the existing package source. In that case IsUpdate = true.
                var isUpdate = request.GetOptionValue(Constants.Parameters.IsUpdate).IsTrue();

                request.Debug(Resources.Messages.VariableCheck, "IsUpdate", isUpdate);


                // check first that we're not clobbering an existing source, unless this is an update
                request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindRegisteredSource -name'{0}'", name));

                var src = request.FindRegisteredSource(name);
                if (src != null && !isUpdate)
                {
                    // tell the user that there's one here already
                    request.WriteError(ErrorCategory.InvalidArgument, name, Constants.Messages.PackageSourceExists, name);
                    return;
                }

                // conversely, if it didn't find one, and it is an update, that's bad too:
                if (src == null && isUpdate)
                {
                    // you can't find that package source? Tell that to the user
                    request.WriteError(ErrorCategory.ObjectNotFound, name, Constants.Messages.UnableToResolveSource, name);
                    return;
                }

                // ok, we know that we're ok to save this source
                // next we check if the location is valid (if we support that kind of thing)
                var validated = false;
                validated = request.ValidateSourceLocation(location);
                if (!validated)
                {
                    request.WriteError(ErrorCategory.InvalidData, name, Constants.Messages.SourceLocationNotValid, location);
                    return;
                }
                else
                {
                    request.Verbose(Resources.Messages.SuccessfullyValidated, name);
                }

                bool force = request.GetOptionValue("Force") != null;
                //if source is UNC location/ copy it to local path;
                Uri uri;
                if (Uri.TryCreate(location, UriKind.Absolute, out uri))
                {
                    if (uri.IsFile && uri.IsUnc)
                    {
                        string fileName        = Path.GetFileNameWithoutExtension(location);
                        string directory       = Path.GetDirectoryName(location);
                        string catalogFilePath = Path.Combine(directory, fileName + ".cat");
                        if (!File.Exists(catalogFilePath))
                        {
                            request.WriteError(ErrorCategory.InvalidData, location, Resources.Messages.CatalogFileMissing, location);
                            return;
                        }
                        if (!TestCatalogFile(location, catalogFilePath, request))
                        {
                            return;
                        }
                        if (force || request.ShouldContinue(Resources.Messages.QueryDownloadPackageSourceList.format(location), Resources.Messages.PackageSourceListNotTrusted))
                        {
                            string destination = Path.Combine(_pslDirLocation, Path.GetFileName(uri.LocalPath));
                            if (File.Exists(destination))
                            {
                                if (force || request.ShouldContinue(Resources.Messages.OverwriteFile, Resources.Messages.FileExists))
                                {
                                    File.Copy(location, destination, true);
                                    location = destination;
                                }
                                else
                                {
                                    return;
                                }
                            }
                            else
                            {
                                File.Copy(location, destination);
                                location = destination;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }

                // it's good to check just before you actually write something to see if the user has cancelled the operation
                if (request.IsCanceled)
                {
                    return;
                }

                // looking good -- store the package source.
                request.AddPackageSource(name, location, trusted, validated);

                // Yield the package source back to the caller.
                request.YieldPackageSource(name, location, trusted, true /*since we just registered it*/, validated);
            } catch (Exception e) {
                e.Dump(request);
            }
        }
コード例 #2
0
ファイル: PackageListProvider.cs プロジェクト: 40a/PowerShell
        /// <summary>
        /// This is called when the user is adding (or updating) a package source
        ///
        /// If this PROVIDER doesn't support user-defined package sources, remove this method.
        /// </summary>
        /// <param name="name">The name of the package source. If this parameter is null or empty the PROVIDER should use the location as the name (if the PROVIDER actually stores names of package sources)</param>
        /// <param name="location">The location (ie, directory, URL, etc) of the package source. If this is null or empty, the PROVIDER should use the name as the location (if valid)</param>
        /// <param name="trusted">A boolean indicating that the user trusts this package source. Packages returned from this source should be marked as 'trusted'</param>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> 
        public void AddPackageSource(string name, string location, bool trusted, PackageSourceListRequest request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

            if (!ValidatePackageManagementVersion(request))
            {
                return;
            }

            try {

                request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "AddPackageSource - ProviderName = '{0}', name='{1}', location='{2}', trusted='{3}'", PackageProviderName, name, location, trusted);

                // Error out if a user does not provide package source Name
                if (string.IsNullOrWhiteSpace(name)) {
                    request.WriteError(ErrorCategory.InvalidArgument, Constants.Parameters.Name, Constants.Messages.MissingRequiredParameter, Constants.Parameters.Name);
                    return;
                }

                if (string.IsNullOrWhiteSpace(location)) {
                    request.WriteError(ErrorCategory.InvalidArgument, Constants.Parameters.Location, Constants.Messages.MissingRequiredParameter, Constants.Parameters.Location);
                    return;
                }

                // Set-PackageSource will update the existing package source. In that case IsUpdate = true.
                var isUpdate = request.GetOptionValue(Constants.Parameters.IsUpdate).IsTrue();

                request.Debug(Resources.Messages.VariableCheck, "IsUpdate", isUpdate);


                // check first that we're not clobbering an existing source, unless this is an update
                request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindRegisteredSource -name'{0}'", name));

                var src = request.FindRegisteredSource(name);
                if (src != null && !isUpdate) {
                    // tell the user that there's one here already
                    request.WriteError(ErrorCategory.InvalidArgument, name, Constants.Messages.PackageSourceExists, name);
                    return;
                }

                // conversely, if it didn't find one, and it is an update, that's bad too:
                if (src == null && isUpdate)
                {
                    // you can't find that package source? Tell that to the user
                    request.WriteError(ErrorCategory.ObjectNotFound, name, Constants.Messages.UnableToResolveSource, name);
                    return;
                }

                // ok, we know that we're ok to save this source
                // next we check if the location is valid (if we support that kind of thing)
                var validated = false;
                validated = request.ValidateSourceLocation(location);
                if (!validated) {
                     request.WriteError(ErrorCategory.InvalidData, name, Constants.Messages.SourceLocationNotValid, location);
                     return;
                }
                else
                { 
                    request.Verbose(Resources.Messages.SuccessfullyValidated, name);
                }

                bool force = request.GetOptionValue("Force") != null;
                //if source is UNC location/ copy it to local path;
                Uri uri;
                if (Uri.TryCreate(location, UriKind.Absolute, out uri))
                {
                    if (uri.IsFile && uri.IsUnc)
                    {
                        string fileName = Path.GetFileNameWithoutExtension(location);
                        string directory = Path.GetDirectoryName(location);
                        string catalogFilePath = Path.Combine(directory, fileName+".cat");
                        if (!File.Exists(catalogFilePath))
                        {
                            request.WriteError(ErrorCategory.InvalidData, location, Resources.Messages.CatalogFileMissing, location);
                            return;
                        }
                        if (!TestCatalogFile(location, catalogFilePath, request))
                        {
                            return;
                        }
                        if (force || request.ShouldContinue(Resources.Messages.QueryDownloadPackageSourceList.format(location), Resources.Messages.PackageSourceListNotTrusted))
                        {                            
                            string destination = Path.Combine(_pslDirLocation, Path.GetFileName(uri.LocalPath));
                            if (File.Exists(destination))
                            {
                                if (force || request.ShouldContinue(Resources.Messages.OverwriteFile, Resources.Messages.FileExists))
                                {
                                    File.Copy(location, destination, true);
                                    location = destination;
                                }
                                else
                                {
                                    return;
                                }
                            }
                            else {
                                File.Copy(location, destination);
                                location = destination;
                            }                  
                        }
                        else
                        {
                            return;
                        }
                    }
                }                   

                // it's good to check just before you actually write something to see if the user has cancelled the operation
                if (request.IsCanceled) {
                    return;
                }

                // looking good -- store the package source.
                request.AddPackageSource(name, location, trusted, validated);

                // Yield the package source back to the caller.
                request.YieldPackageSource(name, location, trusted, true /*since we just registered it*/, validated);
            } catch (Exception e) {
                e.Dump(request);
            }
        }