frhagn / Typewriter

Automatic TypeScript template generation from C# source files
http://frhagn.github.io/Typewriter
Apache License 2.0
536 stars 132 forks source link

Generating .cs files #88

Open SeriousM opened 8 years ago

SeriousM commented 8 years ago

Hi, I love your extension, it's just pure awesomeness!

I found out that it can be very useful to generate c# models as well (for transportation, etc) but there was a bug:

my code:

${
    using Typewriter.Extensions.Types;
    Template(Settings settings)
    {
        settings.OutputFilenameFactory = file => file.Classes.First().FullName + ".cs";
    }
}
namespace WebApi.TST
{
    $Classes(*Model)[
    public class $Name {
        $Properties(*)[
            public $Type $Name { get; set; }
        ]
    }
}

The problem is that it searches for "_Model" and generates as .cs file with a class called "_Model". This turns out to be a endless loop once you save the .tst file because every generated *Model class extended the loop by one.

I didn't found the source-line yet to prove my guess, but I think that the list of found classes/files aren't materialized before the loop starts.

Maybe you could fix the problem? Otherwise, maybe you can tell me how to exclude currently generated files / tst-files from the query at all.

Thanks!!

PS: I could use t4 as well, but I like your syntax much much more! :+1:

frhagn commented 8 years ago

Hi @SeriousM, Thanks, I'm glad you like it :)

I haven't really really thought about using Typewriter to generate C# files, cool idea! I'll look into excluding generated files from being re-generated.. You should be able to use a lambda filter to exclude the generated classes from generation. Try something like this:

${
    using Typewriter.Extensions.Types;
    Template(Settings settings)
    {
        settings.OutputFilenameFactory = file => file.Classes.First().FullName + ".cs";
    }
}
namespace WebApi.TST
{
    $Classes(c => c.Name.EndsWith("Model") && c.Namespace != "WebApi.TST")[
    public class $Name {
        $Properties[
            public $Type $Name { get; set; }
        ]
    }
}
SeriousM commented 8 years ago

I'll look into excluding generated files from being re-generated..

I guess you mean only the files that are generated by the current .tst file, right?

$Classes(c => c.Name.EndsWith("Model") && c.Namespace != "WebApi.TST")[

yep, that worked pretty well, thank you!

frhagn commented 8 years ago

Yes exactly, only to prevent the endless loop. C# files generated by other tst-templates should still be included in the generation.