Skip to content

Commit

Permalink
loresoft#560 Add option to delete unused files
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Boussaer authored and Arnaud Boussaer committed Apr 17, 2024
1 parent 83aa54c commit 6e3264e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/EntityFrameworkCore.Generator.Core/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ private void GenerateQueryExtensions(EntityContext entityContext)

private void GenerateMappingClasses(EntityContext entityContext)
{
if (Options.Data.Mapping.DeleteUnusedFiles)
DeleteUnusedFiles(Options.Data.Mapping.Directory, entityContext.Entities.Select(e => e.MappingClass).ToHashSet(), "mapping");

foreach (var entity in entityContext.Entities)
{
Options.Variables.Set(entity);
Expand All @@ -111,6 +114,9 @@ private void GenerateMappingClasses(EntityContext entityContext)

private void GenerateEntityClasses(EntityContext entityContext)
{
if (Options.Data.Entity.DeleteUnusedFiles)
DeleteUnusedFiles(Options.Data.Entity.Directory, entityContext.Entities.Select(e => e.EntityClass).ToHashSet(), "entity");

foreach (var entity in entityContext.Entities)
{
Options.Variables.Set(entity);
Expand All @@ -130,6 +136,20 @@ private void GenerateEntityClasses(EntityContext entityContext)
}
}

private void DeleteUnusedFiles(string directory, HashSet<string> fileNamesToBeGenerated, string fileType)
{
var existingFiles = Directory.EnumerateFiles(directory, "*.cs");
foreach (var existingFile in existingFiles)
{
var fileName = Path.GetFileNameWithoutExtension(existingFile);
if (!fileNamesToBeGenerated.Contains(fileName))
{
_logger.LogInformation("Deleting {fileType} class: {file}.cs", fileType, fileName);
File.Delete(existingFile);
}
}
}

private void GenerateDataContext(EntityContext entityContext)
{

Expand Down
2 changes: 2 additions & 0 deletions src/EntityFrameworkCore.Generator.Core/OptionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private static void MapMapping(MappingClassOptions option, MappingClass mapping)

option.Temporal = mapping.Temporal;
option.RowVersion = mapping.RowVersion;
option.DeleteUnusedFiles = mapping.DeleteUnusedFiles;
}

private static void MapEntity(EntityClassOptions option, EntityClass entity)
Expand All @@ -154,6 +155,7 @@ private static void MapEntity(EntityClassOptions option, EntityClass entity)
option.EntityNaming = entity.EntityNaming;
option.RelationshipNaming = entity.RelationshipNaming;
option.PrefixWithSchemaName = entity.PrefixWithSchemaName;
option.DeleteUnusedFiles = entity.DeleteUnusedFiles;

MapSelection(option.Renaming, entity.Renaming);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ public EntityClassOptions(VariableDictionary variables, string prefix)
/// The renaming expressions.
/// </value>
public SelectionOptions Renaming { get; }

/// <summary>
/// If true, files without a corresponding database table will be removed in the folder
/// </summary>
[DefaultValue(false)]
public bool DeleteUnusedFiles { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public MappingClassOptions(VariableDictionary variables, string prefix)
[DefaultValue(RowVersionMapping.ByteArray)]
public RowVersionMapping RowVersion { get; set; } = RowVersionMapping.ByteArray;

/// <summary>
/// If true, files without a corresponding database table will be removed in the folder
/// </summary>
[DefaultValue(false)]
public bool DeleteUnusedFiles { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ public EntityClass()
/// The renaming expressions.
/// </value>
public SelectionModel Renaming { get; set; }

/// <summary>
/// If true, files without a corresponding database table will be removed in the folder
/// </summary>
[DefaultValue(false)]
public bool DeleteUnusedFiles { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ public MappingClass()
[DefaultValue(RowVersionMapping.ByteArray)]
public RowVersionMapping RowVersion { get; set; } = RowVersionMapping.ByteArray;

/// <summary>
/// If true, files without a corresponding database table will be removed in the folder
/// </summary>
[DefaultValue(false)]
public bool DeleteUnusedFiles { get; set; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ data:
relationshipNaming: Plural
document: false
prefixWithSchemaName: false
deleteUnusedFiles: false
renaming:
entities:
- ^(sp|tbl|udf|vw)_
Expand All @@ -43,6 +44,7 @@ data:
namespace: '{Project.Namespace}.Data.Mapping'
directory: '{Project.Directory}\Data\Mapping'
document: false
deleteUnusedFiles: false
query:
generate: true
indexPrefix: By
Expand Down

0 comments on commit 6e3264e

Please sign in to comment.