示例#1
0
        public void ModelGeneration_Class_Dependency()
        {
            // create syntax tree
            var personSyntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
public class Person
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public Address Address { get; set; }
}");

            var addressSyntaxTree = CSharpSyntaxTree.ParseText(@"public class Address
{
    public int Id { get; set; }
    public string Country { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string Street { get; set; }
    public string HouseNumber { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { personSyntaxTree, addressSyntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, personSyntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var expected = @"import { Address } from './address.model';

export class Person {
	id: number;
	firstName: string;
	lastName: string;
	dateOfBirth: Date;
	address: Address;
}
";

            Assert.AreEqual(expected, generated);
        }
示例#2
0
        public void ModelGeneration_Class_BaseClass()
        {
            // create syntax tree
            var personSyntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
public class Person
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}");

            var studentSyntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
public class Student : Person
{
    public DateTime? YearOfGraduation { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { personSyntaxTree, studentSyntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, studentSyntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var expected = @"import { Person } from './person.model';

export class Student extends Person {
	yearOfGraduation?: Date;
}
";

            Assert.AreEqual(expected, generated);
        }
示例#3
0
        public Result Handle()
        {
            var analyzer  = new ModelAnalyzer(new ModelAnalysisContext());
            var converter = new ModelConverter();
            var generator = new ModelGenerator();
            var emitter   = new TypeScriptEmitter();

            var analysisResult = analyzer.Analyze(_configuration.InputPath);

            if (!analysisResult.Success)
            {
                return(Result.CreateError($"Source analysis error: {analysisResult.ErrorMessage}"));
            }

            log.Debug("Source analyzed");

            var tsClassModels = converter.ConvertClasses(analysisResult.Value.Classes);
            var tsEnumModels  = converter.ConvertEnums(analysisResult.Value.Enums);

            log.Debug("Models converted");

            foreach (var tsModel in tsClassModels)
            {
                var contents = generator.GenerateClass(tsModel);

                emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Model, contents);

                log.Debug($"Class {tsModel.Name} emitted");

                new TsGenerator().GenerateDataModelAST(tsModel, _configuration.OutputPath);
            }

            foreach (var tsModel in tsEnumModels)
            {
                var contents = generator.GenerateEnum(tsModel);

                emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Enum, contents);

                log.Debug($"Enum {tsModel.Name} emitted");

                new TsGenerator().GenerateEnumAST(tsModel, _configuration.OutputPath);
            }

            return(Result.CreateSuccess());
        }
示例#4
0
        public void ModelGeneration_Class_DateTime()
        {
            // create syntax tree
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
public class Person
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public System.DateTime DateOfBirth { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { syntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, syntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var expected = @"export class Person {
	id: number;
	firstName: string;
	lastName: string;
	dateOfBirth: Date;
}
";

            Assert.AreEqual(expected, generated);
        }
示例#5
0
        public void ModelGeneration_Class_GenericClass()
        {
            // create syntax tree
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System.Collections.Generic;
public class PagedDataResponse<T>
{
    public List<T> Rows { get; set; }
    public long TotalCount { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { syntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, syntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var expected = @"export class PagedDataResponse<T> {
	rows: T[];
	totalCount: number;
}
";

            Assert.AreEqual(expected, generated);
        }
示例#6
0
        public void ModelGeneration_Class_BuiltInTypes()
        {
            // create syntax tree
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
public class AllBuiltInTypes
{
    public bool Truth { get; set; }   
    public byte Bite { get; set; }
    public sbyte SmallBite { get; set; }
    public char Charred { get; set; }
    public decimal Decimus { get; set; }
    public double Twix { get; set; }
    public float Jinn { get; set; }
    public int Intelect { get; set; }
    public uint UnlimitedIntelect { get; set; }
    public long Earthworm { get; set; }
    public ulong Snake { get; set; }
    public object Everyone { get; set; }
    public short Shortayy { get; set; }
    public ushort VShortayy { get; set; }
    public string LoremIpsum { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { syntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, syntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var fields = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("truth", "boolean"),
                new KeyValuePair <string, string>("bite", "number"),
                new KeyValuePair <string, string>("smallBite", "number"),
                new KeyValuePair <string, string>("charred", "string"),
                new KeyValuePair <string, string>("decimus", "number"),
                new KeyValuePair <string, string>("twix", "number"),
                new KeyValuePair <string, string>("jinn", "number"),
                new KeyValuePair <string, string>("intelect", "number"),
                new KeyValuePair <string, string>("unlimitedIntelect", "number"),
                new KeyValuePair <string, string>("earthworm", "number"),
                new KeyValuePair <string, string>("snake", "number"),
                new KeyValuePair <string, string>("everyone", "any"),
                new KeyValuePair <string, string>("shortayy", "number"),
                new KeyValuePair <string, string>("vShortayy", "number"),
                new KeyValuePair <string, string>("loremIpsum", "string")
            };

            var expected = TypeScriptTestHelpers.BuildTypeScriptClass("AllBuiltInTypes", fields);

            Assert.AreEqual(expected, generated);
        }
示例#7
0
        public Result Handle()
        {
            var syncer = new Synchronizer();

            var modelConverter  = new ModelConverter();
            var webApiConverter = new WebApiConverter();

            var modelGenerator     = new ModelGenerator();
            var webClientGenerator = new WebClientGenerator();
            var tsGenerator        = new TsGenerator();

            var emitter = new TypeScriptEmitter();

            Generator generatorEnum;

            if (!Enum.TryParse(_configuration.Generator, true, out generatorEnum))
            {
                return(Result.CreateError("Unknown generator was specified"));
            }

            var analysisResult = syncer.Synchronize(_configuration.InputPath);

            if (!analysisResult.Success)
            {
                return(Result.CreateError($"Source analysis error: {analysisResult.ErrorMessage}"));
            }

            log.Debug("Source analyzed");

            var tsClassModels = modelConverter.ConvertClasses(analysisResult.Value.DataModels.Classes);
            var tsEnumModels  = modelConverter.ConvertEnums(analysisResult.Value.DataModels.Enums);
            var serviceModels = webApiConverter.ConvertControllers(analysisResult.Value.Controllers);

            log.Debug("Models converted");

            foreach (var tsModel in tsClassModels)
            {
                if (generatorEnum == Generator.Template)
                {
                    var contents = modelGenerator.GenerateClass(tsModel);
                    emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Model, contents);
                }
                else if (generatorEnum == Generator.Compiler)
                {
                    tsGenerator.GenerateDataModelAST(tsModel, _configuration.OutputPath);
                }

                log.Debug($"Class {tsModel.Name} emitted");
            }

            foreach (var tsModel in tsEnumModels)
            {
                if (generatorEnum == Generator.Template)
                {
                    var contents = modelGenerator.GenerateEnum(tsModel);
                    emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Enum, contents);
                }
                else if (generatorEnum == Generator.Compiler)
                {
                    tsGenerator.GenerateEnumAST(tsModel, _configuration.OutputPath);
                }

                log.Debug($"Enum {tsModel.Name} emitted");
            }

            foreach (var tsModel in serviceModels)
            {
                if (generatorEnum == Generator.Template)
                {
                    var contents = webClientGenerator.GenerateService(tsModel);
                    emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Service, contents);
                }
                else if (generatorEnum == Generator.Compiler)
                {
                    tsGenerator.GenerateServiceAST(tsModel, _configuration.OutputPath);
                }

                log.Debug($"Service {tsModel.Name} emitted");
            }

            return(Result.CreateSuccess());
        }
示例#8
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                // create an anonymous delegate
                FileExists fileExists = delegate(string p_OutputDir, string p_FileName, ref FileHandlingResult fhResult)
                {
                    // "File " + fileName +" exists.  What should I do?" [Overwrite], [Rename], [Cancel]
                    FileExistsForm fileExistsForm = new FileExistsForm();
                    fileExistsForm.Directory = p_OutputDir;
                    fileExistsForm.File      = p_FileName;
                    fileExistsForm.ShowDialog(this);
                    fhResult = fileExistsForm.Result;
                    fileExistsForm.Dispose();
                };

                ModelGenerator modelGen = new ModelGenerator(fileExists, NameSpace, chkPartial.Checked, chkPropChange.Checked, chkValidation.Checked);
                DbTableInfo    table;
                string         sTemplate;
                tsStatus.Text = "Generating ... ";
                int curPct = 0;
                tsProgressBar.Value = curPct;
                for (int i = 0; i < chkLbTables.Items.Count; i++)
                {
                    curPct = (int)((100.0 * i) / chkLbTables.Items.Count);
                    if (curPct >= tsProgressBar.Value + 5)
                    {
                        tsProgressBar.Value = curPct;
                        this.Refresh();
                    }
                    if (chkLbTables.GetItemChecked(i))
                    {
                        table = (DbTableInfo)chkLbTables.Items[i];

                        for (int j = 0; j < chkLbTemplates.Items.Count; j++)
                        {
                            if (chkLbTemplates.GetItemChecked(j))
                            {
                                sTemplate = (string)chkLbTemplates.Items[j];
                                modelGen.GenerateClass(sTemplate, table, OutputDir);
                                // cancel all
                                if (modelGen.GetFileHandlingResult() == (FileHandlingResult.Cancel | FileHandlingResult.All))
                                {
                                    tsProgressBar.Value = 0;
                                    goto loop_i;
                                }
                            }
                        }
                    }
                }
loop_i:
                tsProgressBar.Value = 100;
                tsStatus.Text       = "Code Generation Complete!";
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Arrow;
            }
        }