-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
190 lines (174 loc) · 7.34 KB
/
Program.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using McMaster.Extensions.CommandLineUtils;
using Flurl.Http;
using Flurl;
var app = new CommandLineApplication();
app.HelpOption();
var path = app.Argument<string>("upload dir", "Upload Base Directory Path").Accepts(x => x.ExistingDirectory());
path.DefaultValue = AppContext.BaseDirectory;
var host = app.Option<string>("-r|--host <REPOHOST>", "Nexus Host Url", CommandOptionType.SingleValue).IsRequired(true);
var repoName = app.Option<string>("-n|--repo-name <REPONAME>", "Nexus Repository Name", CommandOptionType.SingleValue).IsRequired(true);
var repoType = app.Option<RepoType>("-t|--repo-type <REPOTYPE>", "Nexus Repository Type", CommandOptionType.SingleValue).IsRequired(true).Accepts(x => x.Enum<RepoType>());
var user = app.Option<string>("-u|--user <USER>", "Nexus User", CommandOptionType.SingleValue);
var password = app.Option<string>("-p|--password <PASSWORD>", "Nexus Password", CommandOptionType.SingleValue);
app.OnExecuteAsync(async cancellationToken =>
{
Console.WriteLine($"Upload Base Directory Path: {path.ParsedValue}");
Console.WriteLine($"Nexus Host Url: {host.ParsedValue}");
Console.WriteLine($"Nexus Repository Name: {repoName.ParsedValue}");
Console.WriteLine($"Nexus Repository Type: {repoType.ParsedValue}");
Console.WriteLine("Start Uploading");
switch (repoType.ParsedValue)
{
case RepoType.maven:
await uploadMavenAsync(cancellationToken, path.ParsedValue, host.ParsedValue, repoName.ParsedValue, user.ParsedValue, password.ParsedValue);
break;
case RepoType.npm:
await uploadNpmAsync(cancellationToken, path.ParsedValue, host.ParsedValue, repoName.ParsedValue, user.ParsedValue, password.ParsedValue);
break;
case RepoType.nuget:
await uploadNugetAsync(cancellationToken, path.ParsedValue, host.ParsedValue, repoName.ParsedValue, user.ParsedValue, password.ParsedValue);
break;
default:
break;
}
Console.WriteLine("Upload Completed");
});
return app.Execute(args);
async Task uploadMavenAsync(CancellationToken cancellationToken, string dir, string host, string repoName, string user, string password)
{
var pomFiles = Directory.EnumerateFiles(dir, "*.pom", SearchOption.AllDirectories);
Console.WriteLine($"Total jar file count: {pomFiles.Count()}");
if (pomFiles.Any())
{
if (string.IsNullOrEmpty(user))
{
user = Prompt.GetString("Nexus User Name: ");
}
if (string.IsNullOrEmpty(password))
{
password = Prompt.GetPassword("Nexus Password: ");
}
foreach (var pomFileFullPath in pomFiles)
{
Console.WriteLine($"uploading pom file: {pomFileFullPath}");
var pomFileInfo = new FileInfo(pomFileFullPath);
var pomFileName = pomFileInfo.Name;
var pomFileNameWithoutExt = Path.GetFileNameWithoutExtension(pomFileName);
var jarFileName = $"{pomFileNameWithoutExt}.jar";
var jarFileFullPath = Path.Combine(pomFileInfo.DirectoryName ?? "", jarFileName);
var jarFileInfo = new FileInfo(jarFileFullPath);
var request = host.AppendPathSegment("/service/rest/v1/components").SetQueryParams(new
{
repository = repoName
}).AllowAnyHttpStatus().WithBasicAuth(user, password);
IFlurlResponse response;
if (jarFileInfo.Exists)
{
Console.WriteLine($"uploading jar file: {jarFileFullPath}");
response = await request.PostMultipartAsync(mp =>
mp.AddString("maven2.asset1.extension", "jar")
.AddFile("maven2.asset1", jarFileFullPath)
.AddString("maven2.asset2.extension", "pom")
.AddFile("maven2.asset2", pomFileFullPath), cancellationToken);
}
else
{
response = await request.PostMultipartAsync(mp =>
mp.AddString("maven2.asset1.extension", "pom")
.AddFile("maven2.asset1", pomFileFullPath), cancellationToken);
}
if (response.StatusCode == 204)
{
Console.WriteLine($"upload success {await response.GetStringAsync()}");
}
else
{
Console.WriteLine($"upload failed:{response.StatusCode}-{await response.GetStringAsync()}");
}
}
}
else
{
Console.WriteLine("No any maven package");
}
}
async Task uploadNpmAsync(CancellationToken cancellationToken, string dir, string host, string repoName, string user, string password)
{
var tgzFiles = Directory.EnumerateFiles(dir, "*.tgz", SearchOption.AllDirectories);
Console.WriteLine($"Total tgz file count: {tgzFiles.Count()}");
if (tgzFiles.Any())
{
if (string.IsNullOrEmpty(user))
{
user = Prompt.GetString("Nexus User Name: ")??"";
}
if (string.IsNullOrEmpty(password))
{
password = Prompt.GetPassword("Nexus Password: ");
}
foreach (var tgzFileFullPath in tgzFiles)
{
Console.WriteLine($"uploading file: {tgzFileFullPath}");
var response = await host.AppendPathSegment("/service/rest/v1/components").SetQueryParams(new
{
repository = repoName
}).AllowAnyHttpStatus().WithBasicAuth(user, password).PostMultipartAsync(mp =>
mp.AddFile("npm.asset", tgzFileFullPath), cancellationToken);
if (response.StatusCode == 204)
{
Console.WriteLine($"upload success {await response.GetStringAsync()}");
}
else
{
Console.WriteLine($"upload failed:{response.StatusCode}-{await response.GetStringAsync()}");
}
}
}
else
{
Console.WriteLine("No any npm package");
}
}
async Task uploadNugetAsync(CancellationToken cancellationToken, string dir, string host, string repoName, string user, string password)
{
var nupkgFiles = Directory.EnumerateFiles(dir, "*.nupkg", SearchOption.AllDirectories);
Console.WriteLine($"Total nupkg file count: {nupkgFiles.Count()}");
if (nupkgFiles.Any())
{
if (string.IsNullOrEmpty(user))
{
user = Prompt.GetString("Nexus User Name: ") ?? "";
}
if (string.IsNullOrEmpty(password))
{
password = Prompt.GetPassword("Nexus Password: ");
}
foreach (var nupkgFileFullPath in nupkgFiles)
{
Console.WriteLine($"uploading file: {nupkgFileFullPath}");
var response = await host.AppendPathSegment("/service/rest/v1/components").SetQueryParams(new
{
repository = repoName
}).AllowAnyHttpStatus().WithBasicAuth(user, password).PostMultipartAsync(mp =>
mp.AddFile("nuget.asset", nupkgFileFullPath), cancellationToken);
if (response.StatusCode == 204)
{
Console.WriteLine($"upload success {await response.GetStringAsync()}");
}
else
{
Console.WriteLine($"upload failed:{response.StatusCode}-{await response.GetStringAsync()}");
}
}
}
else
{
Console.WriteLine("No any nupkg package");
}
}
public enum RepoType
{
maven,
npm,
nuget
}