public void GenerateStaticPropertyForParameterCanInitilialize(string value)
        {
            var engine = new TemplatingEngine();

            var host = new DummyHost()
            {
                TemplateFile = "test.tt"
            };

            host.Parameters.Add("TestParameter", value);


            var tt = engine.CompileTemplate(T4ParameterSample, host);

            foreach (TemplateError error in host.Errors)
            {
                Console.Error.WriteLine(error.Message);
            }

            Type ttType = tt.textTransformation?.GetType();

            Assert.IsNotNull(ttType);

            var initMethod = ttType.GetMethod("Initialize");
            var parameter  = ttType.GetProperty("TestParameter", BindingFlags.Public | BindingFlags.Static);

            initMethod.Invoke(tt.textTransformation, null);

            Assert.AreEqual(value, parameter.GetValue(null));
        }
Exemple #2
0
        private static void GenerateModelFiles(ResourceLoader loader, DirectoryInfo outputDirectory, out Dictionary <string, string> geometryToClass)
        {
            var outDir = Path.Combine(outputDirectory.FullName, "Models");

            if (!Directory.Exists(outDir))
            {
                Directory.CreateDirectory(outDir);
            }

            geometryToClass = new Dictionary <string, string>();
            Mono.TextTemplating.TemplatingEngine engine = new TemplatingEngine();
            var template = engine.CompileTemplate(File.ReadAllText("../../../Templates/EntityTemplate.tt"), new TemplateGenerator());

            //
            //  var template = new EntityTemplate();
            //template.Initialize();

            //            template.Session["EntityModels"] = loader.EntityModels;
            ResourceConverterContext.EntityModels = loader.EntityModels;

            int count      = 0;
            int totalCount = loader.EntityModels.Count;

            foreach (var model in loader.EntityModels)
            {
                var pct = 100D * ((double)count / (double)totalCount);

                Log.Info($"Starting Template Processing for '{model.Key}'");

                //template.Session["CurrentModelName"] = model.Key;
                //template.Session["CurrentModel"] = model.Value;
                ResourceConverterContext.CurrentModelName = CodeTypeName(model.Value.Name);
                ResourceConverterContext.CurrentModel     = model.Value;

                var output     = template.Process();
                var outputPath = Path.Combine(outDir, CodeTypeName(model.Value.Name) + "Model.cs");
                if (File.Exists(outputPath))
                {
                    Log.Warn($"Class already exists: {outputPath} ({count}/{totalCount}) - {pct:F1}%");
                }
                else
                {
                    geometryToClass.Add(model.Key, ResourceConverterContext.CurrentModelName + "Model");
                    File.WriteAllText(outputPath, output);
                    Log.Info($"Successfully Processed Template for entity '{model.Key}' ({count}/{totalCount}) - {pct:F1}%");
                }



                count++;
                // Log.Info($"Successfully Processed Template for entity '{model.Key}' ({count}/{totalCount}) - {pct:F1}%");
            }
        }