sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
706 stars 230 forks source link

Option to keep enums in one file #770

Closed NeilN1 closed 2 years ago

NeilN1 commented 2 years ago

Is there a way to place all generated enums in one file while allowing everything else to be generated in separate files? It would make providing lists to business analysts and other stakeholders easier.

sjh37 commented 2 years ago

See this single-enum-file-ttinclude.zip file which contains a EF.Reverse.POCO.v3.ttinclude to do just that. Replace your project file with this one.

What's changed:

In private void GenerateCode(IDbContextFilter filter) change the following to be:

WriteCodeOutputForGroup(codeGenerator, "Enumerations", true,
                codeOutputList.Files.Where(x => x.Key.StartsWith(enumType))
                .OrderBy(x => x.Key)
                .Select(x => x.Value)
                .ToList(),
                "Enums.cs"); // <<<----- This will be the name of the single enumerations file.

Replace the following functions with these versions:

private void WriteCodeOutputForGroup(CodeGenerator codeGenerator, string regionNameForGroup, bool writePreHeaderInfo, List<CodeOutput> list, string singleFilename = null)
{
    var count = 0;
    var max = list.Count;
    foreach (var co in list)
    {
        ++count;
        WriteCodeOutput(codeGenerator, co, writePreHeaderInfo, regionNameForGroup, count == 1, count == max, singleFilename);
    }
}

private void WriteCodeOutput(CodeGenerator codeGenerator, CodeOutput code, bool writePreHeaderInfo, string regionNameForGroup = null,
    bool firstInGroup = false, bool lastInGroup = false, string singleFilename = null)
{
    if (Settings.GenerateSeparateFiles)
    {
        if (singleFilename != null)
        {
            if (firstInGroup)
            {
                _fileManagementService.EndBlock();
                _fileManagementService.StartNewFile(singleFilename);

                if (writePreHeaderInfo)
                {
                    var preHeader = _preHeaderInfo.ToString();
                    if (!string.IsNullOrWhiteSpace(preHeader))
                        _fileManagementService.WriteLine(preHeader.Trim());
                }

                var header = _fileHeaderFooter.Header;
                if (!string.IsNullOrWhiteSpace(header))
                    _fileManagementService.WriteLine(header.Trim());

                var usings = codeGenerator.GenerateUsings(code.GetUsings());
                if (!string.IsNullOrWhiteSpace(usings))
                {
                    _fileManagementService.WriteLine("");
                    _fileManagementService.WriteLine(usings.Trim());
                }

                var ns = _fileHeaderFooter.Namespace;
                if (!string.IsNullOrWhiteSpace(ns))
                {
                    _fileManagementService.WriteLine("");
                    _fileManagementService.WriteLine(ns.Trim());
                }
            }

            WriteLines(IndentCode(code, regionNameForGroup, firstInGroup, lastInGroup));

            if(lastInGroup)
                _fileManagementService.WriteLine(_fileHeaderFooter.Footer);

            _fileManagementService.ForceWriteToOuter = false;
        }
        else
        {
            _fileManagementService.EndBlock();
            _fileManagementService.StartNewFile(code.Filename);

            // If generating separate files, check if the db context is the same name as the tt filename.
            // If it is the same, force writing to outer.
            if (Path.GetFileNameWithoutExtension(code.Filename).Equals(Settings.TemplateFile, StringComparison.CurrentCultureIgnoreCase))
                _fileManagementService.ForceWriteToOuter = true;

            if (writePreHeaderInfo)
            {
                var preHeader = _preHeaderInfo.ToString();
                if (!string.IsNullOrWhiteSpace(preHeader))
                    _fileManagementService.WriteLine(preHeader.Trim());
            }

            var header = _fileHeaderFooter.Header;
            if (!string.IsNullOrWhiteSpace(header))
                _fileManagementService.WriteLine(header.Trim());

            var usings = codeGenerator.GenerateUsings(code.GetUsings());
            if (!string.IsNullOrWhiteSpace(usings))
            {
                _fileManagementService.WriteLine("");
                _fileManagementService.WriteLine(usings.Trim());
            }

            var ns = _fileHeaderFooter.Namespace;
            if (!string.IsNullOrWhiteSpace(ns))
            {
                _fileManagementService.WriteLine("");
                _fileManagementService.WriteLine(ns.Trim());
            }

            WriteLines(IndentCode(code, regionNameForGroup, firstInGroup, lastInGroup));

            if (Settings.GenerateSeparateFiles)
                _fileManagementService.WriteLine(_fileHeaderFooter.Footer);

            _fileManagementService.ForceWriteToOuter = false;
        }
    }
}