From 3c439ca8d0c711ee4fdf45501b2e5eb65ee36b8f Mon Sep 17 00:00:00 2001 From: Baudin Date: Mon, 23 Dec 2019 09:25:48 +0100 Subject: [PATCH] Created a Project Context instead of static property on FileProject --- CLI/Commands/Commands.Serve.cs | 3 +- CLI/Commands/Commands.Watch.cs | 37 +++++++++++++++--------- CLI/Controllers/JsonDataController.cs | 2 +- CLI/Controllers/LexiconDataController.cs | 4 +-- CLI/Controllers/ModuleController.cs | 37 +++++++++++++----------- CLI/Controllers/RemoteController.cs | 2 +- CLI/Controllers/TopologyController.cs | 5 ++-- CLI/Database.cs | 3 +- CLI/WebServer.cs | 7 +++-- CLI/wwwroot/build/bundle.css | 6 ++-- CLI/wwwroot/build/bundle.css.map | 12 ++++---- CLI/wwwroot/build/bundle.js | 2 +- CLI/wwwroot/build/bundle.js.map | 2 +- CLI/wwwroot/global.css | 4 +-- Project/FileProject.cs | 6 ++-- Project/IProject.cs | 22 ++++++++++++++ Project/ImportResolver.cs | 2 +- Project/ProjectContext.cs | 19 ++++++++++++ web/src/Home.svelte | 21 +++++++++----- web/src/Preview.svelte | 1 - web/src/navigator.js | 4 +++ 21 files changed, 133 insertions(+), 68 deletions(-) create mode 100644 Project/ProjectContext.cs diff --git a/CLI/Commands/Commands.Serve.cs b/CLI/Commands/Commands.Serve.cs index 14c294a..bbef29d 100644 --- a/CLI/Commands/Commands.Serve.cs +++ b/CLI/Commands/Commands.Serve.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using CLI.Signals; using Microsoft.Extensions.CommandLineUtils; +using Project; namespace CLI.Commands { @@ -32,7 +33,7 @@ To quit the application press 'q' true => fileOption.Value() }; - var project = new Project.FileProject(directory); + var project = ProjectContext.Init(directory); SignalSingleton.ExitSignal.Subscribe(project.Dispose); WebServer.Start(project.OutPath); diff --git a/CLI/Commands/Commands.Watch.cs b/CLI/Commands/Commands.Watch.cs index 6b673db..48d694a 100644 --- a/CLI/Commands/Commands.Watch.cs +++ b/CLI/Commands/Commands.Watch.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using CLI.Signals; using Microsoft.Extensions.CommandLineUtils; +using Project; namespace CLI.Commands { @@ -35,23 +36,33 @@ public static void CreateWatchCommand(CommandLineApplication app) false => Directory.GetCurrentDirectory(), true => fileOption.Value() }; - - var project = new Project.FileProject(directory); - SignalSingleton.ExitSignal.Subscribe(project.Dispose); - - Task? webserverTask = null; - if (serve.HasValue()) + ProjectContext.Init(directory); + var project = ProjectContext.Instance; + if (project != null) { - webserverTask = WebServer.Start(project.OutPath); - } + SignalSingleton.ExitSignal.Subscribe(() => { + project.Dispose(); + }); + + Task? webserverTask = null; + if (serve.HasValue()) + { + webserverTask = WebServer.Start(project.OutPath); + } - project.Watch(); + project.Watch(); - while (Console.ReadKey().Key != ConsoleKey.Q) { } - Console.WriteLine(); + while (Console.ReadKey().Key != ConsoleKey.Q) { } + Console.WriteLine(); - SignalSingleton.ExitSignal.Dispatch(); - return 0; + SignalSingleton.ExitSignal.Dispatch(); + return 0; + } + else + { + Console.WriteLine("Could not initialize the project."); + return 1; + } }); }); } diff --git a/CLI/Controllers/JsonDataController.cs b/CLI/Controllers/JsonDataController.cs index 8c2340d..2aebb2e 100644 --- a/CLI/Controllers/JsonDataController.cs +++ b/CLI/Controllers/JsonDataController.cs @@ -19,7 +19,7 @@ public class JsonDataController : ControllerBase public IActionResult GetData(string module, string type, [FromQuery]bool list) { - Module = Project.FileProject.Current?.Modules.First(m => m.Name == module); + Module = Project.ProjectContext.Instance?.Modules.First(m => m.Name == module); var result = list ? new Faker().Make(10, () => Generate(type)) : Generate(type); if (result is null) { diff --git a/CLI/Controllers/LexiconDataController.cs b/CLI/Controllers/LexiconDataController.cs index a378b2b..33f35e7 100644 --- a/CLI/Controllers/LexiconDataController.cs +++ b/CLI/Controllers/LexiconDataController.cs @@ -42,7 +42,7 @@ public void Delete([FromBody]LexiconEntry entry) [HttpGet("/api/lexicon/config")] public IActionResult ConfigurationData() { - var project = FileProject.Current; + var project = ProjectContext.Instance; if (project is null) return NotFound(); #pragma warning disable CS8602 // Dereference of a possibly null reference. else return Ok(project.CarConfig.LexiconConfig); @@ -53,7 +53,7 @@ public IActionResult ConfigurationData() [HttpGet("/api/lexicon/remote")] public IActionResult GetRemoteLexiconData() { - var config = FileProject.Current?.CarConfig; + var config = ProjectContext.Instance?.CarConfig; if (config is null) return NotFound(); else { diff --git a/CLI/Controllers/ModuleController.cs b/CLI/Controllers/ModuleController.cs index ba3cccd..42133ad 100644 --- a/CLI/Controllers/ModuleController.cs +++ b/CLI/Controllers/ModuleController.cs @@ -17,34 +17,38 @@ public class ModuleController : ControllerBase [HttpGet("/api/modules")] public IEnumerable GetModules() { - return FileProject.Current?.Modules.Select(m => m.Name) ?? Enumerable.Empty(); + return ProjectContext.Instance?.Modules.Select(m => m.Name) ?? Enumerable.Empty(); } [HttpPost("/api/modules/{name}")] public async Task CreateModuleAsync(string name) { - var checkModule = FileProject.Current?.FindModule(name); + var checkModule = ProjectContext.Instance?.FindModule(name); if (checkModule != null) { return BadRequest($"Module: {name}, already exists and cannot be created."); } -#pragma warning disable CS8602 // Dereference of a possibly null reference. - var module = await FileProject.Current?.CreateModule(name); -#pragma warning restore CS8602 // Dereference of a possibly null reference. - - if (module is null) return BadRequest($"Failed to created module {name}."); + var project = ProjectContext.Instance; + if (project != null) + { + Module? module = await project.CreateModule(name, null); + if (module is null) return BadRequest($"Failed to created module {name}."); - return Ok(new List { - module.ToDescriptor("Your newly created module") - }); + return Ok(new List { + module.ToDescriptor("Your newly created module") + }); + } else + { + return BadRequest($"Failed to created module {name}."); + } } [HttpGet("/api/search/{param}")] public IEnumerable Search(string param) { - var moduleDescriptions = FileProject.Current?.Modules.Select(m => + var moduleDescriptions = ProjectContext.Instance?.Modules.Select(m => { return new Descriptor(m.Name) { @@ -60,8 +64,7 @@ public IEnumerable Search(string param) return moduleDescriptions; } - var descriptors = FileProject - .Current? + var descriptors = ProjectContext.Instance? .Modules .SelectMany(m => m.GetDescriptions()); @@ -76,7 +79,7 @@ public IEnumerable Search(string param) public IActionResult RenderDescriptor([FromQuery]Descriptor descriptor) { - var module = Project.FileProject.Current?.Modules.FirstOrDefault(m => m.Name == descriptor.Module); + var module = ProjectContext.Instance?.Modules.FirstOrDefault(m => m.Name == descriptor.Module); var node = module?.Transpiler.AST.FirstOrDefault(a => a is INamable && ((INamable)a).Name == descriptor.Name); if (node is null) return NotFound(descriptor); @@ -89,7 +92,7 @@ public IActionResult RenderDescriptor([FromQuery]Descriptor descriptor) [HttpGet("/api/module/{module}")] public IActionResult GetModuleText(string module) { - var m = FileProject.Current?.FindModule(module); + var m = ProjectContext.Instance?.FindModule(module); if (m is null) return NotFound(); return Ok(m.Code); @@ -98,7 +101,7 @@ public IActionResult GetModuleText(string module) [HttpPost("/api/module/{module}")] public async Task SaveModuleTextAsync(string module) { - var m = FileProject.Current?.FindModule(module); + var m = ProjectContext.Instance?.FindModule(module); if (m is null) @@ -121,7 +124,7 @@ public async Task SaveModuleTextAsync(string module) [HttpGet("/api/module/{module}/errors")] public IActionResult GetModuleErrors(string module) { - var m = FileProject.Current?.FindModule(module); + var m = ProjectContext.Instance?.FindModule(module); if (m is null) return NotFound(); return Ok(m.Generator.Errors); } diff --git a/CLI/Controllers/RemoteController.cs b/CLI/Controllers/RemoteController.cs index cf5b714..ff54ba0 100644 --- a/CLI/Controllers/RemoteController.cs +++ b/CLI/Controllers/RemoteController.cs @@ -13,7 +13,7 @@ public class RemoteController : ControllerBase [HttpGet("/api/remote/module/{module}")] public async Task GetModuleText(string module) { - var project = FileProject.Current; + var project = ProjectContext.Instance; if (project is null) return NotFound(); var url = project.CarConfig?.Remote + "/api/module/" + module; diff --git a/CLI/Controllers/TopologyController.cs b/CLI/Controllers/TopologyController.cs index 4d21ed6..c1d6b04 100644 --- a/CLI/Controllers/TopologyController.cs +++ b/CLI/Controllers/TopologyController.cs @@ -1,6 +1,7 @@ using System; using CLI.Models; using Microsoft.AspNetCore.Mvc; +using Project; namespace CLI.Controllers { @@ -10,7 +11,7 @@ public class TopologyController: ControllerBase [HttpGet("/api/topology")] public IActionResult GetTopology() { - var topology = Project.FileProject.Current?.GetTopology(true); + var topology = ProjectContext.Instance?.GetTopology(true); if (topology is null) return NoContent(); else return Ok(topology); } @@ -18,7 +19,7 @@ public IActionResult GetTopology() [HttpGet("/api/topology/modules")] public IActionResult GetTopologyModules() { - var topology = Project.FileProject.Current?.GetTopology(false); + var topology = ProjectContext.Instance?.GetTopology(false); if (topology is null) return NoContent(); else return Ok(topology); } diff --git a/CLI/Database.cs b/CLI/Database.cs index e8c40d4..e957f1d 100644 --- a/CLI/Database.cs +++ b/CLI/Database.cs @@ -4,12 +4,13 @@ using CLI.Models; using CLI.Signals; using LiteDB; +using Project; namespace CLI { public static class Database { - private static string path = Path.Combine(Project.FileProject.Current?.OutPath ?? "", "Lexicon.db"); + private static string path = Path.Combine(ProjectContext.Instance?.OutPath ?? "", "Lexicon.db"); private static ConnectionString ConnectionString() { var connectionString = new ConnectionString(path); diff --git a/CLI/WebServer.cs b/CLI/WebServer.cs index 96d88d2..9367be3 100644 --- a/CLI/WebServer.cs +++ b/CLI/WebServer.cs @@ -16,7 +16,7 @@ public static class WebServer public static string RootPath = ""; public static Task Start(string rootPath) { - var portNumber = FileProject.Current?.CarConfig?.PortNumber ?? "5000"; + var portNumber = ProjectContext.Instance?.CarConfig?.PortNumber ?? "5000"; RootPath = rootPath; Task.Run(async () => { @@ -24,9 +24,10 @@ public static Task Start(string rootPath) WebServer.OpenBrowser($"http://localhost:{portNumber}/index.html"); }); - if (FileProject.Current != null) + var project = ProjectContext.Instance; + if (project != null) { - WebServer.CreateAssets(FileProject.Current.OutPath); + WebServer.CreateAssets(project.OutPath); } return Task.Run(() => diff --git a/CLI/wwwroot/build/bundle.css b/CLI/wwwroot/build/bundle.css index 64a4712..446b438 100644 --- a/CLI/wwwroot/build/bundle.css +++ b/CLI/wwwroot/build/bundle.css @@ -1,11 +1,11 @@ main.svelte-1kds1eb{text-align:center;padding:1em;max-width:240px;margin:0 auto}@media(min-width: 640px){main.svelte-1kds1eb{max-width:none}} -.errors.svelte-1l9twu{position:fixed;right:1rem;top:6rem}.error.svelte-1l9twu{min-width:300px;max-width:700px}.error.svelte-1l9twu pre.svelte-1l9twu{overflow-wrap:break-word}.editor-container.svelte-1l9twu{margin-top:1rem}.editor-container.svelte-1l9twu,#editor.svelte-1l9twu{height:calc(100% - 4rem)} tr.svelte-1aug0lv:hover{cursor:pointer} textarea.svelte-nhavks{width:750px;min-height:250px} -.preview.svelte-9zwial{height:calc(100% - 60px)}iframe.svelte-9zwial{width:100%;margin:2rem 0;height:100%;border:none} +.preview.svelte-1crxzpc{height:calc(100% - 60px)}iframe.svelte-1crxzpc{width:100%;height:100%;border:none} +.errors.svelte-1l9twu{position:fixed;right:1rem;top:6rem}.error.svelte-1l9twu{min-width:300px;max-width:700px}.error.svelte-1l9twu pre.svelte-1l9twu{overflow-wrap:break-word}.editor-container.svelte-1l9twu{margin-top:1rem}.editor-container.svelte-1l9twu,#editor.svelte-1l9twu{height:calc(100% - 4rem)} .topology.svelte-sq8u03{height:calc(100% - 4rem)}#topology.svelte-sq8u03{height:100%}#topology>.svelte-sq8u03:focus{outline:none !important}.options-form.svelte-sq8u03{position:fixed;right:10px;bottom:10px} -ul.svelte-wjec8i{list-style:none;text-align:left} .descriptor.svelte-86a2ch{border:1px solid lightgray;font-size:14px;padding:1;margin:0;width:450px;margin-bottom:1rem;margin-left:50%;transform:translateX(-50%)}.descriptor.svelte-86a2ch:hover{cursor:pointer}.descriptor.svelte-86a2ch h2.svelte-86a2ch{background:#3083db;color:white;padding:0.5em;font-size:1em;margin:0;position:relative}.descriptor.svelte-86a2ch .description.svelte-86a2ch{color:gray;padding:0 1rem}.pill.svelte-86a2ch{background:orange;border:1 px solid rgb(172, 114, 6);color:white;border-radius:50%;font-size:10px;text-transform:lowercase;padding:0.5em 1em 0.6em 1em;position:absolute;left:10px;top:50%;transform:translateY(-50%)}.pill.type.svelte-86a2ch{background:purple}.pill.field.svelte-86a2ch{background:darkgreen} +ul.svelte-wjec8i{list-style:none;text-align:left} .overlay.svelte-yjqhyw{position:fixed;top:0;left:0;right:0;bottom:0;height:100%;width:100%;background:rgba(0, 0, 0, 0.4);visibility:hidden}.overlay.show.svelte-yjqhyw{visibility:visible}.overlay.svelte-yjqhyw .window.svelte-yjqhyw{background:#313338;border:1px solid 616265;border-radius:5px;position:absolute;top:10%;width:300px;left:50%;transform:translateX(-50%);font-size:2em;color:white} /*# sourceMappingURL=bundle.css.map */ \ No newline at end of file diff --git a/CLI/wwwroot/build/bundle.css.map b/CLI/wwwroot/build/bundle.css.map index 3e58bf1..49d93ae 100644 --- a/CLI/wwwroot/build/bundle.css.map +++ b/CLI/wwwroot/build/bundle.css.map @@ -3,26 +3,26 @@ "file": "bundle.css", "sources": [ "../../../web/src/App.svelte", - "../../../web/src/Editor.svelte", "../../../web/src/Lexicon.svelte", "../../../web/src/LexiconAdmin.svelte", "../../../web/src/Preview.svelte", + "../../../web/src/Editor.svelte", "../../../web/src/ModuleTopology.svelte", - "../../../web/src/FileTree.svelte", "../../../web/src/SearchResult.svelte", + "../../../web/src/FileTree.svelte", "../../../web/src/Controls/Overlay.svelte" ], "sourcesContent": [ "\n\n\n\n
\n\n navigator.navigate('index')}>\n Home\n \n navigator.navigate('module-topology')}>\n Topology\n \n navigator.navigate('lexicon')}>\n Lexicon\n \n {#if navigator.module}\n navigator.navigate('editor')}>\n Editor\n \n navigator.navigate('preview')}>\n Preview\n \n {/if}\n
\n\n{#if route === 'index'}\n \n{:else if route === 'lexicon'}\n \n{:else if route === 'add-lexicon'}\n \n{:else if route === 'edit-lexicon'}\n \n{:else if route === 'lexicon-admin'}\n \n{:else if route === 'preview'}\n \n{:else if route === 'editor'}\n \n{:else if route === 'config'}\n \n{:else if route === 'module-topology'}\n \n{:else if route === 'module-create'}\n \n{:else}\n \n{/if}\n", - "\n\n\n\n
\n Save\n
\n
\n
\n
\n\n{#if errors && errors.length > 0}\n
\n {#each errors as error}\n
\n
{error.title}
\n
{error.message}
\n
\n {/each}\n
\n{/if}\n\n", "\n\n\n\n
\n

Search your lexicon!

\n
\n {\n navigator.navigate('add-lexicon');\n }}>\n Create\n \n

Search your lexicon:

\n e.code === 'Enter' && onkeyup(e.target.value)}\n on:change={e => findData(e.target.value)} />\n
\n
\n\n \n \n \n \n \n \n {#each data as d}\n navigator.navigate('edit-lexicon', d.id)}>\n \n \n \n \n {/each}\n \n
DomainNameDescription
{d.domain}{d.name}{d.description}
\n", "\n\n\n\n

Domains

\n

Please give the domains you would like to restrict your lexicon to.

\n