Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
xezno committed Nov 4, 2022
1 parent 7a38fd6 commit 7e3c259
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 124 deletions.
19 changes: 4 additions & 15 deletions code/AddToolDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private async Task DownloadTool()

foreach ( var target in targets )
{
if ( target is not Item repo )
if ( target is not Repository repo )
return;

Log.Trace( $"Downloading {repo.Name}" );
Expand Down Expand Up @@ -117,19 +117,8 @@ private async Task DownloadTool()
Progress.Update( "Adding Addon", 90, 100 );
await Task.Delay( 50 );

var manifestPath = System.IO.Path.Combine( folder, "tm-manifest.json" );

// Create tools manifest
var manifest = new Manifest();
manifest.ReleaseName = release.Name;
manifest.ReleaseVersion = release.TagName;
manifest.ReleaseDescription = release.Body;

manifest.Repo = repo.FullName;
manifest.Description = repo.Description;
manifest.AutoUpdate = true;

System.IO.File.WriteAllText( manifestPath, manifest.ToJson() );
var manifest = new Manifest( release, repo );
manifest.WriteToFolder( folder );

var configPath = System.IO.Path.Combine( folder, ".addon" );
Utility.Projects.TryAddFromFile( configPath );
Expand All @@ -141,7 +130,7 @@ private void PaintAddonItem( VirtualWidget v )
var rect = v.Rect;
rect = rect.Shrink( 16, 0, 8, 0 );

if ( v.Object is not Item repo )
if ( v.Object is not Repository repo )
return;

Paint.Antialiasing = true;
Expand Down
4 changes: 2 additions & 2 deletions code/GithubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static async Task<string> FetchText( string url )
}

/// <summary>
/// standard search query format
/// Perform a GitHub repository search, see https://docs.github.com/en/rest/search#search-repositories for more info
/// </summary>
public static async Task<Search> FetchSearch( string searchQuery )
{
Expand All @@ -85,7 +85,7 @@ public static async Task<Search> FetchSearch( string searchQuery )
}

/// <summary>
/// in format "author/repo"
/// Get the latest release for a repo, see https://docs.github.com/en/rest/releases/releases#get-the-latest-release for more info
/// </summary>
public static async Task<Release> FetchLatestRelease( string repository )
{
Expand Down
4 changes: 2 additions & 2 deletions code/Header.cs → code/ToolInfoHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// but this isn't ideal.. could do with our own proper widget for stuff
// like this
//
internal class Header : Widget
internal class ToolInfoHeader : Widget
{
const float HeaderHeight = 64 + 8;
private string Title;

public Header( string title, Widget parent = null, bool isDarkWindow = false ) : base( parent, isDarkWindow )
public ToolInfoHeader( string title, Widget parent = null, bool isDarkWindow = false ) : base( parent, isDarkWindow )
{
Title = title;
Height = HeaderHeight;
Expand Down
66 changes: 26 additions & 40 deletions code/Page.cs → code/ToolInfoPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Tools;

internal class Page : Widget
internal class ToolInfoPage : Widget
{
private Header Header;
private ToolInfoHeader Header;
private ToolBar ToolBar;

private Label LatestReleaseName;
Expand All @@ -14,61 +14,53 @@ internal class Page : Widget
private Manifest Manifest;
private LocalProject Project;

private bool HasFetched = false;
private bool HasFetched;

public Page( LocalProject project, Widget parent = null, bool isDarkWindow = false ) : base( parent, isDarkWindow )
public ToolInfoPage( LocalProject project, Widget parent = null, bool isDarkWindow = false ) : base( parent, isDarkWindow )
{
SetLayout( LayoutMode.TopToBottom );

this.Project = project;

Layout.Spacing = 8;
Layout.Margin = 24;

//
// Check if we have a tools manager manifest
//
Project = project;
Manifest = project.GetManifest();

if ( Manifest != null )
{
AddManifestWidgets();
}
else
{
// Display sad face
AddNoManifestWidgets();
}
}

/// <summary>
/// Displays information about this tool, along with release info
/// </summary>
private void AddManifestWidgets()
{
var config = Project.Config;
Header = Layout.Add( new Header( config.Title ) );
Layout.AddSpacingCell( 8 );

{
ToolBar = new ToolBar( this );
ToolBar.SetIconSize( 16 );
// Basic repo info
Header = Layout.Add( new ToolInfoHeader( config.Title ) );
Layout.AddSpacingCell( 8 );

// var autoUpdateOption = new Option( "Toggle Auto-Updates", Manifest.AutoUpdate ? "file_download" : "file_download_off" );
// autoUpdateOption.Triggered = () => ToggleAutoUpdates( autoUpdateOption );
// var option = ToolBar.AddOption( autoUpdateOption );
ToolBar = new ToolBar( this );
ToolBar.SetIconSize( 16 );

ToolBar.AddOption( "Open in Explorer", "folder", () => Utility.OpenFolder( Path.GetDirectoryName( Project.GetRootPath() ) ) );
ToolBar.AddOption( "Open on GitHub", "open_in_new", () => Utility.OpenFolder( $"https://github.com/{Manifest.Repo}" ) );
Layout.Add( ToolBar );
}
ToolBar.AddOption( "Open in Explorer", "folder", () => Utility.OpenFolder( Path.GetDirectoryName( Project.GetRootPath() ) ) );
ToolBar.AddOption( "Open on GitHub", "open_in_new", () => Utility.OpenFolder( $"https://github.com/{Manifest.Repo}" ) );
Layout.Add( ToolBar );

Layout.AddSpacingCell( 8f );

Layout.Add( new Label.Body( $"{Manifest.Description}" ) );

// Installed release info
Layout.Add( new Subheading( $"{Manifest.ReleaseName}" ) );
Layout.Add( new Label.Body( $"{Manifest.ReleaseDescription}" ) );

Layout.AddStretchCell();

// Update info (if available)
if ( Manifest.CheckUpdateAvailable() )
{
var group = new Container( this );
Expand All @@ -86,6 +78,9 @@ private void AddManifestWidgets()
}
}

/// <summary>
/// Downloads the latest update for this page's project using the manifest
/// </summary>
private void DownloadUpdate()
{
GithubApi.FetchLatestRelease( $"{Manifest.Repo}" ).ContinueWith( async t =>
Expand All @@ -100,24 +95,15 @@ private void DownloadUpdate()
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 manifestPath = Path.Combine( folder, "tm-manifest.json" );
File.WriteAllText( manifestPath, Manifest.ToJson() );
Manifest.SetRelease( release );
Manifest.WriteToFolder( folder );
} );
}

private void ToggleAutoUpdates( Option option )
{
Manifest.AutoUpdate = !Manifest.AutoUpdate;
option.Icon = Manifest.AutoUpdate ? "file_download" : "file_download_off";
}

/// <summary>
/// Displays text telling the user to add the tool properly
/// </summary>
private void AddNoManifestWidgets()
{
Layout.Add( new Label.Title( "😔" ) ).SetStyles( "font-size: 64px;" );
Expand Down
20 changes: 10 additions & 10 deletions code/ToolsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ public ToolsManager()
Show();
}

/// <summary>
/// Tools Manager needs to be able to manage itself, so we create a 'dummy' manifest just
/// in case one wasn't packaged with the GitHub release.
/// </summary>
private void WriteDummyManifest()
{
// If we don't have our own manifest, then we need to create
// one and (later) force an update.

var project = Utility.Projects.GetAll().First( x => x.Config.Ident == "tools_manager" );
var folder = project.GetRootPath();

var manifestPath = System.IO.Path.Combine( folder, "tm-manifest.json" );
var manifestPath = Path.Combine( folder, "tm-manifest.json" );

if ( File.Exists( manifestPath ) )
return;

// Create tools manifest
var manifest = new Manifest();
manifest.ReleaseName = "None";
manifest.ReleaseVersion = "0";
Expand All @@ -43,7 +43,7 @@ private void WriteDummyManifest()
manifest.Description = "Manages your tools.";
manifest.AutoUpdate = true;

System.IO.File.WriteAllText( manifestPath, manifest.ToJson() );
manifest.WriteToFile( manifestPath );
}

public void CreateUI()
Expand All @@ -59,7 +59,7 @@ public void CreateUI()

if ( config.PackageType == Sandbox.Package.Type.Tool )
{
var option = toolsList.AddPage( config.Title, "hardware", new Page( project ) );
var option = toolsList.AddPage( config.Title, "hardware", new ToolInfoPage( project ) );
var manifest = project.GetManifest();

option.OnPaintOverride = () => PaintPageOption( option, manifest );
Expand All @@ -71,9 +71,6 @@ public void CreateUI()

var add = footer.Add( new Button.Primary( "Add Tool...", "add" ), 1 );
add.Clicked = () => AddToolDialog.Open();

var update = footer.Add( new Button( null, "download" ) );
update.Clicked = () => ToolUpdateNotice.Open( 4 );
}

private bool PaintPageOption( NavigationView.Option option, Manifest manifest )
Expand Down Expand Up @@ -141,6 +138,9 @@ public static void OnCompileMgrStart()
HasCheckedForUpdates = true;
}

/// <summary>
/// Check every tools project for any updates (if it has a valid manifest)
/// </summary>
private static async Task<int> CheckForUpdates()
{
int count = 0;
Expand Down
4 changes: 2 additions & 2 deletions code/Types/Github/Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Text.Json.Serialization;

namespace Tools;
public class Item
public class Repository
{
[JsonPropertyName( "id" )]
public int Id { get; set; }
Expand Down Expand Up @@ -327,5 +327,5 @@ public class Search
public bool IncompleteResults { get; set; }

[JsonPropertyName( "items" )]
public List<Item> Items { get; set; }
public List<Repository> Items { get; set; }
}
44 changes: 42 additions & 2 deletions code/Types/Manifest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using System.Text.Json;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tools;

/// <summary>
/// Contains information about a GitHub repository and the currently downloaded
/// release, allowing these to be linked to (and contained within) a tools
/// project.
/// </summary>
public class Manifest
{
[JsonPropertyName( "repo" )]
Expand All @@ -21,7 +27,41 @@ public class Manifest
public string ReleaseDescription { get; set; }

[JsonPropertyName( "auto_update" )]
public bool AutoUpdate { get; set; }
public bool AutoUpdate { get; set; } = true;

public Manifest()
{
}

public Manifest( Release release, Repository repo )
{
SetRelease( release );
SetRepo( repo );
}

public void SetRelease( Release release )
{
ReleaseVersion = release.TagName;
ReleaseName = release.Name;
ReleaseDescription = release.Body;
}

public void SetRepo( Repository repo )
{
Repo = repo.FullName;
Description = repo.Description;
}

public void WriteToFile( string path )
{
File.WriteAllText( path, ToJson() );
}

public void WriteToFolder( string folder )
{
var path = Path.Combine( folder, "tm-manifest.json" );
WriteToFile( path );
}

public string ToJson()
{
Expand Down
3 changes: 3 additions & 0 deletions code/Utils/GitUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Tools;

public static class GitUtils
{
/// <summary>
/// Perform a git command and wait for it to finish
/// </summary>
public static async Task Git( string command, string workingDir = null )
{
Log.Trace( $"git {command}" );
Expand Down
6 changes: 6 additions & 0 deletions code/Utils/LocalProjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ namespace Tools;

public static class LocalProjectExtensions
{
/// <summary>
/// Get the Tools Manager manifest path for this project
/// </summary>
public static string GetManifestPath( this Sandbox.LocalProject localProject )
{
return Path.Combine( localProject.GetRootPath(), "tm-manifest.json" );
}

/// <summary>
/// Get the Tools Manager manifest for this project
/// </summary>
public static Manifest GetManifest( this Sandbox.LocalProject localProject )
{
if ( localProject.Config.PackageType != Sandbox.Package.Type.Tool )
Expand Down
Loading

0 comments on commit 7e3c259

Please sign in to comment.