Skip to content

Commit

Permalink
Finalize update downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
xezno committed Nov 4, 2022
1 parent e04967a commit 7a38fd6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
13 changes: 1 addition & 12 deletions code/AddToolDialog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Sandbox;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Tools;
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down
32 changes: 19 additions & 13 deletions code/Page.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Sandbox;
using System.Diagnostics;
using System.IO;

namespace Tools;
Expand Down Expand Up @@ -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();

Expand All @@ -81,28 +82,33 @@ 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;

var folder = Project.GetRootPath();

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() );
} );
}

Expand Down
7 changes: 7 additions & 0 deletions code/ToolsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<int> CheckForUpdates()
Expand Down
25 changes: 25 additions & 0 deletions code/Utils/GitUtils.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 7a38fd6

Please sign in to comment.