Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 1a5f568

Browse files
committed
[.NET version] Validate the upack.json file when packing or pushing.
Improve error handling.
1 parent 97b9c67 commit 1a5f568

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

upack/Command.cs

+64-1
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,75 @@ internal static async Task<UniversalPackageMetadata> ReadManifestAsync(Stream me
220220
return JsonConvert.DeserializeObject<UniversalPackageMetadata>(text);
221221
}
222222

223+
internal static string ValidateManifest(UniversalPackageMetadata info)
224+
{
225+
if (info.Group != null)
226+
{
227+
if (info.Group.Length > 250)
228+
{
229+
return "group must be between 0 and 250 characters long.";
230+
}
231+
232+
var invalid = info.Group.Where(c => (c < '0' || c > '9') && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && c != '-' && c != '.' && c != '/' && c != '_').Distinct().ToArray();
233+
if (invalid.Length == 1)
234+
{
235+
return "group contains invalid character: '" + invalid[0] + "'";
236+
}
237+
else if (invalid.Length > 1)
238+
{
239+
return "group contains invalid characters: '" + string.Join("', '", invalid) + "'";
240+
}
241+
242+
if (info.Group.StartsWith("/") || info.Group.EndsWith("/"))
243+
{
244+
return "group must not start or end with a slash.";
245+
}
246+
}
247+
248+
{
249+
if (string.IsNullOrEmpty(info.Name))
250+
{
251+
return "missing name.";
252+
}
253+
if (info.Name.Length > 50)
254+
{
255+
return "name must be between 1 and 50 characters long.";
256+
}
257+
258+
var invalid = info.Name.Where(c => (c < '0' || c > '9') && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && c != '-' && c != '.' && c != '_').Distinct().ToArray();
259+
if (invalid.Length == 1)
260+
{
261+
return "name contains invalid character: '" + invalid[0] + "'";
262+
}
263+
else if (invalid.Length > 1)
264+
{
265+
return "name contains invalid characters: '" + string.Join("', '", invalid) + "'";
266+
}
267+
}
268+
269+
if (info.Version == null)
270+
{
271+
return "missing or invalid version.";
272+
}
273+
274+
if (info.Title != null && info.Title.Length > 50)
275+
{
276+
return "title must be between 0 and 50 characters long.";
277+
}
278+
279+
return null;
280+
}
281+
223282
internal static void PrintManifest(UniversalPackageMetadata info)
224283
{
225284
if (!string.IsNullOrEmpty(info.Group))
226-
Console.WriteLine($"Package: {info.Group}:{info.Name}");
285+
{
286+
Console.WriteLine($"Package: {info.Group}/{info.Name}");
287+
}
227288
else
289+
{
228290
Console.WriteLine($"Package: {info.Name}");
291+
}
229292

230293
Console.WriteLine($"Version: {info.Version}");
231294
}

upack/CommandDispatcher.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,14 @@ public void Main(string[] args)
139139
{
140140
try
141141
{
142-
Environment.ExitCode = cmd.RunAsync().GetAwaiter().GetResult();
142+
try
143+
{
144+
Environment.ExitCode = cmd.RunAsync().GetAwaiter().GetResult();
145+
}
146+
catch (AggregateException ex) when (ex.InnerException is ApplicationException)
147+
{
148+
throw ex.InnerException;
149+
}
143150
}
144151
catch (ApplicationException ex)
145152
{

upack/Pack.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,10 @@ public override async Task<int> RunAsync()
9090
}
9191
}
9292

93-
if (string.IsNullOrEmpty(info.Name))
93+
var error = ValidateManifest(info);
94+
if (error != null)
9495
{
95-
Console.Error.WriteLine("Missing package name.");
96-
return 2;
97-
}
98-
if (info.Version == null)
99-
{
100-
Console.Error.WriteLine("Missing package version.");
96+
Console.Error.WriteLine("Invalid {0}: {1}", string.IsNullOrWhiteSpace(this.Manifest) ? "parameters" : "upack.json", error);
10197
return 2;
10298
}
10399

upack/Push.cs

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public override async Task<int> RunAsync()
4444
throw new ApplicationException("The specified file is not a valid universal package: " + ex.Message, ex);
4545
}
4646

47+
var error = ValidateManifest(info);
48+
if (error != null)
49+
{
50+
Console.Error.WriteLine("Invalid upack.json: {0}", error);
51+
return 2;
52+
}
53+
4754
packageStream.Position = 0;
4855

4956
var client = CreateClient(this.Target, this.Authentication);

0 commit comments

Comments
 (0)