internal void InstallPackage(string fastPath, BootstrapRequest request, bool errorContinue) { if (fastPath == null) { throw new ArgumentNullException("fastPath"); } if (request == null) { throw new ArgumentNullException("request"); } // ensure that mandatory parameters are present. request.Debug("Calling 'Bootstrap::InstallPackage'"); var triedAndFailed = false; //source can be from install - packageprovider or can be from the pipeline if ((request.LocalSource.Any() || fastPath.IsFile())) { InstallPackageFromFile(fastPath, request); return; } // verify the package integrity (ie, check if it's digitally signed before installing) var provider = request.GetProvider(new Uri(fastPath)); if (provider == null || !provider.IsValid) { var result = errorContinue ? request.Warning(Constants.Messages.UnableToResolvePackage, fastPath) : request.Error(ErrorCategory.InvalidData, fastPath, Constants.Messages.UnableToResolvePackage, fastPath); return; } // first install the dependencies if any var dependencyLinks = provider._swidtag.Links.Where(link => link.Relationship == Iso19770_2.Relationship.Requires).GroupBy(link => link.Artifact); foreach (var depLinks in dependencyLinks) { foreach (var item in depLinks) { Package packages = null; if (string.IsNullOrWhiteSpace(item.Attributes[Iso19770_2.Discovery.Name])) { //select the packages that marked as "latest" packages = (new Feed(request, new[] { item.HRef })).Query().FirstOrDefault(); } else { if (string.IsNullOrWhiteSpace(item.Attributes[Iso19770_2.Discovery.Version])) { //select the packages that marked as "latest" and matches the name specified packages = (new Feed(request, new[] { item.HRef })).Query().FirstOrDefault(p => p.Name.EqualsIgnoreCase(item.Attributes[Iso19770_2.Discovery.Name])); } else { //select the packages that matches version and name packages = (new Feed(request, new[] { item.HRef })).Query(item.Attributes[Iso19770_2.Discovery.Name], item.Attributes[Iso19770_2.Discovery.Version]).FirstOrDefault(); } } if (packages == null) { // no package found request.Warning(Resources.Messages.NoDependencyPackageFound, item.HRef); continue; } // try to install dependent providers. If fails, continue InstallPackage(packages.Location.AbsoluteUri, request, errorContinue: true); } } // group the links along 'artifact' lines var artifacts = provider._swidtag.Links.Where(link => link.Relationship == Iso19770_2.Relationship.InstallationMedia).GroupBy(link => link.Artifact); // try one artifact set at a time. foreach (var artifact in artifacts) { // first time we succeed, we're good to go. foreach (var link in artifact) { switch (link.Attributes[Iso19770_2.Discovery.Type]) { case "assembly": if (InstallAssemblyProvider(provider, link, fastPath, request)) { return; } triedAndFailed = true; continue; default: if (InstallProviderFromInstaller(provider, link, fastPath, request)) { return; } triedAndFailed = true; continue; } } } if (triedAndFailed) { // we tried installing something and it didn't go well. var result = errorContinue ? request.Warning(Constants.Messages.FailedProviderBootstrap, fastPath) : request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.FailedProviderBootstrap, fastPath); } else { // we didn't even find a link to bootstrap. var result = errorContinue ? request.Warning(Resources.Messages.MissingInstallationmedia, fastPath) : request.Error(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.MissingInstallationmedia, fastPath); } }
private bool InstallAssemblyProvider(Package provider, Link link, string fastPath, BootstrapRequest request) { request.Verbose(Resources.Messages.InstallingPackage, fastPath); if (!Directory.Exists(request.DestinationPath(request))) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.DestinationPathNotSet); return false; } var targetFilename = link.Attributes[Iso19770_2.Discovery.TargetFilename]; if (string.IsNullOrWhiteSpace(targetFilename)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.InvalidFilename); return false; } targetFilename = Path.GetFileName(targetFilename); if (string.IsNullOrWhiteSpace(provider.Version)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.MissingVersion); return false; } //the provider is installing to like this folder: \WindowsPowerShell\Modules\PackageManagement\ProviderAssemblies\nuget\2.8.5.127 //... providername\version\.dll var versionFolder = Path.Combine(request.DestinationPath(request), provider.Name, provider.Version); if (!Directory.Exists(versionFolder)) { //we create it Directory.CreateDirectory(versionFolder); } var targetFile = Path.Combine(versionFolder, targetFilename); // download the file var file = request.DownloadAndValidateFile(provider.Name, provider._swidtag); if (file != null) { try { // looks good! let's keep it if (File.Exists(targetFile)) { request.Debug("Removing old file '{0}'", targetFile); targetFile.TryHardToDelete(); } // is that file still there? if (File.Exists(targetFile)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.UnableToRemoveFile, targetFile); return false; } request.Debug("Copying file '{0}' to '{1}'", file, targetFile); try { File.Copy(file, targetFile); } catch (Exception ex) { request.Debug(ex.StackTrace); return false; } //do not need to load the assembly here.The caller in the PackageManangemnt.RequirePackageProvider() is loading assembly //after the install if (File.Exists(targetFile)) { request.Verbose(Resources.Messages.InstalledPackage, provider.Name, targetFile); request.YieldFromSwidtag(provider, fastPath); return true; } } finally { file.TryHardToDelete(); } } return false; }
private bool InstallAssemblyProvider(Package provider, Link link, string fastPath, BootstrapRequest request, bool deleteFile = true) { request.Verbose(Resources.Messages.InstallingPackage, fastPath); if (!Directory.Exists(request.DestinationPath(request))) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.DestinationPathNotSet); return(false); } string targetFilename = fastPath; string file = fastPath; //source can be from install-packageprovider or can be from the pipeline if (!request.LocalSource.Any() && !fastPath.IsFile() && link != null) { targetFilename = link.Attributes[Iso19770_2.Discovery.TargetFilename]; // download the file file = request.DownloadAndValidateFile(provider._swidtag); } if (string.IsNullOrWhiteSpace(targetFilename)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.InvalidFilename); return(false); } targetFilename = Path.GetFileName(targetFilename); if (string.IsNullOrWhiteSpace(provider.Version)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.MissingVersion); return(false); } //the provider is installing to like this folder: \WindowsPowerShell\Modules\PackageManagement\ProviderAssemblies\nuget\2.8.5.127 //... providername\version\.dll var versionFolder = Path.Combine(request.DestinationPath(request), provider.Name, provider.Version); if (!Directory.Exists(versionFolder)) { //we create it Directory.CreateDirectory(versionFolder); } var targetFile = Path.Combine(versionFolder, targetFilename); if (file != null) { try { // looks good! let's keep it if (File.Exists(targetFile)) { request.Debug("Removing old file '{0}'", targetFile); targetFile.TryHardToDelete(); } // is that file still there? if (File.Exists(targetFile)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.UnableToRemoveFile, targetFile); return(false); } request.Debug("Copying file '{0}' to '{1}'", file, targetFile); try { File.Copy(file, targetFile); } catch (Exception ex) { request.Debug(ex.StackTrace); return(false); } if (File.Exists(targetFile)) { request.Verbose(Resources.Messages.InstalledPackage, provider.Name, targetFile); request.YieldFromSwidtag(provider, fastPath); //Load the provider. This is needed when a provider has dependencies. For example, if Nuget provider has a dependent foobar // provider and when 'Install-PackageProvider -name NuGet', we want both NuGet and Foobar provider gets loaded. PackageManagementService.LoadProviderAssembly(request, targetFile, false); return(true); } } finally { if (deleteFile) { file.TryHardToDelete(); } } } return(false); }
public void InstallPackage(string fastPath, BootstrapRequest request) { if (request == null) { throw new ArgumentNullException("request"); } // ensure that mandatory parameters are present. request.Debug("Calling 'Bootstrap::InstallPackage'"); var triedAndFailed = false; // verify the package integrity (ie, check if it's digitally signed before installing) var provider = request.GetProvider(new Uri(fastPath)); if (provider == null || !provider.IsValid) { request.Error(ErrorCategory.InvalidData, fastPath, Constants.Messages.UnableToResolvePackage, fastPath); return; } // group the links along 'artifact' lines var artifacts = provider._swidtag.Links.Where(link => link.Relationship == Iso19770_2.Relationship.InstallationMedia).GroupBy(link => link.Artifact); // try one artifact set at a time. foreach (var artifact in artifacts) { // first time we succeed, we're good to go. foreach (var link in artifact) { switch (link.Attributes[Iso19770_2.Discovery.Type]) { case "assembly": if (InstallAssemblyProvider(provider, link, fastPath, request)) { return; } triedAndFailed = true; continue; default: if (InstallProviderFromInstaller(provider, link, fastPath, request)) { return; } triedAndFailed = true; continue; } } } if (triedAndFailed) { // we tried installing something and it didn't go well. request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.FailedProviderBootstrap, fastPath); } else { // we didn't even find a link to bootstrap. request.Error(ErrorCategory.InvalidOperation, fastPath, "Provider {0} missing installationmedia to install.", fastPath); } }
internal void InstallPackage(string fastPath, BootstrapRequest request, bool errorContinue) { if (fastPath == null) { throw new ArgumentNullException("fastPath"); } if (request == null) { throw new ArgumentNullException("request"); } // ensure that mandatory parameters are present. request.Debug("Calling 'Bootstrap::InstallPackage'"); var triedAndFailed = false; //source can be from install - packageprovider or can be from the pipeline if ((request.LocalSource.Any() || fastPath.IsFile())) { InstallPackageFromFile(fastPath, request); return; } // verify the package integrity (ie, check if it's digitally signed before installing) var provider = request.GetProvider(new Uri(fastPath)); if (provider == null || !provider.IsValid) { var result = errorContinue ? request.Warning(Constants.Messages.UnableToResolvePackage, fastPath) : request.Error(ErrorCategory.InvalidData, fastPath, Constants.Messages.UnableToResolvePackage, fastPath); return; } // first install the dependencies if any var dependencyLinks = provider._swidtag.Links.Where(link => link.Relationship == Iso19770_2.Relationship.Requires).GroupBy(link => link.Artifact); foreach (var depLinks in dependencyLinks) { foreach (var item in depLinks) { Package packages = null; if (string.IsNullOrWhiteSpace(item.Attributes[Iso19770_2.Discovery.Name])) { //select the packages that marked as "latest" packages = (new Feed(request, new[] {item.HRef})).Query().FirstOrDefault(); } else { if (string.IsNullOrWhiteSpace(item.Attributes[Iso19770_2.Discovery.Version])) { //select the packages that marked as "latest" and matches the name specified packages = (new Feed(request, new[] { item.HRef })).Query().FirstOrDefault(p => p.Name.EqualsIgnoreCase(item.Attributes[Iso19770_2.Discovery.Name])); } else { //select the packages that matches version and name packages = (new Feed(request, new[] { item.HRef })).Query(item.Attributes[Iso19770_2.Discovery.Name], item.Attributes[Iso19770_2.Discovery.Version]).FirstOrDefault(); } } if (packages == null) { // no package found request.Warning(Resources.Messages.NoDependencyPackageFound, item.HRef); continue; } // try to install dependent providers. If fails, continue InstallPackage(packages.Location.AbsoluteUri, request, errorContinue: true); } } // group the links along 'artifact' lines var artifacts = provider._swidtag.Links.Where(link => link.Relationship == Iso19770_2.Relationship.InstallationMedia).GroupBy(link => link.Artifact); // try one artifact set at a time. foreach (var artifact in artifacts) { // first time we succeed, we're good to go. foreach (var link in artifact) { switch (link.Attributes[Iso19770_2.Discovery.Type]) { case "assembly": if (InstallAssemblyProvider(provider, link, fastPath, request)) { return; } triedAndFailed = true; continue; default: if (InstallProviderFromInstaller(provider, link, fastPath, request)) { return; } triedAndFailed = true; continue; } } } if (triedAndFailed) { // we tried installing something and it didn't go well. var result = errorContinue ? request.Warning(Constants.Messages.FailedProviderBootstrap, fastPath) : request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.FailedProviderBootstrap, fastPath); } else { // we didn't even find a link to bootstrap. var result = errorContinue ? request.Warning(Resources.Messages.MissingInstallationmedia, fastPath) : request.Error(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.MissingInstallationmedia, fastPath); } }
private bool InstallAssemblyProvider(Package provider, Link link, string fastPath, BootstrapRequest request, bool deleteFile=true) { request.Verbose(Resources.Messages.InstallingPackage, fastPath); if (!Directory.Exists(request.DestinationPath(request))) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.DestinationPathNotSet); return false; } string targetFilename = fastPath; string file = fastPath; //source can be from install-packageprovider or can be from the pipeline if (!request.LocalSource.Any() && !fastPath.IsFile() && link != null) { targetFilename = link.Attributes[Iso19770_2.Discovery.TargetFilename]; // download the file file = request.DownloadAndValidateFile(provider._swidtag); } if (string.IsNullOrWhiteSpace(targetFilename)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.InvalidFilename); return false; } targetFilename = Path.GetFileName(targetFilename); if (string.IsNullOrWhiteSpace(provider.Version)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.MissingVersion); return false; } //the provider is installing to like this folder: \WindowsPowerShell\Modules\PackageManagement\ProviderAssemblies\nuget\2.8.5.127 //... providername\version\.dll var versionFolder = Path.Combine(request.DestinationPath(request), provider.Name, provider.Version); // if version folder exists, remove it if (Directory.Exists(versionFolder)) { RemoveDirectory(versionFolder); } // create the directory if we successfully deleted it if (!Directory.Exists(versionFolder)) { Directory.CreateDirectory(versionFolder); } var targetFile = Path.Combine(versionFolder, targetFilename); if (file != null) { try { // is that file still there? if (File.Exists(targetFile)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.UnableToRemoveFile, targetFile); return false; } request.Debug("Copying file '{0}' to '{1}'", file, targetFile); try { if (File.Exists(file)) { // if this is a file File.Copy(file, targetFile); } else if (Directory.Exists(file)) { // if this is a directory, copy items over CopyDirectory(file, versionFolder); } } catch (Exception ex) { request.Debug(ex.StackTrace); return false; } if (File.Exists(targetFile)) { request.Verbose(Resources.Messages.InstalledPackage, provider.Name, targetFile); request.YieldFromSwidtag(provider, fastPath); //Load the provider. This is needed when a provider has dependencies. For example, if Nuget provider has a dependent foobar // provider and when 'Install-PackageProvider -name NuGet', we want both NuGet and Foobar provider gets loaded. PackageManagementService.LoadProviderAssembly(request, targetFile, false); return true; } } finally { if (deleteFile) { file.TryHardToDelete(); } } } return false; }
private bool InstallAssemblyProvider(Package provider, Link link, string fastPath, BootstrapRequest request) { request.Verbose(Resources.Messages.InstallingPackage, fastPath); if (!Directory.Exists(request.DestinationPath(request))) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.DestinationPathNotSet); return(false); } var targetFilename = link.Attributes[Iso19770_2.Discovery.TargetFilename]; if (string.IsNullOrWhiteSpace(targetFilename)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.InvalidFilename); return(false); } targetFilename = Path.GetFileName(targetFilename); if (string.IsNullOrWhiteSpace(provider.Version)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.MissingVersion); return(false); } //the provider is installing to like this folder: \WindowsPowerShell\Modules\PackageManagement\ProviderAssemblies\nuget\2.8.5.127 //... providername\version\.dll var versionFolder = Path.Combine(request.DestinationPath(request), provider.Name, provider.Version); if (!Directory.Exists(versionFolder)) { //we create it Directory.CreateDirectory(versionFolder); } var targetFile = Path.Combine(versionFolder, targetFilename); // download the file var file = request.DownloadAndValidateFile(provider.Name, provider._swidtag); if (file != null) { try { // looks good! let's keep it if (File.Exists(targetFile)) { request.Debug("Removing old file '{0}'", targetFile); targetFile.TryHardToDelete(); } // is that file still there? if (File.Exists(targetFile)) { request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.UnableToRemoveFile, targetFile); return(false); } request.Debug("Copying file '{0}' to '{1}'", file, targetFile); try { File.Copy(file, targetFile); } catch (Exception ex) { request.Debug(ex.StackTrace); return(false); } //do not need to load the assembly here.The caller in the PackageManangemnt.RequirePackageProvider() is loading assembly //after the install if (File.Exists(targetFile)) { request.Verbose(Resources.Messages.InstalledPackage, provider.Name, targetFile); request.YieldFromSwidtag(provider, fastPath); return(true); } } finally { file.TryHardToDelete(); } } return(false); }