Esempio n. 1
0
        private (FilePath filePath, string warningMessage) FindManifestFile()
        {
            if (!string.IsNullOrWhiteSpace(_explicitManifestFile))
            {
                return(new FilePath(_explicitManifestFile), null);
            }

            var manifestFilesContainPackageId
                = _toolManifestFinder.FindByPackageId(_packageId);

            if (manifestFilesContainPackageId.Any())
            {
                string warning = null;
                if (manifestFilesContainPackageId.Count > 1)
                {
                    warning =
                        string.Format(
                            LocalizableStrings.SamePackageIdInOtherManifestFile,
                            string.Join(
                                Environment.NewLine,
                                manifestFilesContainPackageId.Skip(1).Select(m => $"\t{m}")));
                }

                return(manifestFilesContainPackageId.First(), warning);
            }

            return(_toolManifestFinder.FindFirst(), null);
        }
Esempio n. 2
0
        public override int Execute()
        {
            FilePath manifestFile;

            try
            {
                manifestFile = string.IsNullOrWhiteSpace(_explicitManifestFile)
                   ? _toolManifestFinder.FindFirst()
                   : new FilePath(_explicitManifestFile);
            }
            catch (ToolManifestCannotBeFoundException e)
            {
                throw new GracefulException(new[]
                {
                    e.Message,
                    LocalizableStrings.NoManifestGuide
                },
                                            verboseMessages: new[] { e.VerboseMessage },
                                            isUserError: false);
            }

            _toolManifestEditor.Remove(manifestFile, _packageId);

            _reporter.WriteLine(
                string.Format(
                    LocalizableStrings.UninstallLocalToolSucceeded,
                    _packageId,
                    manifestFile.Value).Green());
            return(0);
        }
        public override int Execute()
        {
            var manifestFile = string.IsNullOrWhiteSpace(_explicitManifestFile)
                ? _toolManifestFinder.FindFirst()
                : new FilePath(_explicitManifestFile);

            _toolManifestEditor.Remove(manifestFile, _packageId);
            _reporter.WriteLine(
                string.Format(
                    LocalizableStrings.UninstallLocalToolSucceeded,
                    _packageId,
                    manifestFile.Value).Green());
            return(0);
        }
Esempio n. 4
0
 private FilePath GetManifestFilePath()
 {
     try
     {
         return(string.IsNullOrWhiteSpace(_explicitManifestFile)
             ? _toolManifestFinder.FindFirst()
             : new FilePath(_explicitManifestFile));
     }
     catch (ToolManifestCannotBeFoundException e)
     {
         throw new GracefulException(new[]
         {
             e.Message,
             LocalizableStrings.NoManifestGuide
         },
                                     isUserError: false);
     }
 }
Esempio n. 5
0
        public override int Execute()
        {
            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));
            }

            FilePath?configFile = null;

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

            string targetFramework = BundledTargetFramework.GetTargetFrameworkMoniker();

            try
            {
                var manifestFile = string.IsNullOrWhiteSpace(_explicitManifestFile)
                    ? _toolManifestFinder.FindFirst()
                    : new FilePath(_explicitManifestFile);

                IToolPackage toolDownloadedPackage =
                    _toolPackageInstaller.InstallPackageToExternalManagedLocation(
                        new PackageLocation(
                            nugetConfig: configFile,
                            additionalFeeds: _sources,
                            rootConfigDirectory: manifestFile.GetDirectoryPath()),
                        _packageId,
                        versionRange,
                        targetFramework,
                        verbosity: _verbosity);

                _toolManifestEditor.Add(
                    manifestFile,
                    toolDownloadedPackage.Id,
                    toolDownloadedPackage.Version,
                    toolDownloadedPackage.Commands.Select(c => c.Name).ToArray());

                foreach (var restoredCommand in toolDownloadedPackage.Commands)
                {
                    _localToolsResolverCache.Save(
                        new Dictionary <RestoredCommandIdentifier, RestoredCommand>
                    {
                        [new RestoredCommandIdentifier(
                             toolDownloadedPackage.Id,
                             toolDownloadedPackage.Version,
                             NuGetFramework.Parse(targetFramework),
                             Constants.AnyRid,
                             restoredCommand.Name)] =
                            restoredCommand
                    },
                        _nugetGlobalPackagesFolder);
                }

                _reporter.WriteLine(
                    string.Format(
                        LocalizableStrings.LocalToolInstallationSucceeded,
                        string.Join(", ", toolDownloadedPackage.Commands.Select(c => c.Name)),
                        toolDownloadedPackage.Id,
                        toolDownloadedPackage.Version.ToNormalizedString(),
                        manifestFile.Value).Green());

                return(0);
            }
            catch (Exception ex) when(InstallToolCommandLowLevelErrorConverter.ShouldConvertToUserFacingError(ex))
            {
                throw new GracefulException(
                          messages: InstallToolCommandLowLevelErrorConverter.GetUserFacingMessages(ex, _packageId),
                          verboseMessages: new[] { ex.ToString() },
                          isUserError: false);
            }
        }
Esempio n. 6
0
        public override int Execute()
        {
            (FilePath? manifestFileOptional, string warningMessage) =
                _toolManifestFinder.ExplicitManifestOrFindManifestContainPackageId(_explicitManifestFile, _packageId);

            var manifestFile = manifestFileOptional ?? _toolManifestFinder.FindFirst();

            var toolDownloadedPackage        = _toolLocalPackageInstaller.Install(manifestFile);
            var existingPackageWithPackageId =
                _toolManifestFinder
                .Find(manifestFile)
                .Where(p => p.PackageId.Equals(_packageId));

            if (!existingPackageWithPackageId.Any())
            {
                return(_toolInstallLocalCommand.Value.Install(manifestFile));
            }

            var existingPackage = existingPackageWithPackageId.Single();

            if (existingPackage.Version > toolDownloadedPackage.Version)
            {
                throw new GracefulException(new[]
                {
                    string.Format(
                        LocalizableStrings.UpdateLocaToolToLowerVersion,
                        toolDownloadedPackage.Version.ToNormalizedString(),
                        existingPackage.Version.ToNormalizedString(),
                        manifestFile.Value)
                },
                                            isUserError: false);
            }

            if (existingPackage.Version != toolDownloadedPackage.Version)
            {
                _toolManifestEditor.Edit(
                    manifestFile,
                    _packageId,
                    toolDownloadedPackage.Version,
                    toolDownloadedPackage.Commands.Select(c => c.Name).ToArray());
            }

            _localToolsResolverCache.SaveToolPackage(
                toolDownloadedPackage,
                _toolLocalPackageInstaller.TargetFrameworkToInstall);

            if (warningMessage != null)
            {
                _reporter.WriteLine(warningMessage.Yellow());
            }

            if (existingPackage.Version == toolDownloadedPackage.Version)
            {
                _reporter.WriteLine(
                    string.Format(
                        LocalizableStrings.UpdateLocaToolSucceededVersionNoChange,
                        toolDownloadedPackage.Id,
                        existingPackage.Version.ToNormalizedString(),
                        manifestFile.Value));
            }
            else
            {
                _reporter.WriteLine(
                    string.Format(
                        LocalizableStrings.UpdateLocalToolSucceeded,
                        toolDownloadedPackage.Id,
                        existingPackage.Version.ToNormalizedString(),
                        toolDownloadedPackage.Version.ToNormalizedString(),
                        manifestFile.Value).Green());
            }

            return(0);
        }