Exemple #1
0
        public void ResolvePackageSources(PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "ResolvePackageSources");

            var selectedSources = request.SelectedSources;

            try {
                foreach (var source in selectedSources)
                {
                    request.Debug(Resources.Messages.YieldingPackageSource, PackageProviderName, source);
                    request.YieldPackageSource(source.Name, source.Location, source.Trusted, source.IsRegistered, source.IsValidated);
                }
            } catch (Exception e) {
                e.Dump(request);
            }

            request.Debug(Resources.Messages.DebugInfoReturnCall, PackageProviderName, "ResolvePackageSources");
        }
Exemple #2
0
        private void GetMsiInstalledPackage(string name, PackageJson package, string requiredVersion, string minimumVersion, string maximumVersion, PackageSourceListRequest request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "GetMsiInstalledPackage' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion));

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request);

            if (provider != null)
            {
                var packagesInstalled = provider.GetInstalledPackages(package.Name, requiredVersion, minimumVersion, maximumVersion, request);

                if (!packagesInstalled.Any())
                {
                    packagesInstalled = provider.GetInstalledPackages(package.DisplayName, requiredVersion, minimumVersion, maximumVersion, request);
                }
                foreach (var i in packagesInstalled)
                {
                    request.Debug("found a package '{0}.{1}' installed from '{2}'", i.Name, i.Version, i.Source);

                    var info = PackageSourceListRequest.MakeFastPathComplex(i.Source, name, (package.DisplayName ?? ""), i.Version, i.FastPackageReference);

                    _fastPackReftable.AddOrSet(i.FastPackageReference, i);

                    // check if the installed version matches with the one specified in the PSL.json.
                    // If so, we choose PSL.json.
                    var version = i.Version.CompareVersion(package.Version) ? package.Version : i.Version;
                    //we use displayname here because msi provider uses the displayname.
                    request.YieldSoftwareIdentity(info, package.DisplayName, version, i.VersionScheme, i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename);
                    return;
                }
            }
        }
        /// <summary>
        /// Uninstalls a package
        /// </summary>
        /// <param name="package">package defined in the PackageSourceList</param>
        /// <param name="fastPackageReference"></param>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param>
        /// <param name="fastPackReftable"></param>
        internal static void UninstallNuGetPackage(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary <string, SoftwareIdentity> fastPackReftable)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var destination = package.Destination;

            // PSL will call NuGet to uninstall the package when: 1) a user explictly specifies -providerName psl; or
            // 2) the psl.json file contains the defintion of Destination.
            // Otherwise, NuGet will handle the uninstall-package
            //
            string userSpecifiedProvider    = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider");
            string providerNameFromPipeline = request.GetProviderNameFromFastPathComplex(fastPackageReference);

            if ((string.IsNullOrWhiteSpace(userSpecifiedProvider) || !Constants.ProviderName.EqualsIgnoreCase(userSpecifiedProvider) &&
                 (string.IsNullOrWhiteSpace(providerNameFromPipeline) || !Constants.ProviderName.EqualsIgnoreCase(providerNameFromPipeline))) && string.IsNullOrWhiteSpace(destination))
            {
                request.Verbose(Resources.Messages.UninstallPackageNotSupported, Constants.ProviderName, package.Name, Constants.ProviderNames.NuGet);
                return;
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, "Calling UninstallNuGetPackage");

            var unInstallRequest = PackageSourceListRequest.ExtendRequest(
                new Dictionary <string, string[]>
            {
                { "Destination", new[] { destination } }
            }, null, request);

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true);

            if (provider != null)
            {
                request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name);

                if (!fastPackReftable.ContainsKey(fastPackageReference))
                {
                    request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
                    return;
                }

                var p = fastPackReftable[fastPackageReference];

                //calling NuGet for uninstall
                var installing = provider.UninstallPackage(p, unInstallRequest);

                foreach (var i in installing)
                {
                    request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);

                    if (request.IsCanceled)
                    {
                        installing.Cancel();
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Uninstalls a package
        /// </summary>
        /// <param name="package">package defined in the PackageSourceList</param>
        /// <param name="fastPackageReference"></param>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param>
        /// <param name="fastPackReftable"></param>
        internal static void UninstallPowershellArtifacts(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary <string, SoftwareIdentity> fastPackReftable)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            // If a user explicitly specifies -providerName psl, e.g., uninstall-package -providername psl -name xjea,
            // PSL will call PowerShellGet to uninstall the package.
            // Otherwise, it is expected that PowerShellGet will handle the uninstall-package and PSL won't
            // participate in the uninstall operation, e.g., uninstall-package xjea
            string userSpecifiedProvider    = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider");
            string providerNameFromPipeline = request.GetProviderNameFromFastPathComplex(fastPackageReference);

            if ((string.IsNullOrWhiteSpace(userSpecifiedProvider) || !Constants.ProviderName.EqualsIgnoreCase(userSpecifiedProvider)) &&
                string.IsNullOrWhiteSpace(providerNameFromPipeline) || !Constants.ProviderName.EqualsIgnoreCase(providerNameFromPipeline))
            {
                request.Verbose(Resources.Messages.UninstallPackageNotSupported, Constants.ProviderName, package.Name, Constants.ProviderNames.PowerShellGet);
                return;
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, "Calling UninstallPowershellArtifacts");

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true);

            if (provider != null)
            {
                request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name);

                if (!fastPackReftable.ContainsKey(fastPackageReference))
                {
                    request.WriteError(Internal.ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
                    return;
                }

                var p = fastPackReftable[fastPackageReference];

                //calling NuGet for uninstall
                var installing = provider.UninstallPackage(p, request);

                foreach (var i in installing)
                {
                    request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);

                    if (request.IsCanceled)
                    {
                        installing.Cancel();
                    }
                }
            }
        }
Exemple #5
0
        public void GetFeatures(PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug("Calling ''{0}'::{1}'", Constants.ProviderName, "GetFeatures");

            request.Debug("Calling 'PackageSourceListProvider::GetFeatures'");
            foreach (var feature in Features)
            {
                request.Yield(feature);
            }
        }
Exemple #6
0
        /// <summary>
        /// Removes/Unregisters a package source
        /// </summary>
        /// <param name="name">The name or location of a package source to remove.</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 RemovePackageSource(string name, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "RemovePackageSource");

            var src = request.FindRegisteredSource(name);

            if (src == null)
            {
                request.Warning(Constants.Messages.UnableToResolveSource, Constants.ProviderName, name);
                return;
            }

            request.RemovePackageSource(src.Name);
            request.YieldPackageSource(src.Name, src.Location, src.Trusted, false, src.IsValidated);
        }
Exemple #7
0
        public void InstallPackage(string fastPath, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            if (request.PackageSources.Any(each => each.EndsWith(".json", StringComparison.OrdinalIgnoreCase)))
            {
                // Warning that PSL won't be able to handle the uninstall-package if the source is not registered.
                request.Warning(Resources.Messages.UsingSourceLocation, Constants.ProviderName);
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "InstallPackage");

            promptForSampleJSON(request);

            var package = request.GetPackageByFastPath(fastPath);

            if (package == null)
            {
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath);
                return;
            }

            InstallProviderFromInstaller(package, fastPath, request);
        }
Exemple #8
0
        public void GetDynamicOptions(string category, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug("Calling ''{0}'::{1} ({2})'", Constants.ProviderName, "GetDynamicOptions", category);

            switch ((category ?? string.Empty).ToLowerInvariant())
            {
            case "package":
                break;

            case "source":
                break;

            case "install":
                request.YieldDynamicOption("Scope", "String", false, new[] { "CurrentUser", "AllUsers" });
                request.YieldDynamicOption("SkipHashValidation", Constants.OptionType.Switch, false);
                request.YieldDynamicOption("AddToPath", Constants.OptionType.Switch, false);
                request.YieldDynamicOption("RemoveFromPath", Constants.OptionType.Switch, false);
                break;
            }
        }
Exemple #9
0
        internal static void GetInstalledZipPackage(PackageJson package, PackageSourceListRequest request)
        {
            try
            {
                if (request.AddToPath.Value)
                {
                    request.Verbose(Resources.Messages.AddOrRemovePath, Constants.ProviderName, "AddToPath", "Install-Package");
                }
                if (request.RemoveFromPath.Value)
                {
                    request.Verbose(Resources.Messages.AddOrRemovePath, Constants.ProviderName, "RemoveFromPath", "Uninstall-Package");
                }

                string path = Path.Combine(package.Destination, package.Name, package.Version);
                if (Directory.Exists(path))
                {
                    if (Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Any())
                    {
                        string userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider");
                        var    fp = PackageSourceListRequest.MakeFastPathComplex(package.Source, package.Name, (package.DisplayName ?? ""), package.Version, path, userSpecifiedProvider ?? "");

                        //the directory exists and contain files, we think the package has been installed.
                        request.YieldSoftwareIdentity(fp, package.Name, package.Version, package.VersionScheme, package.Summary, path, package.Name, path, path);
                    }
                }
            }
            catch (Exception e)
            {
                request.Debug(e.StackTrace);
            }
        }
Exemple #10
0
        public void DownloadPackage(string fastPath, string location, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "DownloadPackage' - fastReference='{0}', location='{1}'", fastPath, location));

            var package = request.GetPackageByFastPath(fastPath);

            if (package == null)
            {
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath);
                return;
            }

            switch (package.Type.ToLowerInvariant())
            {
            case Constants.MediaType.MsiPackage:
                //No-op as the msi provider does not support save-package
                break;

            case Constants.MediaType.MsuPackage:
                //No-op as the msu provider does not support save-package
                break;

            case Constants.MediaType.AppxPackage:
                //TODO for future whenever needed to support appx packages
                break;

            case Constants.MediaType.NuGetPackage:
                NupkgInstaller.DownloadNuGetPackage(fastPath, location, request);
                break;

            case Constants.MediaType.ZipPackage:
                //TODO
                ZipPackageInstaller.DownloadZipPackage(fastPath, location, request);
                break;

            case Constants.MediaType.ExePackage:
                //TODO
                ExePackageInstaller.DownloadExePackage(fastPath, location, request);
                break;

            case Constants.MediaType.PsArtifacts:
                //TODO
                break;

            default:
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type);
                return;
            }
        }
Exemple #11
0
 public void InitializeProvider(PackageSourceListRequest request)
 {
     if (request == null)
     {
         throw new ArgumentNullException("request");
     }
     request.Debug("Initialize PackageSourceListProvider");
 }
Exemple #12
0
        private static void AddEnvironmentVariable(PackageSourceListRequest request, string pathToBeAdded)
        {
            // Do nothing if a user does not specify -AddToPath
            if (!request.AddToPath.Value)
            {
                return;
            }

            try
            {
                var scope = ((request.Scope == null) || string.IsNullOrWhiteSpace(request.Scope.Value)) ? Constants.AllUsers : request.Scope.Value;

                // check if the environment variable exists already
                var envPath     = Environment.GetEnvironmentVariable(Constants.PATH);
                var packagePath = ";" + pathToBeAdded.Trim();

                if (string.IsNullOrWhiteSpace(envPath) || !envPath.Split(';').Where(each => each.EqualsIgnoreCase(pathToBeAdded)).Any())
                {
                    request.Debug("Adding '{0}' to PATH environment variable".format(pathToBeAdded));

                    // allow to add the path to the environment variable
                    switch (scope)
                    {
                    case Constants.AllUsers:
                        SetItemProperty(Constants.PATH, envPath + packagePath, SystemEnvironmentKey);
                        break;

                    case Constants.CurrentUser:
                        SetItemProperty(Constants.PATH, envPath + packagePath, UserEnvironmentKey);
                        break;
                    }
                    Environment.SetEnvironmentVariable(Constants.PATH, envPath + packagePath);
                }
                else
                {
                    request.Debug("Environment variable '{0}' already exists".format(pathToBeAdded));
                }
            }
            //if we cannot set the environment variable, we should not fail the install-package
            catch (Exception e)
            {
                request.Warning("Failed to update Path environment variable. {0}", e.Message);
                e.Dump(request);
            }
        }
        private static void InstallPackageViaPowerShellGet(PackageJson packageJson, PackageSourceListRequest request, SoftwareIdentity[] packages)
        {

            var provider = PackageSourceListRequest.FindProvider(request, packageJson.Type, request, true);
            if (provider == null) return;
            
            IHostApi installRequest = request;

            if (provider.Name.EqualsIgnoreCase(Constants.ProviderNames.PowerShellGet) && !request.ProviderServices.IsElevated) {
                // if we're not elevated, we want powershellget to install to the user scope
                installRequest = PackageSourceListRequest.ExtendRequest(
                    new Dictionary<string, string[]> {
                        {"Scope", new[] {"CurrentUser"}}
                    }, null, packageJson.IsTrustedSource, request);

            } else {
                installRequest = PackageSourceListRequest.ExtendRequest(
                    new Dictionary<string, string[]> {
                        {"Destination", new[] {packageJson.Destination ?? ""}}
                    }, null, packageJson.IsTrustedSource, request);
            }

            request.Debug("Calling '{0}' provider to install the package '{1}.{2}'", provider.Name, packageJson.Name, packageJson.Version);

            var installing = provider.InstallPackage(packages[0], installRequest);
          
            if (installing == null || !installing.Any())
            {
                request.Verbose(Resources.Messages.NumberOfPackagesRecevied, 0, provider.Name, "InstallPackage");
                request.Warning(Resources.Messages.FailToInstallPackage, Constants.ProviderName, packages[0].Name);
                return;
            }

            int packagesRecevied = 0;
            foreach (var i in installing)
            {
                request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);

                if (request.IsCanceled)
                {
                    installing.Cancel();
                }
                else
                {
                    request.Verbose(Resources.Messages.SuccessfullyInstalled, "{0}.{1}".format(packageJson.Name, packageJson.Version));
                    //load provider
                    if (packageJson.IsPackageProvider)
                    {
                        //Per provider development guidance: provider name and module name should be the same otherwise we can not import it.
                        request.PackageManagementService.ImportPackageProvider(request, packageJson.Name, null, null, null, isRooted: false, force: false);
                    }
                }
                packagesRecevied++;
            }

            request.Verbose(Resources.Messages.NumberOfPackagesRecevied, packagesRecevied, provider.Name, "install-package");
        }
Exemple #14
0
        private void UnInstallMsiPackage(PackageSourceListRequest request, string fastPath, PackageJson package)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "UnInstallMsiPackage' - name='{0}', fastPath='{1}'", package.Name, fastPath));

            string sourceLocation;
            string id;
            string displayName;
            string version;
            string fastPackageReference;

            if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference))
            {
                //we don't need to error out even if fastpath is not correct because msi provider is expected to handle the uninstall-package.
                request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name);
                return;
            }

            // Normally uninstall-package will be handled by MSI provider. Here we added a special case for handling uninstall-package nodejs
            // which msi provider unable to deal with (node.js only for msi)
            if (id != null && id.EqualsIgnoreCase("nodejs"))
            {
                var provider = PackageSourceListRequest.FindProvider(request, Constants.ProviderNames.Msi, request);

                if (provider != null)
                {
                    if (!_fastPackReftable.ContainsKey(fastPackageReference))
                    {
                        request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
                        return;
                    }

                    request.Verbose(Resources.Messages.UninstallingPackage, Constants.ProviderName, package.Name);

                    var p = _fastPackReftable[fastPackageReference];

                    var installing = provider.UninstallPackage(p, request);

                    foreach (var i in installing)
                    {
                        request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme,
                                                      i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename);
                        if (request.IsCanceled)
                        {
                            installing.Cancel();
                        }
                        return;
                    }
                }
            }
            else
            {
                //no-op for uninstalling the msi packages. only install-package nodejs is supported because msi can not handle it
                request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name);
                return;
            }
        }
Exemple #15
0
        internal static void DownloadZipPackage(string fastPath, string location, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName,
                          string.Format(CultureInfo.InvariantCulture, "DownloadZipPackage' - location='{0}'", location));
        }
        /// <summary>
        /// Uninstalls a package
        /// </summary>
        /// <param name="package">package defined in the PackageSourceList</param>
        /// <param name="fastPackageReference"></param>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param>
        /// <param name="fastPackReftable"></param>
        internal static void UninstallNuGetPackage(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary <string, SoftwareIdentity> fastPackReftable)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "UninstallNuGetPackage' - fastReference='{0}'", fastPackageReference));

            var unInstallRequest = PackageSourceListRequest.ExtendRequest(
                new Dictionary <string, string[]>
            {
                { "Destination", new[] { package.Destination ?? "" } }
            }, null, package.IsTrustedSource, request);

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true);

            if (provider != null)
            {
                request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name);

                if (!fastPackReftable.ContainsKey(fastPackageReference))
                {
                    request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
                    return;
                }

                var p = fastPackReftable[fastPackageReference];

                //calling NuGet for uninstall
                var installing = provider.UninstallPackage(p, unInstallRequest);

                foreach (var i in installing)
                {
                    request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);

                    if (request.IsCanceled)
                    {
                        installing.Cancel();
                    }
                }
            }
        }
Exemple #17
0
        internal static void GetInstalledPowershellArtifacts(PackageJson package, string requiredVersion, string minimumVersion, string maximumVersion, Dictionary <string, SoftwareIdentity> fastPackReftable, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "GetInstalledPowershellArtifacts' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", package.Name, requiredVersion, minimumVersion, maximumVersion));

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request);

            if (provider == null)
            {
                return;
            }

            //calling the PowerShellGet provider
            request.Debug("Calling '{0}' provider to get installed packages '{1}.{2}'", provider.Name, package.Name, package.Version);

            var packagesInstalled = provider.GetInstalledPackages(package.Name, requiredVersion, minimumVersion, maximumVersion, request).ToArray();

            if (packagesInstalled == null || !packagesInstalled.Any())
            {
                request.Verbose(Resources.Messages.NumberOfPackagesRecevied, 0, provider.Name, "GetInstalledPackages");
                return;
            }

            //Make sure the packages found are defined in the psl.json
            foreach (var i in packagesInstalled.Where(each => new SemanticVersion(each.Version) == new SemanticVersion(package.Version)))
            {
                request.Debug("Found an installed package '{0}.{1} from {2}' ", i.Name, i.Version, i.Source);
                string userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider");
                var    info = PackageSourceListRequest.MakeFastPathComplex(i.Source, i.Name, "", i.Version, "", userSpecifiedProvider ?? "");

                fastPackReftable.AddOrSet(info, i);

                // check if the installed version matches with the one specified in the PSL.json.
                // If so, we choose PSL.json.
                var version = i.Version.CompareVersion(package.Version) ? package.Version : i.Version;
                request.YieldSoftwareIdentity(info, i.Name, version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);
            }
        }
        internal static void GeInstalledNuGetPackages(PackageJson package, string requiredVersion, string minimumVersion, string maximumVersion, Dictionary <string, SoftwareIdentity> fastPackReftable, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "GeInstalledNuGetPackages' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", package.Name, requiredVersion, minimumVersion, maximumVersion));

            //clone the request
            //nuget provider may not know the location of the package gets installed.
            //we need to pass around the destination path to nuget provider
            var installedRequest = PackageSourceListRequest.ExtendRequest(
                new Dictionary <string, string[]> {
                { "Destination", new[] { package.Destination } }
            }, null, request);

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request);

            if (provider != null)
            {
                //calling NuGet provider
                var packagesInstalled = provider.GetInstalledPackages(package.Name, requiredVersion, minimumVersion, maximumVersion, installedRequest);
                if (packagesInstalled != null)
                {
                    //Make sure the packages found are defined in the psl.json
                    foreach (var i in packagesInstalled.Where(each => new SemanticVersion(each.Version) == new SemanticVersion(package.Version)))
                    {
                        request.Debug("Found an installed package '{0}.{1} from {2}' ", i.Name, i.Version, i.Source);
                        string userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider");
                        var    info = PackageSourceListRequest.MakeFastPathComplex(i.Source, i.Name, "", i.Version, "", userSpecifiedProvider ?? "");

                        fastPackReftable.AddOrSet(info, i);

                        // make it semver because in find-package we use semver
                        var version = i.Version.CompareVersion(package.Version) ? package.Version : i.Version;
                        request.YieldSoftwareIdentity(info, i.Name, version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);
                    }
                }
            }
        }
Exemple #19
0
        /// <summary>
        /// Uninstalls a package
        /// </summary>
        /// <param name="fastPackageReference"></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 UninstallPackage(string fastPackageReference, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (!ValidatePackageManagementVersion(request))
            {
                return;
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, fastPackageReference);

            var package = request.GetFastReferenceComplex(fastPackageReference);

            if (package == null)
            {
                request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
                return;
            }

            switch (package.Type.ToLowerInvariant())
            {
            case Constants.MediaType.AppxPackage:
                //TODO for the future
                break;

            case Constants.MediaType.ExePackage:
                ExePackageInstaller.UninstallExePackage(fastPackageReference, request);
                break;

            case Constants.MediaType.MsiPackage:

                UnInstallMsiPackage(request, fastPackageReference, package);
                break;

            case Constants.MediaType.ZipPackage:
                ZipPackageInstaller.UnInstallZipPackage(request, fastPackageReference);
                break;

            case Constants.MediaType.NuGetPackage:
                NupkgInstaller.UninstallNuGetPackage(package, fastPackageReference, request, _fastPackReftable);
                break;

            case Constants.MediaType.PsArtifacts:
                PowerShellArtifactInstaller.UninstallPowershellArtifacts(package, fastPackageReference, request, _fastPackReftable);
                break;

            default:
                request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type);
                break;
            }
        }
Exemple #20
0
        internal static void GeInstalledNuGetPackages(PackageJson package, string requiredVersion, string minimumVersion, string maximumVersion, Dictionary<string, SoftwareIdentity> fastPackReftable, PackageSourceListRequest request)
        { 
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "GeInstalledNuGetPackages' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", package.Name, requiredVersion, minimumVersion, maximumVersion));

            //clone the request
            //nuget provider may not know the location of the package gets installed.
            //we need to pass around the destination path to nuget provider
            var installedRequest = PackageSourceListRequest.ExtendRequest(
                   new Dictionary<string, string[]> {
                        {"Destination", new[] { package.Destination ?? ""}}
                   }, null, package.IsTrustedSource, request);

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request);
            if (provider != null)
            {
                //calling NuGet provider
                var packagesInstalled = provider.GetInstalledPackages(package.Name, requiredVersion, minimumVersion, maximumVersion, installedRequest);
                if (packagesInstalled != null)
                {
                    foreach (var i in packagesInstalled)
                    {
                        request.Debug("Found an installed package '{0}.{1} from {2}' ", i.Name, i.Version, i.Source);
                        var info = PackageSourceListRequest.MakeFastPathComplex(i.Source, i.Name, "", i.Version, "");

                        fastPackReftable.AddOrSet(info, i);

                        // make it semver because in find-package we use semver
                        var version = i.Version.CompareVersion(package.Version) ? package.Version : i.Version;
                        request.YieldSoftwareIdentity(info, i.Name, version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);

                    }
                }
            }
        }
Exemple #21
0
        private static void InstallPackageFile(PackageJson package, string fastPath, PackageSourceListRequest request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallPackageFile' - name='{0}', fastPath='{1}'", package.Name, fastPath));

            // get a temp file name, msi or msu
            var providerType = package.Type.ToLowerInvariant();
            var destination  = Path.ChangeExtension(Path.GetTempFileName(), providerType);

            //download the msi package to the temp file
            WebDownloader.DownloadFile(package.Source, destination, request, null);

            if (!File.Exists(destination))
            {
                return;
            }


            // validate the file
            if (!WebDownloader.VerifyHash(destination, package, request))
            {
                return;
            }

            if (!package.IsTrustedSource)
            {
                if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source))
                {
                    request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name);
                    return;
                }
            }

            var installRequest = PackageSourceListRequest.ExtendRequest(new Dictionary <string, string[]>
            {
                { "Source", new[] { package.Source ?? "" } }
            }, new[] { package.Source ?? "" }, package.IsTrustedSource, request);

            request.Verbose(Resources.Messages.CallMsiForInstall, package.Name);
            if (request.ProviderServices.Install(destination, "", installRequest))
            {
                // it installed ok!exit
                request.YieldFromSwidtag(package, fastPath);
                return;
            }
            else
            {
                request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.PackageFailedInstall, package.Name);
            }
        }
Exemple #22
0
        internal static void InstallProviderFromInstaller(PackageJson package, string fastPath, PackageSourceListRequest request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallProviderFromInstaller' - name='{0}'", package.Name));

            //install any dependency packages
            InstallDependencies(package, request);

            switch (package.Type.ToLowerInvariant())
            {
            case Constants.MediaType.MsiPackage:
            case Constants.MediaType.MsuPackage:
#if CORECLR
                request.WriteError(ErrorCategory.InvalidOperation, package.Type, Resources.Messages.UnsupportedPowerShellEnvironment, Constants.ProviderName, "Install-Package", package.Type);
#else
                InstallPackageFile(package, fastPath, request);
#endif
                break;

            case Constants.MediaType.AppxPackage:
                //TODO for future whenever needed to support appx packages
                break;

            case Constants.MediaType.NuGetPackage:
                NupkgInstaller.InstallNuGetPackage(package, fastPath, request);
                break;

            case Constants.MediaType.ZipPackage:
                ZipPackageInstaller.InstallZipPackage(package, fastPath, request);
                break;

            case Constants.MediaType.ExePackage:
#if CORECLR
                request.WriteError(ErrorCategory.InvalidOperation, package.Type, Resources.Messages.UnsupportedPowerShellEnvironment, Constants.ProviderName, "Install-Package", package.Type);
#else
                ExePackageInstaller.InstallExePackage(package, fastPath, request);
#endif
                break;

            case Constants.MediaType.PsArtifacts:
                PowerShellArtifactInstaller.InstallPowershellArtifacts(package, fastPath, request);
                break;

            default:
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type);
                break;
            }
            return;
        }
Exemple #23
0
        private static void RemoveEnvironmentVariable(PackageSourceListRequest request, string pathToBeRemoved, string target)
        {
            try
            {
                if (!request.RemoveFromPath.Value)
                {
                    return;
                }

                var envPath   = GetItemProperty(Constants.PATH, target);
                var trimchars = new char[] { '\\', ' ' };

                if (!string.IsNullOrWhiteSpace(envPath))
                {
                    pathToBeRemoved = pathToBeRemoved.TrimEnd(trimchars);

                    var newPath = envPath.Split(';').Where(each => !each.TrimEnd(trimchars).EqualsIgnoreCase(pathToBeRemoved))
                                  .Aggregate("", (current, each) => string.IsNullOrEmpty(current) ? each : (current + ";" + each));

                    request.Debug("Removing '{0}' from PATH environment variable".format(pathToBeRemoved));

                    SetItemProperty(Constants.PATH, newPath, UserEnvironmentKey);
                    SetItemProperty(Constants.PATH, newPath, SystemEnvironmentKey);

                    Environment.SetEnvironmentVariable(Constants.PATH, newPath);
                }
                else
                {
                    request.Warning(Resources.Messages.FailedToRetrivePathEnvironmentVariable, Constants.ProviderName);
                }
            }
            catch (Exception e)
            {
                request.Warning("Failed to update Path environment variable. {0}", e.Message);
                e.Dump(request);
            }
        }
Exemple #24
0
        public void InstallPackage(string fastPath, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "InstallPackage' - fastReference='{0}'", fastPath));

            var package = request.GetPackageByFastPath(fastPath);

            if (package == null)
            {
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath);
                return;
            }

            InstallProviderFromInstaller(package, fastPath, request);
        }
        internal static void InstallNuGetPackage(PackageJson package, string fastPath, PackageSourceListRequest request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallNuGetPackage' - name='{0}', fastPath='{1}'", package.Name, fastPath));

            var canonicalId = PackageSourceListRequest.CreateCanonicalId(package, Constants.ProviderNames.NuGet); // "nuget:jquery/2.1.0#http://nuget.org/api/v2";
            var pkgs        = request.PackageManagementService.FindPackageByCanonicalId(canonicalId, request.As <IHostApi>())
                              .Where(each => string.IsNullOrWhiteSpace(package.Version) ||
                                     (new SemanticVersion(each.Version) == new SemanticVersion(package.Version))).ToArray();

            switch (pkgs.Length)
            {
            case 0:
                request.Warning(Resources.Messages.CannotFindPackage, Constants.ProviderName, canonicalId);
                return;

            case 1:
                InstallPackageReference(package, request, pkgs);
                return;

            default:
                request.Warning(Resources.Messages.FoundMorePackages, Constants.ProviderName, pkgs.Length, canonicalId);
                return;
            }
        }
Exemple #26
0
        public void GetInstalledPackages(string name, string requiredVersion, string minimumVersion, string maximumVersion, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (!ValidatePackageManagementVersion(request))
            {
                return;
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "GetInstalledPackages' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion));

            IEnumerable <PackageJson> packagesDefinedInJsonSpec;

            if (string.IsNullOrWhiteSpace(name) || WildcardPattern.ContainsWildcardCharacters(name))
            {
                // In the case of the package name is null or contains wildcards, error out if a user puts version info
                if (!string.IsNullOrWhiteSpace(requiredVersion) || !string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrWhiteSpace(maximumVersion))
                {
                    request.Warning(Resources.Messages.WildCardCharsAreNotSupported, name);
                    return;
                }
                packagesDefinedInJsonSpec = request.GetPackages(name).ToArray();
            }
            else
            {
                //return all installed
                packagesDefinedInJsonSpec = request.GetPackages(name, requiredVersion, minimumVersion, maximumVersion).ToArray();
            }
            _fastPackReftable.Clear();

            if (!packagesDefinedInJsonSpec.Any())
            {
                request.Verbose(Resources.Messages.NoPackageFound, Constants.ProviderName);
                return;
            }

            foreach (var package in packagesDefinedInJsonSpec)
            {
                switch (package.Type.ToLowerInvariant())
                {
                case Constants.MediaType.AppxPackage:
                    //TODO for future
                    break;

                case Constants.MediaType.PsArtifacts:
                    PowerShellArtifactInstaller.GeInstalledPowershellArtifacts(package, requiredVersion, minimumVersion, maximumVersion, _fastPackReftable, request);
                    break;

                case Constants.MediaType.ExePackage:
                    //program provider can handle get-package git for git.exe
                    ExePackageInstaller.GetInstalledExePackages(package, requiredVersion, minimumVersion, minimumVersion, request);
                    break;

                case Constants.MediaType.MsiPackage:
                    //msi provider can handle get-package node.js for node.js.msi
                    GetMsiInstalledPackage(name, package, requiredVersion, minimumVersion, maximumVersion, request);
                    break;

                case Constants.MediaType.ZipPackage:
                    ZipPackageInstaller.GetInstalledZipPackage(package, request);
                    break;

                case Constants.MediaType.NuGetPackage:
                    NupkgInstaller.GeInstalledNuGetPackages(package, requiredVersion, minimumVersion, maximumVersion, _fastPackReftable, request);
                    break;
                } //switch
            }
        }
Exemple #27
0
 public void InitializeProvider(PackageSourceListRequest request)
 {
     request.Debug("Initialize PackageSourceListProvider");
 }
Exemple #28
0
        public void FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion, int id, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindPackage' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion));

            // no package name or the name with wildcard
            if (string.IsNullOrWhiteSpace(name) || WildcardPattern.ContainsWildcardCharacters(name))
            {
                // In the case of the package name is null or contains wildcards, error out if a user puts version info
                if (!string.IsNullOrWhiteSpace(requiredVersion) || !string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrWhiteSpace(maximumVersion))
                {
                    request.Warning(Resources.Messages.WildCardCharsAreNotSupported, name);
                    return;
                }
       
                var packages = request.GetPackages(name);
                if (request.GetOptionValue("AllVersions").IsTrue())
                {
                    // if version is specified then name can not be empty or with wildcard. in this case the cmdlet has been errored out already.
                    // here we just return all packages we can find
                    if (request.FilterOnVersion(packages, requiredVersion, minimumVersion, maximumVersion, minInclusive: true, maxInclusive: true, latest: false).OrderBy(p => p.Name).Any(p => !request.YieldFromSwidtag(p, p.Name)))
                    {
                        return;
                    }
                    return;
                }

                //return the latest version
                if (packages.GroupBy(p => p.Name)
                        .Select(each => each.OrderByDescending(pp => pp.Version).FirstOrDefault()).OrderBy(p=>p.Name).Any( item =>!request.YieldFromSwidtag(item, item.Name)))
                {
                    return;
                }
                
            } else {
 
                // a user specifies name
                // asked for a specific version?
                if (!string.IsNullOrWhiteSpace(requiredVersion))
                {
                    request.YieldFromSwidtag(request.GetPackage(name, requiredVersion), name);
                    return;
                }

                var allVersion = request.GetOptionValue("AllVersions").IsTrue();
                // asked for a version range?
                if (!string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrEmpty(maximumVersion) || allVersion)
                {
                    var packages = request.GetPackagesWithinVersionRange(name, minimumVersion, maximumVersion);
                    if (allVersion)
                    {
                        if (packages.Any(p => !request.YieldFromSwidtag(p, name)))
                        {
                            return;
                        }
                    }
                    else
                    {
                        if (request.YieldFromSwidtag(packages.FirstOrDefault(), name))
                        {
                            return;
                        }
                    }

                    return;
                }

                // just return by name
                request.YieldFromSwidtag(request.GetPackage(name), name);
            }
        }
Exemple #29
0
        internal static void DownloadNuGetPackage(string fastPath, string location, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName,
                string.Format(CultureInfo.InvariantCulture, "DownloadNuGetPackage' - fastReference='{0}', location='{1}'", fastPath, location));

            var package = request.GetPackageByFastPath(fastPath);
            if (package == null)
            {
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath);
                return;
            }

            // let the core figure out how to save this package 
            var canonicalId = PackageSourceListRequest.CreateCanonicalId(package, Constants.ProviderNames.NuGet); // "nuget:jquery/2.1.0#http://nuget.org/api/v2";
            var pkgs = request.PackageManagementService.FindPackageByCanonicalId(canonicalId, request.As<IHostApi>())
                        .Where(each => string.IsNullOrWhiteSpace(package.Version) ||
                        (new SemanticVersion(each.Version) == new SemanticVersion(package.Version))).ToArray();

            switch (pkgs.Length)
            {
                case 0:
                    request.Warning(Resources.Messages.CannotFindPackage, Constants.ProviderName, canonicalId);
                    return;

                case 1:
                    var provider = request.PackageManagementService.GetAvailableProviders(request, new[] {"NuGet"}).FirstOrDefault();
                    if (provider != null)
                    {                      
                        var downloadrequest = PackageSourceListRequest.ExtendRequest(
                            new Dictionary<string, string[]>
                            {
                                {"Destination", new[] {package.Destination ?? ""}}
                            }, 
                            new[] {package.Source ?? ""}, 
                            package.IsTrustedSource, 
                            request);

                        var downloading = provider.DownloadPackage(pkgs[0], location, downloadrequest);

                        foreach (var i in downloading)
                        {
                            request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);

                            if (request.IsCanceled)
                            {
                                downloading.Cancel();
                            }
                        }
                    }
                    break;
                default:
                    request.Warning(Resources.Messages.FoundMorePackages, Constants.ProviderName, pkgs.Length, canonicalId);
                    return;
            }

        }
Exemple #30
0
 public void InitializeProvider(PackageSourceListRequest request)
 {
     request.Debug("Initialize PackageSourceListProvider");
 }
Exemple #31
0
        /// <summary>
        /// Uninstalls a package
        /// </summary>
        /// <param name="fastPackageReference"></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 UninstallPackage(string fastPackageReference, PackageSourceListRequest request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }
            if (!ValidatePackageManagementVersion(request))
            {
                return;
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, fastPackageReference);

            var package = request.GetFastReferenceComplex(fastPackageReference);
            if (package == null)
            {
                request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
                return;
            }

            switch (package.Type.ToLowerInvariant()) {

                case Constants.MediaType.AppxPackage:
                    //TODO for the future
                    break;
                case Constants.MediaType.ExePackage:
                    ExePackageInstaller.UninstallExePackage(fastPackageReference, request);
                    break;
                case Constants.MediaType.MsiPackage:
                    
                    UnInstallMsiPackage(request, fastPackageReference, package);
                    break;
                case Constants.MediaType.ZipPackage:
                    ZipPackageInstaller.UnInstallZipPackage(request, fastPackageReference);
                    break;
                case Constants.MediaType.NuGetPackage:
                    NupkgInstaller.UninstallNuGetPackage(package, fastPackageReference, request, _fastPackReftable);                   
                    break;
                case Constants.MediaType.PsArtifacts:
                    PowerShellArtifactInstaller.UninstallPowershellArtifacts(package, fastPackageReference, request, _fastPackReftable);
                    break;
                default:
                    request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type);
                    break;
            }
        }
Exemple #32
0
        internal static bool InstallZipPackage(PackageJson package, string fastPath, PackageSourceListRequest request)
        {
            // download the exe package
            var file = Path.ChangeExtension(Path.GetTempFileName(), "exe");

            WebDownloader.DownloadFile(package.Source, file, request, null);
            if (!File.Exists(file))
            {
                return false;
            }

            // validate the file
            if (!WebDownloader.VerifyHash(file, package,request))
            {                
                return false;
            }

            if (!package.IsTrustedSource)
            {
                if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source))
                {
                    request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name);
                    return false;
                }
            }


            Timer timer = null;
            object timerLock = new object();
            bool cleanUp = false;

            ProgressTracker tracker = new ProgressTracker(request.StartProgress(0, "Installing Zip Package............"));
            double percent = tracker.StartPercent;
            Action cleanUpAction = () => {
                lock (timerLock)
                {
                    // check whether clean up is already done before or not
                    if (!cleanUp)
                    {
                        try
                        {
                            if (timer != null)
                            {
                                // stop timer
                                timer.Change(Timeout.Infinite, Timeout.Infinite);
                                timer.Dispose();
                                timer = null;
                            }
                        }
                        catch
                        {
                        }

                        cleanUp = true;
                    }
                }
            };
          
            // extracted folder
            string extractedFolder = string.Concat(file.GenerateTemporaryFilename());

            try
            {

                timer = new Timer(_ =>
                {
                    percent += 0.025;
                    var progressPercent = tracker.ConvertPercentToProgress(percent);
                    if (progressPercent < 90)
                    {
                        request.Progress(tracker.ProgressID, (int)progressPercent, string.Format(CultureInfo.CurrentCulture, "Copying files ..."));
                    }
                    if (request.IsCanceled)
                    {
                        cleanUpAction();
                    }
                }, null, 0, 1000);

                //unzip the file
                ZipFile.ExtractToDirectory(file, extractedFolder);

                if (Directory.Exists(extractedFolder))
                {

                    var versionFolder = Path.Combine(package.Destination, package.Name, package.Version);

                    // create the directory version folder if not exist
                    if (!Directory.Exists(versionFolder))
                    {
                        Directory.CreateDirectory(versionFolder);
                    }

                    try
                    {
                        FileUtility.CopyDirectory(extractedFolder, versionFolder, true);
                        request.YieldFromSwidtag(package, fastPath);
                    }
                    catch (Exception e)
                    {
                        request.CompleteProgress(tracker.ProgressID, false);
                        request.Debug(e.StackTrace);

                        if (!(e is UnauthorizedAccessException || e is IOException))
                        {
                            // something wrong, delete the version folder
                            versionFolder.TryHardToDelete();
                            return false;
                        }
                    }
                    return true;
                }
                else
                {
                    request.Warning("Failed to download a Zip package {0} from {1}", package.Name, package.Source);
                }
            }
            finally
            {
                cleanUpAction();
                file.TryHardToDelete();
                extractedFolder.TryHardToDelete();
                request.CompleteProgress(tracker.ProgressID, true);

            }

            return false;
        }
Exemple #33
0
        internal static void InstallProviderFromInstaller(PackageJson package, string fastPath, PackageSourceListRequest request) {
           
            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallProviderFromInstaller' - name='{0}', fastPath='{1}'", package.Name, fastPath));

            //install any dependency packages
            InstallDependencies(package, request);

            switch (package.Type.ToLowerInvariant()) {
                case Constants.MediaType.MsiPackage:
                case Constants.MediaType.MsuPackage:
                    InstallPackageFile(package, fastPath, request);
                    break;
                case Constants.MediaType.AppxPackage:
                    //TODO for future whenever needed to support appx packages
                    break;
                case Constants.MediaType.NuGetPackage:
                    NupkgInstaller.InstallNuGetPackage(package, fastPath, request);
                    break;
                case Constants.MediaType.ZipPackage:
                    ZipPackageInstaller.InstallZipPackage(package, fastPath, request);
                    break;
                case Constants.MediaType.ExePackage:
                    ExePackageInstaller.InstallExePackage(package, fastPath, request);
                    break;               
                case Constants.MediaType.PsArtifacts:
                    PowerShellArtifactInstaller.InstallPowershellArtifacts(package, fastPath, request);
                    break;

                default:
                    request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type);
                   break;
            }
            return;
        }
Exemple #34
0
        internal static void UninstallExePackage(string fastPath, PackageSourceListRequest request)
        {
            if (string.IsNullOrWhiteSpace(fastPath))
            {
                return;
            }
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            string sourceLocation;
            string id;
            string displayName;
            string version;
            string fastPackageReference;
            string providerName;

            if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference, providerName: out providerName))
            {
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath);
                return;
            }

            var ver     = (new SemanticVersion(version)).ToString();
            var package = request.GetPackage(id, ver) ?? request.GetPackage(displayName, ver);

            request.Debug("Calling '{0}::UninstallPackage' '{1}'", Constants.ProviderName, fastPackageReference);

            var path             = fastPackageReference.Split(new[] { '\\' }, 3);
            var uninstallCommand = string.Empty;
            Dictionary <string, string> properties = null;

            if (path.Length == 3)
            {
                switch (path[0].ToLowerInvariant())
                {
                case "hklm64":
#if !CORECLR
                    using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
#else
                    using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2]))
#endif
                    {
                        if (product == null)
                        {
                            return;
                        }
                        properties       = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase);
                        uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? "";
                    }
                    break;

                case "hkcu64":
#if !CORECLR
                    using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
#else
                    using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2]))
#endif
                    {
                        if (product == null)
                        {
                            return;
                        }
                        properties       = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase);
                        uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? "";
                    }
                    break;

                case "hklm32":
#if !CORECLR
                    using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
#else
                    using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2]))
#endif
                    {
                        if (product == null)
                        {
                            return;
                        }
                        properties       = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase);
                        uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? "";
                    }
                    break;

                case "hkcu32":
#if !CORECLR
                    using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
#else
                    using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2]))
#endif
                    {
                        if (product == null)
                        {
                            return;
                        }
                        properties       = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase);
                        uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? "";
                    }
                    break;
                }

                if (properties == null)
                {
                    return;
                }

                var result = false;
                if (!string.IsNullOrWhiteSpace(uninstallCommand))
                {
                    do
                    {
                        if (File.Exists(uninstallCommand))
                        {
                            result = ExecuteUninstallCommand(fastPackageReference, request, uninstallCommand, package.UnInstallAdditionalArguments);
                            break;
                        }

                        // not a single file.
                        // check if it's just quoted.
                        var c = uninstallCommand.Trim('\"');
                        if (File.Exists(c))
                        {
                            result = ExecuteUninstallCommand(fastPackageReference, request, c, package.UnInstallAdditionalArguments);
                            break;
                        }

                        if (uninstallCommand[0] == '"')
                        {
                            var p = uninstallCommand.IndexOf('"', 1);
                            if (p > 0)
                            {
                                var file = uninstallCommand.Substring(1, p - 1);
                                var args = uninstallCommand.Substring(p + 1);
                                if (File.Exists(file))
                                {
                                    args   = string.Join(" ", args, package.UnInstallAdditionalArguments);
                                    result = ExecuteUninstallCommand(fastPackageReference, request, file, args);
                                }
                            }
                        }
                        else
                        {
                            var p = uninstallCommand.IndexOf(' ');
                            if (p > 0)
                            {
                                var file = uninstallCommand.Substring(0, p);
                                var args = uninstallCommand.Substring(p + 1);
                                if (File.Exists(file))
                                {
                                    args   = string.Join(" ", args, package.UnInstallAdditionalArguments);
                                    result = ExecuteUninstallCommand(fastPackageReference, request, file, args);
                                    continue;
                                }

                                var s = 0;
                                do
                                {
                                    s = uninstallCommand.IndexOf(' ', s + 1);
                                    if (s == -1)
                                    {
                                        break;
                                    }
                                    file = uninstallCommand.Substring(0, s);
                                    if (File.Exists(file))
                                    {
                                        args   = uninstallCommand.Substring(s + 1);
                                        args   = string.Join(" ", args, package.UnInstallAdditionalArguments);
                                        result = ExecuteUninstallCommand(fastPackageReference, request, file, args);
                                        break;
                                    }
                                } while (s > -1);

                                if (s == -1)
                                {
                                    // never found a way to parse the command :(
                                    request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage);
                                    return;
                                }
                            }
                        }
                    } while (false);


                    if (result)
                    {
                        YieldPackage(fastPackageReference, fastPackageReference, properties, request);
                        return;
                    }
                }
                request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage);
            }
        }
Exemple #35
0
        public void ResolvePackageSources(PackageSourceListRequest request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "ResolvePackageSources");

            var selectedSources = request.SelectedSources;

            try {
                foreach (var source in selectedSources) {
                    request.Debug(Resources.Messages.YieldingPackageSource, PackageProviderName, source);
                    request.YieldPackageSource(source.Name, source.Location, source.Trusted, source.IsRegistered, source.IsValidated);
                }
            } catch (Exception e) {
                e.Dump(request);
            }

            request.Debug(Resources.Messages.DebugInfoReturnCall, PackageProviderName, "ResolvePackageSources");
        }
Exemple #36
0
        internal static void UninstallExePackage(string fastPath, PackageSourceListRequest request)
        {
            if (string.IsNullOrWhiteSpace(fastPath)) {
                return;
            }
            if (request == null) {
                throw new ArgumentNullException("request");
            }

            string sourceLocation;
            string id;
            string displayName;
            string version;
            string fastPackageReference;

            if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference))
            {
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath);
                return;
            }

            var ver = (new SemanticVersion(version)).ToString();
            var package =  request.GetPackage(id, ver) ?? request.GetPackage(displayName, ver);

            request.Debug("Calling '{0}::UninstallPackage' '{1}'", Constants.ProviderName, fastPackageReference);

            var path = fastPackageReference.Split(new[] { '\\' }, 3);
            var uninstallCommand = string.Empty;
            Dictionary<string, string> properties = null;
            if (path.Length == 3)
            {
                switch (path[0].ToLowerInvariant())
                {
                    case "hklm64":
#if !CORECLR
                        using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
#else
                        using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2]))
#endif
                        {
                            if (product == null)
                            {
                                return;
                            }
                            properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase);
                            uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? "";
                        }
                        break;
                    case "hkcu64":
#if !CORECLR
                        using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
#else
                        using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2]))
#endif
                        {
                            if (product == null)
                            {
                                return;
                            }
                            properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase);
                            uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? "";
                        }
                        break;
                    case "hklm32":
#if !CORECLR
                        using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
#else
                        using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2]))
#endif
                        {
                            if (product == null)
                            {
                                return;
                            }
                            properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase);
                            uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? "";
                        }
                        break;
                    case "hkcu32":
#if !CORECLR
                        using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
#else
                        using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2]))
#endif
                        {
                            if (product == null)
                            {
                                return;
                            }
                            properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase);
                            uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? "";
                        }
                        break;
                }

                if (properties == null)
                {
                    return;
                }

                var result = false;
                if (!string.IsNullOrWhiteSpace(uninstallCommand))
                {
                    do
                    {
                        if (File.Exists(uninstallCommand))
                        {
                            result = ExecuteUninstallCommand(fastPackageReference, request, uninstallCommand, package.UnInstallAdditionalArguments);
                            break;
                        }

                        // not a single file.
                        // check if it's just quoted.
                        var c = uninstallCommand.Trim('\"');
                        if (File.Exists(c))
                        {
                            result = ExecuteUninstallCommand(fastPackageReference, request, c, package.UnInstallAdditionalArguments);
                            break;
                        }
                     
                        if (uninstallCommand[0] == '"')
                        {
                            var p = uninstallCommand.IndexOf('"', 1);
                            if (p > 0)
                            {
                                var file = uninstallCommand.Substring(1, p - 1);
                                var args = uninstallCommand.Substring(p + 1);
                                if (File.Exists(file))
                                {
                                    args = string.Join(" ", args, package.UnInstallAdditionalArguments);
                                    result = ExecuteUninstallCommand(fastPackageReference, request, file, args);
                                }
                            }
                        }
                        else {
                            var p = uninstallCommand.IndexOf(' ');
                            if (p > 0)
                            {
                                var file = uninstallCommand.Substring(0, p);
                                var args = uninstallCommand.Substring(p + 1);
                                if (File.Exists(file))
                                {
                                    args = string.Join(" ", args, package.UnInstallAdditionalArguments);
                                    result = ExecuteUninstallCommand(fastPackageReference, request, file, args);
                                    continue;
                                }

                                var s = 0;
                                do
                                {
                                    s = uninstallCommand.IndexOf(' ', s + 1);
                                    if (s == -1)
                                    {
                                        break;
                                    }
                                    file = uninstallCommand.Substring(0, s);
                                    if (File.Exists(file))
                                    {
                                        args = uninstallCommand.Substring(s + 1);
                                        args = string.Join(" ", args, package.UnInstallAdditionalArguments);
                                        result = ExecuteUninstallCommand(fastPackageReference, request, file, args);
                                        break;
                                    }
                                } while (s > -1);

                                if (s == -1)
                                {
                                    // never found a way to parse the command :(
                                    request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage);
                                    return;
                                }
                            }
                        }
                    } while (false);


                    if (result)
                    {
                        YieldPackage(fastPackageReference, fastPackageReference, properties, request);
                        return;
                    }
                }
                request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage);
            }
        }
Exemple #37
0
        public void InstallPackage(string fastPath, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "InstallPackage' - fastReference='{0}'", fastPath));

            var package = request.GetPackageByFastPath(fastPath);
            if (package == null) 
            {
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath);
                return;
            }

            InstallProviderFromInstaller(package, fastPath, request);
        }
        /// <summary>
        /// Download data from remote via uri query.
        /// </summary>
        /// <param name="fileName">A file to store the downloaded data.</param>
        /// <param name="query">Uri query</param>
        /// <param name="request">An object passed in from the PackageManagement platform that contains APIs that can be used to interact with it </param>
        /// <param name="networkCredential">Credential to pass along to get httpclient</param>
        /// <param name="progressTracker">Utility class to help track progress</param>
        /// <returns></returns>
        internal static async Task <long> DownloadDataToFileAsync(string fileName, string query, PackageSourceListRequest request, NetworkCredential networkCredential, ProgressTracker progressTracker)
        {
            request.Verbose(Resources.Messages.DownloadingPackage, query);

            // try downloading for 3 times
            int  remainingTry    = 3;
            long totalDownloaded = 0;

            CancellationTokenSource cts;
            Stream     input  = null;
            FileStream output = null;

            while (remainingTry > 0)
            {
                // if user cancel the request, no need to do anything
                if (request.IsCanceled)
                {
                    break;
                }

                input           = null;
                output          = null;
                cts             = new CancellationTokenSource();
                totalDownloaded = 0;

                try
                {
                    // decrease try by 1
                    remainingTry -= 1;

                    var httpClient = request.Client;

                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "text/html; charset=iso-8859-1");

                    input = await httpClient.GetStreamAsync(query);


                    // buffer size of 64 KB, this seems to be preferable buffer size, not too small and not too big
                    byte[] bytes = new byte[1024 * 64];
                    output = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);

                    int current = 0;

                    // here we read content that we got from the http response stream into the bytes array
                    current = await input.ReadAsync(bytes, 0, bytes.Length, cts.Token);

                    int progressPercentage = progressTracker.StartPercent;
                    // report initial progress
                    request.Progress(progressTracker.ProgressID, progressPercentage, Resources.Messages.BytesRead, current);

                    int i = progressTracker.StartPercent;

                    while (current > 0)
                    {
                        totalDownloaded += current;

                        // here we write out the bytes array into the file
                        await output.WriteAsync(bytes, 0, current, cts.Token);

                        // report the progress
                        request.Progress(progressTracker.ProgressID, progressPercentage < progressTracker.EndPercent?progressPercentage++:progressTracker.EndPercent, Resources.Messages.BytesRead, totalDownloaded);

                        // continue reading from the stream
                        current = await input.ReadAsync(bytes, 0, bytes.Length, cts.Token);
                    }

                    if (totalDownloaded > 0)
                    {
                        // report that we finished the download
                        request.Progress(progressTracker.ProgressID, progressTracker.EndPercent, Resources.Messages.BytesRead, totalDownloaded);
                        request.CompleteProgress(progressTracker.ProgressID, true);

                        return(totalDownloaded);
                    }

                    // if request is canceled, don't retry
                    if (request.IsCanceled)
                    {
                        return(0);
                    }
                }
                catch (Exception ex)
                {
                    request.CompleteProgress(progressTracker.ProgressID, true);

                    request.Verbose(ex.Message);
                    request.Debug(ex.StackTrace);
                }
                finally
                {
                    // dispose input and output stream
                    if (input != null)
                    {
                        input.Dispose();
                    }

                    // dispose it
                    if (output != null)
                    {
                        output.Dispose();
                    }
                    // delete the file if created and nothing has downloaded
                    if (totalDownloaded == 0 && File.Exists(fileName))
                    {
                        fileName.TryHardToDelete();
                    }

                    if (cts != null)
                    {
                        cts.Dispose();
                    }
                }

                // we have to retry again
                request.Verbose(Resources.Messages.RetryingDownload, query, remainingTry);
            }

            return(totalDownloaded);
        }
Exemple #39
0
        public void DownloadPackage(string fastPath, string location, PackageSourceListRequest request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "DownloadPackage' - fastReference='{0}', location='{1}'", fastPath, location));

            var package = request.GetPackageByFastPath(fastPath);

            if (package == null)
            {
                request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath);
                return;
            }
            
            switch (package.Type.ToLowerInvariant()) {
                case Constants.MediaType.MsiPackage:
                    //No-op as the msi provider does not support save-package
                    break;
                case Constants.MediaType.MsuPackage:
                    //No-op as the msu provider does not support save-package
                    break;
                case Constants.MediaType.AppxPackage:
                    //TODO for future whenever needed to support appx packages
                    break;
                case Constants.MediaType.NuGetPackage:
                    NupkgInstaller.DownloadNuGetPackage(fastPath, location, request);
                    break;
                case Constants.MediaType.ZipPackage:
                    //TODO
                    ZipPackageInstaller.DownloadZipPackage(fastPath, location, request);
                    break;
                case Constants.MediaType.ExePackage:
                    //TODO
                    ExePackageInstaller.DownloadExePackage(fastPath, location, request);
                    break;
                case Constants.MediaType.PsArtifacts:
                    //TODO
                    break;

                default:
                    request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type);
                    return;
            }          
        }
        /// <summary>
        /// Uninstalls a package
        /// </summary>
        /// <param name="package">package defined in the PackageSourceList</param>
        /// <param name="fastPackageReference"></param>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param>
        /// <param name="fastPackReftable"></param> 
        internal static void UninstallPowershellArtifacts(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary<string, SoftwareIdentity> fastPackReftable)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "UninstallNuGetPackage' - fastReference='{0}'", fastPackageReference));

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true);
            if (provider != null)
            {
                request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name);

                if (!fastPackReftable.ContainsKey(fastPackageReference))
                {
                    request.WriteError(Internal.ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
                    return;
                }

                var p = fastPackReftable[fastPackageReference];

                //calling NuGet for uninstall
                var installing = provider.UninstallPackage(p, request);

                foreach (var i in installing)
                {
                    request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);

                    if (request.IsCanceled)
                    {
                        installing.Cancel();
                    }
                }
            }
        }
Exemple #41
0
        private void UnInstallMsiPackage(PackageSourceListRequest request,  string fastPath, PackageJson package)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "UnInstallMsiPackage' - name='{0}', fastPath='{1}'", package.Name, fastPath));

            string sourceLocation;
            string id;
            string displayName;
            string version;
            string fastPackageReference;

            if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference))
            {
                //we don't need to error out even if fastpath is not correct because msi provider is expected to handle the uninstall-package.
                request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name);
                return;
            }

            // Normally uninstall-package will be handled by MSI provider. Here we added a special case for handling uninstall-package nodejs
            // which msi provider unable to deal with (node.js only for msi)
            if (id != null && id.EqualsIgnoreCase("nodejs"))
            {

                var provider = PackageSourceListRequest.FindProvider(request, Constants.ProviderNames.Msi, request);

                if (provider != null)
                {

                    if (!_fastPackReftable.ContainsKey(fastPackageReference))
                    {
                        request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
                        return;
                    }

                    request.Verbose(Resources.Messages.UninstallingPackage, Constants.ProviderName, package.Name);

                    var p = _fastPackReftable[fastPackageReference];

                    var installing = provider.UninstallPackage(p, request);

                    foreach (var i in installing)
                    {
                        request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme,
                            i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename);
                        if (request.IsCanceled)
                        {
                            installing.Cancel();
                        }
                        return;
                    }
                }
            }
            else
            {
                //no-op for uninstalling the msi packages. only install-package nodejs is supported because msi can not handle it
                request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name);
                return;
            }
           
        }
Exemple #42
0
        /// <summary>
        /// Removes/Unregisters a package source
        /// </summary>
        /// <param name="name">The name or location of a package source to remove.</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 RemovePackageSource(string name, PackageSourceListRequest request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "RemovePackageSource");

            var src = request.FindRegisteredSource(name);
            if (src == null) {
                request.Warning(Constants.Messages.UnableToResolveSource, Constants.ProviderName, name);
                return;
            }

            request.RemovePackageSource(src.Name);
            request.YieldPackageSource(src.Name, src.Location, src.Trusted, false, src.IsValidated);
        }
Exemple #43
0
        private void GetMsiInstalledPackage(string name, PackageJson package, string requiredVersion, string minimumVersion, string maximumVersion, PackageSourceListRequest request) {

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "GetMsiInstalledPackage' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion));

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request);
            if (provider != null) {
                var packagesInstalled = provider.GetInstalledPackages(package.Name, requiredVersion, minimumVersion, maximumVersion, request);

                if (!packagesInstalled.Any()) {
                    packagesInstalled = provider.GetInstalledPackages(package.DisplayName, requiredVersion, minimumVersion, maximumVersion, request);
                }
                foreach (var i in packagesInstalled)
                {
                    request.Debug("found a package '{0}.{1}' installed from '{2}'", i.Name, i.Version, i.Source);
 
                    var info = PackageSourceListRequest.MakeFastPathComplex(i.Source, name, (package.DisplayName?? ""), i.Version, i.FastPackageReference);

                    _fastPackReftable.AddOrSet(i.FastPackageReference, i);

                    // check if the installed version matches with the one specified in the PSL.json.
                    // If so, we choose PSL.json.
                    var version = i.Version.CompareVersion(package.Version) ? package.Version : i.Version;
                    //we use displayname here because msi provider uses the displayname. 
                    request.YieldSoftwareIdentity(info, package.DisplayName, version, i.VersionScheme, i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename);
                    return;
                }           
            }
        }
Exemple #44
0
        public void GetDynamicOptions(string category, PackageSourceListRequest request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

            request.Debug("Calling ''{0}'::{1} ({2})'", Constants.ProviderName, "GetDynamicOptions", category);

            switch ((category ?? string.Empty).ToLowerInvariant()) {
                case "package":
                    break;

                case "source":
                    break;

                case "install":
                    request.YieldDynamicOption("Scope", "String", false, new[] {"CurrentUser", "AllUsers"});
                    request.YieldDynamicOption("SkipHashValidation", Constants.OptionType.Switch, false);
                    break;
            }
        }
Exemple #45
0
        public void GetInstalledPackages(string name, string requiredVersion, string minimumVersion, string maximumVersion, PackageSourceListRequest request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }
            if (!ValidatePackageManagementVersion(request))
            {
                return;
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "GetInstalledPackages' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion));

            IEnumerable<PackageJson> packagesDefinedInJsonSpec;

            if (string.IsNullOrWhiteSpace(name) || WildcardPattern.ContainsWildcardCharacters(name)) {
                // In the case of the package name is null or contains wildcards, error out if a user puts version info
                if (!string.IsNullOrWhiteSpace(requiredVersion) || !string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrWhiteSpace(maximumVersion))
                {
                    request.Warning(Resources.Messages.WildCardCharsAreNotSupported, name);
                    return;
                }
                packagesDefinedInJsonSpec = request.GetPackages(name).ToArray();
 
            } else {
                //return all installed
                packagesDefinedInJsonSpec = request.GetPackages(name, requiredVersion, minimumVersion, maximumVersion).ToArray();
            }
            _fastPackReftable.Clear();

            if (!packagesDefinedInJsonSpec.Any())
            {
                request.Verbose(Resources.Messages.NoPackageFound, Constants.ProviderName);
                return;
            }

            foreach (var package in packagesDefinedInJsonSpec) {
                switch (package.Type.ToLowerInvariant()) {

                    case Constants.MediaType.AppxPackage:
                        //TODO for future
                        break;
                    case Constants.MediaType.PsArtifacts:
                        PowerShellArtifactInstaller.GeInstalledPowershellArtifacts(package, requiredVersion, minimumVersion,maximumVersion, _fastPackReftable, request);
                        break;
                    case Constants.MediaType.ExePackage:
                        //program provider can handle get-package git for git.exe
                        ExePackageInstaller.GetInstalledExePackages(package, requiredVersion, minimumVersion, minimumVersion, request);
                        break;
                    case Constants.MediaType.MsiPackage:
                        //msi provider can handle get-package node.js for node.js.msi                       
                        GetMsiInstalledPackage(name, package, requiredVersion, minimumVersion, maximumVersion, request);
                        break;
                    case Constants.MediaType.ZipPackage:
                        ZipPackageInstaller.GetInstalledZipPackage(package, request);
                        break;
                    case Constants.MediaType.NuGetPackage:
                        NupkgInstaller.GeInstalledNuGetPackages(package, requiredVersion, minimumVersion, maximumVersion, _fastPackReftable, request);
                        break;

                } //switch
            }
        }
Exemple #46
0
        public void GetFeatures(PackageSourceListRequest request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

            request.Debug("Calling ''{0}'::{1}'", Constants.ProviderName, "GetFeatures");

            request.Debug("Calling 'PackageSourceListProvider::GetFeatures'");
            foreach (var feature in Features) {
                request.Yield(feature);
            }
        }
Exemple #47
0
        internal static void InstallNuGetPackage(PackageJson package, string fastPath, PackageSourceListRequest request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallNuGetPackage' - name='{0}', fastPath='{1}'", package.Name, fastPath));

            var canonicalId = PackageSourceListRequest.CreateCanonicalId(package, Constants.ProviderNames.NuGet); // "nuget:jquery/2.1.0#http://nuget.org/api/v2";
            var pkgs = request.PackageManagementService.FindPackageByCanonicalId(canonicalId, request.As<IHostApi>())
                        .Where(each => string.IsNullOrWhiteSpace(package.Version) ||
                        (new SemanticVersion(each.Version) == new SemanticVersion(package.Version))).ToArray();

            switch (pkgs.Length)
            {
                case 0:
                    request.Warning(Resources.Messages.CannotFindPackage, Constants.ProviderName, canonicalId);
                    return;
                case 1:
                    InstallPackageReference(package, request, pkgs);
                    return;

                default:
                    request.Warning(Resources.Messages.FoundMorePackages, Constants.ProviderName, pkgs.Length, canonicalId);
                    return;
            }
        }
Exemple #48
0
        public void FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion, int id, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindPackage' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion));

            // no package name or the name with wildcard
            if (string.IsNullOrWhiteSpace(name) || WildcardPattern.ContainsWildcardCharacters(name))
            {
                // In the case of the package name is null or contains wildcards, error out if a user puts version info
                if (!string.IsNullOrWhiteSpace(requiredVersion) || !string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrWhiteSpace(maximumVersion))
                {
                    request.Warning(Resources.Messages.WildCardCharsAreNotSupported, name);
                    return;
                }

                var packages = request.GetPackages(name);
                if (request.GetOptionValue("AllVersions").IsTrue())
                {
                    // if version is specified then name can not be empty or with wildcard. in this case the cmdlet has been errored out already.
                    // here we just return all packages we can find
                    if (request.FilterOnVersion(packages, requiredVersion, minimumVersion, maximumVersion, minInclusive: true, maxInclusive: true, latest: false).OrderBy(p => p.Name).Any(p => !request.YieldFromSwidtag(p, p.Name)))
                    {
                        return;
                    }
                    return;
                }

                //return the latest version
                if (packages.GroupBy(p => p.Name)
                    .Select(each => each.OrderByDescending(pp => pp.Version).FirstOrDefault()).OrderBy(p => p.Name).Any(item => !request.YieldFromSwidtag(item, item.Name)))
                {
                    return;
                }
            }
            else
            {
                // a user specifies name
                // asked for a specific version?
                if (!string.IsNullOrWhiteSpace(requiredVersion))
                {
                    request.YieldFromSwidtag(request.GetPackage(name, requiredVersion), name);
                    return;
                }

                var allVersion = request.GetOptionValue("AllVersions").IsTrue();
                // asked for a version range?
                if (!string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrEmpty(maximumVersion) || allVersion)
                {
                    var packages = request.GetPackagesWithinVersionRange(name, minimumVersion, maximumVersion);
                    if (allVersion)
                    {
                        if (packages.Any(p => !request.YieldFromSwidtag(p, name)))
                        {
                            return;
                        }
                    }
                    else
                    {
                        if (request.YieldFromSwidtag(packages.FirstOrDefault(), name))
                        {
                            return;
                        }
                    }

                    return;
                }

                // just return by name
                request.YieldFromSwidtag(request.GetPackage(name), name);
            }
        }
        internal static string DownloadFile(string queryUrl, string destination, PackageSourceListRequest request, ProgressTracker progressTracker)
        {
            try
            {
                request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(System.Globalization.CultureInfo.InvariantCulture, "DownloadFile - url='{0}', destination='{1}'", queryUrl, destination));

                if (string.IsNullOrWhiteSpace(destination))
                {
                    throw new ArgumentNullException("destination");
                }

                // make sure that the parent folder is created first.
                var folder = Path.GetDirectoryName(destination);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                if (File.Exists(destination))
                {
                    destination.TryHardToDelete();
                }


                if (progressTracker == null)
                {
                    progressTracker = new ProgressTracker(request.StartProgress(0, Resources.Messages.DownloadingPackage, queryUrl));
                }

                Uri uri;

                if (!Uri.TryCreate(queryUrl, UriKind.Absolute, out uri))
                {
                    request.Error(Internal.ErrorCategory.InvalidOperation, Resources.Messages.UnsupportedUriFormat, Constants.ProviderName, queryUrl);
                    return(null);
                }

                if (uri.IsFile)
                {
                    // downloading from a file share
                    using (var input = File.OpenRead(queryUrl))
                    {
                        using (var output = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.Read))
                        {
                            request.Progress(progressTracker.ProgressID, progressTracker.StartPercent, Resources.Messages.Downloading);

                            input.CopyTo(output);
                        }
                    }

                    request.CompleteProgress(progressTracker.ProgressID, true);
                }
                else
                {
                    //Downloading from url
                    var result = DownloadDataToFileAsync(destination, queryUrl, request, PathUtility.GetNetworkCredential(request.CredentialUsername, request.CredentialPassword), progressTracker).Result;
                }

                if (File.Exists(destination))
                {
                    request.Verbose(Resources.Messages.CompletedDownload, queryUrl);
                    return(destination);
                }
                else
                {
                    request.Error(Internal.ErrorCategory.InvalidOperation, Resources.Messages.FailedToDownload, Constants.ProviderName, queryUrl, destination);
                    return(null);
                }
            }
            catch (Exception ex)
            {
                ex.Dump(request);
                request.Warning(ex.Message);
                return(null);
            }
        }
Exemple #50
0
        private static void InstallPackageFile(PackageJson package, string fastPath, PackageSourceListRequest request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallPackageFile' - name='{0}', fastPath='{1}'", package.Name, fastPath));

            // get a temp file name, msi or msu
            var providerType = package.Type.ToLowerInvariant();
            var destination = Path.ChangeExtension(Path.GetTempFileName(), providerType);

            //download the msi package to the temp file
            WebDownloader.DownloadFile(package.Source, destination, request, null);
            
            if (!File.Exists(destination))
            {
                return;
            }


            // validate the file
            if (!WebDownloader.VerifyHash(destination,package, request))
            {
                return;
            }
           
            if (!package.IsTrustedSource)
            {
                if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source))
                {
                    request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name);
                    return;
                }
            }

            var installRequest = PackageSourceListRequest.ExtendRequest(new Dictionary<string, string[]>
                           {
                                {"Source", new[] {package.Source ?? ""}}
                           }, new[] { package.Source ?? "" }, package.IsTrustedSource, request);

            request.Verbose(Resources.Messages.CallMsiForInstall, package.Name);
            if (request.ProviderServices.Install(destination, "", installRequest))
            {
                // it installed ok!exit
                request.YieldFromSwidtag(package, fastPath);
                return;
            }
            else
            {
                request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.PackageFailedInstall, package.Name);
            }
        }
        internal static void GeInstalledPowershellArtifacts(PackageJson package, string requiredVersion, string minimumVersion, string maximumVersion, Dictionary<string, SoftwareIdentity> fastPackReftable, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "GeInstalledPowershellArtifacts' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", package.Name, requiredVersion, minimumVersion, maximumVersion));

            var provider = PackageSourceListRequest.FindProvider(request, package.Type, request);
            if (provider == null) return;

            //calling the PowerShellGet provider
            request.Debug("Calling '{0}' provider to get installed packages '{1}.{2}'", provider.Name, package.Name, package.Version);

            var packagesInstalled = provider.GetInstalledPackages(package.Name, requiredVersion, minimumVersion, maximumVersion, request).ToArray();

            if (packagesInstalled == null || !packagesInstalled.Any())
            {
                request.Verbose(Resources.Messages.NumberOfPackagesRecevied, 0, provider.Name, "GetInstalledPackages");
                return;
            }          
            
            foreach (var i in packagesInstalled)
            {
                request.Debug("Found an installed package '{0}.{1} from {2}' ", i.Name, i.Version, i.Source);
                var info = PackageSourceListRequest.MakeFastPathComplex(i.Source, i.Name, "", i.Version, "");

                fastPackReftable.AddOrSet(info, i);

                // check if the installed version matches with the one specified in the PSL.json.
                // If so, we choose PSL.json.
                var version = i.Version.CompareVersion(package.Version) ? package.Version : i.Version;
                request.YieldSoftwareIdentity(info, i.Name, version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);
            }                       
        }
Exemple #52
0
        internal static string DownloadFile(string queryUrl, string destination, PackageSourceListRequest request, ProgressTracker progressTracker)
        {
            try
            {
                request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(System.Globalization.CultureInfo.InvariantCulture, "DownloadFile - url='{0}', destination='{1}'", queryUrl, destination));

                if (string.IsNullOrWhiteSpace(destination))
                {
                    throw new ArgumentNullException("destination");
                }

                // make sure that the parent folder is created first.
                var folder = Path.GetDirectoryName(destination);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                if (File.Exists(destination))
                {
                    destination.TryHardToDelete();
                }

                
                if (progressTracker == null)
                {
                    progressTracker = new ProgressTracker(request.StartProgress(0, Resources.Messages.DownloadingPackage, queryUrl));
                } 

                Uri uri;

                if (!Uri.TryCreate(queryUrl, UriKind.Absolute, out uri))
                {
                    request.Error(Internal.ErrorCategory.InvalidOperation, Resources.Messages.UnsupportedUriFormat, Constants.ProviderName, queryUrl);
                    return null;
                }

                if (uri.IsFile)
                {
                    // downloading from a file share
                    using (var input = File.OpenRead(queryUrl))
                    {
                        using (var output = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.Read))
                        {
                            request.Progress(progressTracker.ProgressID, progressTracker.StartPercent, Resources.Messages.Downloading);

                            input.CopyTo(output);

                        }
                    }

                    request.CompleteProgress(progressTracker.ProgressID, true);
                }
                else
                {
                    //Downloading from url
                    var result = DownloadDataToFileAsync(destination, queryUrl, request, PathUtility.GetNetworkCredential(request.CredentialUsername, request.CredentialPassword), progressTracker).Result;
                }

                if (File.Exists(destination))
                {
                    request.Verbose(Resources.Messages.CompletedDownload, queryUrl);                    
                    return destination;
                }
                else
                {
                    request.Error(Internal.ErrorCategory.InvalidOperation, Resources.Messages.FailedToDownload, Constants.ProviderName, queryUrl, destination);
                    return null;
                }                
            }
            catch (Exception ex)
            {
                ex.Dump(request);
                request.Warning(ex.Message);
                return null;
            }
        }
Exemple #53
0
        /// <summary>
        /// Download data from remote via uri query.
        /// </summary>
        /// <param name="fileName">A file to store the downloaded data.</param>
        /// <param name="query">Uri query</param>
        /// <param name="request">An object passed in from the PackageManagement platform that contains APIs that can be used to interact with it </param>   
        /// <param name="networkCredential">Credential to pass along to get httpclient</param>
        /// <param name="progressTracker">Utility class to help track progress</param>
        /// <returns></returns>
        internal static async Task<long> DownloadDataToFileAsync(string fileName, string query, PackageSourceListRequest request, NetworkCredential networkCredential, ProgressTracker progressTracker)
        {
            request.Verbose(Resources.Messages.DownloadingPackage, query);

            // try downloading for 3 times
            int remainingTry = 3;
            long totalDownloaded = 0;

            CancellationTokenSource cts;
            Stream input = null;
            FileStream output = null;

            while (remainingTry > 0)
            {
                // if user cancel the request, no need to do anything
                if (request.IsCanceled)
                {
                    break;
                }

                input = null;
                output = null;
                cts = new CancellationTokenSource();
                totalDownloaded = 0;

                try
                {
                    // decrease try by 1
                    remainingTry -= 1;

                    var httpClient = request.Client;

                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "text/html; charset=iso-8859-1");

                    input = await httpClient.GetStreamAsync(query);


                    // buffer size of 64 KB, this seems to be preferable buffer size, not too small and not too big
                    byte[] bytes = new byte[1024 * 64];
                    output = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);

                    int current = 0;

                    // here we read content that we got from the http response stream into the bytes array
                    current = await input.ReadAsync(bytes, 0, bytes.Length, cts.Token);

                    int progressPercentage = progressTracker.StartPercent;
                    // report initial progress
                    request.Progress(progressTracker.ProgressID, progressPercentage, Resources.Messages.BytesRead, current);

                    int i = progressTracker.StartPercent;

                    while (current > 0)
                    {
                        totalDownloaded += current;

                        // here we write out the bytes array into the file
                        await output.WriteAsync(bytes, 0, current, cts.Token);

                        // report the progress
                        request.Progress(progressTracker.ProgressID, progressPercentage<progressTracker.EndPercent?progressPercentage++:progressTracker.EndPercent, Resources.Messages.BytesRead, totalDownloaded);

                        // continue reading from the stream
                        current = await input.ReadAsync(bytes, 0, bytes.Length, cts.Token);
                    }

                    if (totalDownloaded > 0)
                    {
                        // report that we finished the download
                        request.Progress(progressTracker.ProgressID, progressTracker.EndPercent, Resources.Messages.BytesRead, totalDownloaded);
                        request.CompleteProgress(progressTracker.ProgressID, true);

                        return totalDownloaded;
                    }

                    // if request is canceled, don't retry
                    if (request.IsCanceled)
                    {
                        return 0;
                    }
                }
                catch (Exception ex)
                {
                    request.CompleteProgress(progressTracker.ProgressID, true);

                    request.Verbose(ex.Message);
                    request.Debug(ex.StackTrace);
                }
                finally
                {
                    // dispose input and output stream
                    if (input != null)
                    {
                        input.Dispose();
                    }

                    // dispose it
                    if (output != null)
                    {
                        output.Dispose();
                    }
                    // delete the file if created and nothing has downloaded
                    if (totalDownloaded == 0 && File.Exists(fileName))
                    {
                        fileName.TryHardToDelete();
                    }

                    if (cts != null)
                    {
                        cts.Dispose();
                    }
                }

                // we have to retry again
                request.Verbose(Resources.Messages.RetryingDownload, query, remainingTry);
            }

            return totalDownloaded;
        }
Exemple #54
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);
            }
        }
        private static void InstallPackageViaPowerShellGet(PackageJson packageJson, PackageSourceListRequest request, SoftwareIdentity[] packages)
        {
            var provider = PackageSourceListRequest.FindProvider(request, packageJson.Type, request, true);

            if (provider == null)
            {
                return;
            }

            IHostApi installRequest = request;

            if (provider.Name.EqualsIgnoreCase(Constants.ProviderNames.PowerShellGet) && !request.ProviderServices.IsElevated)
            {
                // if we're not elevated, we want powershellget to install to the user scope
                installRequest = PackageSourceListRequest.ExtendRequest(
                    new Dictionary <string, string[]> {
                    { "Scope", new[] { "CurrentUser" } }
                }, null, packageJson.IsTrustedSource, request);
            }
            else
            {
                installRequest = PackageSourceListRequest.ExtendRequest(
                    new Dictionary <string, string[]> {
                    { "Destination", new[] { packageJson.Destination ?? "" } }
                }, null, packageJson.IsTrustedSource, request);
            }

            request.Debug("Calling '{0}' provider to install the package '{1}.{2}'", provider.Name, packageJson.Name, packageJson.Version);

            var installing = provider.InstallPackage(packages[0], installRequest);

            if (installing == null || !installing.Any())
            {
                request.Verbose(Resources.Messages.NumberOfPackagesRecevied, 0, provider.Name, "InstallPackage");
                request.Warning(Resources.Messages.FailToInstallPackage, Constants.ProviderName, packages[0].Name);
                return;
            }

            int packagesReceived = 0;

            foreach (var i in installing)
            {
                request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);

                if (request.IsCanceled)
                {
                    installing.Cancel();
                }
                else
                {
                    request.Verbose(Resources.Messages.SuccessfullyInstalled, "{0}.{1}".format(packageJson.Name, packageJson.Version));
                    //load provider
                    if (packageJson.IsPackageProvider)
                    {
                        //Per provider development guidance: provider name and module name should be the same otherwise we can not import it.
                        request.PackageManagementService.ImportPackageProvider(request, packageJson.Name, null, null, null, isRooted: false, force: false);
                    }
                }
                packagesReceived++;
            }

            request.Verbose(Resources.Messages.NumberOfPackagesRecevied, packagesReceived, provider.Name, "install-package");
        }
Exemple #56
0
        internal static void DownloadZipPackage(string fastPath, string location, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            //TODO
            request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName,
                string.Format(CultureInfo.InvariantCulture, "DownloadZipPackage' - fastReference='{0}', location='{1}'", fastPath, location));
           
        }