Skip to content

Commit f6b242c

Browse files
authored
Merge pull request #15 from nkasco/feature-automaticupdates
Feature automaticupdates
2 parents 0d36c79 + ea5a0ae commit f6b242c

File tree

9 files changed

+207
-17
lines changed

9 files changed

+207
-17
lines changed

App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public App()
6969
}
7070
}
7171

72-
public string AppIcon;
72+
public static string AppIcon;
7373

7474
/// <summary>
7575
/// Invoked when the application is launched normally by the end user. Other entry points

Changelog.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<Changelog>
3-
<Item version="1.0" detail="Initial Launch" />
4-
<Item version="1.02" detail="First WinUI revamp preview" />
3+
<Item version="1.0" detail="Initial Launch (Material Design in XAML UI)" />
4+
<Item version="1.0.2" detail="First WinUI app revamp preview" />
5+
<Item version="1.0.4" detail="Automatic updates now available" />
56
</Changelog>

ITATKWinUI.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242

4343
<ItemGroup>
4444
<PackageReference Include="microsoft.powershell.sdk" Version="7.2.1" />
45-
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
4645
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" />
46+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.1" />
4747
<PackageReference Include="PInvoke.User32" Version="0.7.104" />
4848
<Manifest Include="$(ApplicationManifest)" />
4949
</ItemGroup>
@@ -112,6 +112,9 @@
112112
<None Update="Settings.xml">
113113
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
114114
</None>
115+
<None Update="Updater.ps1">
116+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
117+
</None>
115118
<None Update="XML\Categories.xml">
116119
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
117120
</None>

MainWindow.xaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
mc:Ignorable="d">
99

10-
11-
<NavigationView
10+
<Grid>
11+
<StackPanel Name="LoadingStack" HorizontalAlignment="Center" VerticalAlignment="Center">
12+
<TextBlock Text="Loading..." Name="LoadingText" Width="150" TextAlignment="Center"/>
13+
<ProgressBar Name="LoadingProgressBar" Width="150" IsIndeterminate="True" ShowPaused="False" ShowError="False" />
14+
</StackPanel>
15+
<NavigationView
1216
Name="MainNav"
17+
Visibility="Collapsed"
1318
IsSettingsVisible="True"
1419
IsBackButtonVisible="Collapsed"
1520
IsBackEnabled="False"
@@ -181,4 +186,5 @@
181186
</SplitView>
182187

183188
</NavigationView>
189+
</Grid>
184190
</Window>

MainWindow.xaml.cs

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public partial class MainWindow : Window
6262

6363
private static string SingleOrMulti = "Single";
6464

65-
private static void LaunchScript(object sender, EventArgs e, string scriptPath, string args, string type, string inputType)
65+
private static void LaunchScript(object sender, EventArgs e, string scriptPath, string args, string type, string inputType, string wait, string elevate, string hide)
6666
{
6767
string EXEPath;
6868

@@ -125,14 +125,25 @@ private static void LaunchScript(object sender, EventArgs e, string scriptPath,
125125
{
126126
scriptPath = Environment.CurrentDirectory + "\\" + scriptPath;
127127
}
128-
129-
var process = new Process
128+
var process = new Process();
129+
if (hide == "true")
130130
{
131-
StartInfo = new ProcessStartInfo("\"" + EXEPath + "\"", "-ExecutionPolicy Bypass -NoProfile -File \"" + scriptPath + "\" " + args)
131+
process.StartInfo = new ProcessStartInfo("\"" + EXEPath + "\"", "-ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -File \"" + scriptPath + "\" " + args)
132132
{
133133
CreateNoWindow = false
134-
}
135-
};
134+
};
135+
} else {
136+
process.StartInfo = new ProcessStartInfo("\"" + EXEPath + "\"", "-ExecutionPolicy Bypass -NoProfile -File \"" + scriptPath + "\" " + args)
137+
{
138+
CreateNoWindow = false
139+
};
140+
}
141+
142+
if(elevate == "true")
143+
{
144+
process.StartInfo.Verb = "runas";
145+
process.StartInfo.UseShellExecute = true;
146+
}
136147
//process.StartInfo.RedirectStandardOutput = true;
137148
//process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
138149
//{
@@ -143,7 +154,13 @@ private static void LaunchScript(object sender, EventArgs e, string scriptPath,
143154
// output.Append("\n[" + lineCount + "]: " + e.Data);
144155
// }
145156
//});
146-
process.Start();
157+
if(wait == "true")
158+
{
159+
process.WaitForExit();
160+
} else
161+
{
162+
process.Start();
163+
}
147164
//process.BeginOutputReadLine();
148165
//process.WaitForExit();
149166
//Debug.WriteLine(output);
@@ -154,16 +171,22 @@ private static void LaunchScript(object sender, EventArgs e, string scriptPath,
154171
}
155172
}
156173

174+
private static void LaunchScript(string scriptPath, string args, string type, string inputType, string wait, string elevate, string hide)
175+
{
176+
//Overload condition for what we expect to use
177+
LaunchScript(null, null, scriptPath, args, type, inputType, wait, elevate, hide);
178+
}
179+
157180
private static void LaunchScript(string scriptPath, string args, string type, string inputType)
158181
{
159182
//Overload condition for what we expect to use
160-
LaunchScript(null, null, scriptPath, args, type, inputType);
183+
LaunchScript(null, null, scriptPath, args, type, inputType, "false", "no", "false");
161184
}
162185

163186
private static void LaunchScript(string scriptPath, string type, string inputType)
164187
{
165188
//Overload condition if there are no args
166-
LaunchScript(null,null,scriptPath, "", type, inputType);
189+
LaunchScript(null,null,scriptPath, "", type, inputType, "false", "no", "false");
167190
}
168191

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

450473
//Select the first navigation item
451474
MainNav.SelectedItem = MainNav.MenuItems[1]; //Index 1 because 0 is the "Categories" text header
475+
476+
//We've got to check for updates in the main window due to a WinUI limitation that currently exists
477+
CheckForUpdates();
478+
}
479+
480+
private async void CheckForUpdates()
481+
{
482+
//Check for updates if enabled
483+
XDocument settingsXML = XDocument.Load(@"Settings.xml");
484+
foreach (XElement item in from y in settingsXML.Descendants("Item") select y)
485+
{
486+
if (item.Attribute("Name").Value == "SettingAutomaticUpdates")
487+
{
488+
if (item.Attribute("Setting").Value == "true" || item.Attribute("Setting").Value == "True")
489+
{
490+
bool updateRequired = false;
491+
LoadingText.Text = "Checking for updates...";
492+
XDocument updateXML = XDocument.Load("https://raw.githubusercontent.com/nkasco/IT-Admin-Toolkit-WinUI/master/UpdateInfo.xml");
493+
XElement updateXMLData = updateXML.Descendants("Item").Last();
494+
string updateVersion = updateXMLData.Attribute("version").Value;
495+
Version updateVersionv = new Version(updateVersion);
496+
497+
string currentVersion = CurrentVersion();
498+
Version currentVersionv = new Version(currentVersion);
499+
if (currentVersionv < updateVersionv)
500+
{
501+
updateRequired = true;
502+
}
503+
504+
if (updateRequired == true)
505+
{
506+
LoadingText.Text = "Downloading update...";
507+
string scriptName = "Updater.ps1";
508+
string scriptPath = Environment.CurrentDirectory + "\\" + scriptName;
509+
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
510+
await Task.Run(() => Task.Delay(1000000)); //TODO: This needs fixed, for some reason WaitForExit() isn't working
511+
//TODO: Run LoadingPhrase() on a loop
512+
}
513+
}
514+
}
515+
}
516+
517+
LoadingStack.Visibility = Visibility.Collapsed;
518+
MainNav.Visibility = Visibility.Visible;
519+
}
520+
521+
private string CurrentVersion()
522+
{
523+
string version = "";
524+
525+
XDocument changelogDetail = XDocument.Load(@"Changelog.xml");
526+
version = changelogDetail.Descendants("Item").Last().Attribute("version").Value;
527+
528+
return version;
529+
}
530+
531+
private string LoadingPhrase()
532+
{
533+
//Return a random string for the loading window
534+
string phrase = "";
535+
536+
return phrase;
452537
}
453538

454539
//TODO: Terminal output WIP

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
- Eventually, explore cross-platform support for macOS and Linux.
2828

2929
**Prerequisites (In-Progress):**
30-
- 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.
30+
- 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.
3131
- 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)
3232
- Currently, this project is intended to be unpackaged if you are running the .sln from Visual Studio
3333
- Other potential things to check:

Settings.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<TextBlock FontWeight="Bold">Automatic Updates</TextBlock>
6565
<TextBlock>Automatically check for updates each time you launch this application</TextBlock>
6666
</StackPanel>
67-
<ToggleSwitch Name="SettingAutomaticUpdates" HorizontalAlignment="Right" Grid.Column="1" OffContent="" OnContent="" IsEnabled="False" MinWidth="0"/>
67+
<ToggleSwitch Name="SettingAutomaticUpdates" HorizontalAlignment="Right" Grid.Column="1" OffContent="" OnContent="" MinWidth="0"/>
6868
</Grid>
6969
<Grid Padding="25, 25, 25, 0" >
7070
<Grid.ColumnDefinitions>

UpdateInfo.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Update>
3+
<Item version="1.0.4" link="https://github.com/nkasco/IT-Admin-Toolkit-WinUI/releases/download/v1.0.4/ITATKWinUI.zip" />
4+
</Update>

Updater.ps1

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
########################################################
2+
# IT Admin Toolkit Auto Updater #
3+
# Date: 3/16/2022 #
4+
# Written by: Nathan Kasco #
5+
########################################################
6+
7+
param(
8+
[string]
9+
$InstallPath,
10+
11+
[string]
12+
$DownloadURL
13+
)
14+
15+
#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.
16+
17+
if(!($InstallPath) -or !($DownloadURL)){
18+
Exit 1 #TODO: Make this more meaningful
19+
}
20+
21+
try{
22+
Invoke-WebRequest -Uri $DownloadURL -OutFile "$Env:Temp\ITATKLatest.zip" -UseBasicParsing -ErrorAction Stop
23+
} catch {
24+
Write-Host "Error downloading update $_"
25+
exit 1 #TODO: Make this more meaningful
26+
}
27+
28+
Start-Sleep -Seconds 2
29+
30+
#Extract the update
31+
try{
32+
$TempZipPath = "$Env:Temp\ITATKLatest.zip"
33+
$TempPath = "$Env:Temp\ITATKLatest"
34+
if(Test-Path $TempZipPath){
35+
Expand-Archive -Path $TempZipPath -DestinationPath $TempPath -ErrorAction Stop
36+
} else {
37+
Exit 1 #TODO: Make this more meaningful
38+
}
39+
} catch {
40+
#TODO: Handle errors
41+
}
42+
43+
#Don't start until the main UI is closed to prevent any file in use errors
44+
do{
45+
$ProcessCheck = $null
46+
$ProcessCheck = Get-Process ITATKWinUI -ErrorAction SilentlyContinue
47+
if($ProcessCheck){
48+
$ProcessCheck | Stop-Process -Force -ErrorAction SilentlyContinue
49+
}
50+
start-sleep -seconds 3
51+
} while ($ProcessCheck)
52+
53+
if(Test-Path $TempPath){
54+
#Compare the current version to the new version
55+
$TempFiles = Get-ChildItem -Path $TempPath
56+
$InstallFiles = Get-ChildItem -Path $InstallPath
57+
58+
#Delete current files
59+
foreach($TempFile in $InstallFiles){
60+
if($TempFile.Name -notin "Settings.xml","XML","Scripts","Updater.ps1"){
61+
Remove-Item -Path $TempFile.FullName -Force -Recurse
62+
}
63+
}
64+
65+
#Move the files into place, ensure we skip user files
66+
foreach($TempFile in $TempFiles){
67+
if($TempFile.Name -notin "Settings.xml","XML","Scripts","Updater.ps1"){
68+
Move-Item -Path $TempFile.FullName -Destination $InstallPath -Force
69+
}
70+
}
71+
72+
#Optimization
73+
<#
74+
$CurrentFiles = Get-ChildItem -Path "$InstallPath" -Recurse
75+
$Differences = Compare-Object -ReferenceObject $CurrentFiles -DifferenceObject $TempFiles
76+
foreach($Difference in $Differences){
77+
try{
78+
Move-Item -Path $Difference -Destination $InstallPath -Force -ErrorAction Stop
79+
} catch {
80+
#TODO: Handle errors
81+
}
82+
}#>
83+
84+
#Cleanup
85+
Remove-Item -Path "$Env:Temp\ITATKLatest.zip" -Force -ErrorAction Stop
86+
Remove-Item -Path $TempPath -Recurse -Force -ErrorAction Stop
87+
88+
Start-Process -FilePath "$InstallPath\ITATKWinUI.exe"
89+
} else {
90+
Exit 1 #TODO: Make this more meaningful
91+
}

0 commit comments

Comments
 (0)