Exemplo n.º 1
0
        protected async Task <Manifest> CreateManifestAsync(string defaultProvider,
                                                            string defaultDestination,
                                                            EnvironmentSettings settings,
                                                            CancellationToken cancellationToken)
        {
            if (File.Exists(Settings.ManifestFileName))
            {
                throw new InvalidOperationException(Resources.Text.InitFailedLibmanJsonFileExists);
            }

            Manifest manifest = await Manifest.FromFileAsync(Settings.ManifestFileName, ManifestDependencies, cancellationToken);

            manifest.AddVersion(Manifest.SupportedVersions.Last().ToString());
            manifest.DefaultDestination = string.IsNullOrEmpty(defaultDestination) ? null : defaultDestination;

            defaultProvider = string.IsNullOrEmpty(defaultProvider)
                ? HostEnvironment.InputReader.GetUserInputWithDefault(nameof(settings.DefaultProvider), settings.DefaultProvider)
                : defaultProvider;

            if (ManifestDependencies.GetProvider(defaultProvider) == null)
            {
                IError unknownProviderError = PredefinedErrors.ProviderUnknown(defaultProvider);
                string message = string.Format(Resources.Text.InitFailedUnknownProvider, unknownProviderError.Code, unknownProviderError.Message);
                throw new InvalidOperationException(message);
            }

            manifest.DefaultProvider = defaultProvider;

            await manifest.SaveAsync(Settings.ManifestFileName, cancellationToken);

            return(manifest);
        }
Exemplo n.º 2
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));
            }
        }
Exemplo n.º 3
0
 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");
 }
Exemplo n.º 4
0
        /// <summary>
        /// Restores all libraries in the <see cref="Libraries"/> collection.
        /// </summary>
        /// <param name="cancellationToken">A token that allows for cancellation of the operation.</param>
        public async Task <IEnumerable <ILibraryInstallationResult> > RestoreAsync(CancellationToken cancellationToken)
        {
            //TODO: This should have an "undo scope"
            var results = new List <ILibraryInstallationResult>();
            var tasks   = new List <Task <ILibraryInstallationResult> >();

            if (!IsValidManifestVersion(Version))
            {
                results.Add(LibraryInstallationResult.FromErrors(new IError[] { PredefinedErrors.VersionIsNotSupported(Version) }));

                return(results);
            }

            foreach (ILibraryInstallationState state in Libraries)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    results.Add(LibraryInstallationResult.FromCancelled(state));
                    _hostInteraction.Logger.Log(Resources.Text.RestoreCancelled, LogLevel.Task);
                    return(results);
                }

                if (!state.IsValid(out IEnumerable <IError> errors))
                {
                    results.Add(new LibraryInstallationResult(state, errors.ToArray()));
                    continue;
                }

                _hostInteraction.Logger.Log(string.Format(Resources.Text.RestoringLibrary, state.LibraryId), LogLevel.Operation);

                IProvider provider = _dependencies.GetProvider(state.ProviderId);

                if (provider != null)
                {
                    tasks.Add(provider.InstallAsync(state, cancellationToken));
                }
                else
                {
                    results.Add(new LibraryInstallationResult(state, PredefinedErrors.ProviderUnknown(state.ProviderId)));
                }
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            results.AddRange(tasks.Select(t => t.Result));

            return(results);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Validates <see cref="ILibraryInstallationState"/>
        /// </summary>
        /// <param name="state">The <see cref="ILibraryInstallationState"/> to validate.</param>
        /// <param name="dependencies">The <see cref="IDependencies"/> used to validate <see cref="ILibraryInstallationState"/></param>
        /// <returns><see cref="ILibraryOperationResult"/> with the result of the validation</returns>
        public static async Task <ILibraryOperationResult> IsValidAsync(this ILibraryInstallationState state, IDependencies dependencies)
        {
            if (state == null)
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.UnknownError() }));
            }

            if (string.IsNullOrEmpty(state.ProviderId))
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.ProviderIsUndefined() }));
            }

            IProvider provider = dependencies.GetProvider(state.ProviderId);

            if (provider == null)
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.ProviderUnknown(state.ProviderId) }));
            }

            return(await IsValidAsync(state, provider).ConfigureAwait(false));
        }
Exemplo n.º 6
0
        /// <summary>
        ///  Validates <see cref="ILibraryInstallationState"/>
        /// </summary>
        /// <param name="state">The <see cref="ILibraryInstallationState"/> to validate.</param>
        /// <param name="provider">The <see cref="IProvider"/> used to validate <see cref="ILibraryInstallationState"/></param>
        /// <returns><see cref="ILibraryOperationResult"/> with the result of the validation</returns>
        public static async Task <ILibraryOperationResult> IsValidAsync(this ILibraryInstallationState state, IProvider provider)
        {
            if (state == null)
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.UnknownError() }));
            }

            if (provider == null)
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.ProviderUnknown(provider.Id) }));
            }

            if (string.IsNullOrEmpty(state.Name))
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.LibraryIdIsUndefined() }));
            }

            ILibraryCatalog catalog = provider.GetCatalog();

            try
            {
                await catalog.GetLibraryAsync(state.Name, state.Version, CancellationToken.None).ConfigureAwait(false);
            }
            catch
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.UnableToResolveSource(state.Name, state.Version, provider.Id) }));
            }

            if (string.IsNullOrEmpty(state.DestinationPath))
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.PathIsUndefined() }));
            }

            if (state.DestinationPath.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
            {
                return(new LibraryOperationResult(state, new[] { PredefinedErrors.DestinationPathHasInvalidCharacters(state.DestinationPath) }));
            }

            return(LibraryOperationResult.FromSuccess(state));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Installs a library with the given libraryId
        /// </summary>
        /// <param name="libraryName"></param>
        /// <param name="version"></param>
        /// <param name="providerId"></param>
        /// <param name="files"></param>
        /// <param name="destination"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IEnumerable <ILibraryOperationResult> > InstallLibraryAsync(
            string libraryName,
            string version,
            string providerId,
            IReadOnlyList <string> files,
            string destination,
            CancellationToken cancellationToken)
        {
            ILibraryOperationResult result;

            var desiredState = new LibraryInstallationState()
            {
                Name            = libraryName,
                Version         = version,
                Files           = files,
                ProviderId      = providerId,
                DestinationPath = destination
            };

            UpdateLibraryProviderAndDestination(desiredState, DefaultProvider, DefaultDestination);

            ILibraryOperationResult validationResult = await desiredState.IsValidAsync(_dependencies);

            if (!validationResult.Success)
            {
                return(new [] { validationResult });
            }

            IProvider provider = _dependencies.GetProvider(desiredState.ProviderId);

            if (provider == null)
            {
                return(new [] { new LibraryOperationResult(desiredState, new IError[] { PredefinedErrors.ProviderUnknown(desiredState.ProviderId) }) });
            }

            IEnumerable <ILibraryOperationResult> conflictResults = await CheckLibraryForConflictsAsync(desiredState, cancellationToken).ConfigureAwait(false);

            if (!conflictResults.All(r => r.Success))
            {
                return(conflictResults);
            }

            result = await provider.InstallAsync(desiredState, cancellationToken).ConfigureAwait(false);

            if (result.Success)
            {
                AddLibrary(desiredState);
            }

            return(new [] { result });
        }