private int GenerateSource(HxlCompiler compiler, List <HxlTemplate> templates) { string outputDirectory = "."; if (!string.IsNullOrWhiteSpace(Options.OutputFile)) { outputDirectory = Options.OutputFile; } foreach (var templ in templates) { var generatedSource = compiler.GenerateSource(templ); // TODO Get access to template builder another way var builder = (IHxlTemplateBuilder)templ; var outputFile = Path.Combine(outputDirectory, builder.TemplateName + ".g.cs"); // TODO Handle file errors more gracefully File.WriteAllText(outputFile, generatedSource); _logger.SavedOutputFile(outputFile); } return(0); }
protected void Load(string fixture) { var fixtureFileName = string.Format("Hxl/{0}.fixture", fixture); var myFixture = TestContext.LoadFixture(fixtureFileName).Items[0]; errors.Clear(); this.FixtureName = GetType().Name + "-" + fixture; this.Source = myFixture["input.hxl"]; this.InputHtml = ParseHtml(Source); HxlCompilerSettings settings = new HxlCompilerSettings(); settings.Namespaces.AddNew("test", new Uri("http://example.com/")); HxlCompiler c = HxlCompiler.Create(settings); HxlTemplate temp = c.ParseTemplate(this.Source); // UNDONE Currently, f-spec is using the stream context API from shared instead of f-core, // so we have to copy the data over and hack the generated template names to support the // proper fixture data names. var templates = myFixture.Where(t => t.Key.EndsWith(".hxl", StringComparison.Ordinal)) .Select(t => { var tpl = (ParsedTemplate)c.ParseTemplate(myFixture.GetStreamContext(t.Key).ReadAllText()); string name = WorkaroundTemplateName(fixtureFileName, t.Key); tpl.TemplateName = name; tpl.ClassName = name; return(tpl); }); var results = c.Compile(templates); this.Data = new Properties(); if (myFixture["data.properties"] != null) { var props = Properties.FromStream(myFixture.GetStreamContext("data.properties").OpenRead()); // So that we get some iterable content: // Use array syntax [a,b,c] foreach (var kvp in props) { string parsedValue = Convert.ToString(kvp.Value); if (parsedValue.Length > 0 && parsedValue[0] == '[') { var array = parsedValue.Substring(1, parsedValue.Length - 2).Split(','); this.Data.SetProperty(kvp.Key, array); } else { this.Data.SetProperty(kvp.Key, parsedValue); } } } var es = myFixture["output.cs"]; if (es == null) { _expectedSource = null; } else { var reader = new StringReader(es); var lines = new List <string>(); string line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } _expectedSource = lines; } GeneratedSource = c.GenerateSource(temp); OtherGeneratedSources = templates.Except(temp).ToDictionary(t => ((IHxlTemplateBuilder)t).TemplateName, t => c.GenerateSource(t)); var allErrors = results.Errors.Where(t => !t.IsWarning); if (allErrors.Any()) { WriteCompilerOutput(string.Empty, string.Empty); Assert.Fail("One or more compiler errors: {0}", allErrors.First()); } Assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath( results.PathToAssembly ); ExpectedHtml = myFixture["generated.html"]; CompilerErrors = results.Errors; }