Skip to content

Commit

Permalink
Merge pull request #15 from nkasco/feature-automaticupdates
Browse files Browse the repository at this point in the history
Feature automaticupdates
  • Loading branch information
nkasco authored Mar 18, 2022
2 parents 0d36c79 + ea5a0ae commit f6b242c
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 17 deletions.
2 changes: 1 addition & 1 deletion App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public App()
}
}

public string AppIcon;
public static string AppIcon;

/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
Expand Down
5 changes: 3 additions & 2 deletions Changelog.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<Changelog>
<Item version="1.0" detail="Initial Launch" />
<Item version="1.02" detail="First WinUI revamp preview" />
<Item version="1.0" detail="Initial Launch (Material Design in XAML UI)" />
<Item version="1.0.2" detail="First WinUI app revamp preview" />
<Item version="1.0.4" detail="Automatic updates now available" />
</Changelog>
5 changes: 4 additions & 1 deletion ITATKWinUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

<ItemGroup>
<PackageReference Include="microsoft.powershell.sdk" Version="7.2.1" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.1" />
<PackageReference Include="PInvoke.User32" Version="0.7.104" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
Expand Down Expand Up @@ -112,6 +112,9 @@
<None Update="Settings.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Updater.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="XML\Categories.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
10 changes: 8 additions & 2 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">


<NavigationView
<Grid>
<StackPanel Name="LoadingStack" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Loading..." Name="LoadingText" Width="150" TextAlignment="Center"/>
<ProgressBar Name="LoadingProgressBar" Width="150" IsIndeterminate="True" ShowPaused="False" ShowError="False" />
</StackPanel>
<NavigationView
Name="MainNav"
Visibility="Collapsed"
IsSettingsVisible="True"
IsBackButtonVisible="Collapsed"
IsBackEnabled="False"
Expand Down Expand Up @@ -181,4 +186,5 @@
</SplitView>

</NavigationView>
</Grid>
</Window>
103 changes: 94 additions & 9 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public partial class MainWindow : Window

private static string SingleOrMulti = "Single";

private static void LaunchScript(object sender, EventArgs e, string scriptPath, string args, string type, string inputType)
private static void LaunchScript(object sender, EventArgs e, string scriptPath, string args, string type, string inputType, string wait, string elevate, string hide)
{
string EXEPath;

Expand Down Expand Up @@ -125,14 +125,25 @@ private static void LaunchScript(object sender, EventArgs e, string scriptPath,
{
scriptPath = Environment.CurrentDirectory + "\\" + scriptPath;
}

var process = new Process
var process = new Process();
if (hide == "true")
{
StartInfo = new ProcessStartInfo("\"" + EXEPath + "\"", "-ExecutionPolicy Bypass -NoProfile -File \"" + scriptPath + "\" " + args)
process.StartInfo = new ProcessStartInfo("\"" + EXEPath + "\"", "-ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -File \"" + scriptPath + "\" " + args)
{
CreateNoWindow = false
}
};
};
} else {
process.StartInfo = new ProcessStartInfo("\"" + EXEPath + "\"", "-ExecutionPolicy Bypass -NoProfile -File \"" + scriptPath + "\" " + args)
{
CreateNoWindow = false
};
}

if(elevate == "true")
{
process.StartInfo.Verb = "runas";
process.StartInfo.UseShellExecute = true;
}
//process.StartInfo.RedirectStandardOutput = true;
//process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
//{
Expand All @@ -143,7 +154,13 @@ private static void LaunchScript(object sender, EventArgs e, string scriptPath,
// output.Append("\n[" + lineCount + "]: " + e.Data);
// }
//});
process.Start();
if(wait == "true")
{
process.WaitForExit();
} else
{
process.Start();
}
//process.BeginOutputReadLine();
//process.WaitForExit();
//Debug.WriteLine(output);
Expand All @@ -154,16 +171,22 @@ private static void LaunchScript(object sender, EventArgs e, string scriptPath,
}
}

private static void LaunchScript(string scriptPath, string args, string type, string inputType, string wait, string elevate, string hide)
{
//Overload condition for what we expect to use
LaunchScript(null, null, scriptPath, args, type, inputType, wait, elevate, hide);
}

private static void LaunchScript(string scriptPath, string args, string type, string inputType)
{
//Overload condition for what we expect to use
LaunchScript(null, null, scriptPath, args, type, inputType);
LaunchScript(null, null, scriptPath, args, type, inputType, "false", "no", "false");
}

private static void LaunchScript(string scriptPath, string type, string inputType)
{
//Overload condition if there are no args
LaunchScript(null,null,scriptPath, "", type, inputType);
LaunchScript(null,null,scriptPath, "", type, inputType, "false", "no", "false");
}

public static void LaunchExplorer(Object sender, EventArgs e, string path)
Expand Down Expand Up @@ -449,6 +472,68 @@ public MainWindow()

//Select the first navigation item
MainNav.SelectedItem = MainNav.MenuItems[1]; //Index 1 because 0 is the "Categories" text header

//We've got to check for updates in the main window due to a WinUI limitation that currently exists
CheckForUpdates();
}

private async void CheckForUpdates()
{
//Check for updates if enabled
XDocument settingsXML = XDocument.Load(@"Settings.xml");
foreach (XElement item in from y in settingsXML.Descendants("Item") select y)
{
if (item.Attribute("Name").Value == "SettingAutomaticUpdates")
{
if (item.Attribute("Setting").Value == "true" || item.Attribute("Setting").Value == "True")
{
bool updateRequired = false;
LoadingText.Text = "Checking for updates...";
XDocument updateXML = XDocument.Load("https://raw.githubusercontent.com/nkasco/IT-Admin-Toolkit-WinUI/master/UpdateInfo.xml");
XElement updateXMLData = updateXML.Descendants("Item").Last();
string updateVersion = updateXMLData.Attribute("version").Value;
Version updateVersionv = new Version(updateVersion);

string currentVersion = CurrentVersion();
Version currentVersionv = new Version(currentVersion);
if (currentVersionv < updateVersionv)
{
updateRequired = true;
}

if (updateRequired == true)
{
LoadingText.Text = "Downloading update...";
string scriptName = "Updater.ps1";
string scriptPath = Environment.CurrentDirectory + "\\" + scriptName;
LaunchScript(scriptPath, " -InstallPath \"" + Environment.CurrentDirectory + "\" -DownloadURL \"" + updateXMLData.Attribute("link").Value + "\"", "PS5", "None", "false", "true", "true"); //TODO: Should this be moved to run with the integrated host? Probably can't until elevation support is added to Windows App SDK with v1.1
await Task.Run(() => Task.Delay(1000000)); //TODO: This needs fixed, for some reason WaitForExit() isn't working
//TODO: Run LoadingPhrase() on a loop
}
}
}
}

LoadingStack.Visibility = Visibility.Collapsed;
MainNav.Visibility = Visibility.Visible;
}

private string CurrentVersion()
{
string version = "";

XDocument changelogDetail = XDocument.Load(@"Changelog.xml");
version = changelogDetail.Descendants("Item").Last().Attribute("version").Value;

return version;
}

private string LoadingPhrase()
{
//Return a random string for the loading window
string phrase = "";

return phrase;
}

//TODO: Terminal output WIP
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- Eventually, explore cross-platform support for macOS and Linux.

**Prerequisites (In-Progress):**
- At a minimum if you just want to run the built app you will need the proper [Windows App Runtime Redist](https://aka.ms/windowsappsdk/1.0-stable/msix-installer) installed, this project is primarily targeted towards x64 but please feel free to open an Issue or PR for other platforms if you find they aren't working.
- At a minimum if you just want to run the built app you will need the proper [Windows App Runtime Redist](https://aka.ms/windowsappsdk/1.0-stable/msix-installer) (Currently: Version 1.0.1) installed, this project is primarily targeted towards x64 but please feel free to open an Issue or PR for other platforms if you find they aren't working.
- For development - environmental requirements can be found [here](https://docs.microsoft.com/en-us/windows/apps/winui/winui3/create-your-first-winui3-app#unpackaged-create-a-new-project-for-an-unpackaged-c-or-c-winui-3-desktop-app)
- Currently, this project is intended to be unpackaged if you are running the .sln from Visual Studio
- Other potential things to check:
Expand Down
2 changes: 1 addition & 1 deletion Settings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<TextBlock FontWeight="Bold">Automatic Updates</TextBlock>
<TextBlock>Automatically check for updates each time you launch this application</TextBlock>
</StackPanel>
<ToggleSwitch Name="SettingAutomaticUpdates" HorizontalAlignment="Right" Grid.Column="1" OffContent="" OnContent="" IsEnabled="False" MinWidth="0"/>
<ToggleSwitch Name="SettingAutomaticUpdates" HorizontalAlignment="Right" Grid.Column="1" OffContent="" OnContent="" MinWidth="0"/>
</Grid>
<Grid Padding="25, 25, 25, 0" >
<Grid.ColumnDefinitions>
Expand Down
4 changes: 4 additions & 0 deletions UpdateInfo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<Update>
<Item version="1.0.4" link="https://github.com/nkasco/IT-Admin-Toolkit-WinUI/releases/download/v1.0.4/ITATKWinUI.zip" />
</Update>
91 changes: 91 additions & 0 deletions Updater.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
########################################################
# IT Admin Toolkit Auto Updater #
# Date: 3/16/2022 #
# Written by: Nathan Kasco #
########################################################

param(
[string]
$InstallPath,

[string]
$DownloadURL
)

#Info: This script assumes the check for updates has already been done, therefore we just need to download/extract the update and then move the files into place.

if(!($InstallPath) -or !($DownloadURL)){
Exit 1 #TODO: Make this more meaningful
}

try{
Invoke-WebRequest -Uri $DownloadURL -OutFile "$Env:Temp\ITATKLatest.zip" -UseBasicParsing -ErrorAction Stop
} catch {
Write-Host "Error downloading update $_"
exit 1 #TODO: Make this more meaningful
}

Start-Sleep -Seconds 2

#Extract the update
try{
$TempZipPath = "$Env:Temp\ITATKLatest.zip"
$TempPath = "$Env:Temp\ITATKLatest"
if(Test-Path $TempZipPath){
Expand-Archive -Path $TempZipPath -DestinationPath $TempPath -ErrorAction Stop
} else {
Exit 1 #TODO: Make this more meaningful
}
} catch {
#TODO: Handle errors
}

#Don't start until the main UI is closed to prevent any file in use errors
do{
$ProcessCheck = $null
$ProcessCheck = Get-Process ITATKWinUI -ErrorAction SilentlyContinue
if($ProcessCheck){
$ProcessCheck | Stop-Process -Force -ErrorAction SilentlyContinue
}
start-sleep -seconds 3
} while ($ProcessCheck)

if(Test-Path $TempPath){
#Compare the current version to the new version
$TempFiles = Get-ChildItem -Path $TempPath
$InstallFiles = Get-ChildItem -Path $InstallPath

#Delete current files
foreach($TempFile in $InstallFiles){
if($TempFile.Name -notin "Settings.xml","XML","Scripts","Updater.ps1"){
Remove-Item -Path $TempFile.FullName -Force -Recurse
}
}

#Move the files into place, ensure we skip user files
foreach($TempFile in $TempFiles){
if($TempFile.Name -notin "Settings.xml","XML","Scripts","Updater.ps1"){
Move-Item -Path $TempFile.FullName -Destination $InstallPath -Force
}
}

#Optimization
<#
$CurrentFiles = Get-ChildItem -Path "$InstallPath" -Recurse
$Differences = Compare-Object -ReferenceObject $CurrentFiles -DifferenceObject $TempFiles
foreach($Difference in $Differences){
try{
Move-Item -Path $Difference -Destination $InstallPath -Force -ErrorAction Stop
} catch {
#TODO: Handle errors
}
}#>

#Cleanup
Remove-Item -Path "$Env:Temp\ITATKLatest.zip" -Force -ErrorAction Stop
Remove-Item -Path $TempPath -Recurse -Force -ErrorAction Stop

Start-Process -FilePath "$InstallPath\ITATKWinUI.exe"
} else {
Exit 1 #TODO: Make this more meaningful
}

0 comments on commit f6b242c

Please sign in to comment.