From 7e3c259cc764ea1619158cf5023c4f9dbf6b1ac7 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 4 Nov 2022 02:51:47 +0000 Subject: [PATCH] Clean up --- code/AddToolDialog.cs | 19 ++------ code/GithubApi.cs | 4 +- code/{Header.cs => ToolInfoHeader.cs} | 4 +- code/{Page.cs => ToolInfoPage.cs} | 66 +++++++++++---------------- code/ToolsManager.cs | 20 ++++---- code/Types/Github/Search.cs | 4 +- code/Types/Manifest.cs | 44 +++++++++++++++++- code/Utils/GitUtils.cs | 3 ++ code/Utils/LocalProjectExtensions.cs | 6 +++ code/Utils/Template.cs | 46 ------------------- code/Utils/WidgetExtensions.cs | 10 ++-- 11 files changed, 102 insertions(+), 124 deletions(-) rename code/{Header.cs => ToolInfoHeader.cs} (87%) rename code/{Page.cs => ToolInfoPage.cs} (65%) delete mode 100644 code/Utils/Template.cs diff --git a/code/AddToolDialog.cs b/code/AddToolDialog.cs index ec57a80..3745947 100644 --- a/code/AddToolDialog.cs +++ b/code/AddToolDialog.cs @@ -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}" ); @@ -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 ); @@ -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; diff --git a/code/GithubApi.cs b/code/GithubApi.cs index 3b27610..62fdc45 100644 --- a/code/GithubApi.cs +++ b/code/GithubApi.cs @@ -76,7 +76,7 @@ private static async Task FetchText( string url ) } /// - /// standard search query format + /// Perform a GitHub repository search, see https://docs.github.com/en/rest/search#search-repositories for more info /// public static async Task FetchSearch( string searchQuery ) { @@ -85,7 +85,7 @@ public static async Task FetchSearch( string searchQuery ) } /// - /// 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 /// public static async Task FetchLatestRelease( string repository ) { diff --git a/code/Header.cs b/code/ToolInfoHeader.cs similarity index 87% rename from code/Header.cs rename to code/ToolInfoHeader.cs index 9e6044b..e4fd9dd 100644 --- a/code/Header.cs +++ b/code/ToolInfoHeader.cs @@ -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; diff --git a/code/Page.cs b/code/ToolInfoPage.cs similarity index 65% rename from code/Page.cs rename to code/ToolInfoPage.cs index f7accc7..14b050a 100644 --- a/code/Page.cs +++ b/code/ToolInfoPage.cs @@ -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; @@ -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(); - } } + /// + /// Displays information about this tool, along with release info + /// 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 ); @@ -86,6 +78,9 @@ private void AddManifestWidgets() } } + /// + /// Downloads the latest update for this page's project using the manifest + /// private void DownloadUpdate() { GithubApi.FetchLatestRelease( $"{Manifest.Repo}" ).ContinueWith( async t => @@ -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"; - } - + /// + /// Displays text telling the user to add the tool properly + /// private void AddNoManifestWidgets() { Layout.Add( new Label.Title( "😔" ) ).SetStyles( "font-size: 64px;" ); diff --git a/code/ToolsManager.cs b/code/ToolsManager.cs index d5b560a..79828a2 100644 --- a/code/ToolsManager.cs +++ b/code/ToolsManager.cs @@ -20,20 +20,20 @@ public ToolsManager() Show(); } + /// + /// 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. + /// 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"; @@ -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() @@ -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 ); @@ -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 ) @@ -141,6 +138,9 @@ public static void OnCompileMgrStart() HasCheckedForUpdates = true; } + /// + /// Check every tools project for any updates (if it has a valid manifest) + /// private static async Task CheckForUpdates() { int count = 0; diff --git a/code/Types/Github/Search.cs b/code/Types/Github/Search.cs index c803def..4cfe8f4 100644 --- a/code/Types/Github/Search.cs +++ b/code/Types/Github/Search.cs @@ -3,7 +3,7 @@ using System.Text.Json.Serialization; namespace Tools; -public class Item +public class Repository { [JsonPropertyName( "id" )] public int Id { get; set; } @@ -327,5 +327,5 @@ public class Search public bool IncompleteResults { get; set; } [JsonPropertyName( "items" )] - public List Items { get; set; } + public List Items { get; set; } } diff --git a/code/Types/Manifest.cs b/code/Types/Manifest.cs index e57de9d..0b834f9 100644 --- a/code/Types/Manifest.cs +++ b/code/Types/Manifest.cs @@ -1,8 +1,14 @@ -using System.Text.Json; +using System.IO; +using System.Text.Json; using System.Text.Json.Serialization; namespace Tools; +/// +/// Contains information about a GitHub repository and the currently downloaded +/// release, allowing these to be linked to (and contained within) a tools +/// project. +/// public class Manifest { [JsonPropertyName( "repo" )] @@ -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() { diff --git a/code/Utils/GitUtils.cs b/code/Utils/GitUtils.cs index 05d4174..b17a30b 100644 --- a/code/Utils/GitUtils.cs +++ b/code/Utils/GitUtils.cs @@ -5,6 +5,9 @@ namespace Tools; public static class GitUtils { + /// + /// Perform a git command and wait for it to finish + /// public static async Task Git( string command, string workingDir = null ) { Log.Trace( $"git {command}" ); diff --git a/code/Utils/LocalProjectExtensions.cs b/code/Utils/LocalProjectExtensions.cs index 7808522..6fa7ee2 100644 --- a/code/Utils/LocalProjectExtensions.cs +++ b/code/Utils/LocalProjectExtensions.cs @@ -5,11 +5,17 @@ namespace Tools; public static class LocalProjectExtensions { + /// + /// Get the Tools Manager manifest path for this project + /// public static string GetManifestPath( this Sandbox.LocalProject localProject ) { return Path.Combine( localProject.GetRootPath(), "tm-manifest.json" ); } + /// + /// Get the Tools Manager manifest for this project + /// public static Manifest GetManifest( this Sandbox.LocalProject localProject ) { if ( localProject.Config.PackageType != Sandbox.Package.Type.Tool ) diff --git a/code/Utils/Template.cs b/code/Utils/Template.cs deleted file mode 100644 index 81eeb65..0000000 --- a/code/Utils/Template.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Tools; - -/// -/// Shitty simple template format parser -/// -public class Template -{ - private string TemplateContents { get; set; } - - public Template( string templatePath ) - { - var dirName = Path.GetDirectoryName( templatePath ); - var fileName = Path.GetFileName( templatePath ); - - templatePath = Path.Join( FindBasePath(), dirName, fileName ); - TemplateContents = File.ReadAllText( templatePath ); - } - - private string FindBasePath() - { - var addon = Utility.Projects.GetAll().FirstOrDefault( x => x.Config.Ident == "vtextool" ); - return addon.GetCodePath(); - } - - public string Parse( Dictionary values ) - { - // - // example template: - // "Blah<#= value #>blah" - // - // called with Parse( new[]{ ( "value", "Hello!" ) } ): - // "BlahHello!blah" - // - - var str = TemplateContents; - foreach ( var pair in values ) - { - str = str.Replace( $"<#= {pair.Key} #>", pair.Value ); - } - - return str; - } -} diff --git a/code/Utils/WidgetExtensions.cs b/code/Utils/WidgetExtensions.cs index 549866f..895b0f9 100644 --- a/code/Utils/WidgetExtensions.cs +++ b/code/Utils/WidgetExtensions.cs @@ -17,10 +17,10 @@ void ReadAndSet() ReadAndSet(); - /* var watcher = new System.IO.FileSystemWatcher(); - watcher.Path = Path.GetDirectoryName( combinedPath ); - watcher.Filter = Path.GetFileName( combinedPath ); - watcher.Changed += (_, _) => ReadAndSet(); - watcher.EnableRaisingEvents = true; */ + //var watcher = new FileSystemWatcher(); + //watcher.Path = Path.GetDirectoryName( combinedPath ); + //watcher.Filter = Path.GetFileName( combinedPath ); + //watcher.Changed += (_, _) => ReadAndSet(); + //watcher.EnableRaisingEvents = true; } }