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)) { WriteError("The name of the package to be installed was not specified."); 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.FallbackSourceOptions.Values.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 settings = SettingsUtils.ReadSettings( rootDirectory, RestoreCommand.NuGetConfigFile, RestoreCommand.FileSystem, RestoreCommand.MachineWideSettings); var sourceProvier = PackageSourceBuilder.CreateSourceProvider(settings); var packageFeeds = new List <IPackageFeed>(); var effectiveSources = PackageSourceUtils.GetEffectivePackageSources( sourceProvier, 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)); }