internal Tuple<string, ICompilation, IMetadataImporter, MockErrorReporter> Compile(string source, bool includeLinq = false, bool expectErrors = false) { var sourceFile = new MockSourceFile("file.cs", source); var md = new MetadataImporter.ScriptSharpMetadataImporter(false); var n = new DefaultNamer(); var er = new MockErrorReporter(!expectErrors); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(md, er, n.GetTypeParameterName, tr => { var t = tr.Resolve(compilation.Compilation).GetDefinition(); return new JsTypeReferenceExpression(t.ParentAssembly, md.GetTypeSemantics(t).Name); }); var compiler = new Compiler.Compiler(md, n, rtl, er); var references = includeLinq ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib }; compilation = compiler.CreateCompilation(new[] { sourceFile }, references, null); var compiledTypes = compiler.Compile(compilation); if (expectErrors) { Assert.That(er.AllMessages, Is.Not.Empty, "Compile should have generated errors"); return Tuple.Create((string)null, compilation.Compilation, (IMetadataImporter)md, er); } er.AllMessagesText.Should().BeEmpty("Compile should not generate errors"); var js = new OOPEmulator.ScriptSharpOOPEmulator(md, rtl, er).Rewrite(compiledTypes, compilation.Compilation); js = new GlobalNamespaceReferenceImporter().ImportReferences(js); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); if (Output == OutputType.GeneratedScript) Console.WriteLine(script); return Tuple.Create(script, compilation.Compilation, (IMetadataImporter)md, er); }
internal Tuple <string, ICompilation, IMetadataImporter, MockErrorReporter> Compile(string source, bool includeLinq = false, bool expectErrors = false) { var sourceFile = new MockSourceFile("file.cs", source); var md = new MetadataImporter.ScriptSharpMetadataImporter(false); var n = new DefaultNamer(); var er = new MockErrorReporter(!expectErrors); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(md, er, n.GetTypeParameterName, tr => { var t = tr.Resolve(compilation.Compilation).GetDefinition(); return(new JsTypeReferenceExpression(t.ParentAssembly, md.GetTypeSemantics(t).Name)); }); var compiler = new Compiler.Compiler(md, n, rtl, er, allowUserDefinedStructs: false); var references = includeLinq ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib }; compilation = compiler.CreateCompilation(new[] { sourceFile }, references, null); var compiledTypes = compiler.Compile(compilation); if (expectErrors) { Assert.That(er.AllMessages, Is.Not.Empty, "Compile should have generated errors"); return(Tuple.Create((string)null, compilation.Compilation, (IMetadataImporter)md, er)); } er.AllMessagesText.Should().BeEmpty("Compile should not generate errors"); var js = new OOPEmulator.ScriptSharpOOPEmulator(compilation.Compilation, md, rtl, er).Rewrite(compiledTypes, compilation.Compilation); js = new GlobalNamespaceReferenceImporter().ImportReferences(js); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); if (Output == OutputType.GeneratedScript) { Console.WriteLine(script); } return(Tuple.Create(script, compilation.Compilation, (IMetadataImporter)md, er)); }
protected string Process(IEnumerable<JsType> types, IScriptSharpMetadataImporter metadataImporter = null) { metadataImporter = metadataImporter ?? new MockScriptSharpMetadataImporter(); IProjectContent proj = new CSharpProjectContent(); proj = proj.AddAssemblyReferences(new[] { Common.Mscorlib }); var comp = proj.CreateCompilation(); var er = new MockErrorReporter(true); var obj = new OOPEmulator.ScriptSharpOOPEmulator(metadataImporter, er); Assert.That(er.AllMessages, Is.Empty, "Should not have errors"); var rewritten = obj.Rewrite(types, comp); return string.Join("", rewritten.Select(s => OutputFormatter.Format(s, allowIntermediates: true))); }
protected string Process(IEnumerable<JsType> types, IScriptSharpMetadataImporter metadataImporter = null, IErrorReporter errorReporter = null, IMethod entryPoint = null) { metadataImporter = metadataImporter ?? new MockScriptSharpMetadataImporter(); IProjectContent proj = new CSharpProjectContent(); proj = proj.AddAssemblyReferences(new[] { Common.Mscorlib }); var comp = proj.CreateCompilation(); bool verifyNoErrors = errorReporter == null; errorReporter = errorReporter ?? new MockErrorReporter(); var obj = new OOPEmulator.ScriptSharpOOPEmulator(comp, metadataImporter, new MockRuntimeLibrary(), new MockNamer(), errorReporter); if (verifyNoErrors) Assert.That(((MockErrorReporter)errorReporter).AllMessages, Is.Empty, "Should not have errors"); var rewritten = obj.Process(types, comp, entryPoint); return string.Join("", rewritten.Select(s => OutputFormatter.Format(s, allowIntermediates: true))); }
protected string Process(IEnumerable <JsType> types, IScriptSharpMetadataImporter metadataImporter = null) { metadataImporter = metadataImporter ?? new MockScriptSharpMetadataImporter(); IProjectContent proj = new CSharpProjectContent(); proj = proj.AddAssemblyReferences(new[] { Common.Mscorlib }); var comp = proj.CreateCompilation(); var er = new MockErrorReporter(true); var obj = new OOPEmulator.ScriptSharpOOPEmulator(comp, metadataImporter, new MockRuntimeLibrary(), er); Assert.That(er.AllMessages, Is.Empty, "Should not have errors"); var rewritten = obj.Rewrite(types, comp); return(string.Join("", rewritten.Select(s => OutputFormatter.Format(s, allowIntermediates: true)))); }
private Tuple<string, ICompilation, INamingConventionResolver> Compile(string source, bool includeLinq = false) { var sourceFile = new MockSourceFile("file.cs", source); var nc = new MetadataImporter.ScriptSharpMetadataImporter(false); var er = new MockErrorReporter(true); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(nc, tr => { var t = tr.Resolve(compilation.Compilation).GetDefinition(); return new JsTypeReferenceExpression(t.ParentAssembly, nc.GetTypeSemantics(t).Name); }); var compiler = new Saltarelle.Compiler.Compiler.Compiler(nc, rtl, er); er.AllMessagesText.Should().BeEmpty("Compile should not generate errors"); var references = includeLinq ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib }; compilation = compiler.CreateCompilation(new[] { sourceFile }, references, null); var compiledTypes = compiler.Compile(compilation); var js = new OOPEmulator.ScriptSharpOOPEmulator(nc, er).Rewrite(compiledTypes, compilation.Compilation); js = new GlobalNamespaceReferenceImporter().ImportReferences(js); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); if (Output == OutputType.GeneratedScript) Console.WriteLine(script); return Tuple.Create(script, compilation.Compilation, (INamingConventionResolver)nc); }
public bool Compile(CompilerOptions options, ErrorReporterWrapper er) { string intermediateAssemblyFile = Path.GetTempFileName(), intermediateDocFile = Path.GetTempFileName(); try { // Compile the assembly var settings = MapSettings(options, intermediateAssemblyFile, intermediateDocFile, er); if (er.HasErrors) return false; var ctx = new CompilerContext(settings, new ConvertingReportPrinter(er)); var d = new Mono.CSharp.Driver(ctx); d.Compile(); if (er.HasErrors) return false; // Compile the script var nc = new MetadataImporter.ScriptSharpMetadataImporter(options.MinimizeScript); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(nc, tr => { var t = tr.Resolve(compilation.Compilation).GetDefinition(); return new JsTypeReferenceExpression(t.ParentAssembly, nc.GetTypeSemantics(t).Name); }); var compiler = new Saltarelle.Compiler.Compiler.Compiler(nc, rtl, er); var references = LoadReferences(ctx.Settings.AssemblyReferences, er); if (references == null) return false; compilation = compiler.CreateCompilation(options.SourceFiles.Select(f => new SimpleSourceFile(f)), references, options.DefineConstants); var compiledTypes = compiler.Compile(compilation); var js = new OOPEmulator.ScriptSharpOOPEmulator(nc, er).Rewrite(compiledTypes, compilation.Compilation); js = new GlobalNamespaceReferenceImporter().ImportReferences(js); if (er.HasErrors) return false; string outputAssemblyPath = !string.IsNullOrEmpty(options.OutputAssemblyPath) ? options.OutputAssemblyPath : Path.ChangeExtension(options.SourceFiles[0], ".dll"); string outputScriptPath = !string.IsNullOrEmpty(options.OutputScriptPath) ? options.OutputScriptPath : Path.ChangeExtension(options.SourceFiles[0], ".js"); try { File.Copy(intermediateAssemblyFile, outputAssemblyPath, true); } catch (IOException ex) { er.Message(7950, null, TextLocation.Empty, ex.Message); return false; } if (!string.IsNullOrEmpty(options.DocumentationFile)) { try { File.Copy(intermediateDocFile, options.DocumentationFile, true); } catch (IOException ex) { er.Message(7952, null, TextLocation.Empty, ex.Message); return false; } } string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); try { File.WriteAllText(outputScriptPath, script); } catch (IOException ex) { er.Message(7951, null, TextLocation.Empty, ex.Message); return false; } return true; } catch (Exception ex) { er.InternalError(ex.ToString(), DomRegion.Empty); return false; } finally { try { File.Delete(intermediateAssemblyFile); } catch {} try { File.Delete(intermediateDocFile); } catch {} } }