Skip to content

Commit

Permalink
Working on splitting the different modules into their own parts and c…
Browse files Browse the repository at this point in the history
…reated a memory system
  • Loading branch information
Baudin999 committed Dec 28, 2019
1 parent f9e5386 commit 675cefd
Show file tree
Hide file tree
Showing 30 changed files with 204,486 additions and 246 deletions.
12 changes: 7 additions & 5 deletions ApplicationTests/File/ParseMultipleFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ public async Task Execute()
{
try
{
var tasks = new List<Task>();
tasks.Add(project.CreateModule("First", @"# The first document
var tasks = new List<Task>
{
project.CreateModule("First", @"# The first document
type Person
"));
tasks.Add(project.CreateModule("Second", @"
"),
project.CreateModule("Second", @"
open Person
# The second document
type School
"));
")
};
await Task.WhenAll(tasks.ToArray());
Assert.Equal(2, project.Modules.Count);
}
Expand Down
6 changes: 6 additions & 0 deletions ApplicationTests/InMemoryTests/BasicInMemoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public async Task CreateALotOfModules()
var instance = Project.FileSystems.MemorySystem.Instance;

Assert.NotNull(test);
Assert.NotNull(other);
Assert.NotNull(something);
Assert.NotNull(foo);
Assert.NotNull(bar);

Assert.Equal(5, instance.Count);


}
Expand Down
13 changes: 8 additions & 5 deletions CLI/AssetHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using Project;
using System.IO;
using System.Reflection;
using System.Text;

namespace CLI
{
Expand Down Expand Up @@ -34,12 +32,17 @@ internal static string ReadAsset(string name)

internal static void WriteAsset(string path, string content)
{
File.WriteAllText(path, content);
ProjectContext.FileSystem?.SaveFile(path, content);
//File.WriteAllText(path, content);
}

internal static void ReadAndWriteAsset(string assetName, string outPath)
{
var outName = System.IO.Path.GetFullPath(assetName, outPath);
string outName = $"{outPath}/{assetName}".Replace("//", "/");
if (ProjectContext.FileSystem is Project.FileSystems.FileSystem)
{
outName = System.IO.Path.GetFullPath(assetName, outPath);
}
AssetHelpers.WriteAsset(outName, AssetHelpers.ReadAsset(assetName));
}
}
Expand Down
102,093 changes: 102,050 additions & 43 deletions CLI/Assets/mermaid.min.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion CLI/Assets/mermaid.min.js.map

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions CLI/Assets/vis-network.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions CLI/Assets/vis-network.js.map

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions CLI/CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
<PackageReference Include="System.IO.FileSystem.Watcher" Version="4.3.0" />
<PackageReference Include="Bogus" Version="28.4.4" />
<PackageReference Include="LiteDB" Version="4.1.4" />
<PackageReference Include="System.Reactive" Version="4.3.1" />
<PackageReference Include="System.Reactive" Version="4.3.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Assets\" />
<Folder Include="Commands\" />
<Folder Include="Controllers\" />
<Folder Include="wwwroot\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<None Remove="Assets\style.css" />
<None Remove="Assets\mermaid.min.js" />
<None Remove="Assets\mermaid.min.js.map" />
<None Remove="Assets\vis-network.js" />
<None Remove="Assets\vis-network.js.map" />
</ItemGroup>
<ItemGroup>
<None Update="wwwroot\*">
Expand All @@ -50,5 +50,7 @@
<ItemGroup>
<EmbeddedResource Include="Assets\mermaid.min.js" />
<EmbeddedResource Include="Assets\mermaid.min.js.map" />
<EmbeddedResource Include="Assets\vis-network.js" />
<EmbeddedResource Include="Assets\vis-network.js.map" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions CLI/Commands/Commands.Watch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static void CreateWatchCommand(CommandLineApplication app)
false => Directory.GetCurrentDirectory(),
true => fileOption.Value()
};
ProjectContext.Init(directory);
//ProjectContext.InitInMemory();
//ProjectContext.Init(directory);
ProjectContext.InitInMemory();
var project = ProjectContext.Instance;
if (project != null)
{
Expand Down
123 changes: 44 additions & 79 deletions CLI/Controllers/JsonDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private object GenerateBaseValue(string _type, IRestrictable restrictable, strin
var config = ProjectContext.Instance?.CarConfig ?? new CarConfig();
var faker = new Faker("nl");
dynamic? result = null;

result = fakerDirective?.ToLower() switch
{
// person
Expand Down Expand Up @@ -177,82 +177,47 @@ private static void AddProperty(ExpandoObject expando, string propertyName, obje
expandoDict.Add(propertyName, propertyValue);
}
}


//public static class BogusWrapper
//{
// private static Faker Bogus { get => new Faker("nl"); }
// public static class Person
// {
// public static string FirstName => new Faker("nl").Person.FirstName;
// public static string LastName => BogusWrapper.Bogus.Person.LastName;
// public static string FullName => BogusWrapper.Bogus.Person.FullName;
// public static string DateOfBirth => BogusWrapper.Bogus.Person.DateOfBirth.ToShortDateString();
// public static string Avatar => BogusWrapper.Bogus.Person.Avatar;
// public static string Email => BogusWrapper.Bogus.Person.Email;
// public static string Gender => BogusWrapper.Bogus.Person.Gender.ToString("g");
// public static string Phone => BogusWrapper.Bogus.Person.Phone;
// public static string UserName => BogusWrapper.Bogus.Person.UserName;
// }
// public static class Address
// {
// public static string Street => BogusWrapper.Bogus.Address.StreetName();
// public static string HouseNumber => BogusWrapper.Bogus.Random.Number(1, 1000).ToString();
// public static string HouseNumberExtension => BogusWrapper.Bogus.Random.Char('A', 'Z').ToString();
// public static string PostalCode => BogusWrapper.Bogus.Address.ZipCode();
// public static string City => BogusWrapper.Bogus.Address.City();
// public static string Country => BogusWrapper.Bogus.Address.Country();
// public static string CountryCode => BogusWrapper.Bogus.Address.CountryCode();
// public static string FullAddress => BogusWrapper.Bogus.Address.FullAddress();
// public static string County => BogusWrapper.Bogus.Address.County();
// }

// public static class Finance
// {
// public static string Number => BogusWrapper.Bogus.Finance.Account();
// }
//}


public static class DataHelpers
{
public static object? GetPropValue(this object obj, string name)
{
foreach (var part in name.Split('.'))
{
if (obj == null) { return null; }

Type type = obj.GetType();
var info = type.GetProperty(part);
if (info != null)
{
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
obj = info.GetValue(obj, null);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
}
else
{
var fieldInfo = type.GetField(part);
if (fieldInfo != null)
{
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
obj = fieldInfo.GetValue(obj);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
}
}
}
return obj;
}

public static T GetPropValue<T>(this object obj, String name)
{
var retval = GetPropValue(obj, name);
#pragma warning disable CS8653 // A default expression introduces a null value for a type parameter.
if (retval == null) { return default; }
#pragma warning restore CS8653 // A default expression introduces a null value for a type parameter.

// throws InvalidCastException if types are incompatible
return (T)retval;
}
}
}

// public static class DataHelpers
// {
// public static object? GetPropValue(this object obj, string name)
// {
// foreach (var part in name.Split('.'))
// {
// if (obj == null) { return null; }

// Type type = obj.GetType();
// var info = type.GetProperty(part);
// if (info != null)
// {
//#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
// obj = info.GetValue(obj, null);
//#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
// }
// else
// {
// var fieldInfo = type.GetField(part);
// if (fieldInfo != null)
// {
//#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
// obj = fieldInfo.GetValue(obj);
//#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
// }
// }
// }
// return obj;
// }

// public static T GetPropValue<T>(this object obj, String name)
// {
// var retval = GetPropValue(obj, name);
//#pragma warning disable CS8653 // A default expression introduces a null value for a type parameter.
// if (retval == null) { return default; }
//#pragma warning restore CS8653 // A default expression introduces a null value for a type parameter.

// // throws InvalidCastException if types are incompatible
// return (T)retval;
// }
// }
//}
81 changes: 75 additions & 6 deletions CLI/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Project;
using Project.FileSystems;
using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using System;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System.Text;
using System.Net.Http.Headers;

namespace CLI
{
public class Startup
Expand Down Expand Up @@ -49,6 +56,38 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseExceptionHandler("/Home/Error");
}
app.UseMiddleware<ErrorLoggingMiddleware>();

if (ProjectContext.FileSystem?.FileSystemType == FileSystemType.InMemory)
{
app.Use(async (context, next) =>
{
var path = (ProjectContext.Instance?.OutPath ?? "") + context.Request.Path;
path = path.Replace("//", "/");
if (ProjectContext.FileSystem.HasFile(path))
{
var text = ProjectContext.FileSystem.ReadFileText(path);
if (path.EndsWith(".html"))
{
var result = new FileContentResult(Encoding.ASCII.GetBytes(text), "text/html");
await context.WriteResultAsync(result);
}
else if (path.EndsWith(".js"))
{
var result = new FileContentResult(Encoding.ASCII.GetBytes(text), "application/javascript");
await context.WriteResultAsync(result);
}
else if (path.EndsWith(".json"))
{
var result = new FileContentResult(Encoding.ASCII.GetBytes(text), "application/json");
await context.WriteResultAsync(result);
}
}
else
{
await next.Invoke();
}
});
}
app.UseCors(MyAllowSpecificOrigins);
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
Expand All @@ -66,6 +105,35 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}
}

public static class HttpContextExtensions
{
private static readonly RouteData EmptyRouteData = new RouteData();

private static readonly ActionDescriptor EmptyActionDescriptor = new ActionDescriptor();

public static Task WriteResultAsync<TResult>(this HttpContext context, TResult result)
where TResult : IActionResult
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

var executor = context.RequestServices.GetService<IActionResultExecutor<TResult>>();

if (executor == null)
{
throw new InvalidOperationException($"No result executor for '{typeof(TResult).FullName}' has been registered.");
}

var routeData = context.GetRouteData() ?? EmptyRouteData;

var actionContext = new ActionContext(context, routeData, EmptyActionDescriptor);

return executor.ExecuteAsync(actionContext, result);
}
}

public class ErrorLoggingMiddleware
{
private readonly RequestDelegate _next;
Expand All @@ -88,5 +156,6 @@ public async Task Invoke(HttpContext context)
}
}
}


}
6 changes: 4 additions & 2 deletions CLI/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ private static void OpenBrowser(string url)

private static void CreateAssets(string outPath)
{
AssetHelpers.ReadAndWriteAsset("mermaid.min.js", outPath);
AssetHelpers.ReadAndWriteAsset("mermaid.min.js.map", outPath);
//AssetHelpers.ReadAndWriteAsset("mermaid.min.js", outPath);
//AssetHelpers.ReadAndWriteAsset("mermaid.min.js.map", outPath);
//AssetHelpers.ReadAndWriteAsset("vis-network.js", outPath);
//AssetHelpers.ReadAndWriteAsset("vis-network.js.map", outPath);
}


Expand Down
1 change: 0 additions & 1 deletion CLI/wwwroot/codeflask.js

This file was deleted.

Binary file added CLI/wwwroot/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions CLI/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<html lang='en'>
<head>
<meta http-equiv='Content-type' content='text/html;charset=UTF-8'>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'>

Expand All @@ -13,8 +14,8 @@

<script defer src='/build/bundle.js'></script>
<script src='/vis-network.js'></script>
<script src='/vs/loader.js'></script>
<script src='/vs/loader.js'></script>

</head>

<body>
Expand Down
Loading

0 comments on commit 675cefd

Please sign in to comment.