- Split into three projects: Library, Console, and Tests - Library compiles as QRScript.Interpreter to avoid conflicts
75 lines
2.4 KiB
C#
75 lines
2.4 KiB
C#
namespace QRScript.Console;
|
|
|
|
[Command("build", Description = "Build Glyph program.")]
|
|
public class BuildCommand : ICommand
|
|
{
|
|
[CommandParameter(0, Name = "file", Description = "Path to the Glyph source file.")]
|
|
public string FilePath { get; set; }
|
|
|
|
[CommandOption("dry", 'd', Description = "Dry run.")]
|
|
public bool DryRun { get; set; }
|
|
|
|
[CommandOption("sample", 's', Description = "Generate ASCII sample.")]
|
|
public bool Sample { get; set; }
|
|
|
|
[CommandOption("byte", 'b', Description = "Convert source to byte code.")]
|
|
public bool ByteCode { get; set; }
|
|
|
|
[CommandOption("verbose", 'v', Description = "Enable verbose output.")]
|
|
public bool Verbose { get; set; }
|
|
|
|
public async ValueTask ExecuteAsync(IConsole console)
|
|
{
|
|
|
|
var fileInfo = new FileInfo(FilePath);
|
|
var file = fileInfo.FullName;
|
|
var dir = Path.GetDirectoryName(FilePath);
|
|
var path = Path.Combine(dir, file);
|
|
|
|
if (!File.Exists(FilePath))
|
|
{
|
|
await console.Error.WriteLineAsync($"Error: File not found: {file}");
|
|
return;
|
|
}
|
|
;
|
|
var lines = await File.ReadAllLinesAsync(path);
|
|
var source = lines.JoinLines();
|
|
|
|
if (ByteCode)
|
|
source = source.ToBase64();
|
|
|
|
if (Verbose)
|
|
{
|
|
await console.Output.WriteLineAsync($"Compiling: {path}");
|
|
}
|
|
|
|
if (source.ExceedsLengthLimit(1259))
|
|
await console.Output.WriteAsync($"Warning: source code exceeds {1259:n} characters.");
|
|
|
|
|
|
var qrGenerator = new QRCodeGenerator();
|
|
var qrCodeData = qrGenerator.CreateQrCode(source, QRCodeGenerator.ECCLevel.M);
|
|
var qrCode = new SvgQRCode(qrCodeData);
|
|
var asciiCode = new AsciiQRCode(qrCodeData);
|
|
var qrCodeAsASCII = asciiCode.GetGraphicSmall();
|
|
var qrCodeAsSvg = qrCode.GetGraphic(57);
|
|
|
|
if (string.IsNullOrEmpty(qrCodeAsSvg)
|
|
|| string.IsNullOrEmpty(qrCodeAsASCII))
|
|
{
|
|
await console.Output.WriteLineAsync("There was an issue in the rendering process.");
|
|
return;
|
|
}
|
|
|
|
if (Sample)
|
|
await console.Output.WriteLineAsync(qrCodeAsASCII);
|
|
|
|
if (DryRun)
|
|
return;
|
|
|
|
await console.Output.WriteLineAsync("Rendered successfully.");
|
|
|
|
var srcName = Path.GetFileNameWithoutExtension(fileInfo.Name);
|
|
File.WriteAllText(Path.Combine(dir, $"{srcName}.svg"), qrCodeAsSvg);
|
|
}
|
|
}
|