Skip to content

Commit

Permalink
Implement Install & Update CLI Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Drawbackz committed Mar 24, 2024
1 parent 86c67f0 commit 84bca45
Show file tree
Hide file tree
Showing 20 changed files with 311 additions and 65 deletions.
7 changes: 3 additions & 4 deletions Devcon Installer/App.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
</configuration>
</configuration>
3 changes: 1 addition & 2 deletions Devcon Installer/App.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Application x:Class="Devcon_Installer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Devcon_Installer"
StartupUri="Views/MainWindow.xaml">
xmlns:local="clr-namespace:Devcon_Installer">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
161 changes: 156 additions & 5 deletions Devcon Installer/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,162 @@
using System.Windows;
using devcon_installer;
using devcon_installer.Downloads;
using devcon_installer.Downloads.Base;
using devcon_installer.Utilities;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Devcon_Installer
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>

class DevConDownloadMatch
{
public DevconDownload Download { get; set; }
public SystemArchitecture Architecture { get; set; }

public DevConDownloadMatch(DevconDownload download, SystemArchitecture architecture)
{
Download = download;
Architecture = architecture;
}
}

public partial class App : Application
{
protected async override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
string[] args = e.Args;
if (args.Length > 0)
{

try
{
DevconInstaller installer = new DevconInstaller();
ArgumentProcessor argumentProcessor = new ArgumentProcessor();

installer.OnLog += (log) =>
{
Console.WriteLine(log.Message);
};

DevConDownloadMatch getHashMatch(string hash)
{
DevconDownload[] sources = DevconSources.ReadSaveFile();
if(sources == null) { return null; }

var matchingDownloads = sources.Where(download => download.Sources.Any(source => source.Sha256 == hash));
if (matchingDownloads.Any())
{
DevconDownload download = matchingDownloads.First();
DevconSource source = download.Sources.FirstOrDefault(s => s.Sha256 == hash);
return new DevConDownloadMatch(download, source.Architecture);
}
else
{
return null;
}
}

async Task updateSources()
{
TaskCompletionSource<bool> updateComplete = new TaskCompletionSource<bool>();
installer.OnSourcesUpdated += () =>
{
updateComplete.TrySetResult(true);
};
installer.UpdateSources();
await updateComplete.Task;
}

async Task install()
{
TaskCompletionSource<bool> installTask = new TaskCompletionSource<bool>();

bool update = false;

installer.OnCompleted += (success) =>
{
installTask.TrySetResult(success);
};

string downloadHash = null;
argumentProcessor.AddArgument("-hash", (value) =>
{
downloadHash = value.ToUpper();
}, true);

argumentProcessor.AddArgument("-update", (value) =>
{
update = true;
});

argumentProcessor.AddArgument("-dir", (value) =>
{
var installDirectory = value;
if (!Directory.Exists(installDirectory))
{
throw new Exception($"Install Directory Does Not Exist\r\n{installDirectory}");
}
installer.InstallationDirectory = installDirectory;
installer.DownloadPath = installDirectory;
});

argumentProcessor.AddArgument("-addpath", (value) =>
{
if (!DevconInstaller.IsAdministrator())
{
throw new Exception("Add Path Requires Administrator Access");
}
installer.AddEnvironmentPath = true;
});

argumentProcessor.ProcessArguments(args.Skip(1).ToArray());
DevConDownloadMatch match = getHashMatch(downloadHash);
if(match == null && update)
{
FileInfo sourcesFileInfo = new FileInfo($"{Environment.CurrentDirectory}\\devcon_sources.json");
if (!sourcesFileInfo.Exists || DateTime.Now - sourcesFileInfo.LastWriteTime >= TimeSpan.FromHours(12))
{
await updateSources();
}
match = getHashMatch(downloadHash);
}
if(match == null)
{
throw new Exception($"Unable To Find Dowload {downloadHash}");
}
installer.Install(match.Download, match.Architecture);
await installTask.Task;
}


switch (args[0])
{
case "update":
await updateSources();
break;
case "install":
await install();
break;
default:
throw new ArgumentException("Unknown Command");
}
Current.Shutdown();
}
catch (Exception ex)
{
Console.WriteLine($"ERROR: {ex.Message}");
Current.Shutdown();
}

}
else
{
StartupUri = new Uri("/Views/MainWindow.xaml", UriKind.Relative);
}
}
}
}
}
5 changes: 4 additions & 1 deletion Devcon Installer/Devcon Installer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>Devcon_Installer</RootNamespace>
<AssemblyName>Devcon Installer</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
Expand Down Expand Up @@ -41,6 +41,9 @@
<PropertyGroup>
<ApplicationIcon>Images\Logo\icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="Costura, Version=1.6.2.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>..\packages\Costura.Fody.1.6.2\lib\dotnet\Costura.dll</HintPath>
Expand Down
8 changes: 4 additions & 4 deletions Devcon Installer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]


Expand Down
2 changes: 1 addition & 1 deletion Devcon Installer/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Devcon Installer/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Devcon Installer/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using devcon_installer;
using devcon_installer.Downloads;
using devcon_installer.Logging;
using Devcon_Installer.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Forms;
using devcon_installer;
using devcon_installer.Downloads;
using devcon_installer.Logging;
using Devcon_Installer.ViewModels.Base;

namespace Devcon_Installer.ViewModels
{
Expand Down Expand Up @@ -108,7 +108,7 @@ private void UpdateAvailableDownloads()
{
SelectedDevconDownload = null;

var sources = DevconSources.ReadSaveFile();
var sources = DevconSources.ReadSaveFile(true);
if (sources == null)
{
var dt = DateTime.Now.ToLongTimeString();
Expand All @@ -121,7 +121,7 @@ private void UpdateAvailableDownloads()
{
AvailableDownloads = new ObservableCollection<DevconDownload>(sources);
}
if(AvailableDownloads.Count > 0)
if (AvailableDownloads.Count > 0)
SelectedDevconDownload = AvailableDownloads[0];
}

Expand Down
6 changes: 3 additions & 3 deletions Devcon Installer/ViewModels/WindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Devcon_Installer.ViewModels.Base;
using Devcon_Installer.Views;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Devcon_Installer.ViewModels.Base;
using Devcon_Installer.Views;

namespace Devcon_Installer.ViewModels
{
Expand Down
4 changes: 2 additions & 2 deletions Devcon Installer/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Windows.Controls;
using Devcon_Installer.ViewModels;
using Devcon_Installer.ViewModels;
using System.Windows.Controls;

namespace Devcon_Installer.Views
{
Expand Down
4 changes: 2 additions & 2 deletions Devcon Installer/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Windows;
using Devcon_Installer.ViewModels;
using Devcon_Installer.ViewModels;
using System.Windows;

namespace Devcon_Installer.Views
{
Expand Down
4 changes: 2 additions & 2 deletions Devcon Installer/WindowResizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
else
handled = false;

return (IntPtr) 0;
return (IntPtr)0;
}

#endregion
Expand All @@ -110,7 +110,7 @@ private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)

var lCurrentScreen = MonitorFromPoint(lMousePosition, MonitorOptions.MONITOR_DEFAULTTONEAREST);

var lMmi = (MINMAXINFO) Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
var lMmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

if (lPrimaryScreen.Equals(lCurrentScreen))
{
Expand Down
1 change: 0 additions & 1 deletion Devcon Installer/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="Costura.Fody" version="1.6.2" targetFramework="net461" developmentDependency="true" />
<package id="Fody" version="2.0.0" targetFramework="net461" developmentDependency="true" />
Expand Down
Loading

0 comments on commit 84bca45

Please sign in to comment.