diff --git a/code/AddToolDialog.cs b/code/AddToolDialog.cs index 96caf30..ec57a80 100644 --- a/code/AddToolDialog.cs +++ b/code/AddToolDialog.cs @@ -1,5 +1,4 @@ using Sandbox; -using System.Diagnostics; using System.Threading.Tasks; namespace Tools; @@ -105,7 +104,6 @@ private async Task DownloadTool() Window.Close(); using var progress = Progress.Start( "Downloading Source For " + repo.Name ); - var cancelToken = Progress.GetCancel(); Progress.Update( "Creating Folder", 5, 100 ); await Task.Delay( 50 ); @@ -114,16 +112,7 @@ private async Task DownloadTool() Progress.Update( "Checking Out", 20, 100 ); await Task.Delay( 50 ); - ProcessStartInfo info = new( "git", $"clone --depth 1 -b \"{release.TagName}\" \"{repo.CloneUrl}\" ." ); - info.UseShellExecute = false; - info.CreateNoWindow = false; - info.WorkingDirectory = folder; - - var process = new Process(); - process.StartInfo = info; - process.Start(); - - await process.WaitForExitAsync( cancelToken ); + await GitUtils.Git( $"clone -b \"{release.TagName}\" \"{repo.CloneUrl}\" .", folder ); Progress.Update( "Adding Addon", 90, 100 ); await Task.Delay( 50 ); diff --git a/code/Page.cs b/code/Page.cs index 983d30d..f7accc7 100644 --- a/code/Page.cs +++ b/code/Page.cs @@ -1,5 +1,4 @@ using Sandbox; -using System.Diagnostics; using System.IO; namespace Tools; @@ -63,8 +62,10 @@ private void AddManifestWidgets() Layout.AddSpacingCell( 8f ); - var desc = Layout.Add( new Label( $"{Manifest.Description}" ) ); - desc.WordWrap = true; + Layout.Add( new Label.Body( $"{Manifest.Description}" ) ); + + Layout.Add( new Subheading( $"{Manifest.ReleaseName}" ) ); + Layout.Add( new Label.Body( $"{Manifest.ReleaseDescription}" ) ); Layout.AddStretchCell(); @@ -81,13 +82,13 @@ private void AddManifestWidgets() group.Layout.AddSpacingCell( 8f ); - group.Layout.Add( new Button( "Download Update", "download" ) { Clicked = Update } ); + group.Layout.Add( new Button( "Download Update", "download" ) { Clicked = DownloadUpdate } ); } } - private void Update() + private void DownloadUpdate() { - GithubApi.FetchLatestRelease( $"{Manifest.Repo}" ).ContinueWith( t => + GithubApi.FetchLatestRelease( $"{Manifest.Repo}" ).ContinueWith( async t => { var release = t.Result ?? default; @@ -95,14 +96,19 @@ private void Update() Log.Trace( folder ); - ProcessStartInfo info = new( "git", $"checkout -B \"{release.TagName}\" --force" ); - info.UseShellExecute = false; - info.CreateNoWindow = false; - info.WorkingDirectory = folder; + await GitUtils.Git( $"reset --hard HEAD" ); + await GitUtils.Git( $"pull" ); + await GitUtils.Git( $"checkout \"{release.TagName}\" --force", folder ); + + // + // Update Manifest + // + Manifest.ReleaseVersion = release.TagName; + Manifest.ReleaseName = release.Name; + Manifest.ReleaseDescription = release.Body; - var process = new Process(); - process.StartInfo = info; - process.Start(); + var manifestPath = Path.Combine( folder, "tm-manifest.json" ); + File.WriteAllText( manifestPath, Manifest.ToJson() ); } ); } diff --git a/code/ToolsManager.cs b/code/ToolsManager.cs index 3b44375..d5b560a 100644 --- a/code/ToolsManager.cs +++ b/code/ToolsManager.cs @@ -120,6 +120,8 @@ private bool PaintPageOption( NavigationView.Option option, Manifest manifest ) return true; } + private static bool HasCheckedForUpdates = false; + // // I'm going to use this event because it's a decent one // that runs when the editor starts plus infrequently @@ -128,10 +130,15 @@ private bool PaintPageOption( NavigationView.Option option, Manifest manifest ) [Sandbox.Event( "tools.compilemgr.start" )] public static void OnCompileMgrStart() { + if ( HasCheckedForUpdates ) + return; + var count = CheckForUpdates().Result; if ( count > 0 ) ToolUpdateNotice.Open( count ); + + HasCheckedForUpdates = true; } private static async Task CheckForUpdates() diff --git a/code/Utils/GitUtils.cs b/code/Utils/GitUtils.cs new file mode 100644 index 0000000..05d4174 --- /dev/null +++ b/code/Utils/GitUtils.cs @@ -0,0 +1,25 @@ +using System.Diagnostics; +using System.Threading.Tasks; + +namespace Tools; + +public static class GitUtils +{ + public static async Task Git( string command, string workingDir = null ) + { + Log.Trace( $"git {command}" ); + + ProcessStartInfo info = new( "git", command ); + info.UseShellExecute = false; + info.CreateNoWindow = false; + + if ( workingDir != null ) + info.WorkingDirectory = workingDir; + + var process = new Process(); + process.StartInfo = info; + process.Start(); + + await process.WaitForExitAsync(); + } +}