Skip to content

Commit

Permalink
Created a Project Context instead of static property on FileProject
Browse files Browse the repository at this point in the history
  • Loading branch information
Baudin999 committed Dec 23, 2019
1 parent 3f54b57 commit 3c439ca
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 68 deletions.
3 changes: 2 additions & 1 deletion CLI/Commands/Commands.Serve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using CLI.Signals;
using Microsoft.Extensions.CommandLineUtils;
using Project;

namespace CLI.Commands
{
Expand Down Expand Up @@ -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);
Expand Down
37 changes: 24 additions & 13 deletions CLI/Commands/Commands.Watch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using CLI.Signals;
using Microsoft.Extensions.CommandLineUtils;
using Project;

namespace CLI.Commands
{
Expand Down Expand Up @@ -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;
}
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion CLI/Controllers/JsonDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions CLI/Controllers/LexiconDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
{
Expand Down
37 changes: 20 additions & 17 deletions CLI/Controllers/ModuleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,38 @@ public class ModuleController : ControllerBase
[HttpGet("/api/modules")]
public IEnumerable<string> GetModules()
{
return FileProject.Current?.Modules.Select(m => m.Name) ?? Enumerable.Empty<string>();
return ProjectContext.Instance?.Modules.Select(m => m.Name) ?? Enumerable.Empty<string>();
}

[HttpPost("/api/modules/{name}")]
public async Task<IActionResult> 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<Descriptor> {
module.ToDescriptor("Your newly created module")
});
return Ok(new List<Descriptor> {
module.ToDescriptor("Your newly created module")
});
} else
{
return BadRequest($"Failed to created module {name}.");
}
}

[HttpGet("/api/search/{param}")]
public IEnumerable<Descriptor> Search(string param)
{

var moduleDescriptions = FileProject.Current?.Modules.Select(m =>
var moduleDescriptions = ProjectContext.Instance?.Modules.Select(m =>
{
return new Descriptor(m.Name)
{
Expand All @@ -60,8 +64,7 @@ public IEnumerable<Descriptor> Search(string param)
return moduleDescriptions;
}

var descriptors = FileProject
.Current?
var descriptors = ProjectContext.Instance?
.Modules
.SelectMany(m => m.GetDescriptions());

Expand All @@ -76,7 +79,7 @@ public IEnumerable<Descriptor> 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);

Expand All @@ -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);
Expand All @@ -98,7 +101,7 @@ public IActionResult GetModuleText(string module)
[HttpPost("/api/module/{module}")]
public async Task<IActionResult> SaveModuleTextAsync(string module)
{
var m = FileProject.Current?.FindModule(module);
var m = ProjectContext.Instance?.FindModule(module);


if (m is null)
Expand All @@ -121,7 +124,7 @@ public async Task<IActionResult> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion CLI/Controllers/RemoteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RemoteController : ControllerBase
[HttpGet("/api/remote/module/{module}")]
public async Task<IActionResult> 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;
Expand Down
5 changes: 3 additions & 2 deletions CLI/Controllers/TopologyController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using CLI.Models;
using Microsoft.AspNetCore.Mvc;
using Project;

namespace CLI.Controllers
{
Expand All @@ -10,15 +11,15 @@ 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);
}

[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);
}
Expand Down
3 changes: 2 additions & 1 deletion CLI/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions CLI/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ 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 () =>
{
await Task.Delay(1500);
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(() =>
Expand Down
6 changes: 3 additions & 3 deletions CLI/wwwroot/build/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3c439ca

Please sign in to comment.