Ejemplo n.º 1
0
        private void RunWithHandlingInstallError(Action installAction)
        {
            try
            {
                installAction();
            }
            catch (Exception ex)
                when(InstallToolCommandLowLevelErrorConverter.ShouldConvertToUserFacingError(ex))
                {
                    var message = new List <string>
                    {
                        string.Format(LocalizableStrings.UpdateToolFailed, _packageId)
                    };

                    message.AddRange(
                        InstallToolCommandLowLevelErrorConverter.GetUserFacingMessages(ex, _packageId));


                    throw new GracefulException(
                              messages: message,
                              verboseMessages: new[] { ex.ToString() },
                              isUserError: false);
                }
        }
Ejemplo n.º 2
0
        public override int Execute()
        {
            if (string.IsNullOrWhiteSpace(_toolPath) && !_global)
            {
                throw new GracefulException(LocalizableStrings.InstallToolCommandNeedGlobalOrToolPath);
            }

            if (!string.IsNullOrWhiteSpace(_toolPath) && _global)
            {
                throw new GracefulException(LocalizableStrings.InstallToolCommandInvalidGlobalAndToolPath);
            }

            if (_configFilePath != null && !File.Exists(_configFilePath))
            {
                throw new GracefulException(
                          string.Format(
                              LocalizableStrings.NuGetConfigurationFileDoesNotExist,
                              Path.GetFullPath(_configFilePath)));
            }


            VersionRange versionRange = null;

            if (!string.IsNullOrEmpty(_packageVersion) && !VersionRange.TryParse(_packageVersion, out versionRange))
            {
                throw new GracefulException(
                          string.Format(
                              LocalizableStrings.InvalidNuGetVersionRange,
                              _packageVersion));
            }

            DirectoryPath?toolPath = null;

            if (_toolPath != null)
            {
                toolPath = new DirectoryPath(_toolPath);
            }

            (IToolPackageStore toolPackageStore, IToolPackageInstaller toolPackageInstaller) =
                _createToolPackageStoreAndInstaller(toolPath);
            IShellShimRepository shellShimRepository = _createShellShimRepository(toolPath);

            // Prevent installation if any version of the package is installed
            if (toolPackageStore.EnumeratePackageVersions(_packageId).FirstOrDefault() != null)
            {
                _errorReporter.WriteLine(string.Format(LocalizableStrings.ToolAlreadyInstalled, _packageId).Red());
                return(1);
            }

            FilePath?configFile = null;

            if (_configFilePath != null)
            {
                configFile = new FilePath(_configFilePath);
            }

            try
            {
                IToolPackage package = null;
                using (var scope = new TransactionScope(
                           TransactionScopeOption.Required,
                           TimeSpan.Zero))
                {
                    package = toolPackageInstaller.InstallPackage(
                        packageId: _packageId,
                        versionRange: versionRange,
                        targetFramework: _framework,
                        nugetConfig: configFile,
                        additionalFeeds: _source,
                        verbosity: _verbosity);

                    foreach (var command in package.Commands)
                    {
                        shellShimRepository.CreateShim(command.Executable, command.Name, package.PackagedShims);
                    }

                    scope.Complete();
                }

                foreach (string w in package.Warnings)
                {
                    _reporter.WriteLine(w.Yellow());
                }

                if (_global)
                {
                    _environmentPathInstruction.PrintAddPathInstructionIfPathDoesNotExist();
                }

                _reporter.WriteLine(
                    string.Format(
                        LocalizableStrings.InstallationSucceeded,
                        string.Join(", ", package.Commands.Select(c => c.Name)),
                        package.Id,
                        package.Version.ToNormalizedString()).Green());
                return(0);
            }
            catch (Exception ex) when(InstallToolCommandLowLevelErrorConverter.ShouldConvertToUserFacingError(ex))
            {
                throw new GracefulException(
                          messages: InstallToolCommandLowLevelErrorConverter.GetUserFacingMessages(ex, _packageId),
                          verboseMessages: new[] { ex.ToString() },
                          isUserError: false);
            }
        }
Ejemplo n.º 3
0
        public override int Execute()
        {
            if (!string.IsNullOrEmpty(_configFilePath) && !File.Exists(_configFilePath))
            {
                throw new GracefulException(
                          string.Format(
                              LocalizableStrings.NuGetConfigurationFileDoesNotExist,
                              Path.GetFullPath(_configFilePath)));
            }

            VersionRange versionRange = _parseResult.GetVersionRange();

            DirectoryPath?toolPath = null;

            if (!string.IsNullOrEmpty(_toolPath))
            {
                toolPath = new DirectoryPath(_toolPath);
            }

            (IToolPackageStore toolPackageStore, IToolPackageStoreQuery toolPackageStoreQuery, IToolPackageInstaller toolPackageInstaller) =
                _createToolPackageStoresAndInstaller(toolPath, _forwardRestoreArguments);

            // Prevent installation if any version of the package is installed
            if (toolPackageStoreQuery.EnumeratePackageVersions(_packageId).FirstOrDefault() != null)
            {
                _errorReporter.WriteLine(string.Format(LocalizableStrings.ToolAlreadyInstalled, _packageId).Red());
                return(1);
            }

            FilePath?configFile = null;

            if (!string.IsNullOrEmpty(_configFilePath))
            {
                configFile = new FilePath(_configFilePath);
            }

            try
            {
                IToolPackage package = null;
                using (var scope = new TransactionScope(
                           TransactionScopeOption.Required,
                           TimeSpan.Zero))
                {
                    package = toolPackageInstaller.InstallPackage(
                        new PackageLocation(nugetConfig: configFile, additionalFeeds: _source),
                        packageId: _packageId,
                        versionRange: versionRange,
                        targetFramework: _framework, verbosity: _verbosity);

                    NuGetFramework framework;
                    if (string.IsNullOrEmpty(_framework) && package.Frameworks.Count() > 0)
                    {
                        framework = package.Frameworks
                                    .Where(f => f.Version < (new NuGetVersion(Product.Version)).Version)
                                    .MaxBy(f => f.Version);
                    }
                    else
                    {
                        framework = string.IsNullOrEmpty(_framework)  ?
                                    null :
                                    NuGetFramework.Parse(_framework);
                    }

                    string appHostSourceDirectory            = _shellShimTemplateFinder.ResolveAppHostSourceDirectoryAsync(_architectureOption, framework, RuntimeInformation.ProcessArchitecture).Result;
                    IShellShimRepository shellShimRepository = _createShellShimRepository(appHostSourceDirectory, toolPath);

                    foreach (var command in package.Commands)
                    {
                        shellShimRepository.CreateShim(command.Executable, command.Name, package.PackagedShims);
                    }

                    scope.Complete();
                }

                foreach (string w in package.Warnings)
                {
                    _reporter.WriteLine(w.Yellow());
                }

                if (_global)
                {
                    _environmentPathInstruction.PrintAddPathInstructionIfPathDoesNotExist();
                }

                _reporter.WriteLine(
                    string.Format(
                        LocalizableStrings.InstallationSucceeded,
                        string.Join(", ", package.Commands.Select(c => c.Name)),
                        package.Id,
                        package.Version.ToNormalizedString()).Green());
                return(0);
            }
            catch (Exception ex) when(InstallToolCommandLowLevelErrorConverter.ShouldConvertToUserFacingError(ex))
            {
                throw new GracefulException(
                          messages: InstallToolCommandLowLevelErrorConverter.GetUserFacingMessages(ex, _packageId),
                          verboseMessages: new[] { ex.ToString() },
                          isUserError: false);
            }
        }