-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #368 from fredpointzero/feature/NuGetConfiguration…
…WithPublish Generate a NuGet.config file with resolved properties when publishing a script
- Loading branch information
Showing
15 changed files
with
327 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Dotnet.Script.Tests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Dotnet.Script.DependencyModel/ProjectSystem/NuGetUtilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using NuGet.Configuration; | ||
using System.Collections.Generic; | ||
|
||
namespace Dotnet.Script.DependencyModel.ProjectSystem | ||
{ | ||
internal static class NuGetUtilities | ||
{ | ||
struct NuGetConfigSection | ||
{ | ||
public string Name; | ||
public HashSet<string> KeysForPathValues; | ||
public bool AreAllValuesPaths; | ||
} | ||
|
||
static readonly NuGetConfigSection[] NuGetSections = | ||
{ | ||
new NuGetConfigSection { Name = "config", KeysForPathValues = new HashSet<string> { "globalPackagesFolder", "repositoryPath" } }, | ||
new NuGetConfigSection { Name = "bindingRedirects" }, | ||
new NuGetConfigSection { Name = "packageRestore" }, | ||
new NuGetConfigSection { Name = "solution" }, | ||
new NuGetConfigSection { Name = "packageSources", AreAllValuesPaths = true }, | ||
new NuGetConfigSection { Name = "packageSourceCredentials" }, | ||
new NuGetConfigSection { Name = "apikeys" }, | ||
new NuGetConfigSection { Name = "disabledPackageSources" }, | ||
new NuGetConfigSection { Name = "activePackageSource" }, | ||
}; | ||
|
||
// Create a NuGet file containing all properties with resolved absolute paths | ||
public static void CreateNuGetConfigFromLocation(string pathToEvaluate, string targetDirectory) | ||
{ | ||
var settings = Settings.LoadDefaultSettings(pathToEvaluate); | ||
var target = new Settings(targetDirectory); | ||
|
||
var valuesToSet = new List<SettingValue>(); | ||
foreach (var section in NuGetSections) | ||
{ | ||
// Resolve properly path values | ||
valuesToSet.Clear(); | ||
if (section.AreAllValuesPaths) | ||
{ | ||
// All values are paths | ||
var values = settings.GetSettingValues(section.Name, true); | ||
valuesToSet.AddRange(values); | ||
} | ||
else | ||
{ | ||
var values = settings.GetSettingValues(section.Name, false); | ||
if (section.KeysForPathValues != null) | ||
{ | ||
// Some values are path | ||
foreach (var value in values) | ||
{ | ||
if (section.KeysForPathValues.Contains(value.Key)) | ||
{ | ||
var val = settings.GetValue(section.Name, value.Key, true); | ||
value.Value = val; | ||
} | ||
|
||
valuesToSet.Add(value); | ||
} | ||
} | ||
else | ||
// All values are not path | ||
valuesToSet.AddRange(values); | ||
} | ||
target.SetValues(section.Name, valuesToSet); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
using Dotnet.Script.DependencyModel.ProjectSystem; | ||
using NuGet.Configuration; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
namespace Dotnet.Script.Tests | ||
{ | ||
using SettingsSection = Dictionary<string, Dictionary<string, string>>; | ||
using SettingsValues = Dictionary<string, string>; | ||
|
||
public class NuGetUtilitiesTests | ||
{ | ||
public static object[][] Args = | ||
{ | ||
new object[] | ||
{ | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<configuration> | ||
<config> | ||
<add key=""dependencyVersion"" value=""Highest"" /> | ||
<add key=""globalPackagesFolder"" value=""{0}/packages"" /> | ||
<add key=""repositoryPath"" value=""../installed_packages"" /> | ||
<add key=""http_proxy"" value=""http://company-squid:3128@contoso.com"" /> | ||
</config> | ||
</configuration>", | ||
new SettingsSection | ||
{ | ||
{ | ||
"config", | ||
new SettingsValues | ||
{ | ||
{ "dependencyVersion", "Highest" }, | ||
{ "globalPackagesFolder", @"{1}/packages" }, | ||
{ "repositoryPath", @"{0}../installed_packages" }, | ||
{ "http_proxy", @"http://company-squid:3128@contoso.com" }, | ||
} | ||
}, | ||
} | ||
}, | ||
new object[] | ||
{ | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<configuration> | ||
<bindingRedirects> | ||
<add key=""skip"" value=""true"" /> | ||
</bindingRedirects> | ||
</configuration>", | ||
new SettingsSection | ||
{ | ||
{ | ||
"bindingRedirects", | ||
new SettingsValues | ||
{ | ||
{ "skip", "true" }, | ||
} | ||
}, | ||
} | ||
}, | ||
new object[] | ||
{ | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<configuration> | ||
<packageRestore> | ||
<add key=""enabled"" value=""true"" /> | ||
<add key=""automatic"" value=""true"" /> | ||
</packageRestore > | ||
</configuration>", | ||
new SettingsSection | ||
{ | ||
{ | ||
"packageRestore", | ||
new SettingsValues | ||
{ | ||
{ "enabled", "true" }, | ||
{ "automatic", "true" }, | ||
} | ||
}, | ||
} | ||
}, | ||
new object[] | ||
{ | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<configuration> | ||
<solution> | ||
<add key=""disableSourceControlIntegration"" value=""true"" /> | ||
</solution> | ||
</configuration>", | ||
new SettingsSection | ||
{ | ||
{ | ||
"solution", | ||
new SettingsValues | ||
{ | ||
{ "disableSourceControlIntegration", "true" }, | ||
} | ||
}, | ||
} | ||
}, | ||
new object[] | ||
{ | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<configuration> | ||
<packageSources> | ||
<add key=""nuget.org"" value=""https://api.nuget.org/v3/index.json"" protocolVersion=""3"" /> | ||
<add key=""Contoso"" value=""https://contoso.com/packages/"" /> | ||
<add key=""Test Source"" value=""{0}/packages"" /> | ||
<add key=""Relative Test Source"" value=""../packages"" /> | ||
</packageSources> | ||
</configuration>", | ||
new SettingsSection | ||
{ | ||
{ | ||
"packageSources", | ||
new SettingsValues | ||
{ | ||
{ "nuget.org", "https://api.nuget.org/v3/index.json" }, | ||
{ "Contoso", "https://contoso.com/packages/" }, | ||
{ "Test Source", "{1}/packages" }, | ||
{ "Relative Test Source", "{0}../packages" }, | ||
} | ||
}, | ||
} | ||
}, | ||
new object[] | ||
{ | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<configuration> | ||
<apikeys> | ||
<add key=""https://MyRepo/ES/api/v2/package"" value=""encrypted_api_key"" /> | ||
</apikeys> | ||
</configuration>", | ||
new SettingsSection | ||
{ | ||
{ | ||
"apikeys", | ||
new SettingsValues | ||
{ | ||
{ "https://MyRepo/ES/api/v2/package", "encrypted_api_key" }, | ||
} | ||
}, | ||
} | ||
}, | ||
new object[] | ||
{ | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<configuration> | ||
<disabledPackageSources> | ||
<add key=""Contoso"" value=""true"" /> | ||
</disabledPackageSources> | ||
</configuration>", | ||
new SettingsSection | ||
{ | ||
{ | ||
"disabledPackageSources", | ||
new SettingsValues | ||
{ | ||
{ "Contoso", "true" }, | ||
} | ||
}, | ||
} | ||
}, | ||
new object[] | ||
{ | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<configuration> | ||
<activePackageSource> | ||
<add key=""nuget.org"" value=""https://api.nuget.org/v3/index.json"" /> | ||
</activePackageSource > | ||
</configuration>", | ||
new SettingsSection | ||
{ | ||
{ | ||
"activePackageSource", | ||
new SettingsValues | ||
{ | ||
{ "nuget.org", "https://api.nuget.org/v3/index.json" }, | ||
} | ||
}, | ||
} | ||
}, | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(Args))] | ||
public void ShouldGenerateEvaluatedNuGetConfigFile(string sourceNuGet, SettingsSection targetSettings) | ||
{ | ||
using (var projectFolder = new DisposableFolder()) | ||
{ | ||
// Generate files and directories | ||
var sourceFolder = Path.Combine(projectFolder.Path, "Source"); | ||
var sourceScript = Path.Combine(sourceFolder, "script.cs"); | ||
var targetFolder = Path.Combine(projectFolder.Path, "Target"); | ||
Directory.CreateDirectory(targetFolder); | ||
Directory.CreateDirectory(sourceFolder); | ||
|
||
var rootTokens = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "c:" : string.Empty; | ||
|
||
var resolvedSourceNuGet = string.Format(sourceNuGet, rootTokens); | ||
File.WriteAllText(Path.Combine(sourceFolder, Settings.DefaultSettingsFileName), resolvedSourceNuGet); | ||
|
||
// Evaluate and generate the NuGet config file | ||
NuGetUtilities.CreateNuGetConfigFromLocation(sourceScript, targetFolder); | ||
|
||
// Validate the generated NuGet config file | ||
var targetNuGetPath = Path.Combine(targetFolder, Settings.DefaultSettingsFileName); | ||
Assert.True(File.Exists(targetNuGetPath), $"NuGet.config file was not generated at {targetNuGetPath}"); | ||
|
||
sourceFolder += Path.DirectorySeparatorChar; | ||
var settings = new Settings(targetFolder); | ||
foreach (var expectedSettings in targetSettings) | ||
{ | ||
foreach (var expectedSetting in expectedSettings.Value) | ||
{ | ||
var value = settings.GetValue(expectedSettings.Key, expectedSetting.Key); | ||
var resolvedExpectedSetting = string.Format(expectedSetting.Value, sourceFolder, rootTokens); | ||
Assert.Equal(resolvedExpectedSetting, value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.