This repository was archived by the owner on Mar 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (90 loc) · 3.87 KB
/
Program.cs
File metadata and controls
110 lines (90 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using CommandLine;
using Hyperpack.Models.Internal;
using Hyperpack.Services;
using Hyperpack.Services.Resolvers;
using Hyperpack.Helpers;
namespace Hyperpack
{
class Program
{
private static IServiceProvider provider;
public static async Task<int> Main(string[] args)
{
var result = Parser.Default.ParseArguments(args, new Type[] {
typeof(BuildArguments),
typeof(PublishArguments)
});
BaseArguments baseArgs;
if (result.Tag == ParserResultType.Parsed) {
var parsed = result as Parsed<object>;
baseArgs = (BaseArguments) parsed.Value;
} else {
// print errors
return 1;
}
// Load our cache path
var cachePath = PathHelpers.GetCachePath();
var cache = new CacheService(new DirectoryInfo(cachePath));
await cache.InitAsync();
// Load injected services
var collection = new ServiceCollection();
collection.AddSingleton<CurseApiService>();
collection.AddSingleton<CacheService>(cache);
collection.AddScoped<CurseResolver>();
collection.AddScoped<UrlResolver>();
collection.AddScoped<PackService>();
collection.AddScoped<ResolverService>();
collection.AddScoped<FetcherService>();
collection.AddLogging(logging => {
logging.AddConsole();
if (baseArgs.Verbose) {
logging.SetMinimumLevel(LogLevel.Debug);
} else {
logging.SetMinimumLevel(LogLevel.Information);
}
});
provider = collection.BuildServiceProvider();
// Parse arguments
return result.MapResult(
(BuildArguments a) => Build(a).GetAwaiter().GetResult(),
(PublishArguments a) => Publish(a).GetAwaiter().GetResult(),
errs => 1
);
}
private static async Task<int> Build(BuildArguments args) {
var pack = await LoadPack(args.PackDirectory);
if (pack == null) return 1;
return await pack.Build() ? 0 : 1;
}
private static async Task<int> Publish(PublishArguments args) {
var pack = await LoadPack(args.PackDirectory, true);
if (pack == null) return 1;
return await pack.Publish() ? 0 : 1;
}
private static async Task<PackService> LoadPack(string dir, bool lockfile = false) {
var path = PackHelpers.GetPackPath(dir);
var pack = await PackHelpers.LoadPack(path, lockfile);
if (pack == null) return null;
var service = provider.GetRequiredService<PackService>();
service.InitFrom(pack, new DirectoryInfo(path));
return service;
}
}
class BaseArguments {
[Option('v', "verbose", Required = false, HelpText = "Print verbose output to the console.")]
public bool Verbose { get; set; }
[Option('d', "dry-run", Required = false, HelpText = "Run without actually doing anything.")]
public bool DryRun { get; set; }
[Option('p', "pack", Required = false, HelpText = "Specifies the path to a pack directory, instead of using the current working directory.")]
public string PackDirectory { get; set; }
}
[Verb("build", HelpText = "Builds the pack.")]
class BuildArguments : BaseArguments {}
[Verb("publish", HelpText = "Publishes the pack.")]
class PublishArguments : BaseArguments {}
}