/// <summary> /// Expands the files property for each library /// </summary> /// <param name="libraries"></param> /// <param name="dependencies"></param> /// <param name="defaultDestination"></param> /// <param name="defaultProvider"></param> /// <param name="cancellationToken"></param> /// <returns></returns> private static async Task <IEnumerable <ILibraryOperationResult> > ExpandLibrariesAsync( IEnumerable <ILibraryInstallationState> libraries, IDependencies dependencies, string defaultDestination, string defaultProvider, CancellationToken cancellationToken) { List <ILibraryOperationResult> expandedLibraries = new List <ILibraryOperationResult>(); foreach (ILibraryInstallationState library in libraries) { cancellationToken.ThrowIfCancellationRequested(); string installDestination = string.IsNullOrEmpty(library.DestinationPath) ? defaultDestination : library.DestinationPath; string providerId = string.IsNullOrEmpty(library.ProviderId) ? defaultProvider : library.ProviderId; IProvider provider = dependencies.GetProvider(providerId); if (provider == null) { return(new[] { LibraryOperationResult.FromError(PredefinedErrors.ProviderIsUndefined()) }); } ILibraryOperationResult desiredState = await provider.UpdateStateAsync(library, cancellationToken); if (!desiredState.Success) { return(new[] { desiredState }); } expandedLibraries.Add(desiredState); } return(expandedLibraries); }
private async Task <ILibraryOperationResult> RestoreLibraryAsync(ILibraryInstallationState libraryState, CancellationToken cancellationToken) { string libraryId = LibraryIdToNameAndVersionConverter.Instance.GetLibraryId(libraryState.Name, libraryState.Version, libraryState.ProviderId); _hostInteraction.Logger.Log(string.Format(Resources.Text.Restore_RestoreOfLibraryStarted, libraryId, libraryState.DestinationPath), LogLevel.Operation); if (cancellationToken.IsCancellationRequested) { return(LibraryOperationResult.FromCancelled(libraryState)); } IProvider provider = _dependencies.GetProvider(libraryState.ProviderId); if (provider == null) { return(new LibraryOperationResult(libraryState, new IError[] { PredefinedErrors.ProviderUnknown(libraryState.ProviderId) })); } try { return(await provider.InstallAsync(libraryState, cancellationToken).ConfigureAwait(false)); } catch (OperationCanceledException) { return(LibraryOperationResult.FromCancelled(libraryState)); } }
/// <summary> /// Uninstalls the specified library and removes it from the <see cref="Libraries"/> collection. /// </summary> /// <param name="libraryId">The library identifier.</param> /// <param name="deleteFilesFunction"></param> /// <param name="cancellationToken"></param> public async Task <ILibraryOperationResult> UninstallAsync(string libraryId, Func <IEnumerable <string>, Task <bool> > deleteFilesFunction, CancellationToken cancellationToken) { ILibraryInstallationState library = Libraries.FirstOrDefault(l => l.LibraryId == libraryId); if (cancellationToken.IsCancellationRequested) { return(LibraryOperationResult.FromCancelled(library)); } if (library != null) { try { ILibraryOperationResult result = await DeleteLibraryFilesAsync(library, deleteFilesFunction, cancellationToken).ConfigureAwait(false); if (result.Success) { _libraries.Remove(library); return(result); } } catch (OperationCanceledException) { return(LibraryOperationResult.FromCancelled(library)); } } return(LibraryOperationResult.FromError(PredefinedErrors.CouldNotDeleteLibrary(library.LibraryId))); }
private async Task <ILibraryOperationResult> DeleteLibraryFilesAsync(ILibraryInstallationState state, Func <IEnumerable <string>, Task <bool> > deleteFilesFunction, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); string libraryId = LibraryIdToNameAndVersionConverter.Instance.GetLibraryId(state.Name, state.Version, state.ProviderId); try { IProvider provider = _dependencies.GetProvider(state.ProviderId); ILibraryOperationResult updatedStateResult = await provider.UpdateStateAsync(state, CancellationToken.None).ConfigureAwait(false); if (updatedStateResult.Success) { List <string> filesToDelete = new List <string>(); state = updatedStateResult.InstallationState; foreach (string file in state.Files) { var url = new Uri(file, UriKind.RelativeOrAbsolute); if (!url.IsAbsoluteUri) { string relativePath = Path.Combine(state.DestinationPath, file).Replace('\\', '/'); filesToDelete.Add(relativePath); } } bool success = true; if (deleteFilesFunction != null) { success = await deleteFilesFunction.Invoke(filesToDelete).ConfigureAwait(false); } if (success) { return(LibraryOperationResult.FromSuccess(updatedStateResult.InstallationState)); } else { return(LibraryOperationResult.FromError(PredefinedErrors.CouldNotDeleteLibrary(libraryId))); } } return(updatedStateResult); } catch (OperationCanceledException) { return(LibraryOperationResult.FromCancelled(state)); } catch (Exception) { return(LibraryOperationResult.FromError(PredefinedErrors.CouldNotDeleteLibrary(libraryId))); } }
/// <summary> /// Generates a single ILibraryOperationResult with a collection of IErros based on the collection of FileConflict /// </summary> /// <param name="fileConflicts"></param> /// <returns></returns> private static ILibraryOperationResult GetConflictErrors(IEnumerable <FileConflict> fileConflicts) { if (fileConflicts.Any()) { var errors = new List <IError>(); foreach (FileConflict conflictingLibraryGroup in fileConflicts) { errors.Add(PredefinedErrors.ConflictingFilesInManifest(conflictingLibraryGroup.File, conflictingLibraryGroup.Libraries.Select(l => l.Name).ToList())); } return(new LibraryOperationResult(errors.ToArray())); } return(LibraryOperationResult.FromSuccess(null)); }
private static IEnumerable <ILibraryOperationResult> GetDuplicateLibrariesErrors(IEnumerable <ILibraryInstallationState> libraries) { var errors = new List <ILibraryOperationResult>(); HashSet <string> duplicateLibraries = GetDuplicateLibraries(libraries); if (duplicateLibraries.Count > 0) { foreach (string libraryName in duplicateLibraries) { errors.Add(LibraryOperationResult.FromError(PredefinedErrors.DuplicateLibrariesInManifest(libraryName))); } } return(errors); }
/// <summary> /// Returns a collection of <see cref="ILibraryOperationResult"/> that represents the status for validation of the Manifest and its libraries /// </summary> /// <param name="manifest">The <see cref="Manifest"/> to be validated</param> /// <param name="dependencies"><see cref="IDependencies"/>used to validate the libraries</param> /// <param name="cancellationToken"></param> /// <returns></returns> public static async Task <IEnumerable <ILibraryOperationResult> > GetManifestErrorsAsync( Manifest manifest, IDependencies dependencies, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (manifest == null) { return(new ILibraryOperationResult[] { LibraryOperationResult.FromError(PredefinedErrors.ManifestMalformed()) }); } if (!IsValidManifestVersion(manifest.Version)) { return(new ILibraryOperationResult[] { LibraryOperationResult.FromError(PredefinedErrors.VersionIsNotSupported(manifest.Version)) }); } return(await GetLibrariesErrorsAsync(manifest.Libraries, dependencies, manifest.DefaultDestination, manifest.DefaultProvider, cancellationToken)); }
/// <summary> /// Validates <see cref="ILibraryInstallationState"/> /// </summary> /// <param name="state">The <see cref="ILibraryInstallationState"/> to validate.</param> /// <param name="dependencies">The <see cref="IDependencies"/> used to validate <see cref="ILibraryInstallationState"/></param> /// <returns><see cref="ILibraryOperationResult"/> with the result of the validation</returns> public static async Task <ILibraryOperationResult> IsValidAsync(this ILibraryInstallationState state, IDependencies dependencies) { if (state == null) { return(LibraryOperationResult.FromError(PredefinedErrors.UnknownError())); } if (string.IsNullOrEmpty(state.ProviderId)) { return(LibraryOperationResult.FromError(PredefinedErrors.ProviderIsUndefined())); } IProvider provider = dependencies.GetProvider(state.ProviderId); if (provider == null) { return(LibraryOperationResult.FromError(PredefinedErrors.ProviderUnknown(state.ProviderId))); } return(await IsValidAsync(state, provider).ConfigureAwait(false)); }
/// <summary> /// Validates <see cref="ILibraryInstallationState"/> /// </summary> /// <param name="state">The <see cref="ILibraryInstallationState"/> to validate.</param> /// <param name="provider">The <see cref="IProvider"/> used to validate <see cref="ILibraryInstallationState"/></param> /// <returns><see cref="ILibraryOperationResult"/> with the result of the validation</returns> public static async Task <ILibraryOperationResult> IsValidAsync(this ILibraryInstallationState state, IProvider provider) { if (state == null) { return(new LibraryOperationResult(state, new[] { PredefinedErrors.UnknownError() })); } if (provider == null) { return(new LibraryOperationResult(state, new[] { PredefinedErrors.ProviderUnknown(provider.Id) })); } if (string.IsNullOrEmpty(state.Name)) { return(new LibraryOperationResult(state, new[] { PredefinedErrors.LibraryIdIsUndefined() })); } ILibraryCatalog catalog = provider.GetCatalog(); try { await catalog.GetLibraryAsync(state.Name, state.Version, CancellationToken.None).ConfigureAwait(false); } catch { return(new LibraryOperationResult(state, new[] { PredefinedErrors.UnableToResolveSource(state.Name, state.Version, provider.Id) })); } if (string.IsNullOrEmpty(state.DestinationPath)) { return(new LibraryOperationResult(state, new[] { PredefinedErrors.PathIsUndefined() })); } if (state.DestinationPath.IndexOfAny(Path.GetInvalidPathChars()) >= 0) { return(new LibraryOperationResult(state, new[] { PredefinedErrors.DestinationPathHasInvalidCharacters(state.DestinationPath) })); } return(LibraryOperationResult.FromSuccess(state)); }
/// <summary> /// Validates the values of each Library property and returns a collection of ILibraryOperationResult for each of them /// </summary> /// <param name="libraries"></param> /// <param name="dependencies"></param> /// <param name="cancellationToken"></param> /// <returns></returns> private static async Task <IEnumerable <ILibraryOperationResult> > ValidatePropertiesAsync(IEnumerable <ILibraryInstallationState> libraries, IDependencies dependencies, CancellationToken cancellationToken) { var validationStatus = new List <ILibraryOperationResult>(); foreach (ILibraryInstallationState library in libraries) { cancellationToken.ThrowIfCancellationRequested(); ILibraryOperationResult result = await library.IsValidAsync(dependencies).ConfigureAwait(false); if (!result.Success) { validationStatus.Add(result); } else { validationStatus.Add(LibraryOperationResult.FromSuccess(library)); } } return(validationStatus); }