Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented list command #369

Merged
merged 5 commits into from
May 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 61 additions & 21 deletions source/BCU-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ what applications to uninstall.
[drive:][path] – Specifies drive and directory to where the export should be saved.
filename – Specifies filename of the .xml file to save the exported application information to.

BCU-console list [/Q] [/U] [/V] - Display a list of installed applications.

Switches:
/Q - Use quiet uninstallers wherever possible (by default only use loud).
/U - Unattended mode (do not ask user for confirmation). WARNING: ONLY USE AFTER
Expand Down Expand Up @@ -73,40 +75,81 @@ private static int Main(string[] args)
{
case "uninstall":
return ProcessUninstallCommand(args.Skip(1).ToArray());

case "list":
return ProcessListCommand(args.Skip(1).ToArray());
case "export":
return ProcessExportCommand(args.Skip(1).ToArray());

default:
Console.WriteLine("Invalid command \"{0}\"\n", args[0]);
Console.WriteLine($"Invalid command \"{args[0]}\"\n");
ShowHelp();
return 1;
}
}
catch (SystemException ex)
{
Console.WriteLine("Encountered an unexpected error!");
Console.WriteLine(@"Encountered an unexpected error!");
Console.WriteLine(ex);
return 13;
}
}

private static int ProcessListCommand(string[] args)
{
var isVerbose = args.Any(x => x.Equals("/V", StringComparison.OrdinalIgnoreCase));
var isQuiet = args.Any(x => x.Equals("/Q", StringComparison.OrdinalIgnoreCase));
var isUnattended = args.Any(x => x.Equals("/U", StringComparison.OrdinalIgnoreCase));

var serializer = new ApplicationEntrySerializer(QueryApps(isQuiet, isUnattended, isVerbose));

Console.WriteLine($@"{"Display Name",-40} {"Version",-20} {"Source",-40}");
var sb = new StringBuilder(82);
for (var i = 0; i < 102; i++)
{
sb.Append('-');
}
Console.WriteLine(sb.ToString());

foreach (var entry in serializer.Items)
{
var displayName = entry.DisplayNameTrimmed;
if (displayName.Length > 40)
{
displayName = displayName.Substring(0, 40);
}

var version = entry.DisplayVersion ?? string.Empty;
if (version.Length > 20)
{
version = version.Substring(0, 20);
}

var source = entry.AboutUrl ?? string.Empty;
if (source.Length > 40)
{
source = source.Substring(0, 40);
}

Console.WriteLine($"{displayName,-40} {version,-20} {source,-40}");
}
return 0;
}

private static int ProcessExportCommand(string[] args)
{
var isVerbose = args.Any(x => x.Equals("/V", StringComparison.OrdinalIgnoreCase));
var isQuiet = args.Any(x => x.Equals("/Q", StringComparison.OrdinalIgnoreCase));
var isUnattended = args.Any(x => x.Equals("/U", StringComparison.OrdinalIgnoreCase));

args = args.Where(x => !x.StartsWith("/")).ToArray();
args = args.Where(x => !x.StartsWith("/", StringComparison.Ordinal)).ToArray();
if (args.Length != 1)
return ShowInvalidSyntaxError("Missing export filename or invalid arguments");

Console.WriteLine($"Starting export to {args[0]}");
Console.WriteLine($@"Starting export to {args[0]}");
var apps = QueryApps(isQuiet, isUnattended, isVerbose);

Console.WriteLine("Exporting data...");
Console.WriteLine(@"Exporting data...");
ApplicationEntrySerializer.SerializeApplicationEntries(args[0], apps);
Console.WriteLine("Success!");
Console.WriteLine(@"Success!");
return 0;
}

Expand Down Expand Up @@ -136,37 +179,37 @@ private static int ProcessUninstallCommand(string[] args)
var isUnattended = args.Any(x => x.Equals("/U", StringComparison.OrdinalIgnoreCase));

if (isUnattended)
Console.WriteLine("WARNING: Running in unattended mode. To abort press Ctrl+C or close the window.");
Console.WriteLine(@"WARNING: Running in unattended mode. To abort press Ctrl+C or close the window.");

return RunUninstall(list, isQuiet, isUnattended, isVerbose);
}

private static int RunUninstall(UninstallList list, bool isQuiet, bool isUnattended, bool isVerbose)
{
Console.WriteLine("Starting bulk uninstall...");
Console.WriteLine(@"Starting bulk uninstall...");
var apps = QueryApps(isQuiet, isUnattended, isVerbose);

apps = apps.Where(a => list.TestEntry(a) == true).OrderBy(x => x.DisplayName).ToList();

if (apps.Count == 0)
{
Console.WriteLine("No applications matched the supplied uninstall list.");
Console.WriteLine(@"No applications matched the supplied uninstall list.");
return 0;
}

Console.WriteLine("{0} application(s) were matched by the list: {1}", apps.Count,
string.Join("; ", apps.Select(x => x.DisplayName)));

Console.WriteLine("These applications will now be uninstalled PERMANENTLY.");
Console.WriteLine(@"These applications will now be uninstalled PERMANENTLY.");

if (!isUnattended)
{
Console.WriteLine("Do you want to continue? [Y]es/[N]o");
Console.WriteLine(@"Do you want to continue? [Y]es/[N]o");
if (Console.ReadKey(true).Key != ConsoleKey.Y)
return CancelledByUser();
}

Console.WriteLine("Setting-up for the uninstall task...");
Console.WriteLine(@"Setting-up for the uninstall task...");
var targets = apps.Select(a => new BulkUninstallEntry(a, a.QuietUninstallPossible, UninstallStatus.Waiting))
.ToList();
var task = UninstallManager.CreateBulkUninstallTask(targets,
Expand All @@ -188,14 +231,11 @@ private static int RunUninstall(UninstallList list, bool isQuiet, bool isUnatten
{
isDone = true;
Console.WriteLine();
Console.WriteLine("Uninstall task Finished.");
Console.WriteLine(@"Uninstall task Finished.");

foreach (var error in task.AllUninstallersList.Where(x =>
x.CurrentStatus != UninstallStatus.Completed && x.CurrentError != null))
{
Console.WriteLine("Error: {0} - {1}", error.UninstallerEntry.DisplayName,
error.CurrentError.Message);
}
Console.WriteLine($@"Error: {error.UninstallerEntry.DisplayName} - {error.CurrentError.Message}");
}
};
task.Start();
Expand All @@ -216,15 +256,15 @@ public static void ClearCurrentConsoleLine()

private static int CancelledByUser()
{
Console.WriteLine("Operation cancelled by the user.");
Console.WriteLine(@"Operation cancelled by the user.");
return 1223;
}

private static IList<ApplicationUninstallerEntry> QueryApps(bool isQuiet, bool isUnattended, bool isVerbose)
{
ConfigureUninstallTools();

Console.WriteLine("Looking for applications...");
Console.WriteLine(@"Looking for applications...");
string previousMain = null;

IList<ApplicationUninstallerEntry> result;
Expand Down