From 655ba015a07c87e7b1a24ce8cafbe171103acad1 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 8 Oct 2022 23:47:27 +0800 Subject: [PATCH] WIP --- extra/exe-builder/DownloadForm.cs | 127 ++++++++++++++++++++++++++-- extra/exe-builder/Program.cs | 2 +- extra/exe-builder/UptimeKuma.csproj | 3 +- 3 files changed, 121 insertions(+), 11 deletions(-) diff --git a/extra/exe-builder/DownloadForm.cs b/extra/exe-builder/DownloadForm.cs index 9c740e312..5bb88b6d3 100644 --- a/extra/exe-builder/DownloadForm.cs +++ b/extra/exe-builder/DownloadForm.cs @@ -1,14 +1,19 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.IO; +using System.IO.Compression; using System.Net; +using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; namespace UptimeKuma { public partial class DownloadForm : Form { private readonly Queue downloadQueue = new(); private readonly WebClient webClient = new(); + private DownloadItem currentDownloadItem; public DownloadForm() { InitializeComponent(); @@ -18,17 +23,19 @@ namespace UptimeKuma { webClient.DownloadProgressChanged += DownloadProgressChanged; webClient.DownloadFileCompleted += DownloadFileCompleted; - if (!File.Exists("node")) { + if (!Directory.Exists("node")) { downloadQueue.Enqueue(new DownloadItem { URL = "https://nodejs.org/dist/v16.17.1/node-v16.17.1-win-x64.zip", - Filename = "node.zip" + Filename = "node.zip", + TargetFolder = "node" }); } - if (!File.Exists("node")) { + if (!Directory.Exists("node")) { downloadQueue.Enqueue(new DownloadItem { URL = "https://github.com/louislam/uptime-kuma/archive/refs/tags/1.18.3.zip", - Filename = "core.zip" + Filename = "core.zip", + TargetFolder = "core" }); } @@ -38,28 +45,130 @@ namespace UptimeKuma { void DownloadNextFile() { if (downloadQueue.Count > 0) { var item = downloadQueue.Dequeue(); - label.Text = item.URL; - webClient.DownloadFileAsync(new Uri(item.URL), item.Filename); + + currentDownloadItem = item; + + // Download if the zip file is not existing + if (!File.Exists(item.Filename)) { + label.Text = item.URL; + webClient.DownloadFileAsync(new Uri(item.URL), item.Filename); + } else { + progressBar.Value = 100; + label.Text = "Use local " + item.Filename; + DownloadFileCompleted(null, null); + } } else { - // TODO: Finished, extract? + npmSetup(); } } + void npmSetup() { + if (Directory.Exists("core/node_modules")) { + // Application.Restart(); + } + + label.Text = "npm run setup"; + progressBar.Value = 50; + labelData.Text = ""; + + var startInfo = new ProcessStartInfo { + FileName = "cmd.exe", + Arguments = "run setup", + RedirectStandardOutput = false, + RedirectStandardError = false, + RedirectStandardInput = true, + UseShellExecute = false, + CreateNoWindow = false, + WorkingDirectory = "core" + }; + + var process = new Process(); + process.StartInfo = startInfo; + process.EnableRaisingEvents = true; + process.Exited += (object _, EventArgs e) => { + // Application.Restart(); + progressBar.Value = 100; + + if (process.ExitCode == 0) { + label.Text = "Done"; + } else { + label.Text = "Failed, exit code: " + process.ExitCode; + } + + }; + process.Start(); + process.StandardInput.WriteLine("\"../node/npm\" run setup"); + } + void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { progressBar.Value = e.ProgressPercentage; var total = e.TotalBytesToReceive / 1024; var current = e.BytesReceived / 1024; - labelData.Text = $"{current}KB/{total}KB"; + + if (total > 0) { + labelData.Text = $"{current}KB/{total}KB"; + } } - void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { + async void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { + Extract(currentDownloadItem); DownloadNextFile(); } + + void Extract(DownloadItem item) { + if (Directory.Exists(item.TargetFolder)) { + var dir = new DirectoryInfo(item.TargetFolder); + dir.Delete(true); + } + + if (Directory.Exists("temp")) { + var dir = new DirectoryInfo("temp"); + dir.Delete(true); + } + + labelData.Text = $"Extracting {item.Filename}..."; + + ZipFile.ExtractToDirectory(item.Filename, "temp"); + + string[] dirList; + + // Move to the correct level + dirList = Directory.GetDirectories("temp"); + + + + if (dirList.Length > 0) { + var dir = dirList[0]; + + // As sometime ExtractToDirectory is still locking the directory, loop until ok + while (true) { + try { + Directory.Move(dir, item.TargetFolder); + break; + } catch (Exception exception) { + Thread.Sleep(1000); + } + } + + } else { + MessageBox.Show("Unexcepted Error: Cannot move extracted files, folder not found."); + } + + labelData.Text = $"Extracted"; + + if (Directory.Exists("temp")) { + var dir = new DirectoryInfo("temp"); + dir.Delete(true); + } + + File.Delete(item.Filename); + } } public class DownloadItem { public string URL { get; set; } public string Filename { get; set; } + public string TargetFolder { get; set; } } } diff --git a/extra/exe-builder/Program.cs b/extra/exe-builder/Program.cs index 84aa6e456..1b78f0385 100644 --- a/extra/exe-builder/Program.cs +++ b/extra/exe-builder/Program.cs @@ -45,7 +45,7 @@ namespace UptimeKuma { trayIcon.MouseDoubleClick += new MouseEventHandler(Open); trayIcon.Visible = true; - if (File.Exists("core") && File.Exists("node")) { + if (Directory.Exists("core") && Directory.Exists("node") && Directory.Exists("core/node_modules")) { // Go go go StartProcess(); } else { diff --git a/extra/exe-builder/UptimeKuma.csproj b/extra/exe-builder/UptimeKuma.csproj index aa0a8bf85..2ad857b2c 100644 --- a/extra/exe-builder/UptimeKuma.csproj +++ b/extra/exe-builder/UptimeKuma.csproj @@ -35,11 +35,12 @@ 4 - COPY "$(SolutionDir)bin\Debug\uptime-kuma.exe" "C:\Users\LouisLam\Desktop\uptime-kuma-win64\" + COPY "$(SolutionDir)bin\Debug\uptime-kuma.exe" "%UserProfile%\Desktop\uptime-kuma-win64\" +