mycroforge/MycroForge.CLI/CodeGen/DbEnvModifier.cs

38 lines
1014 B
C#

namespace MycroForge.CLI.CodeGen;
public class DbEnvModifier
{
private readonly Source _source;
private readonly string _importPath;
private readonly string _className;
private SourceMatch? _lastImport;
public DbEnvModifier(string source, string importPath, string className)
{
_source = new Source(source);
_importPath = importPath;
_className = className;
}
public string Rewrite()
{
InsertEntityImport();
return _source.Text;
}
private void InsertEntityImport()
{
_lastImport = _source
.FindAll($@"from\s+{Features.Db.FeatureName}\.entities(?:\..+)+\s*import\s+.+\s")
.LastOrDefault();
if (_lastImport is null)
throw new Exception("Could not find import insertion point.");
_source.InsertSingleLine(
_lastImport.EndIndex,
$"from {_importPath} import {_className}\n"
);
}
}