Esempio n. 1
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));
        }
Esempio n. 2
0
        public HxlCompilerResults Compile(HxlCompilerSettings settings,
                                          IEnumerable <HxlTemplate> templates)
        {
            var parseOptions = new CSharpParseOptions(languageVersion: LanguageVersion.CSharp5);

            var session = new HxlCompilerSession(settings);

            string[] sourceFiles = templates.Select(t => GenerateSourceTempFile(session, t)).ToArray();

            // TODO Support generation of version in the output asm more easily
            // TODO Support private key signing in the output asm more easily
            string moduleName = "Hxl-" + session.SessionID;

            var emitOptions = new EmitOptions();

            var compilationOptions = new CSharpCompilationOptions(
                OutputKind.DynamicallyLinkedLibrary,
                moduleName,
                null,
                null,
                Empty <string> .List,
                settings.Debug ? OptimizationLevel.Debug : OptimizationLevel.Release,
                true,
                false,
                null,
                null,
                ImmutableArray <byte> .Empty,
                null,
                Platform.AnyCpu,
                ReportDiagnostic.Default,
                1,
                null,
                false,
                null,
                null,
                null,
                null,
                null
                );

            // TODO Previously, we indirectly supported other CodeDOM compiler parameters
            //   (embedded resources, etc.)
            settings.ImplicitAssemblyReferencePolicy.AddAssemblies(session.ImplicitAssemblyReferences);

            // TODO Only local paths are allowed
            var references = new List <MetadataReference>();

            references.AddRange(settings.Assemblies.Select(
                                    t => MetadataReference.CreateFromFile(t.Source.LocalPath,
                                                                          MetadataReferenceProperties.Assembly,
                                                                          null)).ToArray());

            var compilation = CSharpCompilation.Create(
                moduleName,
                GetParseTrees(parseOptions, sourceFiles),
                references,
                compilationOptions);
            var outputPath   = session.GetFileName(moduleName + ".dll");
            var outputStream = File.OpenWrite(outputPath);

            var results = compilation.Emit(outputStream,
                                           null,
                                           null,
                                           null,
                                           Empty <ResourceDescription> .List,
                                           emitOptions,
                                           CancellationToken.None);

            outputStream.Flush();
            outputStream.Dispose();

            return(new HxlCompilerResults(new HxlCompilerResultsAdapter(outputPath, results)));
        }