Skip to content

Commit

Permalink
Add /SuperLTIPackage command line option
Browse files Browse the repository at this point in the history
Add support for storing zip files in directories
Add support for dynamically finding zip based on SuperLTI executable name
  • Loading branch information
prettyokay-software committed Sep 25, 2020
1 parent ec2d128 commit bb8a2b3
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
112 changes: 110 additions & 2 deletions SuperLTI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Management.Automation;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;

namespace SuperLTI
{
Expand All @@ -13,10 +16,115 @@ static class Program
[STAThread]
static void Main(string[] args)
{
if (File.Exists("SuperLTI.zip"))
// packageName represents the custom zip and folder we're going to look for
// Defaults to the name of the SuperLTI executable
string packageName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
string zipPath = $"{packageName}.zip";

// check if args contains the switch /SuperLTIPackage to specify a folder and/or zip name
// Use the commandline option if it exists
if (args.Length > 1 && Array.Exists(args, arg => arg.Equals("/SuperLTIPackage", StringComparison.OrdinalIgnoreCase)))
{
// Find the index in the array where /SuperLTIName exists, case insensitve, as long as exists
int index = Array.FindIndex(args, arg => arg.IndexOf("/SuperLTIPackage", StringComparison.OrdinalIgnoreCase) >= 0);
// The data will be the one right after the switch
packageName = args[index + 1].ToString();

if (args.Length > 2)
{
// Remove the SuperLTI option from args and pass the rest to the script
List<string> argsList = new List<string>(args);
argsList.RemoveAt(index);
argsList.RemoveAt(index + 1);
args = argsList.ToArray();
}
else
{
// No other args, so clear it
args = new string[0];
}
}

if (! File.Exists(zipPath))
{
Application.Run(new ProgressDialogHost(args));
zipPath = FindZip(packageName);
}

if (string.IsNullOrEmpty(zipPath))
{
Application.Exit();
}
else
{
Application.Run(new ProgressDialogHost(args, zipPath));
}
}

private static void WriteEventLog(string logText, EventLogEntryType messageType)
{
EventLog eventLog = new EventLog("Application");
eventLog.Source = "SuperLTI";

if (!EventLog.SourceExists(eventLog.Source)){
EventLog.CreateEventSource("SuperLTI", "Application");
}
eventLog.WriteEntry(logText, messageType);
}

private static string FindZip(string packageName)
{
// packageName.zip
// packageName/packageName.zip
// packageName/SuperLTI.zip
// SuperLTI.zip

string zipName = string.Empty;

// If the user gave us something with an extension make sure it's valid
if (Path.HasExtension(packageName) && ! Path.GetExtension(packageName).Equals(".zip", StringComparison.OrdinalIgnoreCase)){
WriteEventLog($"/SuperLTIPackage was given {packageName}, which is not a zip file. Attempting to match anyway", EventLogEntryType.Warning);
packageName = Path.GetFileNameWithoutExtension(packageName);
}

// Determine if the user gave us a name or a full zip name
if (Path.HasExtension(packageName))
{
if (File.Exists(packageName)) { return packageName; }
}
else
{
zipName = $"{packageName}.zip";
if (File.Exists(zipName)) { return zipName; }
}

// Just a name, look for a folder
if (Directory.Exists(packageName))
{
DirectoryInfo directoryInfo = new DirectoryInfo(packageName);
FileInfo[] fileInfo = directoryInfo.GetFiles();

if (Array.FindIndex(fileInfo, file => file.Name.Equals(zipName, StringComparison.OrdinalIgnoreCase)) > -1)
{
// packagename/packagename.zip
return Array.Find(fileInfo, file => file.Name.Equals(zipName, StringComparison.OrdinalIgnoreCase)).FullName;
}
else if (Array.FindIndex(fileInfo, file => file.Name.Equals("SuperLTI.zip", StringComparison.OrdinalIgnoreCase)) > -1)
{
// packagename/superlti.zip
return Array.Find(fileInfo, file => file.Name.Equals("SuperLTI.zip", StringComparison.OrdinalIgnoreCase)).FullName;
}
}

// SuperLTI.zip
if (File.Exists("SuperLTI.zip")) {
return "SuperLTI.zip";
}
else
{
// No Zip and No Directory. So nothing
WriteEventLog($"SuperLTI couldn't find {Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)}\\{zipName}, {packageName}\\{zipName}, or {packageName}\\SuperLTI.zip", EventLogEntryType.Error);
return string.Empty;
}
}
}
}
6 changes: 4 additions & 2 deletions SuperLTI/ProgressDialogHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public partial class ProgressDialogHost : Form
private PowerShell ps = null;
private bool IgnoreProgressUI = false;
private string[] Arguments = new string[0];
private string ZipPath = string.Empty;
private string WindowGUID = Guid.NewGuid().ToString();
private IntPtr dialog = IntPtr.Zero;
private Icon dialogIcon = new Icon(Properties.Resources.icon, 16, 16);
public ProgressDialogHost(string[] args)
public ProgressDialogHost(string[] args, string zipPath)
{
Arguments = args;
ZipPath = zipPath;
InitializeComponent();
Icon = Properties.Resources.icon;
progDialog = new ProgressDialog(Handle);
Expand Down Expand Up @@ -111,7 +113,7 @@ private Task CopyAndExtractTask()
return Task.Run(() =>
{
Directory.CreateDirectory(@"C:\SuperLTI");
FileInfo zip = new FileInfo("SuperLTI.zip");
FileInfo zip = new FileInfo(ZipPath);
long totalBytes = zip.Length;
ProgressIntervalTitle = "Copying files...";
Copy.CopyTo(zip, new FileInfo(@"C:\SuperLTI\SuperLTI.zip"), new Action<int>((int progress) => {
Expand Down

0 comments on commit bb8a2b3

Please sign in to comment.