From afe7d6213a38a25535a8dc736c2d710ca1f1027d Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Thu, 20 Nov 2025 17:09:43 +0900 Subject: [PATCH] Refactor deploy time recording and improve instance setup Moved RecordFirstDeployTime from MainForm to Program for better separation of concerns. Updated instance directory creation and asset copying logic in Program.cs to support JS entrypoints, including library copying and first deploy time recording. Added a sleep call to helloworld.js for demonstration purposes. --- .../WelsonJS.Launcher/MainForm.cs | 36 +------ WelsonJS.Toolkit/WelsonJS.Launcher/Program.cs | 99 +++++++++++++------ helloworld.js | 2 + 3 files changed, 71 insertions(+), 66 deletions(-) diff --git a/WelsonJS.Toolkit/WelsonJS.Launcher/MainForm.cs b/WelsonJS.Toolkit/WelsonJS.Launcher/MainForm.cs index 2f8d849..cc4cd79 100644 --- a/WelsonJS.Toolkit/WelsonJS.Launcher/MainForm.cs +++ b/WelsonJS.Toolkit/WelsonJS.Launcher/MainForm.cs @@ -168,7 +168,7 @@ namespace WelsonJS.Launcher ZipFile.ExtractToDirectory(_filePath, _workingDirectory); // record the first deploy time - RecordFirstDeployTime(_workingDirectory, _instanceId); + Program.RecordFirstDeployTime(_workingDirectory, _instanceId); // follow the sub-directory _workingDirectory = Program.GetWorkingDirectory(_instanceId, true); @@ -207,40 +207,6 @@ namespace WelsonJS.Launcher return Program._resourceServer.IsRunning(); } - private void RecordFirstDeployTime(string directory, string instanceId) - { - // get current time - DateTime now = DateTime.Now; - - // record to the metadata database - InstancesForm instancesForm = new InstancesForm(); - try - { - instancesForm.GetDatabaseInstance().Insert(new Dictionary - { - ["InstanceId"] = instanceId, - ["FirstDeployTime"] = now - }, out _); - } - catch (Exception ex) - { - _logger.Error($"Failed to record first deploy time: {ex.Message}"); - } - instancesForm.Dispose(); - - // record to the instance directory - try - { - string filePath = Path.Combine(directory, ".welsonjs_first_deploy_time"); - string text = now.ToString(_dateTimeFormat); - File.WriteAllText(filePath, text); - } - catch (Exception ex) - { - _logger.Error($"Failed to record first deploy time: {ex.Message}"); - } - } - private bool IsInAdministrator() { try diff --git a/WelsonJS.Toolkit/WelsonJS.Launcher/Program.cs b/WelsonJS.Toolkit/WelsonJS.Launcher/Program.cs index d143460..40eb3f8 100644 --- a/WelsonJS.Toolkit/WelsonJS.Launcher/Program.cs +++ b/WelsonJS.Toolkit/WelsonJS.Launcher/Program.cs @@ -4,12 +4,13 @@ // https://github.com/gnh1201/welsonjs // using System; +using System.Collections.Generic; +using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; using System.Windows.Forms; -using System.Configuration; namespace WelsonJS.Launcher { @@ -19,9 +20,13 @@ namespace WelsonJS.Launcher public static Mutex _mutex; public static ResourceServer _resourceServer; + public static string _dateTimeFormat; static Program() { + // get the date time format + _dateTimeFormat = GetAppConfig("DateTimeFormat"); + // set up logger _logger = new TraceLogger(); @@ -79,6 +84,40 @@ namespace WelsonJS.Launcher _mutex.Dispose(); } + public static void RecordFirstDeployTime(string directory, string instanceId) + { + // get current time + DateTime now = DateTime.Now; + + // record to the metadata database + InstancesForm instancesForm = new InstancesForm(); + try + { + instancesForm.GetDatabaseInstance().Insert(new Dictionary + { + ["InstanceId"] = instanceId, + ["FirstDeployTime"] = now + }, out _); + } + catch (Exception ex) + { + _logger.Error($"Failed to record first deploy time: {ex.Message}"); + } + instancesForm.Dispose(); + + // record to the instance directory + try + { + string filePath = Path.Combine(directory, ".welsonjs_first_deploy_time"); + string text = now.ToString(_dateTimeFormat); + File.WriteAllText(filePath, text); + } + catch (Exception ex) + { + _logger.Error($"Failed to record first deploy time: {ex.Message}"); + } + } + private static string GetTargetFilePath(string[] args) { if (args == null || args.Length == 0) return null; @@ -115,15 +154,13 @@ namespace WelsonJS.Launcher if (fileExtension.Equals(".zip", StringComparison.OrdinalIgnoreCase)) { - var mainForm = new MainForm(_logger); - mainForm.Show(); - mainForm.RunFromZipFile(filePath); - return; + throw new NotImplementedException("Not implemented yet."); } if (fileExtension.Equals(".js", StringComparison.OrdinalIgnoreCase)) { - string workingDirectory = CreateInstanceDirectory(); + string instanceId = Guid.NewGuid().ToString(); + string workingDirectory = CreateInstanceDirectory(instanceId); string appRoot = GetAppRootDirectory(); string appBaseSource = Path.Combine(appRoot, "app.js"); @@ -139,9 +176,15 @@ namespace WelsonJS.Launcher string assetsDestination = Path.Combine(workingDirectory, "app", "assets", "js"); CopyDirectoryRecursive(assetsSource, assetsDestination); + string libSource = Path.Combine(appRoot, "lib"); + string libDestination = Path.Combine(workingDirectory, "lib"); + CopyDirectoryRecursive(libSource, libDestination); + string entrypointDestination = Path.Combine(workingDirectory, "bootstrap.js"); File.Copy(filePath, entrypointDestination, overwrite: true); + RecordFirstDeployTime(workingDirectory, instanceId); + RunCommandPrompt( workingDirectory: workingDirectory, entryFileName: "app.js", @@ -178,9 +221,8 @@ namespace WelsonJS.Launcher throw new FileNotFoundException("Could not locate app.js in any known application root directory."); } - private static string CreateInstanceDirectory() + private static string CreateInstanceDirectory(string instanceId) { - string instanceId = Guid.NewGuid().ToString(); string workingDirectory = GetWorkingDirectory(instanceId); try @@ -193,35 +235,30 @@ namespace WelsonJS.Launcher Directory.CreateDirectory(workingDirectory); } - catch (IOException ex) + catch { - throw new Exception("Instance Initialization failed due to an IO error.", ex); - } - catch (UnauthorizedAccessException ex) - { - throw new Exception("Instance Initialization failed due to insufficient permissions.", ex); - } - catch (InvalidOperationException) - { - // Let InvalidOperationException bubble up as it is thrown intentionally above - throw; + throw new Exception("Instance Initialization failed"); } + return workingDirectory; + } - var sourceDirInfo = new DirectoryInfo(sourceDir); + private static void CopyDirectoryRecursive(string sourceDir, string destDir) + { + if (!Directory.Exists(sourceDir)) + { + throw new DirectoryNotFoundException("Source directory not found: " + sourceDir); + } - // Create all subdirectories - foreach (DirectoryInfo dir in sourceDirInfo.GetDirectories("*", SearchOption.AllDirectories)) - { - Directory.CreateDirectory(Path.Combine(destDir, dir.FullName.Substring(sourceDirInfo.FullName.Length + 1))); - } + Directory.CreateDirectory(destDir); + + foreach (var file in Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories)) + { + string relativePath = file.Substring(sourceDir.Length).TrimStart( + Path.DirectorySeparatorChar, + Path.AltDirectorySeparatorChar + ); - // Copy all files - foreach (FileInfo file in sourceDirInfo.GetFiles("*", SearchOption.AllDirectories)) - { - string targetPath = Path.Combine(destDir, file.FullName.Substring(sourceDirInfo.FullName.Length + 1)); - file.CopyTo(targetPath, true); - } string targetPath = Path.Combine(destDir, relativePath); string targetDir = Path.GetDirectoryName(targetPath); if (!Directory.Exists(targetDir)) diff --git a/helloworld.js b/helloworld.js index 4829b83..ecd65a0 100644 --- a/helloworld.js +++ b/helloworld.js @@ -14,6 +14,8 @@ function main(args) { } catch (e) { console.error("lib/http: Something wrong"); } + + sleep(100000); //Toolkit.create(); }