Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally emit debug information with compiled scripts #24

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added input.yarn
Empty file.
49 changes: 47 additions & 2 deletions src/YarnSpinner.Console/Commands/CompileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ namespace YarnSpinnerConsole
{
using System.IO;
using System.Linq;
using System.Text.Json;
using Yarn.Compiler;

public static class CompileCommand
{
public static void CompileFiles(FileInfo[] inputs, DirectoryInfo outputDirectory, string outputName, string outputStringTableName, string outputMetadataTableName, bool stdout)
public static void CompileFiles(YarnSpinnerConsole.CompileCommandOptions options)
{
var inputs = options.Inputs;
var compiledResults = YarnSpinnerConsole.CompileProgram(inputs);

if (stdout)
if (options.Stdout)
{
EmitCompilationResult(compiledResults, System.Console.Out);
return;
Expand All @@ -29,12 +31,17 @@ public static void CompileFiles(FileInfo[] inputs, DirectoryInfo outputDirectory

// ok so basically in here we do a quick check of the number of files we have
// if we only have one AND output is the default then we use that as our output name instead of Output
var outputName = options.OutputName;
if (inputs.Length == 1 && outputName.Equals("Output"))
{
// weird that this doesn't exist in the FileInfo...
outputName = Path.GetFileNameWithoutExtension(inputs[0].Name);
}

var outputStringTableName = options.OutputStringTableName;
var outputMetadataTableName = options.OutputMetadataTableName;
var outputDebugInfoName = options.OutputDebugInfoName;

if (string.IsNullOrEmpty(outputStringTableName))
{
outputStringTableName = $"{outputName}-Lines.csv";
Expand All @@ -44,7 +51,14 @@ public static void CompileFiles(FileInfo[] inputs, DirectoryInfo outputDirectory
outputMetadataTableName = $"{outputName}-Metadata.csv";
}

if (string.IsNullOrEmpty(outputDebugInfoName))
{
outputDebugInfoName = $"{outputName}-Debug.json";
}

var outputDirectory = options.OutputDirectory;
var programOutputPath = Path.Combine(outputDirectory.FullName, $"{outputName}.yarnc");
var debugInfoOutputPath = Path.Combine(outputDirectory.FullName, outputDebugInfoName);
var stringTableOutputPath = Path.Combine(outputDirectory.FullName, outputStringTableName);
var stringMetadatOutputPath = Path.Combine(outputDirectory.FullName, outputMetadataTableName);

Expand All @@ -56,6 +70,37 @@ public static void CompileFiles(FileInfo[] inputs, DirectoryInfo outputDirectory

Log.Info($"Wrote {programOutputPath}");

if (options.Debug)
{
using (var outStream = new FileStream(debugInfoOutputPath, FileMode.Create))
using (var jsonWriter = new Utf8JsonWriter(outStream))
{
jsonWriter.WriteStartObject();

foreach (var (name, node) in compiledResults.Program.Nodes)
{
jsonWriter.WritePropertyName(name);
jsonWriter.WriteStartArray();

for (var index = 0; index < node.Instructions.Count; index++)
{
var lineInfo = compiledResults.DebugInfo[name].GetLineInfo(index);

jsonWriter.WriteStartObject();
jsonWriter.WriteNumber("line", lineInfo.LineNumber);
jsonWriter.WriteNumber("column", lineInfo.CharacterNumber);
jsonWriter.WriteEndObject();
}

jsonWriter.WriteEndArray();
}

jsonWriter.WriteEndObject();
}

Log.Info($"Wrote {debugInfoOutputPath}");
}

using (var writer = new StreamWriter(stringTableOutputPath))
{
// Use the invariant culture when writing the CSV
Expand Down
31 changes: 29 additions & 2 deletions src/YarnSpinner.Console/YarnSpinnerConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ public class YarnSpinnerConsole
},
};

public class CompileCommandOptions
{
public FileInfo[] Inputs { get; set; }

public DirectoryInfo OutputDirectory { get; set; }

public string OutputName { get; set; }

public string OutputStringTableName { get; set; }

public string OutputMetadataTableName { get; set; }

public string OutputDebugInfoName { get; set; }

public bool Stdout { get; set; }

public bool Debug { get; set; }
}

/// <summary>
/// The entry point for the app.
/// </summary>
Expand Down Expand Up @@ -57,13 +76,21 @@ public static int Main(string[] args)
outputMetadataTableOption.AddAlias("--output-metadata-table-name");
compileCommand.AddOption(outputMetadataTableOption);

var outputDebugInfoOption = new Option<string>("-d", "Output debug information filename (default: {name}-Debug.json");
outputDebugInfoOption.AddAlias("--output-debug-info-name");
compileCommand.AddOption(outputDebugInfoOption);

var stdoutOption = new Option<bool>("--stdout", "Output machine-readable compilation result to stdout instead of to files");
compileCommand.AddOption(stdoutOption);

var generateDebugInfoOption = new Option<bool>("-g", "Generate debug files with line number information");
generateDebugInfoOption.AddAlias("--debug");
compileCommand.AddOption(generateDebugInfoOption);
}

compileCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create<FileInfo[], DirectoryInfo, string, string, string, bool>(CompileCommand.CompileFiles);
compileCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create<CompileCommandOptions>(CompileCommand.CompileFiles);

var runCommand = new System.CommandLine.Command("run", "Runs Yarn scripts in an interactive manner");
Command runCommand = new Command("run", "Runs Yarn scripts in an interactive manner");
{
Argument<FileInfo[]> inputsArgument = new Argument<FileInfo[]>("inputs", "the files to run")
{
Expand Down