-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic code generation structures, and the burrito API outline.
- Loading branch information
Showing
19 changed files
with
770 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/> | ||
</startup> | ||
</configuration> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace burritocli | ||
{ | ||
internal static class Logger | ||
{ | ||
public static void Log(string msg) | ||
{ | ||
if (msg.StartsWith("[ERR]")) { Console.ForegroundColor = ConsoleColor.Red; } | ||
Console.WriteLine(msg); | ||
Console.ResetColor(); | ||
} | ||
|
||
public static void Exit(string msg) | ||
{ | ||
Console.WriteLine(msg); | ||
Environment.Exit(-1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace Burrito | ||
{ | ||
/// <summary> | ||
/// Represents a single instance of the Burrito API. | ||
/// </summary> | ||
public class BurritoAPI | ||
{ | ||
//Whether to compile the library after generating it. | ||
public bool CompileAfterGeneration { get; set; } = false; | ||
|
||
//What path to generate the library at. | ||
public string GenerationPath { get; set; } = Environment.CurrentDirectory; | ||
|
||
//Where to draw the API schema from to use. | ||
public string APISchemaPath { get; set; } = null; | ||
|
||
//The verbosity level of the generator. | ||
//0 - No logging. | ||
//1 - Critical logging only (errors). | ||
//2 - Normal logging (status updates & errors). | ||
//3 - Debug logging. | ||
public int VerbosityLevel | ||
{ | ||
get { return Logger.Verbosity; } | ||
set { Logger.Verbosity = value; } | ||
} | ||
|
||
public int Run() | ||
{ | ||
//Try and deserialize the schema. | ||
APISchema schema; | ||
try | ||
{ | ||
schema = JsonConvert.DeserializeObject<APISchema>(File.ReadAllText(APISchemaPath)); | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.Write("[ERR] - Failed to load API schema, '" + e.Message + "'.", 1); | ||
return 0; | ||
} | ||
|
||
//All relevant properties exist? | ||
if (schema.Sections == null || schema.Name == null || schema.RootPath == null) | ||
{ | ||
Logger.Write("[ERR] - Required chema property missing (one of 'name', 'root' or 'sections').", 1); | ||
return 0; | ||
} | ||
|
||
//Valid schema name? | ||
if (!Regex.IsMatch(schema.Name, "^[A-Za-z0-9_]+$")) | ||
{ | ||
Logger.Write("[ERR] - Invalid schema name given. Must only be alphanumeric or underscores.", 1); | ||
return 0; | ||
} | ||
|
||
//Create a project module with the given schema and namespaces. | ||
var project = new ProjectModule(schema.Name); | ||
project.AddNamespace("Data"); | ||
project.AddNamespace("@"); | ||
|
||
//Check all the sections in the schema have unique names. | ||
var sectionNames = new List<string>(); | ||
foreach (var module in schema.Sections) | ||
{ | ||
if (sectionNames.Contains(module.Name)) | ||
{ | ||
Logger.Write("[ERR] - Duplicate section name '" + module.Name + "' detected.", 1); | ||
return 0; | ||
} | ||
sectionNames.Add(module.Name); | ||
} | ||
|
||
//Loop through the routes and generate code modules for each of them. | ||
foreach (var module in schema.Sections) | ||
{ | ||
var moduleClass = new ClassModule(project); | ||
foreach (var route in module.Routes) | ||
{ | ||
//URL exists? | ||
if (route.RelativeURL == null) | ||
{ | ||
Logger.Write("[ERR] - No URL provided for route in sectoin '" + module.Name + "'.", 1); | ||
return 0; | ||
} | ||
|
||
//What method does this route use? | ||
switch (route.HTTPMethod) | ||
{ | ||
case null: | ||
Logger.Write("[ERR] - No HTTP method defined for route '" + route.RelativeURL + "'.", 1); | ||
return 0; | ||
case "GET": | ||
case "get": | ||
//Figure out a data type from the API endpoint. | ||
|
||
|
||
//Add the method. | ||
moduleClass.Methods.Add(new GETMethodModule() | ||
{ | ||
Async = route.Async, | ||
Route = route.RelativeURL, | ||
XMLSummary = route.GetSummary(), | ||
Name = route.GetMethodName() | ||
}); | ||
break; | ||
case "POST": | ||
case "post": | ||
//Add the method. | ||
moduleClass.Methods.Add(new POSTMethodModule() | ||
{ | ||
Async = route.Async, | ||
Route = route.RelativeURL, | ||
XMLSummary = route.GetSummary(), | ||
DataType = route.SentDataName, | ||
Name = route.GetMethodName() | ||
}); | ||
break; | ||
|
||
} | ||
} | ||
|
||
//Add the class to root namespace. | ||
project.Namespaces["@"].Add(moduleClass); | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the logger used by Burrito to the provided method. | ||
/// </summary> | ||
public void SetLogger(Action<string> logger) | ||
{ | ||
Logger.Log = logger; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace Burrito | ||
{ | ||
/// <summary> | ||
/// Represents a single API method within a class. | ||
/// </summary> | ||
public abstract class APIMethodModule | ||
{ | ||
//The XML comment summary for this method. | ||
public string XMLSummary; | ||
|
||
//Whether this method is asynchronous or not. | ||
public bool Async; | ||
|
||
//The route for this method to call. | ||
public string Route; | ||
|
||
//The name of this method. | ||
public string Name; | ||
|
||
//Name of the data type that is received back. | ||
public ClassModule ReceivedDataType; | ||
} | ||
|
||
/// <summary> | ||
/// A single POST API method. | ||
/// </summary> | ||
public class POSTMethodModule : APIMethodModule | ||
{ | ||
//The data type to POST. | ||
public string DataType; | ||
} | ||
|
||
/// <summary> | ||
/// A single GET API method. | ||
/// </summary> | ||
public class GETMethodModule : APIMethodModule { } | ||
} |
Oops, something went wrong.