protected async override Task <int> ExecuteInternalAsync() { Manifest manifest = await GetManifestAsync(); IEnumerable <ILibraryInstallationState> installedLibraries = ValidateParametersAndGetLibrariesToUninstall(manifest); if (installedLibraries == null || !installedLibraries.Any()) { Logger.Log(string.Format(Resources.Text.NoLibraryToUninstall, LibraryId.Value), LogLevel.Operation); return(0); } ILibraryInstallationState libraryToUninstall = null; if (installedLibraries.Count() > 1) { Logger.Log(string.Format(Resources.Text.MoreThanOneLibraryFoundToUninstall, LibraryId.Value), LogLevel.Operation); libraryToUninstall = LibraryResolver.ResolveLibraryByUserChoice(installedLibraries, HostEnvironment); } else { libraryToUninstall = installedLibraries.First(); } Task <bool> deleteFileAction(IEnumerable <string> s) => HostInteractions.DeleteFilesAsync(s, CancellationToken.None); string libraryId = LibraryIdToNameAndVersionConverter.Instance.GetLibraryId(libraryToUninstall.Name, libraryToUninstall.Version, libraryToUninstall.ProviderId); ILibraryOperationResult result = await manifest.UninstallAsync(libraryToUninstall.Name, libraryToUninstall.Version, deleteFileAction, CancellationToken.None); if (result.Success) { await manifest.SaveAsync(Settings.ManifestFileName, CancellationToken.None); Logger.Log(string.Format(Resources.Text.UninstalledLibrary, libraryId), LogLevel.Operation); } else { Logger.Log(string.Format(Resources.Text.UninstallFailed, libraryId), LogLevel.Error); foreach (IError error in result.Errors) { Logger.Log($"[{error.Code}]: {error.Message}", LogLevel.Error); } } return(0); }
public async Task TestResolveByUserChoiceAsync() { string libmanjsonPath = Path.Combine(WorkingDir, "libman.json"); File.WriteAllText(libmanjsonPath, _manifestContents); Manifest manifest = await Manifest.FromFileAsync( libmanjsonPath, _dependencies, CancellationToken.None); var inputReader = HostEnvironment.InputReader as TestInputReader; string outputStr = "Select an option:\r\n-----------------\r\n1. {[email protected], cdnjs, wwwroot}\r\n2. {[email protected], cdnjs, lib}\r\n3. {jquery, filesystem, wwwroot}"; inputReader.Inputs[outputStr] = "1"; ILibraryInstallationState result = LibraryResolver.ResolveLibraryByUserChoice(manifest.Libraries, HostEnvironment); Assert.AreEqual("[email protected]", result.LibraryId); }
protected override async Task <int> ExecuteInternalAsync() { Manifest manifest = await GetManifestAsync(); IEnumerable <ILibraryInstallationState> installedLibraries = ValidateParametersAndGetLibrariesToUpdate(manifest); if (installedLibraries == null || !installedLibraries.Any()) { Logger.Log(string.Format(Resources.NoLibraryFoundToUpdate, LibraryName.Value), LogLevel.Operation); return(0); } ILibraryInstallationState libraryToUpdate = null; if (installedLibraries.Count() > 1) { Logger.Log(string.Format(Resources.MoreThanOneLibraryFoundToUpdate, LibraryName.Value), LogLevel.Operation); libraryToUpdate = LibraryResolver.ResolveLibraryByUserChoice(installedLibraries, HostEnvironment); } else { libraryToUpdate = installedLibraries.First(); } string newLibraryId = null; if (ToVersion.HasValue()) { newLibraryId = LibraryIdToNameAndVersionConverter.Instance.GetLibraryId(libraryToUpdate.Name, ToVersion.Value(), libraryToUpdate.ProviderId); } else { string latestVersion = await GetLatestVersionAsync(libraryToUpdate, CancellationToken.None); if (!string.IsNullOrEmpty(latestVersion)) { newLibraryId = LibraryIdToNameAndVersionConverter.Instance.GetLibraryId(libraryToUpdate.Name, latestVersion, libraryToUpdate.ProviderId); } } if (newLibraryId == null || newLibraryId == libraryToUpdate.LibraryId) { Logger.Log(string.Format(Resources.LatestVersionAlreadyInstalled, libraryToUpdate.LibraryId), LogLevel.Operation); return(0); } Manifest backup = manifest.Clone(); string oldLibraryName = libraryToUpdate.Name; manifest.ReplaceLibraryId(libraryToUpdate, newLibraryId); // Delete files from old version of the library. await backup.RemoveUnwantedFilesAsync(manifest, CancellationToken.None); IEnumerable <ILibraryOperationResult> results = await manifest.RestoreAsync(CancellationToken.None); ILibraryOperationResult result = null; foreach (ILibraryOperationResult r in results) { if (!r.Success && r.Errors.Any(e => e.Message.Contains(libraryToUpdate.LibraryId))) { result = r; break; } else if (r.Success && r.InstallationState.LibraryId == libraryToUpdate.LibraryId && r.InstallationState.ProviderId == libraryToUpdate.ProviderId && r.InstallationState.DestinationPath == libraryToUpdate.DestinationPath) { result = r; break; } } if (result.Success) { await manifest.SaveAsync(HostEnvironment.EnvironmentSettings.ManifestFileName, CancellationToken.None); Logger.Log(string.Format(Resources.LibraryUpdated, oldLibraryName, newLibraryId), LogLevel.Operation); } else if (result.Errors != null) { if (ToVersion.HasValue()) { Logger.Log(string.Format(Resources.UpdateLibraryFailed, oldLibraryName, ToVersion.Value()), LogLevel.Error); } else { Logger.Log(string.Format(Resources.UpdateLibraryToLatestFailed, oldLibraryName), LogLevel.Error); } foreach (IError error in result.Errors) { Logger.Log(string.Format("[{0}]: {1}", error.Code, error.Message), LogLevel.Error); } } return(0); }