Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Better error messages for malformed packages
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Feb 16, 2022
1 parent 3d378b7 commit 7273039
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/Squirrel/Internal/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ public static IEnumerable<T> Return<T>(T value)
yield return value;
}

/// <summary>
/// Essentially just .Single() but with context aware error messages which are more helpful to a user.
/// Eg. "Invalid {is}. Only a single {what} expected in {in}."
/// </summary>
public static T ContextualSingle<T>(this IEnumerable<T> source, string strIs, string strWhat, string strIn = null)
{
var c = System.Linq.Enumerable.Count(source);
if (c == 0) {
throw new InvalidOperationException(
$"Invalid {strIs}: One {strWhat} expected" +
(strIn == null ? "." : $" in {strIn}.") +
" None was found.");
}

if (c > 1) {
throw new InvalidOperationException(
$"Invalid {strIs}: Only a single {strWhat} expected" +
(strIn == null ? "." : $" in {strIn}.") +
$" Actually found: {c}.");
}

return System.Linq.Enumerable.First(source);
}

/// <summary>
/// Enumerates the sequence and invokes the given action for each value in the sequence.
/// </summary>
Expand Down
15 changes: 9 additions & 6 deletions src/SquirrelCli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -190,12 +190,15 @@ static void Releasify(ReleasifyOptions options)
}

foreach (var file in toProcess) {
Log.Info("Creating release package: " + file.FullName);
Log.Info("Creating release for package: " + file.FullName);

var rp = new ReleasePackage(file.FullName);
rp.CreateReleasePackage(Path.Combine(di.FullName, rp.SuggestedReleaseFileName), contentsPostProcessHook: (pkgPath, zpkg) => {
var nuspecPath = Directory.GetFiles(pkgPath, "*.nuspec", SearchOption.TopDirectoryOnly).First();
var libDir = Directory.GetDirectories(Path.Combine(pkgPath, "lib")).First();
var nuspecPath = Directory.GetFiles(pkgPath, "*.nuspec", SearchOption.TopDirectoryOnly)
.ContextualSingle("package", "*.nuspec", "top level directory");
var libDir = Directory.GetDirectories(Path.Combine(pkgPath, "lib"))
.ContextualSingle("package", "'lib' folder");
var awareExes = SquirrelAwareExecutableDetector.GetAllSquirrelAwareApps(libDir);
// unless the validation has been disabled, do not allow the creation of packages without a SquirrelAwareApp inside
Expand Down Expand Up @@ -224,10 +227,10 @@ static void Releasify(ReleasifyOptions options)
// often supports x86 emulation, so we want to pick the least compatible architecture
// for the package.
var archOrder = new[] { /*RuntimeCpu.Arm64,*/ RuntimeCpu.X64, RuntimeCpu.X86 };
var pkgarch = archOrder.First(o => pearchs.Contains(o));
var pkgarch = archOrder.FirstOrDefault(o => pearchs.Contains(o));
if (pkgarch == RuntimeCpu.Unknown) {
Log.Warn("Unable to detect package machine architecture. Are the SquirrelAware binaries compatible?");
Log.Warn("Unable to detect package machine architecture from SquirrelAware binaries.");
} else {
Log.Info($"Package architecture: {pkgarch} (implicit, from a SquirrelAware binary)");
}
Expand Down

0 comments on commit 7273039

Please sign in to comment.