示例#1
0
		public void ReferenceInAdditionalLibPathCanBeLocated() {
			UsingFiles(() => {
				Directory.CreateDirectory("MyAdditionalReferencePath");
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);

				File.WriteAllText(Path.GetFullPath("MyAdditionalReferencePath\\Ref.cs"), @"public class Class1 { public void M() {} }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("MyAdditionalReferencePath\\Ref.cs") },
					OutputAssemblyPath = Path.GetFullPath("MyAdditionalReferencePath\\Ref.dll"),
					OutputScriptPath   = Path.GetFullPath("MyAdditionalReferencePath\\Ref.js"),
				};
				bool result = driver.Compile(options, false);
				Assert.That(result, Is.True);
				Assert.That(er.AllMessages, Is.Empty);

				File.WriteAllText(Path.GetFullPath("Out.cs"), @"class Class2 : Class1 { public void M2() {} }");
				options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath), new Reference("Ref.dll") },
					SourceFiles        = { Path.GetFullPath("Out.cs") },
					OutputAssemblyPath = Path.GetFullPath("Out.dll"),
					OutputScriptPath   = Path.GetFullPath("Out.js"),
					AdditionalLibPaths = { Path.GetFullPath("MyAdditionalReferencePath") },
				};

				result = driver.Compile(options, false);
				Assert.That(result, Is.True);
				Assert.That(File.Exists(Path.GetFullPath("Out.dll")), Is.True);

			}, "MyAdditionalReferencePath", "Out.cs", "Out.dll", "Out.js");
		}
示例#2
0
		public void UsingAliasedReferenceCausesError7998() {
			UsingFiles(() => {
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);

				File.WriteAllText(Path.GetFullPath("Ref.cs"), @"public class Class1 { public void M() {} }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("Ref.cs") },
					OutputAssemblyPath = Path.GetFullPath("Ref.dll"),
					OutputScriptPath   = Path.GetFullPath("Ref.js"),
				};
				bool result = driver.Compile(options, false);
				Assert.That(result, Is.True);
				Assert.That(er.AllMessages, Is.Empty);

				File.WriteAllText(Path.GetFullPath("Out.cs"), @"extern alias myalias; class Class2 : myalias::Class1 { public void M2() {} }");
				options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath), new Reference(Path.GetFullPath("Ref.dll"), alias: "myalias") },
					SourceFiles        = { Path.GetFullPath("Out.cs") },
					OutputAssemblyPath = Path.GetFullPath("Out.dll"),
					OutputScriptPath   = Path.GetFullPath("Out.js"),
				};

				result = driver.Compile(options, false);
				Assert.That(result, Is.False);
				Assert.That(er.AllMessages.Single().Code, Is.EqualTo(7998));
				Assert.That(er.AllMessages.Single().Args[0], Is.EqualTo("aliased reference"));

			}, "Ref.cs", "Ref.dll", "Ref.js", "Out.cs", "Out.dll", "Out.js");
		}
示例#3
0
		public void ConditionalSymbolsWork() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("File.cs"),
@"
public class C1 {
	public void M() {
#if MY_SYMBOL
		var x = ""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"";
#endif
	}
}");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("File.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js"),
					DefineConstants    = { "MY_SYMBOL" }
				};
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True);
				Assert.That(er.AllMessages.Select(m => m.Code).ToList(), Is.EquivalentTo(new[] { 219 }));                              // Verify that the symbol was passed to mcs.
				Assert.That(File.ReadAllText(Path.GetFullPath("Test.js")).Contains("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"));    // Verify that the symbol was passed to the script compiler.
			}, "File.cs", "Test.dll", "Test.js");
		}
示例#4
0
		public void ChangingTheWarningLevelWorks() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("File.cs"), @"public class C1 { public void M() { var x = 0l; } }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("File.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js"),
				};
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True);
				Assert.That(er.AllMessages.Select(m => m.Code).ToList(), Is.EquivalentTo(new[] { 219, 78 }));
			}, "File.cs", "Test.dll", "Test.js");

			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("File.cs"), @"public class C1 { public void M() { var x = 0l; } }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("File.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js"),
					WarningLevel       = 3,
				};
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True);
				Assert.That(er.AllMessages.Select(m => m.Code), Is.EqualTo(new[] { 219 }));
			}, "File.cs", "Test.dll", "Test.js");
		}
示例#5
0
        public int CompileSingleFileProgram()
        {
            var result = CompilerDriver.Compile(new CompilationOptions("."),
                                                _sourceProvider, _nullOutputProvider);

            return(result.Diagnostics.Count);
        }
示例#6
0
        public void Compile_exits_on_multiple_declaration_error()
        {
            const string source1        = @"namespace Test;
public int32 IntFunction() {}";
            const string source2        = @"namespace Test;
internal int32 IntFunction() {}";
            var          sourceProvider = new TestingSourceFileProvider();

            sourceProvider.Add(".", "file1.cle", source1);
            sourceProvider.Add(".", "file2.cle", source2);

            using (var outputProvider = new TestingOutputFileProvider())
            {
                var result = CompilerDriver.Compile(new CompilationOptions("."), sourceProvider, outputProvider);

                Assert.That(result.FailedCount, Is.EqualTo(1));
                Assert.That(result.ModuleCount, Is.EqualTo(1));
                Assert.That(result.SucceededCount, Is.EqualTo(0));

                Assert.That(result.Diagnostics, Has.Exactly(1).Items);
                Assert.That(result.Diagnostics[0].Code, Is.EqualTo(DiagnosticCode.MethodAlreadyDefined));
                Assert.That(result.Diagnostics[0].Actual, Is.EqualTo("Test::IntFunction"));
                Assert.That(result.Diagnostics[0].Position.Line, Is.EqualTo(2));
                Assert.That(result.Diagnostics[0].Position.ByteInLine, Is.EqualTo(0));
                Assert.That(result.Diagnostics[0].Module, Is.EqualTo("."));
                Assert.That(result.Diagnostics[0].Filename, Is.EqualTo("file2.cle"));
            }
        }
示例#7
0
        public void Compile_exits_on_semantic_error()
        {
            const string source         = @"namespace Test;

public bool TypeMismatch() { return 2; }";
            var          sourceProvider = new TestingSourceFileProvider();

            sourceProvider.Add(".", "main.cle", source);

            using (var outputProvider = new TestingOutputFileProvider())
            {
                var result = CompilerDriver.Compile(new CompilationOptions("."), sourceProvider, outputProvider);

                Assert.That(result.FailedCount, Is.EqualTo(1));
                Assert.That(result.ModuleCount, Is.EqualTo(1));
                Assert.That(result.SucceededCount, Is.EqualTo(0));

                Assert.That(result.Diagnostics, Has.Exactly(1).Items);
                Assert.That(result.Diagnostics[0].Code, Is.EqualTo(DiagnosticCode.TypeMismatch));
                Assert.That(result.Diagnostics[0].Actual, Is.EqualTo("int32"));
                Assert.That(result.Diagnostics[0].Expected, Is.EqualTo("bool"));
                Assert.That(result.Diagnostics[0].Position.Line, Is.EqualTo(3));
                Assert.That(result.Diagnostics[0].Position.ByteInLine, Is.EqualTo(36));
                Assert.That(result.Diagnostics[0].Module, Is.EqualTo("."));
                Assert.That(result.Diagnostics[0].Filename, Is.EqualTo("main.cle"));
            }
        }
示例#8
0
        public void Compile_exits_on_declaration_error()
        {
            const string source         = @"namespace Test;

public NonexistentType Main() {}";
            var          sourceProvider = new TestingSourceFileProvider();

            sourceProvider.Add(".", "main.cle", source);

            using (var outputProvider = new TestingOutputFileProvider())
            {
                var result = CompilerDriver.Compile(new CompilationOptions("."), sourceProvider, outputProvider);

                Assert.That(result.FailedCount, Is.EqualTo(1));
                Assert.That(result.ModuleCount, Is.EqualTo(1));
                Assert.That(result.SucceededCount, Is.EqualTo(0));

                Assert.That(result.Diagnostics, Has.Exactly(1).Items);
                Assert.That(result.Diagnostics[0].Code, Is.EqualTo(DiagnosticCode.TypeNotFound));
                Assert.That(result.Diagnostics[0].Actual, Is.EqualTo("NonexistentType"));
                Assert.That(result.Diagnostics[0].Position.Line, Is.EqualTo(3));
                Assert.That(result.Diagnostics[0].Position.ByteInLine, Is.EqualTo(7));
                Assert.That(result.Diagnostics[0].Module, Is.EqualTo("."));
                Assert.That(result.Diagnostics[0].Filename, Is.EqualTo("main.cle"));
            }
        }
示例#9
0
        public void Compile_produces_output_successfully(bool emitDisassembly)
        {
            const string source         = @"namespace Test;
[EntryPoint]
private int32 Main() { return 0; }";
            var          sourceProvider = new TestingSourceFileProvider();

            sourceProvider.Add(".", "main.cle", source);

            using (var outputProvider = new TestingOutputFileProvider())
            {
                var options = new CompilationOptions(".", emitDisassembly: emitDisassembly);
                var result  = CompilerDriver.Compile(options, sourceProvider, outputProvider);

                Assert.That(result.FailedCount, Is.EqualTo(0));
                Assert.That(result.ModuleCount, Is.EqualTo(1));
                Assert.That(result.SucceededCount, Is.EqualTo(1));
                Assert.That(result.Diagnostics, Has.Exactly(0).Items);

                Assert.That(outputProvider.ExecutableStream, Is.Not.Null);
                Assert.That(outputProvider.ExecutableStream !.Position, Is.GreaterThan(0));

                if (emitDisassembly)
                {
                    Assert.That(outputProvider.DisassemblyWriter, Is.Not.Null);
                    Assert.That(outputProvider.DisassemblyWriter !.ToString(), Does.Contain("Test::Main"));
                }
                else
                {
                    Assert.That(outputProvider.DisassemblyWriter, Is.Null);
                }
            }
        }
示例#10
0
		public void CanCompileAsyncTaskGenericMethod() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("File1.cs"), @"
using System.Threading.Tasks;
public class C1 {
	public async Task<int> M() {
		var t = new Task(() => {});
		await t;
		return 0;
	}
}");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("File1.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js")
				};
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True);
				Assert.That(File.Exists(Path.GetFullPath("Test.dll")), Is.True, "Assembly should be written");
				Assert.That(File.Exists(Path.GetFullPath("Test.js")), Is.True, "Script should be written");
			}, "File1.cs", "Test.dll", "Test.js");
		}
示例#11
0
        public void Compile_exits_if_main_module_provides_multiple_entry_points()
        {
            const string source1        = @"namespace Test;
[EntryPoint]
private int32 Main() { return 0; }";
            const string source2        = @"namespace Test::BetterFunctions;
[EntryPoint]
private int32 MuchBetterMain() { return 42; }";
            var          sourceProvider = new TestingSourceFileProvider();

            sourceProvider.Add(".", "file1.cle", source1);
            sourceProvider.Add(".", "file2.cle", source2);

            using (var outputProvider = new TestingOutputFileProvider())
            {
                var result = CompilerDriver.Compile(new CompilationOptions("."), sourceProvider, outputProvider);

                Assert.That(result.FailedCount, Is.EqualTo(1));
                Assert.That(result.ModuleCount, Is.EqualTo(1));
                Assert.That(result.SucceededCount, Is.EqualTo(0));

                Assert.That(result.Diagnostics, Has.Exactly(1).Items);
                Assert.That(result.Diagnostics[0].Code, Is.EqualTo(DiagnosticCode.MultipleEntryPointsProvided));
                Assert.That(result.Diagnostics[0].Actual, Is.EqualTo("Test::BetterFunctions::MuchBetterMain"));
                Assert.That(result.Diagnostics[0].Position.Line, Is.EqualTo(3));
                Assert.That(result.Diagnostics[0].Module, Is.EqualTo("."));
                Assert.That(result.Diagnostics[0].Filename, Is.EqualTo("file2.cle"));
            }
        }
示例#12
0
        public void Compile_fails_if_output_file_cannot_be_created(bool failExecutable, bool failDisassembly)
        {
            const string source         = @"namespace Test;
[EntryPoint]
private int32 Main() { return 0; }";
            var          sourceProvider = new TestingSourceFileProvider();

            sourceProvider.Add(".", "main.cle", source);

            using (var outputProvider = new TestingOutputFileProvider())
            {
                outputProvider.FailExecutable  = failExecutable;
                outputProvider.FailDisassembly = failDisassembly;
                var options = new CompilationOptions(".", emitDisassembly: true);
                var result  = CompilerDriver.Compile(options, sourceProvider, outputProvider);

                Assert.That(result.FailedCount, Is.EqualTo(1));
                Assert.That(result.ModuleCount, Is.EqualTo(1));
                Assert.That(result.SucceededCount, Is.EqualTo(0));

                Assert.That(result.Diagnostics, Has.Exactly(1).Items);
                Assert.That(result.Diagnostics[0].Code, Is.EqualTo(DiagnosticCode.CouldNotCreateOutputFile));
                Assert.That(result.Diagnostics[0].Position.Line, Is.EqualTo(0));
                Assert.That(result.Diagnostics[0].Module, Is.EqualTo("."));
                Assert.That(result.Diagnostics[0].Filename, Is.Null); // TODO: Remove this limitation
            }
        }
示例#13
0
        public int CompileSingleFileWithSemanticError()
        {
            var result = CompilerDriver.Compile(new CompilationOptions("."),
                                                _invalidSourceProvider, _nullOutputProvider);

            return(result.Diagnostics.Count);
        }
示例#14
0
		public void CanSwitchOnString() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("File1.cs"), @"
using System.Collections.Generic;
public class C1 {
	string F() { return ""X""; }
	public int M() {
		switch (F()) {
			case ""X"": return 1;
			case ""Y"": return 2;
			case ""Z"": return 3;
			default:    return 0;
		}
	}
}");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("File1.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js")
				};
				var driver = new CompilerDriver(new MockErrorReporter());
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True);
				Assert.That(File.Exists(Path.GetFullPath("Test.dll")), Is.True, "Assembly should be written");
				Assert.That(File.Exists(Path.GetFullPath("Test.js")), Is.True, "Script should be written");
			}, "File1.cs", "Test.dll", "Test.js");
		}
示例#15
0
文件: Program.cs 项目: polsys/cle
        public static int Main(string[] args)
        {
            // Read command line parameters
            if (!CommandLineParser.TryParseArguments(args, Console.Out, out var options))
            {
                return(1);
            }
            Debug.Assert(options != null);

            // TODO: Create a logger for diagnostics and output messages as they are produced

            // Create file interfaces
            var          sourceProvider = new SourceFileProvider(Directory.GetCurrentDirectory());
            const string outputName     = "out.exe"; // TODO: Determine this in CommandLineParser

            using (var outputProvider = new OutputFileProvider(Directory.GetCurrentDirectory(), outputName))
            {
                // Initialize a compiler instance and call it
                var result = CompilerDriver.Compile(options, sourceProvider, outputProvider);

                // Write an output summary
                PrintDiagnostics(result);
                PrintSummary(result);

                // Return an according return value
                return(result.FailedCount);
            }
        }
示例#16
0
		public void SigningWorks() {
			string key = "0702000000240000525341320004000001000100BF8CF25A8FAE18A956C58C7F0484E846B1DAF18C64DDC3C04B668894E90AFB7C796F86B2926EB59548DDF82097805AE0A981C553A639A0669B39BECD22C1026A3F8E0F90E01BF6993EA18F8E2EA60F4F1B1563FDBB9F8D501A0E0736C3ACCD6BA86B6B2002D20AE83A5E62218BC2ADA819FF0B1521E56801684FA07726EB6DAAC9DF138633A3495C1687045E1B98ECAC630F4BB278AEFF7D6276A88DFFFF02D556562579E144591166595656519A0620F272E8FE1F29DC6EAB1D14319A77EDEB479C09294F0970F1293273AA6E5A8DB32DB6C156E070672F7EEA2C1111E040FB8B992329CD8572D48D9BB256A5EE0329B69ABAFB227BBEEEF402F7383DE4EDB83947AF3B87F9ED7B2A3F3F4572F871020606778C0CEF86C77ECF6F9E8A5112A5B06FA33255A1D8AF6F2401DFA6AC3220181B1BB99D79C931B416E06926DA0E21B79DA68D3ED95CBBFE513990B3BFB4419A390206B48AC93BC397183CD608E0ECA794B66AEC94521E655559B7A098711D2FFD531BED25FF797B8320E415E99F70995777243C3940AF6672976EF37D851D93F765EC0F35FE641279F14400E227A1627CDDCCE09F6B3543681544A169DC78B6AF734AFDAF2C50015E6B932E6BD913619BA04FB5BE03428EAB072C64F7743E1E9DDDADE9DCA6A1E47C648BE01D9133F7D227FAE72337E662459B6A0CA11410FA0179F22312A534B5CABE611742A11A890B1893CD0402CE01778EDC921F0D27CBC96AEE75ECB4D4E083301A843E9716BBB0AD689FDEE275321EA915FD44F696883DAF4E3CAB3D0229283ED43FB12747";
			byte[] keyBytes = Enumerable.Range(0, key.Length / 2).Select(i => Convert.ToByte(key.Substring(i * 2, 2), 16)).ToArray();

			UsingFiles(() => {
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);

				File.WriteAllBytes(Path.GetFullPath("Key.snk"), keyBytes);
				File.WriteAllText(Path.GetFullPath("File.cs"), @"public class Class1 { public void M() {} }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("File.cs") },
					OutputAssemblyPath = Path.GetFullPath("File.dll"),
					OutputScriptPath   = Path.GetFullPath("File.js"),
					KeyFile            = Path.GetFullPath("Key.snk"),
				};

				bool result = driver.Compile(options, false);
				Assert.That(result, Is.True);
				Assert.That(er.AllMessages, Is.Empty);

				var asm = AssemblyDefinition.ReadAssembly("File.dll");
				Assert.That(asm.Name.PublicKeyToken, Is.EqualTo(new[] { 0xf5, 0xa5, 0x6d, 0x86, 0x8e, 0xa6, 0xbd, 0x2e }));
			}, "Key.snk", "File.cs", "File.dll", "File.js");
		}
示例#17
0
        static int Main(string[] args)
        {
            var options = ParseOptions(args, Console.Out, Console.Error);

            if (options != null)
            {
                var  driver = new CompilerDriver(new ExecutableErrorReporter(Console.Out));
                bool result = driver.Compile(options, true);
                return(result ? 0 : 1);
            }
            return(1);
        }
示例#18
0
		public void CompilingLiftedEqualityWorks() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("Test.cs"), @"public class C1 { public void M() { int? i1 = null; int i2 = 0; bool b = (i1 == i2); } }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("Test.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js")
				};
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True, "Compilation failed with " + string.Join(Environment.NewLine, er.AllMessagesText));
			}, "File1.cs", "Test.dll", "Test.js");
		}
示例#19
0
		public void NotSpecifyingAnyFilesToCompileIsAnError() {
			UsingFiles(() => {
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = {},
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js"),
				};
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);
				var result = driver.Compile(options, false);

				Assert.That(result, Is.False);
				Assert.That(er.AllMessages.Where(m => m.Severity == MessageSeverity.Error), Is.Not.Empty);
			}, "Test.dll", "Test.js");
		}
示例#20
0
		public void CanInitializeListWithCollectionInitializer() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("Test.cs"), @"using System.Collections.Generic; public class C1 { public void M() { var l = new List<int> { 1 }; } }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("Test.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js")
				};
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True, "Compilation failed with " + string.Join(Environment.NewLine, er.AllMessagesText));
			}, "File1.cs", "Test.dll", "Test.js");
		}
示例#21
0
        public void CanCompileMscorlib()
        {
            var opts = ReadProject(Path.GetFullPath(@"..\..\..\Runtime\src\Libraries\CoreLib\CoreLib.csproj"));

            try {
                var  er     = new MockErrorReporter();
                var  d      = new CompilerDriver(er);
                bool result = d.Compile(opts, false);
                Assert.That(result, Is.True);
                Assert.That(er.AllMessages, Is.Empty);
            }
            finally {
                try { File.Delete(Path.GetFullPath("output.dll")); } catch {}
                try { File.Delete(Path.GetFullPath("output.js")); } catch {}
            }
        }
示例#22
0
		private Tuple<bool, List<Message>> Compile(string source, string baseName, params string[] references) {
			var er = new MockErrorReporter();
			var driver = new CompilerDriver(er);

			File.WriteAllText(Path.GetFullPath(baseName + ".cs"), source);
			var options = new CompilerOptions { References = { new Reference(Common.MscorlibPath) },
				SourceFiles        = { Path.GetFullPath(baseName + ".cs") },
				OutputAssemblyPath = Path.GetFullPath(baseName + ".dll"),
				OutputScriptPath   = Path.GetFullPath(baseName + ".js"),
			};
			foreach (var r in references)
				options.References.Add(new Reference(Path.GetFullPath(r + ".dll")));

			bool result = driver.Compile(options, false);
			return Tuple.Create(result, er.AllMessages);
		}
示例#23
0
		public void NonExistentSourceFilesAreHandledGracefully() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("ExistentFile.cs"), @"class C1 { public void M() {} }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("NonExistentFile.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js"),
				};
				var er = new MockErrorReporter();
				var driver = new CompilerDriver(er);
				var result = driver.Compile(options, false);

				Assert.That(result, Is.False);
				Assert.That(er.AllMessages.Where(m => m.Severity == MessageSeverity.Error && m.Format.Contains("NonExistentFile.cs")), Is.Not.Empty);
			}, "ExistentFile.cs", "Test.dll", "Test.js");
		}
示例#24
0
        public void Debug_log_is_not_created_if_logging_is_disabled()
        {
            const string source         = @"namespace Test;
public bool SimpleMethod() { return true; }
public int32 ComplexMethod() { return 42; }";
            var          sourceProvider = new TestingSourceFileProvider();

            sourceProvider.Add(".", "main.cle", source);

            using (var outputProvider = new TestingOutputFileProvider())
            {
                var _ = CompilerDriver.Compile(new CompilationOptions("."),
                                               sourceProvider, outputProvider);

                Assert.That(outputProvider.DebugWriter, Is.Null);
            }
        }
示例#25
0
		public void CanCompileLockStatement() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("File1.cs"), @"using System.Collections.Generic; public class C1 { public void M() { lock (new object()) {} } }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("File1.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js")
				};
				var driver = new CompilerDriver(new MockErrorReporter());
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True);
				Assert.That(File.Exists(Path.GetFullPath("Test.dll")), Is.True, "Assembly should be written");
				Assert.That(File.Exists(Path.GetFullPath("Test.js")), Is.True, "Script should be written");
			}, "File1.cs", "Test.dll", "Test.js");
		}
示例#26
0
        public static bool DoWork(dynamic scTask, Func <AppDomain> appDomainInitializer)
        {
            var options = GetOptions(scTask);

            if (options == null)
            {
                return(false);
            }
            var driver = new CompilerDriver(new TaskErrorReporter(scTask.Log));

            try {
                return(driver.Compile(options, appDomainInitializer));
            }
            catch (Exception ex) {
                scTask.Log.LogErrorFromException(ex);
                return(false);
            }
        }
示例#27
0
		public void SimpleCompilationWorks() {
			UsingFiles(() => {
				File.WriteAllText(Path.GetFullPath("File1.cs"), @"using System.Collections; public class C1 { public JsDictionary M() { return null; } }");
				File.WriteAllText(Path.GetFullPath("File2.cs"), @"using System.Collections; public class C2 { public JsDictionary M() { return null; } }");
				var options = new CompilerOptions {
					References         = { new Reference(Common.MscorlibPath) },
					SourceFiles        = { Path.GetFullPath("File1.cs"), Path.GetFullPath("File2.cs") },
					OutputAssemblyPath = Path.GetFullPath("Test.dll"),
					OutputScriptPath   = Path.GetFullPath("Test.js")
				};
				var driver = new CompilerDriver(new MockErrorReporter());
				var result = driver.Compile(options, false);

				Assert.That(result, Is.True);
				Assert.That(File.Exists(Path.GetFullPath("Test.dll")), Is.True, "Assembly should be written");
				Assert.That(File.Exists(Path.GetFullPath("Test.js")), Is.True, "Script should be written");
			}, "File1.cs", "File2.cs", "Test.dll", "Test.js");
		}
示例#28
0
        public override bool Execute()
        {
            var options = GetOptions();

            if (options == null)
            {
                return(false);
            }
            var driver = new CompilerDriver(new TaskErrorReporter(Log));

            try {
                return(driver.Compile(options, true));
            }
            catch (Exception ex) {
                Log.LogErrorFromException(ex);
                return(false);
            }
        }
示例#29
0
        public static bool DoWork(dynamic taskOptions, TaskLoggingHelper log)
        {
            var options = GetOptions(taskOptions, log);

            if (options == null)
            {
                return(false);
            }

            var driver = new CompilerDriver(new TaskErrorReporter(log));

            try {
                return(driver.Compile(options));
            }
            catch (Exception ex) {
                log.LogErrorFromException(ex);
                return(false);
            }
        }
示例#30
0
        public void CanCompileProject()
        {
            var opts = ReadProject(Path.GetFullPath(@"..\..\..\Runtime\src\Tests\CoreLibTests\CoreLibTests.csproj"));

            opts.References.Clear();
            opts.References.Add(new Reference(Common.MscorlibPath));
            opts.AlreadyCompiled = true;
            try {
                var  er     = new MockErrorReporter();
                var  d      = new CompilerDriver(er);
                bool result = d.Compile(opts, false);
                Assert.That(result, Is.True);
                Assert.That(er.AllMessages, Is.Empty);
            }
            finally {
                try { File.Delete(Path.GetFullPath("output.dll")); } catch {}
                try { File.Delete(Path.GetFullPath("output.js")); } catch {}
            }
        }