Skip to content

Commit

Permalink
1.1.0.0 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
rex706 committed Mar 10, 2019
1 parent 4bf7330 commit 986e878
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 39 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
# Updater
Light weight program updater that takes file update info as command line arguments.
Light weight file updater.

![alt tag](http://i.imgur.com/FztHxfx.png)

* Parameters are separated by spaces or new lines.
* First parameter expected is an executable to be run when the updater finishes.
- Ideally the program that calls the updater.
- This is auto generated by UpdateCheck.cs used in all of my programs.
* Feed update manifest url as command line argument.
- i.e. a URL to a text file.
* Update parameters are separated by new lines.
* First paramenter expected is the new package version.
- This is usually only needed for UpdateCheck.cs, which opens the Updater.
* Second parameter expected is an executable to be run when the updater finishes.
- Ideally the program that opens the updater, but can be anything.
* Rest of parameters expected will be in pairs of two
- File download URL
- File path and/or File name

Example:

```
SAM.exe
https://drive.google.com/uc?export=download&id=0B2byNRcR0k4vblQxNWNEakNISFU
SAM.exe
https://drive.google.com/uc?export=download&id=0B2byNRcR0k4veXpSUUktVTNZSms
Updater.exe
1.2.3.4
fileToRunWhenComplete.exe
(file2 direct download URL)
file2.dll
(file3 direct download URL)
file3.txt
```
95 changes: 70 additions & 25 deletions Updater/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

Expand All @@ -30,43 +32,86 @@ public MainWindow()
{
InitializeComponent();

this.Loaded += MainWindow_Loaded;
this.Loaded += MainWindow_LoadedAsync;
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
private async void MainWindow_LoadedAsync(object sender, RoutedEventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
List<Download> arguments = new List<Download>();

if (args.Length < 4)
// There is always one argument by default
// and there is only one additional argument expected.
if (args.Length < 2)
{
MessageBox.Show("No arguments found. Please run the updater with command line arguments.");
Environment.Exit(1);
MessageBox.Show("No arguments found. Please provide update manifest.");
Close();
}

string executable = args[1];
string manifestUrl = args[1];

// Get file download info from command line args.
for (int i = 2; i < args.Length; i += 2)
ConsoleBox.AppendText("Reading update manifest . . .\n");

// Open the text file using a stream reader.
using (Stream stream = await new HttpClient().GetStreamAsync(manifestUrl))
{
// If a new version of the updater is being acquired, rename it to be handled in UpdateCheck.cs
if (args[i + 1] == "Updater.exe")
StreamReader reader = new StreamReader(stream);

// Initialize variables.
Version latest = null;
string executable = null;

try
{
arguments.Add(new Download { url = args[i], file = "Updater_new.exe" });
// First two expected items in manifest will be the new version and the starting executable.
latest = Version.Parse(await reader.ReadLineAsync());
executable = await reader.ReadLineAsync();
}
else
{
arguments.Add(new Download { url = args[i], file = args[i + 1] });
catch (Exception m)
{
MessageBox.Show("Error parsing new version number or launch executable.");
Close();
}
}

if (arguments.Count == 0)
{
MessageBox.Show("No arguments found. Please run the updater with command line arguments.");
Environment.Exit(1);
}

Update(executable, arguments);
// Load manifest urls and file names.
while (!reader.EndOfStream)
{
try
{
// Expected in pairs of two.
string downloadUrl = await reader.ReadLineAsync();
string downloadFile = await reader.ReadLineAsync();

if (downloadUrl == null || downloadFile == null)
{
MessageBox.Show("Uneven update manifest.\nPlease review format.");
Close();
}

// If a new version of the updater is being acquired, rename it to be handled in UpdateCheck.cs
if (downloadFile == "Updater.exe")
{
arguments.Add(new Download { url = downloadUrl, file = "Updater_new.exe" });
}
else
{
arguments.Add(new Download { url = downloadUrl, file = downloadFile });
}
}
catch (Exception m)
{
MessageBox.Show("Uneven update manifest.\nPlease review format.");
Close();
}
}
if (arguments.Count == 0)
{
MessageBox.Show("No download arguments found in manifest.");
Close();
}

Update(executable, arguments);
}
}

private async void Update(string executable, List<Download> downloads)
Expand All @@ -80,11 +125,11 @@ private async void Update(string executable, List<Download> downloads)
ConsoleBox.AppendText("Update complete!\n");
MessageBox.Show("Update complete!");

// Open updated program.
// The last executable file to be downloaded will be executed.

// First argument is the program to be opened when the update is complete.
if (executable != null && executable.Length > 1 && executable.Contains(".exe"))
{
ConsoleBox.AppendText("Attempting to start" + executable + " . . .\n");

try
{
Process.Start(executable);
Expand Down
6 changes: 2 additions & 4 deletions Updater/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

Expand Down Expand Up @@ -51,5 +49,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]

0 comments on commit 986e878

Please sign in to comment.