private async Task <Tuple <string, string> > ResolvePackageIdAndVersion(string packageId, string packageVersion) { if (string.IsNullOrEmpty(packageId)) { return(null); } // For nupkgs, get the id and version from the package if (packageId.EndsWith(".nupkg", StringComparison.OrdinalIgnoreCase)) { if (!File.Exists(packageId)) { WriteError(string.Format("Could not find the file {0}.", packageId)); return(null); } var packagePath = Path.GetFullPath(packageId); var packageDirectory = Path.GetDirectoryName(packagePath); var zipPackage = new NuGet.ZipPackage(packagePath); FeedOptions.FallbackSources.Add(packageDirectory); return(new Tuple <string, string>( zipPackage.Id, zipPackage.Version.ToString())); } // If the version is missing, try to find the latest version if (string.IsNullOrEmpty(packageVersion)) { var rootDirectory = ProjectResolver.ResolveRootDirectory(_commandsRepository.Root.Root); var config = NuGetConfig.ForSolution(rootDirectory, RestoreCommand.FileSystem); var packageFeeds = new List <IPackageFeed>(); var effectiveSources = PackageSourceUtils.GetEffectivePackageSources( config.Sources, FeedOptions.Sources, FeedOptions.FallbackSources); foreach (var source in effectiveSources) { var feed = PackageSourceUtils.CreatePackageFeed( source, FeedOptions.NoCache, FeedOptions.IgnoreFailedSources, Reports); if (feed != null) { packageFeeds.Add(feed); } } var package = await PackageSourceUtils.FindLatestPackage(packageFeeds, packageId); if (package == null) { Reports.Error.WriteLine("Unable to locate the package {0}".Red(), packageId); return(null); } return(new Tuple <string, string>( packageId, package.Version.ToString())); } // Otherwise, just assume that what you got is correct return(new Tuple <string, string>(packageId, packageVersion)); }
public async Task <bool> ExecuteCommand() { if (string.IsNullOrEmpty(_addCommand.Name)) { Reports.Error.WriteLine("Name of dependency to install is required.".Red()); return(false); } SemanticVersion version = null; if (!string.IsNullOrEmpty(_addCommand.Version)) { version = SemanticVersion.Parse(_addCommand.Version); } // Create source provider from solution settings _addCommand.ProjectDir = _addCommand.ProjectDir ?? Directory.GetCurrentDirectory(); var rootDir = ProjectResolver.ResolveRootDirectory(_addCommand.ProjectDir); var fileSystem = new PhysicalFileSystem(Directory.GetCurrentDirectory()); var settings = SettingsUtils.ReadSettings(solutionDir: rootDir, nugetConfigFile: null, fileSystem: fileSystem, machineWideSettings: new CommandLineMachineWideSettings()); var sourceProvider = PackageSourceBuilder.CreateSourceProvider(settings); var effectiveSources = PackageSourceUtils.GetEffectivePackageSources(sourceProvider, _restoreCommand.FeedOptions.Sources, _restoreCommand.FeedOptions.FallbackSources); var packageFeeds = new PackageFeedCache(); var feeds = new List <IPackageFeed>(); foreach (var source in effectiveSources) { var feed = packageFeeds.GetPackageFeed( source, _restoreCommand.FeedOptions.NoCache, _restoreCommand.FeedOptions.IgnoreFailedSources, Reports); if (feed != null) { feeds.Add(feed); } } PackageInfo result = null; if (version == null) { result = await PackageSourceUtils.FindLatestPackage(feeds, _addCommand.Name); } else { result = await PackageSourceUtils.FindBestMatchPackage(feeds, _addCommand.Name, new SemanticVersionRange(version)); } if (result == null) { Reports.Error.WriteLine("Unable to locate {0} >= {1}", _addCommand.Name.Red().Bold(), _addCommand.Version); return(false); } if (string.IsNullOrEmpty(_addCommand.Version)) { _addCommand.Version = result.Version.ToString(); } return(_addCommand.ExecuteCommand() && (await _restoreCommand.Execute())); }
private async Task <bool> Execute(string restoreDirectory, PackageFeedCache packageFeeds, SummaryContext summary) { try { var sw = Stopwatch.StartNew(); IEnumerable <string> projectJsonFiles; if (!RestoreProjectsCollector.Find(restoreDirectory, out projectJsonFiles)) { var errorMessage = $"The given root {restoreDirectory.Red().Bold()} is invalid."; summary.ErrorMessages.GetOrAdd(restoreDirectory, _ => new List <string>()).Add(errorMessage); Reports.Error.WriteLine(errorMessage); return(false); } var rootDirectory = ProjectResolver.ResolveRootDirectory(restoreDirectory); ReadSettings(rootDirectory); var settings = Config.Settings as Settings; if (settings != null) { var configFiles = settings.GetConfigFiles(); foreach (var file in configFiles) { summary.InformationMessages.GetOrAdd("NuGet Config files used:", _ => new List <string>()).Add(file); } } string packagesDirectory = FeedOptions.TargetPackagesFolder; if (string.IsNullOrEmpty(packagesDirectory)) { packagesDirectory = NuGetDependencyResolver.ResolveRepositoryPath(rootDirectory); } var packagesFolderFileSystem = CreateFileSystem(packagesDirectory); var pathResolver = new DefaultPackagePathResolver(packagesDirectory); var effectiveSources = PackageSourceUtils.GetEffectivePackageSources( Config.Sources, FeedOptions.Sources, FeedOptions.FallbackSources); var remoteProviders = new List <IWalkProvider>(); AddRemoteProvidersFromSources(remoteProviders, effectiveSources, packageFeeds, summary); int restoreCount = 0; int successCount = 0; Func <string, Task> restorePackage = async projectJsonPath => { Interlocked.Increment(ref restoreCount); var success = await RestoreForProject(projectJsonPath, rootDirectory, packagesDirectory, remoteProviders, summary); if (success) { Interlocked.Increment(ref successCount); } }; if (!RestoringInParallel()) { // Restoring in parallel on Mono throws native exception foreach (var projectJsonFile in projectJsonFiles) { await restorePackage(projectJsonFile); } } else { await ForEachAsync( projectJsonFiles, MaxDegreesOfConcurrency, restorePackage); } if (restoreCount > 1) { Reports.Information.WriteLine(string.Format("Total time {0}ms", sw.ElapsedMilliseconds)); } if (summary.InstallCount > 0) { summary.InformationMessages.GetOrAdd("Installed:", _ => new List <string>()).Add($"{summary.InstallCount} package(s) to {packagesDirectory}"); } return(restoreCount == successCount); } catch (Exception ex) { Reports.Information.WriteLine("----------"); Reports.Information.WriteLine(ex.ToString()); Reports.Information.WriteLine("----------"); Reports.Information.WriteLine("Restore failed"); Reports.Information.WriteLine(ex.Message); return(false); } }
private async Task <bool> Execute(string restoreDirectory, SummaryContext summary) { try { var sw = Stopwatch.StartNew(); var projectJsonFiles = new List <string>(); if (string.Equals( Runtime.Project.ProjectFileName, Path.GetFileName(restoreDirectory), StringComparison.OrdinalIgnoreCase) && File.Exists(restoreDirectory)) { // If the path is a project.json file we don't do recursive search in subfolders projectJsonFiles.Add(restoreDirectory); } else if (Directory.Exists(restoreDirectory)) { var projectJsonFile = Path.Combine(restoreDirectory, Runtime.Project.ProjectFileName); if (File.Exists(projectJsonFile)) { // If the path contains a project.json file we don't do recursive search in subfolders projectJsonFiles.Add(projectJsonFile); } else { projectJsonFiles.AddRange(Directory.EnumerateFiles( restoreDirectory, Runtime.Project.ProjectFileName, SearchOption.AllDirectories)); } } else { var errorMessage = $"The given root {restoreDirectory.Red().Bold()} is invalid."; summary.ErrorMessages.GetOrAdd(restoreDirectory, _ => new List <string>()).Add(errorMessage); Reports.Error.WriteLine(errorMessage); return(false); } var rootDirectory = ProjectResolver.ResolveRootDirectory(restoreDirectory); ReadSettings(rootDirectory); var settings = Config.Settings as Settings; if (settings != null) { var configFiles = settings.GetConfigFiles(); foreach (var file in configFiles) { summary.InformationMessages.GetOrAdd("NuGet Config files used:", _ => new List <string>()).Add(file); } } string packagesDirectory = FeedOptions.TargetPackagesFolder; if (string.IsNullOrEmpty(packagesDirectory)) { packagesDirectory = NuGetDependencyResolver.ResolveRepositoryPath(rootDirectory); } var packagesFolderFileSystem = CreateFileSystem(packagesDirectory); var pathResolver = new DefaultPackagePathResolver(packagesDirectory); int restoreCount = 0; int successCount = 0; Func <string, Task> restorePackage = async projectJsonPath => { Interlocked.Increment(ref restoreCount); var success = await RestoreForProject(projectJsonPath, rootDirectory, packagesDirectory, summary); if (success) { Interlocked.Increment(ref successCount); } }; if (!RestoringInParallel()) { // Restoring in parallel on Mono throws native exception foreach (var projectJsonFile in projectJsonFiles) { await restorePackage(projectJsonFile); } } else { await ForEachAsync( projectJsonFiles, MaxDegreesOfConcurrency, restorePackage); } if (restoreCount > 1) { Reports.Information.WriteLine(string.Format("Total time {0}ms", sw.ElapsedMilliseconds)); } if (summary.InstallCount > 0) { summary.InformationMessages.GetOrAdd("Installed:", _ => new List <string>()).Add($"{summary.InstallCount} package(s) to {packagesDirectory}"); } return(restoreCount == successCount); } catch (Exception ex) { Reports.Information.WriteLine("----------"); Reports.Information.WriteLine(ex.ToString()); Reports.Information.WriteLine("----------"); Reports.Information.WriteLine("Restore failed"); Reports.Information.WriteLine(ex.Message); return(false); } }