예제 #1
0
        private bool InstallPackage(PackageReference package, DirectoryPath path)
        {
            _log.Debug("Installing NuGet package {0}...", package.Package);

            var nugetPath = GetNuGetPath();
            var process   = _processRunner.Start(nugetPath, new ProcessSettings
            {
                Arguments = GetArguments(package, path, _config),
                RedirectStandardOutput = true,
                Silent = _log.Verbosity < Verbosity.Diagnostic
            });

            process.WaitForExit();

            var exitCode = process.GetExitCode();

            if (exitCode != 0)
            {
                _log.Warning("NuGet exited with {0}", exitCode);
                var output = string.Join(Environment.NewLine, process.GetStandardOutput());
                _log.Verbose(Verbosity.Diagnostic, "Output:\r\n{0}", output);
                return(false);
            }

            return(true);
        }
예제 #2
0
        private static NuGetVersion GetNuGetVersion(PackageReference package, NuGetSourceRepositoryProvider sourceRepositoryProvider, NuGetFramework targetFramework, ILogger logger)
        {
            if (package.Parameters.ContainsKey("version"))
            {
                return(new NuGetVersion(package.Parameters["version"].First()));
            }

            var includePrerelease = false;

            if (package.Parameters.ContainsKey("prerelease"))
            {
                bool.TryParse(package.Parameters["prerelease"].FirstOrDefault() ?? bool.TrueString, out includePrerelease);
            }

            foreach (var sourceRepository in sourceRepositoryProvider.GetRepositories())
            {
                var dependencyInfoResource = sourceRepository.GetResourceAsync <DependencyInfoResource>().Result;
                var dependencyInfo         = dependencyInfoResource.ResolvePackages(package.Package, targetFramework, logger, CancellationToken.None).Result;
                var version = dependencyInfo
                              .Where(p => p.Listed && (includePrerelease || !p.Version.IsPrerelease))
                              .OrderByDescending(p => p.Version, VersionComparer.Default)
                              .Select(p => p.Version)
                              .FirstOrDefault();

                if (version != null)
                {
                    return(version);
                }
            }
            return(null);
        }
예제 #3
0
        /// <summary>
        /// Installs the specified resource at the given location.
        /// </summary>
        /// <param name="package">The package reference.</param>
        /// <param name="type">The package type.</param>
        /// <param name="path">The location where to install the package.</param>
        /// <returns>The installed files.</returns>
        public IReadOnlyCollection <IFile> Install(PackageReference package, PackageType type, DirectoryPath path)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            // Create the addin directory if it doesn't exist.
            path = GetPackagePath(path.MakeAbsolute(_environment), package);
            var root = _fileSystem.GetDirectory(path);

            if (!root.Exists)
            {
                _log.Debug("Creating directory {0}", path);
                root.Create();
            }

            // Package already exist?
            var packagePath = GetPackagePath(root, package.Package);

            if (packagePath != null)
            {
                // Fetch available content from disc.
                var content = _contentResolver.GetFiles(packagePath, package, type);
                if (content.Any())
                {
                    _log.Debug("Package {0} has already been installed.", package.Package);
                    return(content);
                }
            }

            // Install the package.
            InstallPackage(package, path);

            // Try locating the install folder again.
            packagePath = GetPackagePath(root, package.Package);

            // Get the files.
            var result = _contentResolver.GetFiles(packagePath, package, type);

            if (result.Count == 0)
            {
                if (type == PackageType.Addin)
                {
                    var framework = _environment.Runtime.TargetFramework;
                    _log.Warning("Could not find any assemblies compatible with {0}.", framework.FullName);
                }
                else if (type == PackageType.Tool)
                {
                    const string format = "Could not find any relevant files for tool '{0}'. Perhaps you need an include parameter?";
                    _log.Warning(format, package.Package);
                }
            }

            return(result);
        }
예제 #4
0
 public bool CanInstall(PackageReference package, PackageType type)
 {
     if (package == null)
     {
         throw new ArgumentNullException(nameof(package));
     }
     return(package.Scheme.Equals("nuget", StringComparison.OrdinalIgnoreCase));
 }
예제 #5
0
 private static DirectoryPath GetPackagePath(DirectoryPath root, PackageReference package)
 {
     if (package.Parameters.ContainsKey("version"))
     {
         var version = package.Parameters["version"].First();
         return(root.Combine($"{package.Package}.{version}".ToLowerInvariant()));
     }
     return(root.Combine(package.Package.ToLowerInvariant()));
 }
예제 #6
0
        private static ProcessArgumentBuilder GetArguments(
            PackageReference definition,
            DirectoryPath installationRoot, ICakeConfiguration config)
        {
            var arguments = new ProcessArgumentBuilder();

            arguments.Append("install");
            arguments.AppendQuoted(definition.Package);

            // Output directory
            arguments.Append("-OutputDirectory");
            arguments.AppendQuoted(installationRoot.FullPath);

            // if an absolute uri is specified for source, use this
            // otherwise check config for customise package source/s
            if (definition.Address != null)
            {
                arguments.Append("-Source");
                arguments.AppendQuoted(definition.Address.AbsoluteUri);
            }
            else
            {
                var nugetSource = config.GetValue(Constants.NuGet.Source);
                if (!string.IsNullOrWhiteSpace(nugetSource))
                {
                    arguments.Append("-Source");
                    arguments.AppendQuoted(nugetSource);
                }
            }

            // Version
            if (definition.Parameters.ContainsKey("version"))
            {
                arguments.Append("-Version");
                arguments.AppendQuoted(definition.Parameters["version"].First());
            }

            // Prerelease
            if (definition.Parameters.ContainsKey("prerelease"))
            {
                arguments.Append("-Prerelease");
            }

            // NoCache
            if (definition.Parameters.ContainsKey("nocache"))
            {
                arguments.Append("-NoCache");
            }

            arguments.Append("-ExcludeVersion");
            arguments.Append("-NonInteractive");
            return(arguments);
        }
예제 #7
0
        public IReadOnlyCollection <IFile> Install(PackageReference package, PackageType type, DirectoryPath path)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            var targetFramework          = type == PackageType.Addin ? _currentFramework : NuGetFramework.AnyFramework;
            var sourceRepositoryProvider = new NuGetSourceRepositoryProvider(_nugetSettings, _config, package);
            var packageIdentity          = GetPackageId(package, sourceRepositoryProvider, targetFramework, _nugetLogger);

            if (packageIdentity == null)
            {
                return(Array.Empty <IFile>());
            }

            var packageRoot    = path.MakeAbsolute(_environment).FullPath;
            var pathResolver   = new PackagePathResolver(packageRoot);
            var nuGetProject   = new NugetFolderProject(_fileSystem, _contentResolver, _config, _log, pathResolver, packageRoot, targetFramework);
            var packageManager = new NuGetPackageManager(sourceRepositoryProvider, _nugetSettings, packageRoot)
            {
                PackagesFolderNuGetProject = nuGetProject
            };

            var sourceRepositories = sourceRepositoryProvider.GetRepositories();
            var includePrerelease  = false;

            if (package.Parameters.ContainsKey("prerelease"))
            {
                bool.TryParse(package.Parameters["prerelease"].FirstOrDefault() ?? bool.TrueString, out includePrerelease);
            }
            var resolutionContext = new ResolutionContext(DependencyBehavior.Lowest, includePrerelease, false, VersionConstraints.None);
            var projectContext    = new NuGetProjectContext(_log);

            packageManager.InstallPackageAsync(nuGetProject, packageIdentity, resolutionContext, projectContext,
                                               sourceRepositories, Array.Empty <SourceRepository>(),
                                               CancellationToken.None).Wait();

            return(nuGetProject.GetFiles(path, package, type));
        }
예제 #8
0
        private static PackageIdentity GetPackageId(PackageReference package, NuGetSourceRepositoryProvider sourceRepositoryProvider, NuGetFramework targetFramework, ILogger logger)
        {
            var version = GetNuGetVersion(package, sourceRepositoryProvider, targetFramework, logger);

            return(version == null ? null : new PackageIdentity(package.Package, version));
        }
예제 #9
0
        private PackageIdentity GetPackageId(PackageReference package, NuGetSourceRepositoryProvider sourceRepositoryProvider)
        {
            var version = GetNuGetVersion(package, sourceRepositoryProvider);

            return(version == null ? null : new PackageIdentity(package.Package, version));
        }