示例#1
0
        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));
            }
        }
示例#2
0
        /// <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)));
        }
示例#3
0
        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)));
            }
        }