Skip to content

Commit 8a70be4

Browse files
committed
added nuget -install and command factory
1 parent 4d1b065 commit 8a70be4

27 files changed

Lines changed: 812 additions & 63 deletions

src/ScriptCs.Core/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public static class Constants
44
{
55
public const string PackagesFile = "packages.config";
66
public const string PackagesFolder = "packages";
7+
public const string DefaultRepositoryUrl = "https://nuget.org/api/v2/";
78

89
public const string DebugContractName = "Debug";
910
public const string RunContractName = "Run";
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using ScriptCs.Package;
24

35
namespace ScriptCs
46
{
57
public interface IPackageAssemblyResolver
68
{
7-
IEnumerable<string> GetAssemblyNames(string workingDirectory);
9+
IEnumerable<IPackageReference> GetPackages(string workingDirectory);
10+
IEnumerable<string> GetAssemblyNames(string workingDirectory, Action<string> outputCallback = null);
811
}
912
}

src/ScriptCs.Core/Package/IPackageContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace ScriptCs.Package
55
public interface IPackageContainer
66
{
77
IEnumerable<IPackageReference> FindReferences(string path);
8-
IPackageObject FindPackage(string path, string packageId);
8+
IPackageObject FindPackage(string path, IPackageReference packageReference);
99
}
1010
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ScriptCs.Package
5+
{
6+
public interface IPackageInstaller
7+
{
8+
void InstallPackages(IEnumerable<IPackageReference> packageIds, bool allowPreRelease = false, Action<string> packageInstalled = null);
9+
}
10+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Runtime.Versioning;
34

@@ -6,8 +7,12 @@ namespace ScriptCs.Package
67
public interface IPackageObject
78
{
89
string Id { get; }
9-
string Version { get; }
10+
string TextVersion { get; }
11+
Version Version { get; }
1012
string FullName { get; }
1113
IEnumerable<string> GetCompatibleDlls(FrameworkName frameworkName);
14+
FrameworkName FrameworkName { get; }
15+
16+
IEnumerable<IPackageObject> Dependencies { get; }
1217
}
1318
}

src/ScriptCs.Core/Package/IPackageReference.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Runtime.Versioning;
23

34
namespace ScriptCs.Package
@@ -6,5 +7,7 @@ public interface IPackageReference
67
{
78
string PackageId { get; }
89
FrameworkName FrameworkName { get; }
10+
Version Version { get; set; }
11+
string SpecialVersion { get; }
912
}
1013
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ScriptCs.Package.InstallationProvider
5+
{
6+
public interface IInstallationProvider
7+
{
8+
IEnumerable<string> GetRepositorySources(string path);
9+
bool InstallPackage(IPackageReference packageId, bool allowPreRelease = false, Action<string> packageInstalled = null);
10+
}
11+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using NuGet;
6+
7+
namespace ScriptCs.Package.InstallationProvider
8+
{
9+
internal class NugetInstallationProvider : IInstallationProvider
10+
{
11+
private readonly PackageManager _manager;
12+
private readonly IEnumerable<string> _repositoryUrls;
13+
14+
public NugetInstallationProvider(IFileSystem fileSystem)
15+
{
16+
var path = Path.Combine(fileSystem.CurrentDirectory, Constants.PackagesFolder);
17+
_repositoryUrls = GetRepositorySources(path);
18+
var remoteRepository = new AggregateRepository(PackageRepositoryFactory.Default, _repositoryUrls, true);
19+
_manager = new PackageManager(remoteRepository, path);
20+
}
21+
22+
public IEnumerable<string> GetRepositorySources(string path)
23+
{
24+
var configFileSystem = new PhysicalFileSystem(path);
25+
var settings = Settings.LoadDefaultSettings(configFileSystem);
26+
if (settings == null) return new[] { Constants.DefaultRepositoryUrl };
27+
28+
var sourceProvider = new PackageSourceProvider(settings);
29+
var sources = sourceProvider.LoadPackageSources();
30+
31+
if (sources == null || !sources.Any()) return new[] { Constants.DefaultRepositoryUrl };
32+
33+
return sources.Select(i => i.Source);
34+
}
35+
36+
public bool InstallPackage(IPackageReference packageId, bool allowPreRelease = false, Action<string> packageInstalled = null)
37+
{
38+
var useVersion = packageId.Version.CompareTo(new Version()) != 0;
39+
try
40+
{
41+
if (useVersion)
42+
{
43+
_manager.InstallPackage(packageId.PackageId,
44+
new SemanticVersion(packageId.Version, packageId.SpecialVersion), false,
45+
allowPreRelease);
46+
}
47+
else
48+
{
49+
_manager.InstallPackage(packageId.PackageId);
50+
}
51+
52+
if (packageInstalled != null)
53+
packageInstalled("Installed: " + packageId.PackageId + " " + (useVersion ? packageId.Version.ToString() : ""));
54+
55+
return true;
56+
}
57+
catch (Exception e)
58+
{
59+
if (packageInstalled != null)
60+
{
61+
packageInstalled("Installation failed: " + packageId.PackageId + " " + (useVersion ? packageId.Version.ToString() : ""));
62+
packageInstalled(e.Message);
63+
}
64+
return false;
65+
}
66+
}
67+
}
68+
}

src/ScriptCs.Core/Package/PackageContainer.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using System.Linq;
34
using NuGet;
45

56
namespace ScriptCs.Package
67
{
78
internal class PackageContainer : IPackageContainer
89
{
9-
public IPackageObject FindPackage(string path, string packageId)
10+
private readonly IFileSystem _fileSystem;
11+
12+
public PackageContainer(IFileSystem fileSystem)
13+
{
14+
_fileSystem = fileSystem;
15+
}
16+
17+
public IPackageObject FindPackage(string path, IPackageReference packageRef)
1018
{
1119
var repository = new LocalPackageRepository(path);
20+
IPackage package;
21+
if (packageRef.Version != null)
22+
{
23+
package = repository.FindPackage(packageRef.PackageId, new SemanticVersion(packageRef.Version, packageRef.SpecialVersion), true, true);
24+
}
25+
else
26+
{
27+
package = repository.FindPackage(packageRef.PackageId);
28+
}
1229

13-
var package = repository.FindPackage(packageId);
14-
return package == null ? null : new PackageObject(package);
30+
return package == null ? null : new PackageObject(package, packageRef.FrameworkName);
1531
}
1632

1733
public IEnumerable<IPackageReference> FindReferences(string path)
1834
{
1935
var packageReferenceFile = new PackageReferenceFile(path);
2036
var references = packageReferenceFile.GetPackageReferences();
21-
if (references == null) return Enumerable.Empty<IPackageReference>();
2237

23-
var packages = references.Select(i => new PackageReference(i.Id, i.TargetFramework));
38+
if (!references.Any())
39+
{
40+
var packagesFolder = Path.Combine(_fileSystem.GetWorkingDirectory(path), Constants.PackagesFolder);
41+
if (_fileSystem.DirectoryExists(packagesFolder))
42+
{
43+
var repository = new LocalPackageRepository(packagesFolder);
44+
var arbitraryPackages = repository.GetPackages().Where(i => i.GetSupportedFrameworks().Any(x => x.FullName == VersionUtility.ParseFrameworkName("net40").FullName)).ToList();
45+
if (arbitraryPackages.Any())
46+
{
47+
return arbitraryPackages.Select(i => new PackageReference(i.Id, VersionUtility.ParseFrameworkName("net40"), i.Version.Version) { SpecialVersion = i.Version.SpecialVersion });
48+
}
49+
}
50+
51+
return Enumerable.Empty<IPackageReference>();
52+
}
53+
54+
var packages = references.Select(i => new PackageReference(i.Id, i.TargetFramework, i.Version.Version) { SpecialVersion = i.Version.SpecialVersion });
2455
return packages;
2556
}
2657
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using ScriptCs.Package.InstallationProvider;
5+
6+
namespace ScriptCs.Package
7+
{
8+
public class PackageInstaller : IPackageInstaller
9+
{
10+
private readonly IInstallationProvider _installer;
11+
12+
public PackageInstaller(IInstallationProvider installer)
13+
{
14+
_installer = installer;
15+
}
16+
17+
public void InstallPackages(IEnumerable<IPackageReference> packageIds, bool allowPreRelease = false, Action<string> packageInstalled = null)
18+
{
19+
if (packageIds == null) throw new ArgumentNullException("packageIds");
20+
packageIds = packageIds.ToList();
21+
22+
if (!packageIds.Any())
23+
{
24+
if (packageInstalled != null)
25+
{
26+
packageInstalled("Nothing to install.");
27+
}
28+
return;
29+
}
30+
31+
var successful = true;
32+
foreach (var packageId in packageIds)
33+
{
34+
var result = _installer.InstallPackage(packageId, allowPreRelease, packageInstalled);
35+
successful = successful && result;
36+
}
37+
38+
if (packageInstalled != null && packageIds.Count() > 1)
39+
{
40+
packageInstalled(successful ? "Installation successful" : "Installation unsuccessful");
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)