Esempio n. 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="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>
        /// 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));
        }