/// <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); }
/// <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))); } }
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(LibraryOperationResult.FromError(PredefinedErrors.UnknownError())); } if (provider == null) { return(LibraryOperationResult.FromError(PredefinedErrors.ProviderUnknown(provider.Id))); } if (string.IsNullOrEmpty(state.LibraryId)) { return(LibraryOperationResult.FromError(PredefinedErrors.LibraryIdIsUndefined())); } ILibraryCatalog catalog = provider.GetCatalog(); try { await catalog.GetLibraryAsync(state.LibraryId, CancellationToken.None).ConfigureAwait(false); } catch { return(LibraryOperationResult.FromError(PredefinedErrors.UnableToResolveSource(state.LibraryId, provider.Id))); } if (string.IsNullOrEmpty(state.DestinationPath)) { return(LibraryOperationResult.FromError(PredefinedErrors.PathIsUndefined())); } if (state.DestinationPath.IndexOfAny(Path.GetInvalidPathChars()) >= 0) { return(LibraryOperationResult.FromError(PredefinedErrors.DestinationPathHasInvalidCharacters(state.DestinationPath))); } return(LibraryOperationResult.FromSuccess(state)); }