Example #1
0
        /// <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="Microsoft.Web.LibraryManager.Contracts.ILibraryOperationResult" /> from the installation process.
        /// </returns>
        public override async Task <ILibraryOperationResult> InstallAsync(ILibraryInstallationState desiredState, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(LibraryOperationResult.FromCancelled(desiredState));
            }

            try
            {
                ILibraryOperationResult result = await UpdateStateAsync(desiredState, cancellationToken);

                if (!result.Success)
                {
                    return(result);
                }

                desiredState = result.InstallationState;

                foreach (string file in desiredState.Files)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(LibraryOperationResult.FromCancelled(desiredState));
                    }

                    if (string.IsNullOrEmpty(file))
                    {
                        return(new LibraryOperationResult(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 LibraryOperationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file)));
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                return(new LibraryOperationResult(desiredState, PredefinedErrors.PathOutsideWorkingDirectory()));
            }
            catch (ResourceDownloadException ex)
            {
                return(new LibraryOperationResult(desiredState, PredefinedErrors.FailedToDownloadResource(ex.Url)));
            }
            catch (Exception ex)
            {
                HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
                return(new LibraryOperationResult(desiredState, PredefinedErrors.UnknownException()));
            }

            return(LibraryOperationResult.FromSuccess(desiredState));
        }
        /// <summary>
        /// Copies ILibraryInstallationState files to cache
        /// </summary>
        /// <param name="state"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task <ILibraryOperationResult> RefreshCacheAsync(ILibraryInstallationState state, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(LibraryOperationResult.FromCancelled(state));
            }

            var tasks = new List <Task>();

            try
            {
                if (!string.IsNullOrEmpty(state.Name) && !string.IsNullOrEmpty(state.Version))
                {
                    string libraryDir = Path.Combine(CacheFolder, state.Name);
                    List <CacheServiceMetadata> librariesMetadata = new List <CacheServiceMetadata>();

                    foreach (string sourceFile in state.Files)
                    {
                        string cacheFile = Path.Combine(libraryDir, state.Version, sourceFile);
                        string url       = string.Format(_downloadUrlFormat, state.Name, state.Version, sourceFile);

                        CacheServiceMetadata newEntry = new CacheServiceMetadata(url, cacheFile);
                        if (!librariesMetadata.Contains(newEntry))
                        {
                            librariesMetadata.Add(new CacheServiceMetadata(url, cacheFile));
                        }
                    }

                    await _cacheService.RefreshCacheAsync(librariesMetadata, cancellationToken);
                }
            }
            catch (ResourceDownloadException ex)
            {
                HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
                return(new LibraryOperationResult(state, PredefinedErrors.FailedToDownloadResource(ex.Url)));
            }
            catch (OperationCanceledException)
            {
                return(LibraryOperationResult.FromCancelled(state));
            }
            catch (Exception ex)
            {
                HostInteraction.Logger.Log(ex.InnerException.ToString(), LogLevel.Error);
                return(new LibraryOperationResult(state, PredefinedErrors.UnknownException()));
            }

            return(LibraryOperationResult.FromSuccess(state));
        }
        /// <summary>
        /// Copies ILibraryInstallationState files to cache
        /// </summary>
        /// <param name="state"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task <ILibraryOperationResult> RefreshCacheAsync(ILibraryInstallationState state, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(LibraryOperationResult.FromCancelled(state));
            }

            string libraryDir = Path.Combine(CacheFolder, state.Name, state.Version);

            try
            {
                var librariesMetadata = new HashSet <CacheFileMetadata>();
                foreach (string sourceFile in state.Files)
                {
                    string cacheFile = Path.Combine(libraryDir, sourceFile);
                    string url       = GetDownloadUrl(state, sourceFile);

                    var newEntry = new CacheFileMetadata(url, cacheFile);
                    librariesMetadata.Add(newEntry);
                }
                await _cacheService.RefreshCacheAsync(librariesMetadata, HostInteraction.Logger, cancellationToken);
            }
            catch (ResourceDownloadException ex)
            {
                HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
                return(new LibraryOperationResult(state, PredefinedErrors.FailedToDownloadResource(ex.Url)));
            }
            catch (OperationCanceledException)
            {
                return(LibraryOperationResult.FromCancelled(state));
            }
            catch (Exception ex)
            {
                HostInteraction.Logger.Log(ex.InnerException.ToString(), LogLevel.Error);
                return(new LibraryOperationResult(state, PredefinedErrors.UnknownException()));
            }

            return(LibraryOperationResult.FromSuccess(state));
        }