Safely handle files and directories when building
This commit is contained in:
parent
a3d060735f
commit
eb5539f45f
1 changed files with 19 additions and 10 deletions
|
@ -15,20 +15,23 @@ public class BuildCommand : ICommand
|
|||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
|
||||
var file = new FileInfo(FilePath);
|
||||
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: {FilePath}");
|
||||
await console.Error.WriteLineAsync($"Error: File not found: {file}");
|
||||
return;
|
||||
}
|
||||
|
||||
var lines = await File.ReadAllLinesAsync(FilePath);
|
||||
;
|
||||
var lines = await File.ReadAllLinesAsync(path);
|
||||
var source = ScriptHelper.LinesToString(lines);
|
||||
|
||||
if (Verbose)
|
||||
{
|
||||
await console.Output.WriteLineAsync($"Compiling: {FilePath}");
|
||||
await console.Output.WriteLineAsync($"Compiling: {path}");
|
||||
}
|
||||
|
||||
var counter = ScriptHelper.CharacterCounter(1259, source);
|
||||
|
@ -36,15 +39,21 @@ public class BuildCommand : ICommand
|
|||
if (counter.checker)
|
||||
await console.Output.WriteAsync(counter.output);
|
||||
|
||||
if (DryRun)
|
||||
return;
|
||||
|
||||
var qrGenerator = new QRCodeGenerator();
|
||||
var qrCodeData = qrGenerator.CreateQrCode(source, QRCodeGenerator.ECCLevel.H);
|
||||
var qrCode = new SvgQRCode(qrCodeData);
|
||||
var qrCodeAsSvg = qrCode.GetGraphic(57);
|
||||
var path = Path.GetDirectoryName(FilePath);
|
||||
var srcName = Path.GetFileNameWithoutExtension(file.Name);
|
||||
File.WriteAllText(Path.Combine(path, $"{srcName}.svg"), qrCodeAsSvg);
|
||||
|
||||
if (string.IsNullOrEmpty(qrCodeAsSvg))
|
||||
return;
|
||||
|
||||
await console.Output.WriteLineAsync("Rendered successfully.");
|
||||
|
||||
if (DryRun)
|
||||
return;
|
||||
|
||||
var srcName = Path.GetFileNameWithoutExtension(fileInfo.Name);
|
||||
File.WriteAllText(Path.Combine(dir, $"{srcName}.svg"), qrCodeAsSvg);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue