public async Task <ILibraryInstallationResult> InstallAsync(ILibraryInstallationState desiredState, CancellationToken cancellationToken) { try { foreach (string file in desiredState.Files) { if (cancellationToken.IsCancellationRequested) { return(LibraryInstallationResult.FromCancelled(desiredState)); } string path = Path.Combine(desiredState.DestinationPath, file); var func = new Func <Stream>(() => GetStreamAsync(desiredState, file, cancellationToken).Result); bool writeOk = await HostInteraction.WriteFileAsync(path, func, desiredState, cancellationToken).ConfigureAwait(false); if (!writeOk) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file))); } } } catch (Exception ex) when(ex is InvalidLibraryException || ex.InnerException is InvalidLibraryException) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId))); } catch (Exception ex) { HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error); return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnknownException())); } return(LibraryInstallationResult.FromSuccess(desiredState)); }
public void Predefined() { TestError(PredefinedErrors.UnknownException(), "LIB000"); TestError(PredefinedErrors.ProviderUnknown("_prov_"), "LIB001", "_prov_"); TestError(PredefinedErrors.UnableToResolveSource("_libid_", "_prov_"), "LIB002", "_libid_", "_prov_"); TestError(PredefinedErrors.CouldNotWriteFile("file.js"), "LIB003", "file.js"); TestError(PredefinedErrors.ManifestMalformed(), "LIB004"); }
/// <summary> /// Installs a library as specified in the <paramref name="desiredState" /> parameter. /// </summary> /// <param name="desiredState">The details about the library to install.</param> /// <param name="cancellationToken">A token that allows for the operation to be cancelled.</param> /// <returns> /// The <see cref="T:Microsoft.Web.LibraryManager.Contracts.ILibraryInstallationResult" /> from the installation process. /// </returns> public async Task <ILibraryInstallationResult> InstallAsync(ILibraryInstallationState desiredState, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(LibraryInstallationResult.FromCancelled(desiredState)); } if (!desiredState.IsValid(out IEnumerable <IError> errors)) { return(new LibraryInstallationResult(desiredState, errors.ToArray())); } try { ILibraryInstallationResult result = await UpdateStateAsync(desiredState, cancellationToken); if (!result.Success) { return(result); } desiredState = result.InstallationState; foreach (string file in desiredState.Files) { if (cancellationToken.IsCancellationRequested) { return(LibraryInstallationResult.FromCancelled(desiredState)); } if (string.IsNullOrEmpty(file)) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file))); } string path = Path.Combine(desiredState.DestinationPath, file); var sourceStream = new Func <Stream>(() => GetStreamAsync(desiredState, file, cancellationToken).Result); bool writeOk = await HostInteraction.WriteFileAsync(path, sourceStream, desiredState, cancellationToken).ConfigureAwait(false); if (!writeOk) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file))); } } } catch (UnauthorizedAccessException) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.PathOutsideWorkingDirectory())); } catch (Exception ex) { HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error); return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnknownException())); } return(LibraryInstallationResult.FromSuccess(desiredState)); }
/// <summary> /// Copy files from the download cache to the desired installation state /// </summary> /// <remarks>Precondition: all files must already exist in the cache</remarks> protected async Task <ILibraryOperationResult> WriteToFilesAsync(ILibraryInstallationState state, CancellationToken cancellationToken) { if (state.Files != null) { try { foreach (string file in state.Files) { if (cancellationToken.IsCancellationRequested) { return(LibraryOperationResult.FromCancelled(state)); } if (string.IsNullOrEmpty(file)) { string id = LibraryNamingScheme.GetLibraryId(state.Name, state.Version); return(new LibraryOperationResult(state, PredefinedErrors.FileNameMustNotBeEmpty(id))); } string sourcePath = GetCachedFileLocalPath(state, file); string destinationPath = Path.Combine(state.DestinationPath, file); bool writeOk = await HostInteraction.CopyFileAsync(sourcePath, destinationPath, cancellationToken); if (!writeOk) { return(new LibraryOperationResult(state, PredefinedErrors.CouldNotWriteFile(file))); } } } catch (UnauthorizedAccessException) { return(new LibraryOperationResult(state, PredefinedErrors.PathOutsideWorkingDirectory())); } catch (Exception ex) { HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error); return(new LibraryOperationResult(state, PredefinedErrors.UnknownException())); } } return(LibraryOperationResult.FromSuccess(state)); }
private async Task <ILibraryOperationResult> WriteToFilesAsync(ILibraryInstallationState state, CancellationToken cancellationToken) { if (state.Files != null) { try { foreach (string file in state.Files) { if (cancellationToken.IsCancellationRequested) { return(LibraryOperationResult.FromCancelled(state)); } if (string.IsNullOrEmpty(file)) { return(new LibraryOperationResult(state, PredefinedErrors.CouldNotWriteFile(file))); } string destinationPath = Path.Combine(state.DestinationPath, file); var sourceStream = new Func <Stream>(() => GetStreamAsync(state, file, cancellationToken).Result); bool writeOk = await HostInteraction.WriteFileAsync(destinationPath, sourceStream, state, cancellationToken).ConfigureAwait(false); if (!writeOk) { return(new LibraryOperationResult(state, PredefinedErrors.CouldNotWriteFile(file))); } } } catch (UnauthorizedAccessException) { return(new LibraryOperationResult(state, PredefinedErrors.PathOutsideWorkingDirectory())); } catch (Exception ex) { HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error); return(new LibraryOperationResult(state, PredefinedErrors.UnknownException())); } } return(LibraryOperationResult.FromSuccess(state)); }
/// <summary> /// Installs a library as specified in the <paramref name="desiredState" /> parameter. /// </summary> /// <param name="desiredState">The details about the library to install.</param> /// <param name="cancellationToken">A token that allows for the operation to be cancelled.</param> /// <returns> /// The <see cref="T:Microsoft.Web.LibraryInstaller.Contracts.ILibraryInstallationResult" /> from the installation process. /// </returns> /// <exception cref="InvalidLibraryException"></exception> public async Task <ILibraryInstallationResult> InstallAsync(ILibraryInstallationState desiredState, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(LibraryInstallationResult.FromCancelled(desiredState)); } if (!desiredState.IsValid(out IEnumerable <IError> errors)) { return(new LibraryInstallationResult(desiredState, errors.ToArray())); } try { var catalog = (CdnjsCatalog)GetCatalog(); ILibrary library = await catalog.GetLibraryAsync(desiredState.LibraryId, cancellationToken).ConfigureAwait(false); if (library == null) { throw new InvalidLibraryException(desiredState.LibraryId, Id); } await HydrateCacheAsync(library, cancellationToken).ConfigureAwait(false); var files = desiredState.Files?.ToList(); // "Files" is optional on this provider, so when none are specified all should be used if (files == null || files.Count == 0) { desiredState = new LibraryInstallationState { ProviderId = Id, LibraryId = desiredState.LibraryId, DestinationPath = desiredState.DestinationPath, Files = library.Files.Keys.ToList(), }; } foreach (string file in desiredState.Files) { if (cancellationToken.IsCancellationRequested) { return(LibraryInstallationResult.FromCancelled(desiredState)); } string path = Path.Combine(desiredState.DestinationPath, file); var sourceStream = new Func <Stream>(() => GetStreamAsync(desiredState, file, cancellationToken).Result); bool writeOk = await HostInteraction.WriteFileAsync(path, sourceStream, desiredState, cancellationToken).ConfigureAwait(false); if (!writeOk) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file))); } } } catch (Exception ex) when(ex is InvalidLibraryException || ex.InnerException is InvalidLibraryException) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId))); } catch (UnauthorizedAccessException) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.PathOutsideWorkingDirectory())); } catch (Exception ex) { HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error); return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnknownException())); } return(LibraryInstallationResult.FromSuccess(desiredState)); }
public async Task <ILibraryInstallationResult> InstallAsync(ILibraryInstallationState desiredState, CancellationToken cancellationToken) { try { if (cancellationToken.IsCancellationRequested) { return(LibraryInstallationResult.FromCancelled(desiredState)); } var catalog = (CdnjsCatalog)GetCatalog(); ILibrary library = await catalog.GetLibraryAsync(desiredState.LibraryId, cancellationToken).ConfigureAwait(false); if (library == null) { throw new InvalidLibraryException(desiredState.LibraryId, Id); } await HydrateCacheAsync(library, cancellationToken).ConfigureAwait(false); var files = desiredState.Files?.ToList(); if (files == null || files.Count == 0) { desiredState = new LibraryInstallationState { ProviderId = Id, LibraryId = desiredState.LibraryId, DestinationPath = desiredState.DestinationPath, Files = library.Files.Keys.ToList() }; } foreach (string file in desiredState.Files) { if (cancellationToken.IsCancellationRequested) { return(LibraryInstallationResult.FromCancelled(desiredState)); } string path = Path.Combine(desiredState.DestinationPath, file); var func = new Func <Stream>(() => GetStream(desiredState, file)); bool writeOk = await HostInteraction.WriteFileAsync(path, func, desiredState, cancellationToken).ConfigureAwait(false); if (!writeOk) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file))); } } } catch (Exception ex) when(ex is InvalidLibraryException || ex.InnerException is InvalidLibraryException) { return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId))); } catch (Exception ex) { HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error); return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnknownException())); } return(LibraryInstallationResult.FromSuccess(desiredState)); }