public IToolPackage InstallPackageToExternalManagedLocation(
            PackageLocation packageLocation,
            PackageId packageId,
            VersionRange versionRange = null,
            string targetFramework    = null,
            string verbosity          = null)
        {
            _installCallback?.Invoke();

            var packageDirectory = new DirectoryPath(NuGetGlobalPackagesFolder.GetLocation()).WithSubDirectories(packageId.ToString());

            _fileSystem.Directory.CreateDirectory(packageDirectory.Value);
            var executable = packageDirectory.WithFile("exe");

            _fileSystem.File.CreateEmptyFile(executable.Value);

            MockFeedPackage package = _projectRestorer.GetPackage(
                packageId.ToString(),
                versionRange ?? VersionRange.Parse("*"),
                packageLocation.NugetConfig,
                packageLocation.RootConfigDirectory);

            return(new TestToolPackage
            {
                Id = packageId,
                Version = NuGetVersion.Parse(package.Version),
                Commands = new List <RestoredCommand> {
                    new RestoredCommand(new ToolCommandName(package.ToolCommandName), "runner", executable)
                },
                Warnings = Array.Empty <string>(),
                PackagedShims = Array.Empty <FilePath>()
            });
        }
Exemple #2
0
 private static bool MatchPackageVersion(MockFeedPackage p, string packageId, string packageVersion)
 {
     if (packageVersion == null)
     {
         return(p.PackageId == packageId);
     }
     return(p.PackageId == packageId && p.Version == packageVersion);
 }
Exemple #3
0
 private static bool MatchPackageVersion(MockFeedPackage p, string packageId, string packageVersion)
 {
     if (string.IsNullOrEmpty(packageVersion))
     {
         return(p.PackageId == packageId);
     }
     return(p.PackageId == packageId && p.Version == packageVersion);
 }
Exemple #4
0
        private static bool MatchPackage(MockFeedPackage p, string packageId, VersionRange versionRange)
        {
            if (string.Compare(p.PackageId, packageId, StringComparison.CurrentCultureIgnoreCase) != 0)
            {
                return(false);
            }

            return(versionRange == null ||
                   versionRange.FindBestMatch(new[] { NuGetVersion.Parse(p.Version) }) != null);
        }
Exemple #5
0
        public ToolConfigurationAndExecutablePath ObtainAndReturnExecutablePath(
            string packageId,
            string packageVersion  = null,
            FilePath?nugetconfig   = null,
            string targetframework = null,
            string source          = null)
        {
            _beforeRunObtain();

            PickFeedByNugetConfig(nugetconfig);
            PickFeedBySource(source);

            MockFeedPackage package = _mockFeeds
                                      .SelectMany(f => f.Packages)
                                      .Where(p => MatchPackageVersion(p, packageId, packageVersion)).OrderByDescending(p => p.Version)
                                      .FirstOrDefault();

            if (package == null)
            {
                throw new PackageObtainException("simulated cannot find package");
            }

            packageVersion  = package.Version;
            targetframework = targetframework ?? "targetframework";

            var packageIdVersionDirectory = Path.Combine("toolPath", packageId, packageVersion);

            _fakeExecutableDirectory = Path.Combine(packageIdVersionDirectory,
                                                    packageId, packageVersion, "morefolders", "tools",
                                                    targetframework);
            var fakeExecutable = Path.Combine(_fakeExecutableDirectory, FakeEntrypointName);

            if (!_fileSystem.Directory.Exists(_fakeExecutableDirectory))
            {
                _fileSystem.Directory.CreateDirectory(_fakeExecutableDirectory);
            }

            _fileSystem.File.CreateEmptyFile(Path.Combine(packageIdVersionDirectory, "project.assets.json"));
            _fileSystem.File.CreateEmptyFile(fakeExecutable);

            return(new ToolConfigurationAndExecutablePath(
                       toolConfiguration: new ToolConfiguration(FakeCommandName, FakeEntrypointName),
                       executable: new FilePath(fakeExecutable)));
        }
Exemple #6
0
        public ToolConfigurationAndExecutablePath ObtainAndReturnExecutablePath(
            string packageId,
            string packageVersion  = null,
            FilePath?nugetconfig   = null,
            string targetframework = null,
            string source          = null,
            string verbosity       = null)
        {
            var  stagedFile = Path.Combine(_toolsPath, ".stage", Path.GetRandomFileName());
            bool afterStage = false;

            var toolPackageObtainTransaction = new ToolPackageObtainTransaction(
                obtainAndReturnExecutablePath: (_) =>
            {
                if (Directory.Exists(Path.Combine(_toolsPath, packageId)))
                {
                    throw new PackageObtainException(
                        string.Format(CommonLocalizableStrings.ToolPackageConflictPackageId, packageId));
                }

                _beforeRunObtain();

                PickFeedByNugetConfig(nugetconfig);
                PickFeedBySource(source);

                MockFeedPackage package = _mockFeeds
                                          .SelectMany(f => f.Packages)
                                          .Where(p => MatchPackageVersion(p, packageId, packageVersion)).OrderByDescending(p => p.Version)
                                          .FirstOrDefault();

                if (package == null)
                {
                    throw new PackageObtainException("simulated cannot find package");
                }

                packageVersion  = package.Version;
                targetframework = targetframework ?? "targetframework";

                _packageIdVersionDirectory = Path.Combine(_toolsPath, packageId, packageVersion);

                _fakeExecutableDirectory = Path.Combine(_packageIdVersionDirectory,
                                                        packageId, packageVersion, "morefolders", "tools",
                                                        targetframework);

                SimulateStageFile();
                _duringObtain();

                _fileSystem.File.Delete(stagedFile);
                afterStage = true;

                _fileSystem.Directory.CreateDirectory(_packageIdVersionDirectory);
                _fileSystem.File.CreateEmptyFile(Path.Combine(_packageIdVersionDirectory, "project.assets.json"));
                _fileSystem.Directory.CreateDirectory(_fakeExecutableDirectory);
                var fakeExecutable = Path.Combine(_fakeExecutableDirectory, FakeEntrypointName);
                _fileSystem.File.CreateEmptyFile(fakeExecutable);

                return(new ToolConfigurationAndExecutablePath(
                           toolConfiguration: new ToolConfiguration(FakeCommandName, FakeEntrypointName),
                           executable: new FilePath(fakeExecutable)));;
            },

                rollback: (_) =>
            {
                if (afterStage == false)
                {
                    if (_fileSystem.File.Exists(stagedFile))
                    {
                        _fileSystem.File.Delete(stagedFile);
                    }
                }
                else
                {
                    if (_fileSystem.Directory.Exists(Path.Combine(_toolsPath, packageId)))
                    {
                        _fileSystem.Directory.Delete(Path.Combine(_toolsPath, packageId), true);
                    }
                }
            }
                );

            using (var transactionScope = new TransactionScope())
            {
                Transaction.Current.EnlistVolatile(toolPackageObtainTransaction, EnlistmentOptions.None);
                var toolConfigurationAndExecutablePath = toolPackageObtainTransaction.ObtainAndReturnExecutablePath();

                transactionScope.Complete();
                return(toolConfigurationAndExecutablePath);
            }
        }