Renamed to QR Script

- Split into three projects: Library, Console, and Tests
- Library compiles as QRScript.Interpreter to avoid conflicts
This commit is contained in:
Tony Bark 2025-05-07 15:22:16 -04:00
parent e681e6144c
commit 6501b72973
16 changed files with 82 additions and 20 deletions

View file

@ -0,0 +1,75 @@
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);
}
}