mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
133 lines
4.7 KiB
C#
133 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Semmle.Util;
|
|
|
|
namespace Semmle.Extraction.CSharp.DependencyFetching
|
|
{
|
|
/// <summary>
|
|
/// Manage the downloading of NuGet packages.
|
|
/// Locates packages in a source tree and downloads all of the
|
|
/// referenced assemblies to a temp folder.
|
|
/// </summary>
|
|
internal class NugetPackages
|
|
{
|
|
private readonly string nugetExe;
|
|
private readonly ProgressMonitor progressMonitor;
|
|
|
|
/// <summary>
|
|
/// The list of package files.
|
|
/// </summary>
|
|
public FileInfo[] PackageFiles { get; }
|
|
|
|
/// <summary>
|
|
/// The source directory used.
|
|
/// </summary>
|
|
public string SourceDirectory { get; }
|
|
|
|
/// <summary>
|
|
/// The computed packages directory.
|
|
/// This will be in the Temp location
|
|
/// so as to not trample the source tree.
|
|
/// </summary>
|
|
public TemporaryDirectory PackageDirectory { get; }
|
|
|
|
/// <summary>
|
|
/// Create the package manager for a specified source tree.
|
|
/// </summary>
|
|
public NugetPackages(string sourceDir, TemporaryDirectory packageDirectory, ProgressMonitor progressMonitor)
|
|
{
|
|
SourceDirectory = sourceDir;
|
|
PackageDirectory = packageDirectory;
|
|
this.progressMonitor = progressMonitor;
|
|
|
|
// Expect nuget.exe to be in a `nuget` directory under the directory containing this exe.
|
|
var currentAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
|
|
var directory = Path.GetDirectoryName(currentAssembly)
|
|
?? throw new FileNotFoundException($"Directory path '{currentAssembly}' of current assembly is null");
|
|
nugetExe = Path.Combine(directory, "nuget", "nuget.exe");
|
|
|
|
if (!File.Exists(nugetExe))
|
|
throw new FileNotFoundException(string.Format("NuGet could not be found at {0}", nugetExe));
|
|
|
|
PackageFiles = new DirectoryInfo(SourceDirectory)
|
|
.EnumerateFiles("packages.config", SearchOption.AllDirectories)
|
|
.ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restore all files in a specified package.
|
|
/// </summary>
|
|
/// <param name="package">The package file.</param>
|
|
/// <param name="pm">Where to log progress/errors.</param>
|
|
private void RestoreNugetPackage(string package)
|
|
{
|
|
progressMonitor.NugetInstall(package);
|
|
|
|
/* Use nuget.exe to install a package.
|
|
* Note that there is a clutch of NuGet assemblies which could be used to
|
|
* invoke this directly, which would arguably be nicer. However they are
|
|
* really unwieldy and this solution works for now.
|
|
*/
|
|
|
|
string exe, args;
|
|
if (Util.Win32.IsWindows())
|
|
{
|
|
exe = nugetExe;
|
|
args = string.Format("install -OutputDirectory {0} {1}", PackageDirectory, package);
|
|
}
|
|
else
|
|
{
|
|
exe = "mono";
|
|
args = string.Format("{0} install -OutputDirectory {1} {2}", nugetExe, PackageDirectory, package);
|
|
}
|
|
|
|
var pi = new ProcessStartInfo(exe, args)
|
|
{
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false
|
|
};
|
|
|
|
try
|
|
{
|
|
using var p = Process.Start(pi);
|
|
|
|
if (p is null)
|
|
{
|
|
progressMonitor.FailedNugetCommand(pi.FileName, pi.Arguments, "Couldn't start process.");
|
|
return;
|
|
}
|
|
|
|
var output = p.StandardOutput.ReadToEnd();
|
|
var error = p.StandardError.ReadToEnd();
|
|
|
|
p.WaitForExit();
|
|
if (p.ExitCode != 0)
|
|
{
|
|
progressMonitor.FailedNugetCommand(pi.FileName, pi.Arguments, output + error);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
when (ex is System.ComponentModel.Win32Exception || ex is FileNotFoundException)
|
|
{
|
|
progressMonitor.FailedNugetCommand(pi.FileName, pi.Arguments, ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Download the packages to the temp folder.
|
|
/// </summary>
|
|
/// <param name="pm">The progress monitor used for reporting errors etc.</param>
|
|
public void InstallPackages()
|
|
{
|
|
foreach (var package in PackageFiles)
|
|
{
|
|
RestoreNugetPackage(package.FullName);
|
|
}
|
|
}
|
|
}
|
|
}
|