Пример #1
0
        public override int Execute()
        {
            var global   = _options.ValueOrDefault <bool>("global");
            var toolPath = _options.SingleArgumentOrDefault("tool-path");

            if (string.IsNullOrWhiteSpace(toolPath) && !global)
            {
                throw new GracefulException(LocalizableStrings.UninstallToolCommandNeedGlobalOrToolPath);
            }

            if (!string.IsNullOrWhiteSpace(toolPath) && global)
            {
                throw new GracefulException(LocalizableStrings.UninstallToolCommandInvalidGlobalAndToolPath);
            }

            DirectoryPath?toolDirectoryPath = null;

            if (!string.IsNullOrWhiteSpace(toolPath))
            {
                toolDirectoryPath = new DirectoryPath(toolPath);
            }

            IToolPackageStore    toolPackageStore    = _createToolPackageStoreAndInstaller(toolDirectoryPath);
            IShellShimRepository shellShimRepository = _createShellShimRepository(toolDirectoryPath);

            var          packageId = new PackageId(_options.Arguments.Single());
            IToolPackage package   = null;

            try
            {
                package = toolPackageStore.EnumeratePackageVersions(packageId).SingleOrDefault();
                if (package == null)
                {
                    throw new GracefulException(
                              messages: new[]
                    {
                        string.Format(
                            LocalizableStrings.ToolNotInstalled,
                            packageId),
                    },
                              isUserError: false);
                }
            }
            catch (InvalidOperationException)
            {
                throw new GracefulException(
                          messages: new[]
                {
                    string.Format(
                        LocalizableStrings.ToolHasMultipleVersionsInstalled,
                        packageId),
                },
                          isUserError: false);
            }

            try
            {
                using (var scope = new TransactionScope(
                           TransactionScopeOption.Required,
                           TimeSpan.Zero))
                {
                    foreach (var command in package.Commands)
                    {
                        shellShimRepository.RemoveShim(command.Name);
                    }

                    package.Uninstall();

                    scope.Complete();
                }

                _reporter.WriteLine(
                    string.Format(
                        LocalizableStrings.UninstallSucceeded,
                        package.Id,
                        package.Version.ToNormalizedString()).Green());
                return(0);
            }
            catch (Exception ex) when(UninstallToolCommandLowLevelErrorConverter.ShouldConvertToUserFacingError(ex))
            {
                throw new GracefulException(
                          messages: UninstallToolCommandLowLevelErrorConverter.GetUserFacingMessages(ex, packageId),
                          verboseMessages: new[] { ex.ToString() },
                          isUserError: false);
            }
        }
Пример #2
0
        public override int Execute()
        {
            if (!_options.ValueOrDefault <bool>("global"))
            {
                throw new GracefulException(LocalizableStrings.UninstallToolCommandOnlySupportsGlobal);
            }

            var          packageId = new PackageId(_options.Arguments.Single());
            IToolPackage package   = null;

            try
            {
                package = _toolPackageStore.EnumeratePackageVersions(packageId).SingleOrDefault();
                if (package == null)
                {
                    _errorReporter.WriteLine(
                        string.Format(
                            LocalizableStrings.ToolNotInstalled,
                            packageId).Red());
                    return(1);
                }
            }
            catch (InvalidOperationException)
            {
                _errorReporter.WriteLine(
                    string.Format(
                        LocalizableStrings.ToolHasMultipleVersionsInstalled,
                        packageId).Red());
                return(1);
            }

            try
            {
                using (var scope = new TransactionScope(
                           TransactionScopeOption.Required,
                           TimeSpan.Zero))
                {
                    foreach (var command in package.Commands)
                    {
                        _shellShimRepository.RemoveShim(command.Name);
                    }

                    package.Uninstall();

                    scope.Complete();
                }

                _reporter.WriteLine(
                    string.Format(
                        LocalizableStrings.UninstallSucceeded,
                        package.Id,
                        package.Version.ToNormalizedString()).Green());
                return(0);
            }
            catch (ToolPackageException ex)
            {
                if (Reporter.IsVerbose)
                {
                    Reporter.Verbose.WriteLine(ex.ToString().Red());
                }

                _errorReporter.WriteLine(ex.Message.Red());
                return(1);
            }
            catch (Exception ex) when(ex is ToolConfigurationException || ex is ShellShimException)
            {
                if (Reporter.IsVerbose)
                {
                    Reporter.Verbose.WriteLine(ex.ToString().Red());
                }

                _errorReporter.WriteLine(
                    string.Format(
                        LocalizableStrings.FailedToUninstallTool,
                        packageId,
                        ex.Message).Red());
                return(1);
            }
        }
Пример #3
0
        public override int Execute()
        {
            if (!_global)
            {
                throw new GracefulException(LocalizableStrings.InstallToolCommandOnlySupportGlobal);
            }

            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));
            }

            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,
                        source: _source,
                        verbosity: _verbosity);

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

                    scope.Complete();
                }

                _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 (ToolPackageException ex)
            {
                if (Reporter.IsVerbose)
                {
                    Reporter.Verbose.WriteLine(ex.ToString().Red());
                }

                _errorReporter.WriteLine(ex.Message.Red());
                _errorReporter.WriteLine(string.Format(LocalizableStrings.ToolInstallationFailed, _packageId).Red());
                return(1);
            }
            catch (ToolConfigurationException ex)
            {
                if (Reporter.IsVerbose)
                {
                    Reporter.Verbose.WriteLine(ex.ToString().Red());
                }

                _errorReporter.WriteLine(
                    string.Format(
                        LocalizableStrings.InvalidToolConfiguration,
                        ex.Message).Red());
                _errorReporter.WriteLine(string.Format(LocalizableStrings.ToolInstallationFailedContactAuthor, _packageId).Red());
                return(1);
            }
            catch (ShellShimException ex)
            {
                if (Reporter.IsVerbose)
                {
                    Reporter.Verbose.WriteLine(ex.ToString().Red());
                }

                _errorReporter.WriteLine(
                    string.Format(
                        LocalizableStrings.FailedToCreateToolShim,
                        _packageId,
                        ex.Message).Red());
                _errorReporter.WriteLine(string.Format(LocalizableStrings.ToolInstallationFailed, _packageId).Red());
                return(1);
            }
        }
Пример #4
0
        public override int Execute()
        {
            var global   = _options.ValueOrDefault <bool>("global");
            var toolPath = _options.SingleArgumentOrDefault("tool-path");

            if (string.IsNullOrWhiteSpace(toolPath) && !global)
            {
                throw new GracefulException(LocalizableStrings.UninstallToolCommandNeedGlobalOrToolPath);
            }

            if (!string.IsNullOrWhiteSpace(toolPath) && global)
            {
                throw new GracefulException(LocalizableStrings.UninstallToolCommandInvalidGlobalAndToolPath);
            }

            DirectoryPath?toolDirectoryPath = null;

            if (!string.IsNullOrWhiteSpace(toolPath))
            {
                toolDirectoryPath = new DirectoryPath(toolPath);
            }

            IToolPackageStore    toolPackageStore    = _createToolPackageStoreAndInstaller(toolDirectoryPath);
            IShellShimRepository shellShimRepository = _createShellShimRepository(toolDirectoryPath);

            var          packageId = new PackageId(_options.Arguments.Single());
            IToolPackage package   = null;

            try
            {
                package = toolPackageStore.EnumeratePackageVersions(packageId).SingleOrDefault();
                if (package == null)
                {
                    _errorReporter.WriteLine(
                        string.Format(
                            LocalizableStrings.ToolNotInstalled,
                            packageId).Red());
                    return(1);
                }
            }
            catch (InvalidOperationException)
            {
                _errorReporter.WriteLine(
                    string.Format(
                        LocalizableStrings.ToolHasMultipleVersionsInstalled,
                        packageId).Red());
                return(1);
            }

            try
            {
                using (var scope = new TransactionScope(
                           TransactionScopeOption.Required,
                           TimeSpan.Zero))
                {
                    foreach (var command in package.Commands)
                    {
                        shellShimRepository.RemoveShim(command.Name);
                    }

                    package.Uninstall();

                    scope.Complete();
                }

                _reporter.WriteLine(
                    string.Format(
                        LocalizableStrings.UninstallSucceeded,
                        package.Id,
                        package.Version.ToNormalizedString()).Green());
                return(0);
            }
            catch (ToolPackageException ex)
            {
                if (Reporter.IsVerbose)
                {
                    Reporter.Verbose.WriteLine(ex.ToString().Red());
                }

                _errorReporter.WriteLine(ex.Message.Red());
                return(1);
            }
            catch (Exception ex) when(ex is ToolConfigurationException || ex is ShellShimException)
            {
                if (Reporter.IsVerbose)
                {
                    Reporter.Verbose.WriteLine(ex.ToString().Red());
                }

                _errorReporter.WriteLine(
                    string.Format(
                        LocalizableStrings.FailedToUninstallTool,
                        packageId,
                        ex.Message).Red());
                return(1);
            }
        }