internal static void InstallPowershellArtifacts(PackageJson package, string fastPath, PackageSourceListRequest request)
        {
            var provider1 = PackageSourceListRequest.FindProvider(request, package.Type, request, true);         
            if (provider1 == null) return;

            // As the PowerShellGet may access the -source via $request.Options or PackageSources,
            // so we need to fill in both options and sources parameters
            var installRequest = PackageSourceListRequest.ExtendRequest(new Dictionary<string, string[]>
                           {
                                {"Source", new[] {package.Source ?? ""}}
                           }, new[] { package.Source ?? "" }, package.IsTrustedSource, request);

            var psid = PackageSourceListRequest.CreateCanonicalId(package, Constants.ProviderNames.PowerShellGet);
            var pkgs = request.PackageManagementService.FindPackageByCanonicalId(psid, installRequest)
             .Where(each => new SemanticVersion(each.Version) == new SemanticVersion(package.Version)).ToArray();
            
            switch (pkgs.Length)
            {
                case 0:
                    request.Warning(Resources.Messages.CannotFindPackage, Constants.ProviderName, psid);
                    break;
                case 1:
                    InstallPackageViaPowerShellGet(package, request, pkgs);
                    break;

                default:
                    request.Warning(Resources.Messages.FoundMorePackages, Constants.ProviderName, pkgs.Length, psid);
                    break;
            }
            return;
        }
        internal static bool UnInstallZipPackage(PackageSourceListRequest request, string fastPath)
        {
            string sourceLocation;
            string id;
            string displayName;
            string version;
            string path;

            var package = request.GetFastReferenceComplex(fastPath);

            if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out path))
            {
                request.Error(ErrorCategory.InvalidOperation, "package", Constants.Messages.UnableToUninstallPackage);
                return(false);
            }

            if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path))
            {
                path.TryHardToDelete();
                request.YieldFromSwidtag(package, path);
                return(true);
            }
            else
            {
                request.Error(ErrorCategory.InvalidData, "folder {0} does not exist", path);
                return(false);
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
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;
                }
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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;
            }
        }
Ejemplo n.º 7
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");
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        private static void InstallPackageReference(PackageJson package, PackageSourceListRequest request, SoftwareIdentity[] packages)
        {
            var installRequest = PackageSourceListRequest.ExtendRequest(
                new Dictionary <string, string[]>
            {
                { "Destination", new[] { package.Destination ?? "" } }
            },
                new[] { package.Source ?? "" },
                package.IsTrustedSource,
                request);

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

            if (provider == null)
            {
                return;
            }

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

            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();
                }
            }
        }
Ejemplo n.º 10
0
        internal static bool TestCatalogFile(string jsonFile, string catalogFile, PackageSourceListRequest request)
        {
            try
            {
                PSObject result = null;
                using (PowerShell powershell = PowerShell.Create())
                {
                    if (powershell != null)
                    {
                        result = powershell
                                 .AddCommand("Test-FileCatalog")
                                 .AddParameter("CatalogFilePath", catalogFile)
                                 .AddParameter("Path", jsonFile)
                                 .Invoke().FirstOrDefault();
                    }
                    if (result.ToString().EqualsIgnoreCase("Valid"))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                request.WriteError(ErrorCategory.InvalidData, catalogFile, Resources.Messages.CatalogFileVerificationFailedWithError, catalogFile, ex.Message.ToString());
                return(false);
            }

            request.WriteError(ErrorCategory.InvalidData, catalogFile, Resources.Messages.CatalogFileVerificationFailed, jsonFile);
            return(false);
        }
Ejemplo n.º 11
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;
            }
        }
Ejemplo n.º 12
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 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();
                    }
                }
            }
        }
Ejemplo n.º 13
0
 public void InitializeProvider(PackageSourceListRequest request)
 {
     if (request == null)
     {
         throw new ArgumentNullException("request");
     }
     request.Debug("Initialize PackageSourceListProvider");
 }
Ejemplo n.º 14
0
        public void DownloadPackage(string fastPath, string location, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Warning("PSL:Save-Package is not supported.");
        }
        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");
        }
Ejemplo n.º 16
0
        internal static void DownloadExePackage(string fastPath, string location, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            // TODO do we need to support save-package for executable packages?
        }
Ejemplo n.º 17
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;
            }
        }
Ejemplo n.º 18
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));
        }
Ejemplo n.º 19
0
        internal static bool UnInstallZipPackage(PackageJson package, PackageSourceListRequest request, string fastPath)
        {
            string sourceLocation;
            string id;
            string displayName;
            string version;
            string path;
            string providerName;

            if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out path, providerName: out providerName))
            {
                request.Error(ErrorCategory.InvalidOperation, "package", Constants.Messages.UnableToUninstallPackage);
                return(false);
            }

            if (request.AddToPath.Value)
            {
                request.Warning(Resources.Messages.AddOrRemovePath, Constants.ProviderName, "AddToPath", "Install-Package");
            }

            if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path))
            {
                //Try to delete the directory to see if we can. Error out if we donot have permission
                if (Directory.Exists(path))
                {
                    try {
                        Directory.Delete(path, true);
                    }
                    catch (System.UnauthorizedAccessException) {
                        request.WriteError(ErrorCategory.InvalidOperation, package.Name, Resources.Messages.UninstallFailed, "UnInstall-Package", "UnauthorizedAccessException. The requested operation likely requires elevation, i.e., launch PowerShell as administer");
                    }
                }
                path.TryHardToDelete();

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

                // delete an empty directory
                if (Directory.Exists(dir) && (!Directory.GetDirectories(dir).Any()) && !(Directory.GetFiles(dir).Any()))
                {
                    dir.TryHardToDelete();
                }

                request.YieldFromSwidtag(package, path);

                RemoveEnvironmentVariable(request, path, SystemEnvironmentKey);
                RemoveEnvironmentVariable(request, path, UserEnvironmentKey);

                return(true);
            }
            else
            {
                request.WriteError(ErrorCategory.InvalidData, path, Resources.Messages.DirectoryNotExist, Constants.ProviderName, path);
                return(false);
            }
        }
Ejemplo n.º 20
0
        private void promptForSampleJSON(PackageSourceListRequest request)
        {
            var    srcs = request.SelectedSources;
            string userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider");
            bool   providerNameSpecified = !string.IsNullOrWhiteSpace(userSpecifiedProvider) && Constants.ProviderName.EqualsIgnoreCase(userSpecifiedProvider);

            if ((srcs == null || srcs.Count() == 0) && providerNameSpecified)
            {
                request.PromptAndDownloadSampleJSON();
            }
        }
Ejemplo n.º 21
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;
            }
        }
Ejemplo n.º 22
0
        internal static bool VerifyHash(string fileFullPath, PackageJson package, PackageSourceListRequest request)
        {
            //skip in case the skip switch is specified
            if (request.SkipHashValidation.Value)
            {
                request.Verbose(Resources.Messages.SkipHashValidation);
                return(true);
            }
            PackageHash packageHash = package.Hash;

            if (packageHash == null || string.IsNullOrWhiteSpace(packageHash.algorithm) || string.IsNullOrWhiteSpace(packageHash.hashCode))
            {
                request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.HashNotSpecified, package.Name);
                return(false);
            }
            try
            {
                string hashAlgorithm = packageHash.algorithm.ToLowerInvariant();
                if (!("sha256".Equals(hashAlgorithm) || "md5".Equals(hashAlgorithm) || "sha512".Equals(hashAlgorithm)))
                {
                    request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.InvalidHashAlgorithm, packageHash.algorithm);
                    return(false);
                }

                // compute the hash
                string computedHash = ComputeHash(fileFullPath, hashAlgorithm, request);
                if (computedHash == null)
                {
                    request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source);
                    return(false);
                }
                // hash from json
                string hashFromJSON = package.Hash.hashCode;
                //compare computed hash with hash from json
                if (!hashFromJSON.Equals(computedHash))
                {
                    request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source);
                    return(false);
                }
                else
                {
                    request.Verbose(Resources.Messages.HashValidationSuccessfull);
                }
            }
            catch
            {
                request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 23
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();
                    }
                }
            }
        }
Ejemplo n.º 24
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);
            }
        }
Ejemplo n.º 25
0
        internal static void GetInstalledZipPackage(PackageJson package, PackageSourceListRequest request)
        {

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

                    //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);
                }
            }
        }
Ejemplo n.º 26
0
        internal static void GetInstalledZipPackage(PackageJson package, PackageSourceListRequest request)
        {
            string path = Path.Combine(package.Destination, package.Name, package.Version);

            if (Directory.Exists(path))
            {
                if (Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Any())
                {
                    var fp = PackageSourceListRequest.MakeFastPathComplex(package.Source, package.Name, (package.DisplayName ?? ""), package.Version, path);

                    //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);
                }
            }
        }
Ejemplo n.º 27
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);
            }
        }
Ejemplo n.º 28
0
        internal PackageQuery(PackageSource packageSource, PackageSourceListRequest request)
        {
            if (packageSource == null)
            {
                throw new ArgumentNullException("packageSource");
            }

            if(string.IsNullOrWhiteSpace(packageSource.Location) || !System.IO.File.Exists(packageSource.Location))
            {
                request.Warning(Resources.Messages.PackageSourceManifestNotFound, packageSource.Location, Constants.ProviderName);
                return;
            }

            Uri uri;

            if (Uri.TryCreate(packageSource.Location, UriKind.Absolute, out uri))
            {
                if (uri.IsFile)
                {
                    try
                    {
                        //process the file
                        _PackageSourceList = JsonParser.ReadPackageSpec(packageSource, null);
                    }
                    catch (Exception ex)
                    {
                        request.Warning(ex.Message);
                        while (ex.InnerException != null)
                        {
                            ex = ex.InnerException;
                            request.Warning(ex.Message);
                        }
                        request.Warning(string.Format(CultureInfo.CurrentCulture, Resources.Messages.InvalidPackageListFormat, uri.AbsolutePath));
                        ex.Dump(request);
                    }
                }

                // Uri?
                //TODO: ask a user whether go ahead for downloading an sample PSL.json
            }
            else
            {
                //TODO: Check with GP, DSC Settings, 
                
                request.Verbose(Resources.Messages.UnsupportedPackageSource, packageSource.Location);
                return;
            }
        }
Ejemplo n.º 29
0
        internal PackageQuery(PackageSource packageSource, PackageSourceListRequest request)
        {
            if (packageSource == null)
            {
                throw new ArgumentNullException("packageSource");
            }

            if (string.IsNullOrWhiteSpace(packageSource.Location) || !System.IO.File.Exists(packageSource.Location))
            {
                request.Warning(Resources.Messages.PackageSourceManifestNotFound, packageSource.Location, Constants.ProviderName);
                return;
            }

            Uri uri;

            if (Uri.TryCreate(packageSource.Location, UriKind.Absolute, out uri))
            {
                if (uri.IsFile)
                {
                    try
                    {
                        //process the file
                        _PackageSourceList = JsonParser.ReadPackageSpec(packageSource, null);
                    }
                    catch (Exception ex)
                    {
                        request.Warning(ex.Message);
                        while (ex.InnerException != null)
                        {
                            ex = ex.InnerException;
                            request.Warning(ex.Message);
                        }
                        request.Warning(string.Format(CultureInfo.CurrentCulture, Resources.Messages.InvalidPackageListFormat, uri.AbsolutePath));
                        ex.Dump(request);
                    }
                }

                // Uri?
                //TODO: ask a user whether go ahead for downloading an sample PSL.json
            }
            else
            {
                //TODO: Check with GP, DSC Settings,

                request.Verbose(Resources.Messages.UnsupportedPackageSource, packageSource.Location);
                return;
            }
        }
Ejemplo n.º 30
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);
            }
        }
Ejemplo n.º 31
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 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();
                    }
                }
            }
        }
Ejemplo n.º 32
0
 internal PackageQuery(PackageSource packageSource, string file, PackageSourceListRequest request)
 {
     try
     {
         //process the file
         _PackageSourceList = JsonParser.ReadPackageSpec(packageSource, file);
     }
     catch (Exception ex)
     {
         request.Warning(ex.Message);
         while (ex.InnerException != null)
         {
             ex = ex.InnerException;
             request.Warning(ex.Message);
         }
         request.Warning(string.Format(CultureInfo.CurrentCulture, Resources.Messages.InvalidPackageListFormat, file));
         ex.Dump(request);
     }
 }
Ejemplo n.º 33
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);
            }
        }
Ejemplo n.º 34
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 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();
                    }
                }
            } 
        }
Ejemplo n.º 35
0
        internal static void InstallDependencies(PackageJson packageJson, PackageSourceListRequest request)
        {
            //TODO dependency chain check

            //let's install dependency first in case it is needed for installing the actual package
            if (packageJson.DependencyObjects != null)
            {
                var dependencies = GetDependencies(packageJson, request);

                if (dependencies != null)
                {
                    foreach (var dep in dependencies.Where(dep => (dep != null) && !dep.IsCommonDefinition))
                    {
                        //dependency source trusty follows its parent
                        var id = PackageSourceListRequest.CreateCanonicalId(dep, dep.Name);
                        InstallProviderFromInstaller(dep, id, request);
                    }
                }
            }
        }
Ejemplo n.º 36
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);

                    }
                }
            }
        }
Ejemplo n.º 37
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
            }
        }
Ejemplo n.º 38
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;
                }           
            }
        }
Ejemplo n.º 39
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;
            }
           
        }
Ejemplo n.º 40
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;
            }
        }
Ejemplo n.º 41
0
        private static IEnumerable<PackageJson> GetDependencies(PackageJson packageJson, PackageSourceListRequest request) {

            if (packageJson.DependencyObjects == null) {
                yield break;
            }

            bool force = request.GetOptionValue("Force") != null;
            foreach (var dep in packageJson.DependencyObjects.Where(dep => (dep != null) && !dep.IsCommonDefinition)) {
                if (!force) {
                    var provider = PackageSourceListRequest.FindProvider(request, dep.Type, request, true);

                    if(provider == null)
                    {
                        //FindProvider logged an error already
                        break;
                    }
                    //Check whether the dependency package is installed
                    var installedPackages = provider.GetInstalledPackages(dep.Name, requiredVersion: null, minimumVersion: dep.Version, maximumVersion: null, requestObject: request);

                    if (installedPackages == null || !installedPackages.Any()) {
                        request.Verbose(Resources.Messages.DependencyNotInstalled, dep.Name);
                        yield return dep;
                    } else {
                        request.Verbose(Resources.Messages.DependencyInstalled, dep.Name);
                    }
                } else {
                    yield return dep;
                }
            }
        }
        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);
            }                       
        }
Ejemplo n.º 43
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;
            }
        }
Ejemplo n.º 44
0
        internal static bool VerifyHash(string fileFullPath,PackageJson package, PackageSourceListRequest request)
        {
            //skip in case the skip switch is specified
            if (request.SkipHashValidation.Value)
            {
                request.Verbose(Resources.Messages.SkipHashValidation);
                return true;                   
            }
            PackageHash packageHash = package.Hash;
            if (packageHash==null || string.IsNullOrWhiteSpace(packageHash.algorithm) || string.IsNullOrWhiteSpace(packageHash.hashCode))
            {
                request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.HashNotSpecified, package.Name);
                return false;
            }
            try
            {
                HashAlgorithm hashAlgorithm = null;
                switch (packageHash.algorithm.ToLowerInvariant())
                {
                    case "sha256":
                        hashAlgorithm = SHA256.Create();
                        break;

                    case "md5":
                        hashAlgorithm = MD5.Create();
                        break;

                    case "sha512":
                        hashAlgorithm = SHA512.Create();
                        break;                    
                    default:
                        request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.InvalidHashAlgorithm, packageHash.algorithm);
                        return false;
                }

                using (FileStream stream = File.OpenRead(fileFullPath))
                {
                    // compute the hash
                    byte[] computedHash = hashAlgorithm.ComputeHash(stream);
                    // convert the original hash we got from json
                    byte[] hashFromJSON = Convert.FromBase64String(package.Hash.hashCode);
                    if (!Enumerable.SequenceEqual(computedHash, hashFromJSON))
                    {
                        request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source);
                        return false;
                    }
                    else
                    {
                        request.Verbose(Resources.Messages.HashValidationSuccessful);
                    }
                }
            }
            catch
            {
                request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source);
                return false;
            }
            
           return true;
        }
Ejemplo n.º 45
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;
        }
Ejemplo n.º 46
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;
            }
        }
Ejemplo n.º 47
0
        internal static bool TestCatalogFile(string jsonFile, string catalogFile, PackageSourceListRequest request)
        {
            try
            {
                PSObject result = null;
                using (PowerShell powershell = PowerShell.Create())
                {
                    if (powershell != null)
                    {
                        result = powershell
                        .AddCommand("Test-FileCatalog")
                        .AddParameter("CatalogFilePath", catalogFile)
                        .AddParameter("Path", jsonFile)
                        .Invoke().FirstOrDefault();
                    }
                    if (result.ToString().EqualsIgnoreCase("Valid"))
                        return true;
                }
            }
            catch(Exception ex) 
            {
                request.WriteError(ErrorCategory.InvalidData, catalogFile, Resources.Messages.CatalogFileVerificationFailedWithError, catalogFile, ex.Message.ToString());
                return false;
            }

            request.WriteError(ErrorCategory.InvalidData, catalogFile, Resources.Messages.CatalogFileVerificationFailed, jsonFile);
            return false;

        }
Ejemplo n.º 48
0
        internal static void InstallDependencies(PackageJson packageJson, PackageSourceListRequest request)
        {
            //TODO dependency chain check

            //let's install dependency first in case it is needed for installing the actual package
            if (packageJson.DependencyObjects != null)
            {
                var dependencies = GetDependencies(packageJson, request);

                if (dependencies != null)
                {
                    foreach (var dep in dependencies.Where(dep => (dep != null) && !dep.IsCommonDefinition))
                    {
                        //dependency source trusty follows its parent
                        dep.IsTrustedSource |= packageJson.IsTrustedSource;
                        var id = PackageSourceListRequest.CreateCanonicalId(dep, dep.Name);
                        InstallProviderFromInstaller(dep, id, request);
                    }
                }
            }
        }
Ejemplo n.º 49
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);
            }
        }
Ejemplo n.º 50
0
 public void InitializeProvider(PackageSourceListRequest request)
 {
     request.Debug("Initialize PackageSourceListProvider");
 }
Ejemplo n.º 51
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;
            }

        }
Ejemplo n.º 52
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);
            }
        }
Ejemplo n.º 53
0
        private static void InstallPackageReference(PackageJson package, PackageSourceListRequest request, SoftwareIdentity[] packages)
        {
            var installRequest = PackageSourceListRequest.ExtendRequest(
                           new Dictionary<string, string[]>
                           {
                                {"Destination", new[] {package.Destination ?? ""}}
                           },
                           new[] { package.Source ?? "" },
                           package.IsTrustedSource,
                           request);

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

            var installing = provider.InstallPackage(packages[0], installRequest);
            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();
                }
            }
        }
Ejemplo n.º 54
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;
            }          
        }
Ejemplo n.º 55
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);
            }
        }
Ejemplo n.º 56
0
        private bool ValidatePackageManagementVersion(PackageSourceListRequest request)
        {
            var userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider");

            if (_currentPackageManagementVersion == null)
            {
                var moduleInfo = GetModule("PackageManagement");
                              
                if(moduleInfo == null || !moduleInfo.Any())
                {
                    request.Verbose(Resources.Messages.CannotFindPackageManagementVersion);
                    return false;
                }
              
                _currentPackageManagementVersion = moduleInfo.OrderByDescending(each => each.Version).FirstOrDefault().Version;
                if (_currentPackageManagementVersion < new Version(RequiredPackageManagementVersion))
                {
                    _doesPackageManagementVersionMatch = false;
                }
                else
                {
                    _doesPackageManagementVersionMatch = true;
                }
            }

            if (!_doesPackageManagementVersionMatch)
            {
                if (userSpecifiedProvider != null && userSpecifiedProvider.IndexOf(Constants.ProviderName, StringComparison.OrdinalIgnoreCase) > -1)
                {
                    request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.MinimumVersionCheck, Constants.ProviderName, RequiredPackageManagementVersion, _currentPackageManagementVersion);
                }
                else
                {
                    request.Verbose(Resources.Messages.MinimumVersionCheck, Constants.ProviderName, RequiredPackageManagementVersion, _currentPackageManagementVersion);
                }
            }

            return _doesPackageManagementVersionMatch;
        }
Ejemplo n.º 57
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;
        }
Ejemplo n.º 58
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);
        }
Ejemplo n.º 59
0
        internal static void DownloadExePackage(string fastPath, string location, PackageSourceListRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            // TODO do we need to support save-package for executable packages?
        }
Ejemplo n.º 60
0
        internal static bool InstallExePackage(PackageJson package, string fastPath, PackageSourceListRequest request) {

            ProgressTracker tracker = new ProgressTracker(request.StartProgress(0, Resources.Messages.Installing));
         
            var exePackage = Path.ChangeExtension(Path.GetTempFileName(), "exe");
            WebDownloader.DownloadFile(package.Source, exePackage, request, tracker);

            if (File.Exists(exePackage)) {
                request.Verbose("Package: '{0}'", exePackage);

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

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

                // Prepare the process to run
                var processArguments = string.IsNullOrWhiteSpace(package.InstallArguments)
                    ? "/VERYSILENT /CLOSEAPPLICATIONS /NORESTART /NOCANCEL /SP /qn"
                    : package.InstallArguments;

                var start = new ProcessStartInfo {
                    FileName = exePackage,
                    Arguments = processArguments,
                    UseShellExecute = false,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true,
                    //LoadUserProfile = true,
                };


                double percent = tracker.StartPercent;
                Timer timer = null;
                object timerLock = new object();
                bool cleanUp = false;

                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);
                                    // dispose it
                                    timer.Dispose();
                                }
                            } catch {
                            }

                            cleanUp = true;
                        }
                    }
                };

                // Run the external process & wait for it to finish
                using (var proc = Process.Start(start)) {
                   var timer1 = timer;
                    timer = new Timer(_ => {
                        percent += 0.025;

                        // percent between startProgress and endProgress
                        var progressPercent = tracker.ConvertPercentToProgress(percent);
                        if (progressPercent < 90) {
                            request.Progress(tracker.ProgressID, (int)progressPercent, Resources.Messages.InstallingPackage, package.Source);
                        }
                        if (request.IsCanceled) {
                            cleanUpAction();
                        }
                    }, null, 100, 3000);

                    proc.WaitForExit();

                    // Retrieve the app's exit code
                    var exitCode = proc.ExitCode;
                    if (exitCode != 0) {
                        request.WriteError(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.InstallFailed, package.Name, proc.StandardError.ReadToEnd());
                        request.CompleteProgress(tracker.ProgressID, false);
                        return false;
                    }
                    else {
                        request.CompleteProgress(tracker.ProgressID, true);
                        request.YieldFromSwidtag(package, fastPath);
                        request.Verbose(Resources.Messages.SuccessfullyInstalled, package.Name);
                    }
                    cleanUpAction();
                }
                return true;
            }
            else
            {
                request.Error(ErrorCategory.InvalidOperation, Resources.Messages.FailedToDownload, Constants.ProviderName, package.Source, exePackage);
            }

            return false;
        }