Esempio n. 1
0
        /// <summary>
        /// Generates TypeScript code and outputs it in the file system.
        /// </summary>
        /// <returns></returns>
        public override async Task Generate(CodeModel cm)
        {
            var codeModel = cm as CodeModelTs;

            if (codeModel == null)
            {
                throw new InvalidCastException("CodeModel is not a TypeScript code model.");
            }

            try
            {
                var utilsTemplate = new UtilsTemplate()
                {
                    Model = null
                };
                await Write(utilsTemplate, "httpUtil.ts");

                var modelsBinder       = new ModelsModelBinder();
                var clientModelsBinder = new ClientGroupsModelBinder();

                var models       = modelsBinder.Bind(codeModel);
                var clientModels = clientModelsBinder.Bind(codeModel);

                var modelTemplate = new ModelTemplate {
                    Model = models
                };
                await Write(modelTemplate, "model.ts");

                var clientTemplate = new ClientTemplate {
                    Model = clientModels
                };
                await Write(clientTemplate, "api.ts");
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var options = new CommandLineOptions();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                Environment.Exit(-1);
            }
            int randomSeed      = options.Seed ?? new Random().Next();
            var masterRandom    = new Random(randomSeed);
            var generatedScript = TSLGenerator.GetRandomTSLScript(randomSeed, masterRandom);

            var outputFolder = options.OutputFolder ?? ".";
            var testName     = options.TestName ?? $"GeneratedTSLTest{randomSeed}";
            var outputPath   = Path.Combine(outputFolder, testName);

            Directory.CreateDirectory(outputPath);

            var generatorContext = new TestCodeGeneratorContext(testName, generatedScript, masterRandom);

            Console.WriteLine("Generating files...");

            Console.WriteLine("Generating tsl script...");
            var tslTemplate = new TSLTemplate(generatedScript);

            File.WriteAllText(Path.Combine(outputPath, testName + ".tsl"), tslTemplate.TransformText());

            Console.WriteLine("Generating C# test code...");
            var csFileList = new List <string>();

            foreach (var element in generatedScript.TopLevelElements)
            {
                var fileName = $"{testName}_{element.Name}.cs";
                csFileList.Add(fileName);
                Console.WriteLine($"\tGenerating {fileName}...");
                var testCodeTemplate = new TestCodeTemplate(generatorContext, element);
                File.WriteAllText(Path.Combine(outputPath, fileName), testCodeTemplate.TransformText());
            }

            Console.WriteLine("Generating Utils.cs...");
            var utilsTemplate = new UtilsTemplate(testName);

            File.WriteAllText(Path.Combine(outputPath, "Utils.cs"), utilsTemplate.TransformText());

            Console.WriteLine("Generating packages.config...");
            var packagesConfigTemplate = new PackagesConfigTemplate();

            File.WriteAllText(Path.Combine(outputPath, "packages.config"), packagesConfigTemplate.TransformText());

            Console.WriteLine("Generating netfx project file...");
            var netfxProjectFileTemplate = new NetfxProjectFileTemplate(generatorContext, csFileList);

            File.WriteAllText(Path.Combine(outputPath, testName + ".csproj"), netfxProjectFileTemplate.TransformText());

            Console.WriteLine("Generating netcore project file...");
            var netcoreProjectFileTemplate = new NetcoreProjectFileTemplate(generatorContext);

            File.WriteAllText(Path.Combine(outputPath, testName + "_coreclr.csproj"), netcoreProjectFileTemplate.TransformText());

            Console.WriteLine("Done.");
        }