public static void Register(CommandLineApplication cmdApp, ReportsFactory reportsFactory, IApplicationEnvironment applicationEnvironment) { cmdApp.Command("restore", c => { c.Description = "Restore packages"; var argRoot = c.Argument("[root]", "List of projects and project folders to restore. Each value can be: a path to a project.json or global.json file, or a folder to recursively search for project.json files.", multipleValues: true); var feedCommandLineOptions = FeedCommandLineOptions.Add(c); var optLock = c.Option("--lock", "Creates dependencies file with locked property set to true. Overwrites file if it exists.", CommandOptionType.NoValue); var optUnlock = c.Option("--unlock", "Creates dependencies file with locked property set to false. Overwrites file if it exists.", CommandOptionType.NoValue); c.HelpOption("-?|-h|--help"); c.OnExecute(async() => { c.ShowRootCommandFullNameAndVersion(); var feedOptions = feedCommandLineOptions.GetOptions(); var command = new RestoreCommand(applicationEnvironment); command.Reports = reportsFactory.CreateReports(feedOptions.Quiet); command.RestoreDirectories.AddRange(argRoot.Values); command.FeedOptions = feedOptions; command.Lock = optLock.HasValue(); command.Unlock = optUnlock.HasValue(); if (!string.IsNullOrEmpty(feedOptions.Proxy)) { Environment.SetEnvironmentVariable("http_proxy", feedOptions.Proxy); } var success = await command.Execute(); return(success ? 0 : 1); }); }); }
public static void Register(CommandLineApplication cmdApp, ReportsFactory reportsFactory, IApplicationEnvironment applicationEnvironment) { cmdApp.Command("restore", c => { c.Description = "Restore packages"; var argRoot = c.Argument("[root]", "List of projects and project folders to restore. Each value can be: a path to a project.json or global.json file, or a folder to recursively search for project.json files.", multipleValues: true); var feedCommandLineOptions = FeedCommandLineOptions.Add(c); var optLock = c.Option("--lock", "Creates dependencies file with locked property set to true. Overwrites file if it exists.", CommandOptionType.NoValue); var optUnlock = c.Option("--unlock", "Creates dependencies file with locked property set to false. Overwrites file if it exists.", CommandOptionType.NoValue); c.HelpOption("-?|-h|--help"); c.OnExecute(async () => { c.ShowRootCommandFullNameAndVersion(); var feedOptions = feedCommandLineOptions.GetOptions(); var command = new RestoreCommand(applicationEnvironment); command.Reports = reportsFactory.CreateReports(feedOptions.Quiet); command.RestoreDirectories.AddRange(argRoot.Values); command.FeedOptions = feedOptions; command.Lock = optLock.HasValue(); command.Unlock = optUnlock.HasValue(); if (!string.IsNullOrEmpty(feedOptions.Proxy)) { Environment.SetEnvironmentVariable("http_proxy", feedOptions.Proxy); } var success = await command.Execute(); return success ? 0 : 1; }); }); }
public async Task <bool> Execute(string packageId, string packageVersion) { // 0. Resolve the actual package id and version var packageIdAndVersion = await ResolvePackageIdAndVersion(packageId, packageVersion); if (packageIdAndVersion == null) { WriteError($"Failed to install {packageId}"); return(false); } packageId = packageIdAndVersion.Item1; packageVersion = packageIdAndVersion.Item2; WriteVerbose("Resolved package id: {0}", packageId); WriteVerbose("Resolved package version: {0}", packageVersion); // 1. Get the package without dependencies. We cannot resolve them now because // we don't know the target frameworks that the package supports if (string.IsNullOrEmpty(FeedOptions.TargetPackagesFolder)) { FeedOptions.TargetPackagesFolder = _commandsRepository.PackagesRoot.Root; } var temporaryProjectFileFullPath = CreateTemporaryProject(FeedOptions.TargetPackagesFolder, packageId, packageVersion); var packageFullPath = Path.Combine( _commandsRepository.PackagesRoot.Root, _commandsRepository.PathResolver.GetPackageDirectory(packageId, new SemanticVersion(packageVersion))); if (OverwriteCommands) { if (Directory.Exists(packageFullPath)) { WriteInfo($"Deleting {packageFullPath}"); FileOperationUtils.DeleteFolder(packageFullPath); Directory.Delete(packageFullPath); } } try { RestoreCommand.RestoreDirectories.Add(temporaryProjectFileFullPath); if (!await RestoreCommand.Execute()) { return(false); } } finally { var folderToDelete = Path.GetDirectoryName(temporaryProjectFileFullPath); FileOperationUtils.DeleteFolder(folderToDelete); Directory.Delete(folderToDelete); } if (!ValidateApplicationPackage(packageFullPath)) { return(false); } var packageAppFolder = Path.Combine( packageFullPath, InstallBuilder.CommandsFolderName); // 2. Now, that we have a valid app package, we can resolve its dependecies RestoreCommand.RestoreDirectories.Clear(); RestoreCommand.RestoreDirectories.Add(packageAppFolder); if (!await RestoreCommand.Execute()) { return(false); } // 3. Dependencies are in place, now let's install the commands if (!InstallCommands(packageId, packageFullPath)) { return(false); } return(true); }
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())); }