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 List <IPackageFeed>(); foreach (var source in effectiveSources) { var feed = PackageSourceUtils.CreatePackageFeed( source, _restoreCommand.FeedOptions.NoCache, _restoreCommand.FeedOptions.IgnoreFailedSources, Reports); if (feed != null) { packageFeeds.Add(feed); } } PackageInfo result = null; if (version == null) { result = await PackageSourceUtils.FindLatestPackage(packageFeeds, _addCommand.Name); } else { result = await PackageSourceUtils.FindBestMatchPackage(packageFeeds, _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 <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 PackageFeedCache(); var feeds = new List <IPackageFeed>(); var effectiveSources = PackageSourceUtils.GetEffectivePackageSources( config.Sources, FeedOptions.Sources, FeedOptions.FallbackSources); foreach (var source in effectiveSources) { var feed = packageFeeds.GetPackageFeed( source, FeedOptions.NoCache, FeedOptions.IgnoreFailedSources, Reports); if (feed != null) { feeds.Add(feed); } } var package = await PackageSourceUtils.FindLatestPackage(feeds, 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)); }