Example #1
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)));
        }
Example #2
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)));
            }
        }