8000 Improved support for local NuGet.Config files. by seesharper · Pull Request #466 · dotnet-script/dotnet-script · GitHub
[go: up one dir, main page]

Skip to content

Improved support for local NuGet.Config files. #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public CompilationDependencyResolver(LogFactory logFactory)

public IEnumerable<CompilationDependency> GetDependencies(string targetDirectory, IEnumerable<string> scriptFiles, bool enableScriptNugetReferences, string defaultTargetFramework = "net46")
{
var pathToProjectFile = _scriptProjectProvider.CreateProject(targetDirectory, scriptFiles,defaultTargetFramework, enableScriptNugetReferences);
_restorer.Restore(pathToProjectFile, packageSources: Array.Empty<string>());
var pathToAssetsFile = Path.Combine(Path.GetDirectoryName(pathToProjectFile), "obj", "project.assets.json");
var projectFileInfo = _scriptProjectProvider.CreateProject(targetDirectory, scriptFiles, defaultTargetFramework, enableScriptNugetReferences);
_restorer.Restore(projectFileInfo, packageSources: Array.Empty<string>());
var pathToAssetsFile = Path.Combine(Path.GetDirectoryName(projectFileInfo.Path), "obj", "project.assets.json");
var dependencyContext = _scriptDependencyContextReader.ReadDependencyContext(pathToAssetsFile);
var result = new List<CompilationDependency>();
foreach (var scriptDependency in dependencyContext.Dependencies)
Expand All @@ -51,7 +51,7 @@ public IEnumerable<CompilationDependency> GetDependencies(string targetDirectory
private static IRestorer CreateRestorer(LogFactory logFactory)
{
var commandRunner = new CommandRunner(logFactory);
return new ProfiledRestorer(new DotnetRestorer(commandRunner, logFactory),logFactory);
return new ProfiledRestorer(new DotnetRestorer(commandRunner, logFactory), logFactory);
}
}
}
12 changes: 6 additions & 6 deletions src/Dotnet.Script.DependencyModel/Context/CachedRestorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public CachedRestorer(IRestorer restorer, LogFactory logFactory)
public bool CanRestore => _restorer.CanRestore;

/// <inheritdoc/>
public void Restore(string pathToProjectFile, string[] packageSources)
public void Restore(ProjectFileInfo projectFileInfo, string[] packageSources)
{
var projectFile = new ProjectFile(File.ReadAllText(pathToProjectFile));
var pathToCachedProjectFile = $"{pathToProjectFile}.cache";
var projectFile = new ProjectFile(File.ReadAllText(projectFileInfo.Path));
var pathToCachedProjectFile = $"{projectFileInfo.Path}.cache";
if (File.Exists(pathToCachedProjectFile))
{
_logger.Debug($"Found cached csproj file at: {pathToCachedProjectFile}");
var cachedProjectFile = new ProjectFile(File.ReadAllText(pathToCachedProjectFile));
if (projectFile.Equals(cachedProjectFile))
{
_logger.Debug($"Skipping restore. {pathToProjectFile} and {pathToCachedProjectFile} are identical.");
_logger.Debug($"Skipping restore. {projectFileInfo.Path} and {pathToCachedProjectFile} are identical.");
return;
}
else
Expand All @@ -55,15 +55,15 @@ public void Restore(string pathToProjectFile, string[] packageSources)

void RestoreAndCacheProjectFile()
{
_restorer.Restore(pathToProjectFile, packageSources);
_restorer.Restore(projectFileInfo, packageSources);
if (projectFile.IsCacheable)
{
_logger.Debug($"Caching project file : {pathToCachedProjectFile}");
projectFile.Save(pathToCachedProjectFile);
}
else
{
_logger.Warning($"Unable to cache {pathToProjectFile}. For caching and optimal performance, ensure that the script(s) references Nuget packages with a pinned version.");
_logger.Warning($"Unable to cache {projectFileInfo.Path}. For caching and optimal performance, ensure that the script(s) references Nuget packages with a pinned version.");
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/Dotnet.Script.DependencyModel/Context/DotnetRestorer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Dotnet.Script.DependencyModel.Environment;
using Dotnet.Script.DependencyModel.Logging;
using Dotnet.Script.DependencyModel.Process;
using Dotnet.Script.DependencyModel.ProjectSystem;
using System;
using System.Linq;

Expand All @@ -19,18 +20,18 @@ public DotnetRestorer(CommandRunner commandRunner, LogFactory logFactory)
_scriptEnvironment = ScriptEnvironment.Default;
}

public void Restore(string pathToProjectFile, string[] packageSources)
public void Restore(ProjectFileInfo projectFileInfo, string[] packageSources)
{
var packageSourcesArgument = CreatePackageSourcesArguments();
var configFileArgument = CreateConfigFileArgument();
var runtimeIdentifier = _scriptEnvironment.RuntimeIdentifier;

_logger.Debug($"Restoring {pathToProjectFile} using the dotnet cli. RuntimeIdentifier : {runtimeIdentifier}");
var exitcode = _commandRunner.Execute("dotnet", $"restore \"{pathToProjectFile}\" -r {runtimeIdentifier} {packageSourcesArgument}");
//var exitcode = _commandRunner.Execute("dotnet", $"restore \"{pathToProjectFile}\" {packageSourcesArgument}");
_logger.Debug($"Restoring {projectFileInfo.Path} using the dotnet cli. RuntimeIdentifier : {runtimeIdentifier} NugetConfigFile: {projectFileInfo.NuGetConfigFile}");
var exitcode = _commandRunner.Execute("dotnet", $"restore \"{projectFileInfo.Path}\" -r {runtimeIdentifier} {packageSourcesArgument} {configFileArgument}");
if (exitcode != 0)
{
// We must throw here, otherwise we may incorrectly run with the old 'project.assets.json'
throw new Exception($"Unable to restore packages from '{pathToProjectFile}'. Make sure that all script files contains valid NuGet references");
throw new Exception($"Unable to restore packages from '{projectFileInfo.Path}'. Make sure that all script files contains valid NuGet references");
}

string CreatePackageSourcesArguments()
Expand All @@ -40,6 +41,14 @@ string CreatePackageSourcesArguments()
: packageSources.Select(s => $"-s {s}")
.Aggregate((current, next) => $"{current} {next}");
}

string CreateConfigFileArgument()
{
return string.IsNullOrWhiteSpace(projectFileInfo.NuGetConfigFile)
? string.Empty
: $"--configfile {projectFileInfo.NuGetConfigFile}";

}
}

public bool CanRestore => true;
Expand Down
7 changes: 5 additions & 2 deletions src/Dotnet.Script.DependencyModel/Context/IRestorer.cs
8000
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Dotnet.Script.DependencyModel.Context
using Dotnet.Script.DependencyModel.ProjectSystem;

namespace Dotnet.Script.DependencyModel.Context
{
/// <summary>
/// Represents a class that is capable of restoring a project file.
Expand All @@ -10,7 +12,8 @@ public interface IRestorer
/// </summary>
/// <param name="pathToProjectFile"></param>
/// <param name="packageSources">A list of packages sources to be used when restoring NuGet packages.</param>
void Restore(string pathToProjectFile, string[] packageSources);
/// <param name="configPath">The path to the NuGet config file to be used when restoring.</param>
void Restore(ProjectFileInfo projectFileInfo, string[] packageSources);

/// <summary>
/// Gets a <see cref="bool"/> value that indicates if this <see cref="IRestorer"/> is available on the system.
Expand Down
7 changes: 4 additions & 3 deletions src/Dotnet.Script.DependencyModel/Context/ProfiledRestorer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using Dotnet.Script.DependencyModel.Logging;
using Dotnet.Script.DependencyModel.ProjectSystem;

namespace Dotnet.Script.DependencyModel.Context
{
Expand All @@ -16,11 +17,11 @@ public ProfiledRestorer(IRestorer restorer, LogFactory logFactory)

public bool CanRestore => _restorer.CanRestore;

public void Restore(string pathToProjectFile, string[] packageSources)
public void Restore(ProjectFileInfo projectFileInfo, string[] packageSources)
{
var stopwatch = Stopwatch.StartNew();
_restorer.Restore(pathToProjectFile, packageSources);
_logger.Debug($"Restoring {pathToProjectFile} took {stopwatch.ElapsedMilliseconds}ms");
_restorer.Restore(projectFileInfo, packageSources);
_logger.Debug($"Restoring {projectFileInfo.Path} took {stopwatch.ElapsedMilliseconds}ms");
}
}
}
56 changes: 3 additions & 53 deletions src/Dotnet.Script.DependencyModel/ProjectSystem/NuGetUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,14 @@
using NuGet.Configuration;
using System.Collections.Generic;
using System.Linq;

namespace Dotnet.Script.DependencyModel.ProjectSystem
{
internal static class NuGetUtilities
{
public static void CreateNuGetConfigFromLocation(string pathToEvaluate, string targetDirectory)
public static string GetNearestConfigPath(string pathToEvaluate)
{
var sourceSettings = Settings.LoadDefaultSettings(pathToEvaluate);
var targetSettings = new Settings(targetDirectory);

CopySection(sourceSettings, targetSettings, "config");
CopySection(sourceSettings, targetSettings, "bindingRedirects");
CopySection(sourceSettings, targetSettings, "packageRestore");
CopySection(sourceSettings, targetSettings, "solution");
CopySection(sourceSettings, targetSettings, "packageSources");
CopySection(sourceSettings, targetSettings, "packageSourceCredentials");
CopySection(sourceSettings, targetSettings, "apikeys");
CopySection(sourceSettings, targetSettings, "disabledPackageSources");
CopySection(sourceSettings, targetSettings, "activePackageSource");

targetSettings.SaveToDisk();
}

private static void CopySection(ISettings sourceSettings, ISettings targetSettings, string sectionName)
{
var existingAddItems = sourceSettings.GetSection(sectionName)?.Items.Where(item => item is object && (item is SourceItem || item is AddItem) && item.ElementName.ToLowerInvariant() == "add").Cast<AddItem>();

if (existingAddItems == null)
{
return;
}

foreach (var addItem in existingAddItems)
{
if (ShouldResolvePath(sectionName, addItem.Key))
{
targetSettings.AddOrUpdate(sectionName, new AddItem(addItem.Key, addItem.GetValueAsPath()));
}
else
{
targetSettings.AddOrUpdate(sectionName, addItem);
}
}
}

private static bool ShouldResolvePath(string sectionName, string key)
{
if (sectionName == "packageSources")
{
return true;
}

if (sectionName == "config")
{
return key == "globalPackagesFolder" || key == "repositoryPath";
}

return false;
var settings = Settings.LoadDefaultSettings(pathToEvaluate);
return settings.GetConfigFilePaths().FirstOrDefault();
}
}
}
30 changes: 30 additions & 0 deletions src/Dotnet.Script.DependencyModel/ProjectSystem/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Dotnet.Script.DependencyModel.ProjectSystem
{
/// <summary>
/// Contains information about the generated project file and
/// where to find the "nearest" NuGet.Config file.
/// </summary>
public class ProjectFileInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="ProjectFileInfo"/> class.
/// </summary>
/// <param name="path">The path to the generated project file to be used during restore.</param>
/// <param name="nugetConfigFile">The path to the nearest NuGet.Config file seen from the target script folder.</param>
public ProjectFileInfo(string path, string nugetConfigFile)
{
Path = path;
NuGetConfigFile = nugetConfigFile;
}

/// <summary>
/// Gets the path of the generated project file to be used during restore.
/// </summary>
public string Path { get; }

/// <summary>
/// Gets the path to the nearest NuGet.Config file seen from the target script folder.
/// </summary>
public string NuGetConfigFile { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private ScriptProjectProvider(ScriptParser scriptParser, ScriptFilesResolver scr
{
}

public string CreateProjectForRepl(string code, string targetDirectory, string defaultTargetFramework = "net46")
public ProjectFileInfo CreateProjectForRepl(string code, string targetDirectory, string defaultTargetFramework = "net46")
{
var scriptFiles = _scriptFilesResolver.GetScriptFilesFromCode(code);
targetDirectory = Path.Combine(targetDirectory, "interactive");
Expand Down Expand Up @@ -61,8 +61,7 @@ public string CreateProjectForRepl(string code, string targetDirectory, string d

LogProjectFileInfo(pathToProjectFile);

EvaluateAndGenerateNuGetConfigFile(targetDirectory, Path.GetDirectoryName(pathToProjectFile));
return pathToProjectFile;
return new ProjectFileInfo(pathToProjectFile, NuGetUtilities.GetNearestConfigPath(targetDirectory));
}

private void LogProjectFileInfo(string pathToProjectFile)
Expand All @@ -72,12 +71,12 @@ private void LogProjectFileInfo(string pathToProjectFile)
_logger.Debug(content);
}

public string CreateProject(string targetDirectory, string defaultTargetFramework = "net46", bool enableNuGetScriptReferences = false)
public ProjectFileInfo CreateProject(string targetDirectory, string defaultTargetFramework = "net46", bool enableNuGetScriptReferences = false)
{
return CreateProject(targetDirectory, Directory.GetFiles(targetDirectory, "*.csx", SearchOption.AllDirectories), defaultTargetFramework, enableNuGetScriptReferences);
}

public string CreateProject(string targetDirectory, IEnumerable<string> scriptFiles, string defaultTargetFramework = "net46", bool enableNuGetScriptReferences = false)
public ProjectFileInfo CreateProject(string targetDirectory, IEnumerable<string> scriptFiles, string defaultTargetFramework = "net46", bool enableNuGetScriptReferences = false)
{
if (scriptFiles == null || !scriptFiles.Any())
{
Expand All @@ -95,14 +94,14 @@ public string CreateProject(string targetDirectory, IEnumerable<string> scriptFi
return SaveProjectFileFromScriptFiles(targetDirectory, defaultTargetFramework, scriptFiles.ToArray());
}

public string CreateProjectForScriptFile(string scriptFile)
public ProjectFileInfo CreateProjectForScriptFile(string scriptFile)
{
_logger.Debug($"Creating project file for {scriptFile}");
var scriptFiles = _scriptFilesResolver.GetScriptFiles(scriptFile);
return SaveProjectFileFromScriptFiles(Path.GetDirectoryName(scriptFile), _scriptEnvironment.TargetFramework, scriptFiles.ToArray());
}

private string SaveProjectFileFromScriptFiles(string targetDirectory, string defaultTargetFramework, string[] csxFiles)
private ProjectFileInfo SaveProjectFileFromScriptFiles(string targetDirectory, string defaultTargetFramework, string[] csxFiles)
{
ProjectFile projectFile = CreateProjectFileFromScriptFiles(defaultTargetFramework, csxFiles);

Expand All @@ -111,8 +110,7 @@ private string SaveProjectFileFromScriptFiles(string targetDirectory, string def

LogProjectFileInfo(pathToProjectFile);

EvaluateAndGenerateNuGetConfigFile(targetDirectory, Path.GetDirectoryName(pathToProjectFile));
return pathToProjectFile;
return new ProjectFileInfo(pathToProjectFile, NuGetUtilities.GetNearestConfigPath(targetDirectory));
}

public ProjectFile CreateProjectFileFromScriptFiles(string defaultTargetFramework, string[] csxFiles)
Expand All @@ -130,17 +128,6 @@ public ProjectFile CreateProjectFileFromScriptFiles(string defaultTargetFramewor
return projectFile;
}

private void EvaluateAndGenerateNuGetConfigFile(string targetDirectory, string pathToProjectFileFolder)
{
var pathToDestinationNuGetConfigFile = Path.Combine(pathToProjectFileFolder, Settings.DefaultSettingsFileName);

if (File.Exists(pathToDestinationNuGetConfigFile))
File.Delete(pathToDestinationNuGetConfigFile);

_logger.Debug($"Generating NuGet config evaluated at {targetDirectory} to {pathToDestinationNuGetConfigFile}");
NuGetUtilities.CreateNuGetConfigFromLocation(targetDirectory, pathToProjectFileFolder);
}

public static string GetPathToProjectFile(string targetDirectory)
{
var pathToProjectDirectory = FileUtils.CreateTempFolder(targetDirectory);
Expand Down
Loading
0