Skip to content

Commit

Permalink
Fix declaration file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
xjarvik committed Mar 26, 2023
1 parent d96596e commit 48f61cd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
18 changes: 16 additions & 2 deletions FlatSharpDelta.Compiler/CodeWriters/SchemaCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public static string WriteCode(Schema schema, FileInfo declarationFile, Director

foreach (reflection.Object obj in schema.objects)
{
if (obj.declaration_file != IDeclarationFilePropertyExtensions.GetDeclarationFileString(declarationFile.FullName, declarationFileRelativeTo.FullName))
bool declarationFilePathsAreEqual = IDeclarationFilePropertyExtensions.DeclarationFilePathsAreEqual(
obj.declaration_file,
declarationFileRelativeTo.FullName,
IDeclarationFilePropertyExtensions.GetDeclarationFileString(declarationFile.FullName, declarationFileRelativeTo.FullName),
declarationFileRelativeTo.FullName
);

if (!declarationFilePathsAreEqual)
{
continue;
}
Expand All @@ -69,7 +76,14 @@ public static string WriteCode(Schema schema, FileInfo declarationFile, Director

foreach (reflection.Enum _enum in schema.enums)
{
if (_enum.declaration_file != IDeclarationFilePropertyExtensions.GetDeclarationFileString(declarationFile.FullName, declarationFileRelativeTo.FullName))
bool declarationFilePathsAreEqual = IDeclarationFilePropertyExtensions.DeclarationFilePathsAreEqual(
_enum.declaration_file,
declarationFileRelativeTo.FullName,
IDeclarationFilePropertyExtensions.GetDeclarationFileString(declarationFile.FullName, declarationFileRelativeTo.FullName),
declarationFileRelativeTo.FullName
);

if (!declarationFilePathsAreEqual)
{
continue;
}
Expand Down
9 changes: 8 additions & 1 deletion FlatSharpDelta.Compiler/FlatSharpCodeModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ public static string ModifyGeneratedCode(string generatedCode, IDictionary<FileI

foreach (reflection.Object obj in schema.objects)
{
if (obj.declaration_file == IDeclarationFilePropertyExtensions.GetDeclarationFileString(declarationFile.FullName, Program.ExecutingDirectory.FullName) && obj.IsReferenceType())
bool declarationFilePathsAreEqual = IDeclarationFilePropertyExtensions.DeclarationFilePathsAreEqual(
obj.declaration_file,
Program.ExecutingDirectory.FullName,
IDeclarationFilePropertyExtensions.GetDeclarationFileString(declarationFile.FullName, Program.ExecutingDirectory.FullName),
Program.ExecutingDirectory.FullName
);

if (declarationFilePathsAreEqual && obj.IsReferenceType())
{
generatedCode = ModifyListTypesInReferenceType(generatedCode, schema, obj);
}
Expand Down
10 changes: 9 additions & 1 deletion FlatSharpDelta.Compiler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,15 @@ static void RunCompiler(CompilerStartInfo startInfo)
// FlatSharp only generates code for an object if the declaration_file value equals the name of the file in which the object is declared.
schemaWithDeltaTypes.ForEachDeclarationFileProperty(property =>
{
FileInfo declarationFile = startInfo.InputFiles.Where(f => IDeclarationFilePropertyExtensions.GetDeclarationFileString(f.FullName, ExecutingDirectory.FullName) == property.declaration_file).FirstOrDefault();
FileInfo declarationFile = startInfo.InputFiles.Where(f =>
IDeclarationFilePropertyExtensions.DeclarationFilePathsAreEqual(
IDeclarationFilePropertyExtensions.GetDeclarationFileString(f.FullName, ExecutingDirectory.FullName),
ExecutingDirectory.FullName,
property.declaration_file,
ExecutingDirectory.FullName
)
)
.FirstOrDefault();

if (declarationFile != null)
{
Expand Down
10 changes: 10 additions & 0 deletions FlatSharpDelta.Compiler/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public static void SetAttribute(this IAttributesProperty self, string key, strin
static class IDeclarationFilePropertyExtensions
{
public static string GetDeclarationFileString(string declarationFilePath, string declarationFileRelativeTo) => $"//{Path.GetRelativePath(declarationFileRelativeTo, declarationFilePath).Replace("\\", "/")}";

public static bool DeclarationFilePathsAreEqual(string path1, string path1Base, string path2, string path2Base)
{
path1 = path1.Replace("\\", "/").Replace("/:/", "/").TrimStart('/');
path1Base = path1Base.Replace("\\", "/").Replace("/:/", "/").TrimStart('/');
path2 = path2.Replace("\\", "/").Replace("/:/", "/").TrimStart('/');
path2Base = path2Base.Replace("\\", "/").Replace("/:/", "/").TrimStart('/');

return Path.GetFullPath(path1, path1Base) == Path.GetFullPath(path2, path2Base);
}
}

static class SchemaExtensions
Expand Down

0 comments on commit 48f61cd

Please sign in to comment.