Skip to content

Commit

Permalink
- - Added support for NuGet.config (Triggered by PR #263)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-shilo committed Dec 29, 2021
1 parent bb4d7f8 commit efe91fc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 42 deletions.
27 changes: 17 additions & 10 deletions src/cscs/NuGet.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static public string NuGetExeView

static public bool newPackageWasInstalled => NuGetCore.NewPackageWasInstalled;

static public void InstallPackage(string packageNameMask) => NuGetCore.InstallPackage(packageNameMask);
static public void InstallPackage(string packageNameMask, string nugetConfig = null) => NuGetCore.InstallPackage(packageNameMask, nugetConfig);

static public void ListPackages()
{
Expand All @@ -45,8 +45,8 @@ static public void ListPackages()
NuGetCore.ListPackages();
}

static public string[] Resolve(string[] packages, bool suppressDownloading, string script, string packagesConfig = null)
=> NuGetCore.Resolve(packages, suppressDownloading, script, packagesConfig);
static public string[] Resolve(string[] packages, bool suppressDownloading, string script)
=> NuGetCore.Resolve(packages, suppressDownloading, script);
}

class NuGetCore
Expand All @@ -61,7 +61,7 @@ class NuGetCore

static public bool NewPackageWasInstalled { get; set; }

static public void InstallPackage(string packageNameMask, string version = null, string nugetArgs = null)
static public void InstallPackage(string packageNameMask, string version = null, string nugetArgs = null, string nugetConfig = null)
{
var packages = new string[0];
//index is 1-based, exactly as it is printed with ListPackages
Expand Down Expand Up @@ -96,6 +96,10 @@ static public void InstallPackage(string packageNameMask, string version = null,
if (!File.Exists(proj_template))
{
"dotnet".Run("new console", nuget_dir);

if (nugetConfig.FileExists())
File.Copy(nugetConfig, nuget_dir.PathJoin(nugetConfig.GetFileName()), true);

foreach (var name in packages)
{
var ver = "";
Expand Down Expand Up @@ -341,24 +345,27 @@ static PackageInfo FindPackage(string name, string version)
(version.IsEmpty() || version == x.Version));
}

static public string[] Resolve(string[] packages, bool suppressDownloading, string script, string packagesConfig = null)
static public string[] Resolve(string[] packages, bool suppressDownloading, string script)
{
var assemblies = new List<string>();
var all_packages = new List<PackageInfo>();

var packagesFromConfig = XDocument.Load(packagesConfig)
var allPackages = packages.ToList();

string packagesConfig = script.ChangeFileName("packages.config");
if (packagesConfig.FileExists())
allPackages.AddRange(XDocument.Load(packagesConfig)
.Descendants("package")
.Select(n =>
{
var package = n.Attribute("id").Value;
if (n.Attribute("version") != null)
package += $" -ver:\"{n.Attribute("version").Value}\"";
return package;
})
.ToArray();
}));

bool promptPrinted = false;
foreach (string item in packages.Concat(packagesFromConfig))
foreach (string item in packages.Concat(allPackages))
{
// //css_nuget -noref -ng:"-IncludePrerelease –version 1.0beta" cs-script
// //css_nuget -noref -ver:"4.1.0-alpha1" -ng:"-Pre" NLog
Expand Down Expand Up @@ -406,7 +413,7 @@ static public string[] Resolve(string[] packages, bool suppressDownloading, stri

try
{
InstallPackage(package, packageVersion, nugetArgs);
InstallPackage(package, packageVersion, nugetArgs, script.ChangeFileName("NuGet.config"));
package_info = FindPackage(package, packageVersion);
NewPackageWasInstalled = true;
}
Expand Down
18 changes: 1 addition & 17 deletions src/cscs/ScriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public string[] ReferencedResources
public string[] ResolvePackages(bool suppressDownloading = false)
{
#if !class_lib
return NuGet.Resolve(Packages, suppressDownloading, this.ScriptPath, this.PackagesConfig);
return NuGet.Resolve(Packages, suppressDownloading, this.ScriptPath);
#else
return new string[0];
#endif
Expand All @@ -124,22 +124,6 @@ public string[] ResolvePackages(bool suppressDownloading = false)
/// </summary>
public string[] Packages => packages.ToArray();

/// <summary>
/// Gets the location of the `packages.config` file if present.
/// </summary>
public string PackagesConfig
{
get
{
var packagesConfig = ScriptPath.GetDirName().PathJoin("packages.config");

if (packagesConfig.FileExists())
return packagesConfig;
else
return null;
}
}

/// <summary>
/// Collection of referenced assemblies. All assemblies are referenced either from
/// command-line, code or resolved from referenced namespaces.
Expand Down
36 changes: 21 additions & 15 deletions src/cscs/Utils/CoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace csscript
#endif
{
/// <summary>
///
/// </summary>
public static partial class CoreExtensions
{
Expand Down Expand Up @@ -54,8 +53,7 @@ internal static int Run(this string exe, string args, string dir = null, Action<

process.WaitForExit();

// try { error.Abort(); } catch { }
// try { output.Abort(); } catch { }
// try { error.Abort(); } catch { } try { output.Abort(); } catch { }

return process.ExitCode;
}
Expand Down Expand Up @@ -134,8 +132,8 @@ internal static XElement SelectFirst(this XContainer element, string path)
}

/// <summary>
/// Removes the duplicated file system path items from the collection.The duplicates are identified
/// based on the path being case sensitive depending on the hosting OS file system.
/// Removes the duplicated file system path items from the collection.The duplicates are
/// identified based on the path being case sensitive depending on the hosting OS file system.
/// </summary>
/// <param name="list">The list.</param>
/// <returns>A list with the unique items</returns>
Expand All @@ -160,9 +158,7 @@ internal static string[] RemovePathDuplicates(this string[] list)
/// Determines whether [is shared assembly].
/// </summary>
/// <param name="path">The path.</param>
/// <returns>
/// <c>true</c> if [is shared assembly] [the specified path]; otherwise, <c>false</c>.
/// </returns>
/// <returns><c>true</c> if [is shared assembly] [the specified path]; otherwise, <c>false</c>.</returns>
internal static bool IsSharedAssembly(this string path) => path.StartsWith(sdk_root, StringComparison.OrdinalIgnoreCase);

/// <summary>
Expand Down Expand Up @@ -194,6 +190,19 @@ public static string RemoveAssemblyExtension(this string asmName)
public static bool SamePathAs(this string path1, string path2) =>
string.Compare(path1, path2, Runtime.IsWin) == 0;

public static bool IfSateliteExists(this string file, string sateliteName, out string sateliteFile)
{
sateliteFile = null;
var sateliteFilePath = file.GetDirName().PathJoin(sateliteName);

if (sateliteFilePath.FileExists())
{
sateliteFile = sateliteFilePath;
return true;
}
return false;
}

/// <summary>
/// Captures the exception dispatch information.
/// </summary>
Expand All @@ -203,8 +212,7 @@ public static Exception CaptureExceptionDispatchInfo(this Exception ex)
{
try
{
// on .NET 4.5 ExceptionDispatchInfo can be used
// ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
// on .NET 4.5 ExceptionDispatchInfo can be used ExceptionDispatchInfo.Capture(ex.InnerException).Throw();

typeof(Exception).GetMethod("PrepForRemoting", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(ex, new object[0]);
Expand Down Expand Up @@ -295,8 +303,8 @@ internal static List<string> AddPathIfNotThere(this List<string> items, string i
}
}

// it's not critical at this stage as the whole options.SearchDirs (the reason for this routine)
// is rebuild from ground to top if it has no sections
// it's not critical at this stage as the whole options.SearchDirs (the
// reason for this routine) is rebuild from ground to top if it has no sections
var createMissingSection = false;

if (!added)
Expand Down Expand Up @@ -368,9 +376,7 @@ public class TempFileCollection
/// <summary>
/// Gets or sets the items (file paths) composing the temporary files collections.
/// </summary>
/// <value>
/// The items.
/// </value>
/// <value>The items.</value>
public List<string> Items { get; set; } = new List<string>();

/// <summary>
Expand Down

0 comments on commit efe91fc

Please sign in to comment.