コード例 #1
0
        private int CompileSource(HxlCompiler compiler, List <HxlTemplate> templates)
        {
            var compilerResult = compiler.Compile(templates);

            _logger.LogCompilerErrors(compilerResult.Errors);

            // TODO Not handling compile errors appropriately

            if (compilerResult.Errors.HasErrors)
            {
                throw new NotImplementedException();
            }

            if (compilerResult.NativeCompilerReturnValue != 0)
            {
                throw new NotImplementedException();
            }

            // TODO Handle file errors more gracefully

            File.Copy(
                compilerResult.PathToAssembly,
                Options.OutputFile,
                true);

            _logger.SavedOutputFile(Options.OutputFile);
            return(0);
        }
コード例 #2
0
        string GenerateSourceTempFile(HxlCompilerSession session, HxlTemplate template)
        {
            string name, type;

            HxlTemplateAttribute.NameOrDefault(template, out name, out type);
            string tempFile = CodeUtility.Slug(name) + "-" + ((ParsedTemplate)template).Signature + ".cs";

            using (var sw = session.CreateText(tempFile)) {
                HxlCompiler.GenerateOneSourceFile(session, template, sw);
            }
            return(session.GetFileName(tempFile));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        private int RunCore()
        {
            // TODO Validate options

            var collectedReferences = LoadReferences();
            var settings            = new HxlCompilerSettings();

            foreach (var reference in collectedReferences)
            {
                settings.Assemblies.AddNew(reference.Key, new Uri(reference.Value, UriKind.RelativeOrAbsolute));
            }
            // TODO Support output assembly name
            //   Path.GetFileNameWithoutExtension(Options.OutputFile);
            foreach (var kvp in Options.Namespaces)
            {
                settings.Namespaces.AddNew(kvp.Key, new Uri(kvp.Value));
            }

            var compiler  = HxlCompiler.Create(settings);
            var templates = new List <HxlTemplate>();

            foreach (var item in Options.Inputs.EnumerateFiles())
            {
                string file = item.File;
                _logger.ParsingTemplate(file);

                // TODO Get access to template builder another way
                var template = compiler.LoadTemplate(file);
                var builder  = (IHxlTemplateBuilder)template;

                var viewType = Options.BaseType
                               ?? TypeReference.Parse("Carbonfrost.Commons.Hxl.HxlViewTemplate");

                // TODO Drop this- should be using the type selector
                // TODO Allow type selector to be configurable
                builder.BaseClass = viewType;
                if (builder.ModelType == null)
                {
                    builder.BaseClass = viewType;
                }
                else
                {
                    string text = viewType.OriginalString + "<" + builder.ModelType + ">";
                    builder.BaseClass = TypeReference.Parse(text);
                }

                builder.TemplateName = PathHelper.UnixPath(PathHelper.MakeRelative(item.OriginalGlob, file));
                builder.ClassName    = CodeUtility.Slug(builder.TemplateName);
                templates.Add(template);
            }
            if (templates.Count == 0)
            {
                _logger.NoSourceFilesSpecified();
                return(1);
            }

            if (Options.NoCompile)
            {
                return(GenerateSource(compiler, templates));
            }
            else
            {
                return(CompileSource(compiler, templates));
            }
        }
コード例 #5
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;
        }