public void CompileRetryIfErrors()
        {
            const string dllName = "CompileRetryIfErrorsAd";
            var path = TestUtils.GetExecutionPath();
            var sourceCorrect = ResourceContentLoader.LoadText("CorrectObjectAd.cs", Assembly.GetExecutingAssembly());
            var sourceFail = ResourceContentLoader.LoadText("FailObjectAd.cs", Assembly.GetExecutingAssembly());

            var sc = new SourceCompiler(dllName, path);
            sc.UseAppdomain = true;

            sc.AddFile("SharpTemplate.Test.Resources.Compilers", "CorrectObjectAd", sourceCorrect);
            sc.AddFile("SharpTemplate.Test.Resources.Compilers", "FailObjectAd", sourceFail);
            sc.LoadCurrentAssemblies();
            var loadedAssembly = sc.Compile(2);

            Assert.IsNotNull(loadedAssembly);
            Assert.IsTrue(sc.HasErrors);
            Assert.AreEqual(1, sc.Errors.Count);
            Assert.AreEqual(1, sc.Errors[0].Count);
            Assert.IsTrue(sc.Errors[0][0].Contains("SharpTemplate.Test.Compilers.ILoadedClass.GetAssemblyName"));

            AddFile(loadedAssembly);

            var content = File.ReadAllBytes(loadedAssembly);
            var compileSimpleObjectAsm = Assembly.Load(content);
            var instance = (ILoadedClass)Activator.CreateInstance(compileSimpleObjectAsm.FullName, "SharpTemplate.Test.Resources.Compilers.CorrectObjectAd").Unwrap();
            Assert.IsNotNull(instance);

            var result = instance.GetAssemblyName();
            Assert.AreEqual(dllName, result);
        }
        public void CompileRetryIfErrors()
        {
            const string dllName = "CompileRetryIfErrors";
            var path = TestUtils.GetExecutionPath();
            var sourceCorrect = ResourceContentLoader.LoadText("CorrectObject.cs", Assembly.GetExecutingAssembly());
            var sourceFail = ResourceContentLoader.LoadText("FailObject.cs", Assembly.GetExecutingAssembly());

            var sc = new SourceCompiler(dllName, path);

            sc.AddFile("SharpTemplate.Test.Resources.Compilers", "CorrectObject", sourceCorrect);
            sc.AddFile("SharpTemplate.Test.Resources.Compilers", "FailObject", sourceFail);
            sc.LoadCurrentAssemblies();
            var loadedAssembly = sc.Compile(2);

            Assert.IsNotNull(loadedAssembly);
            Assert.IsTrue(sc.HasErrors);
            Assert.AreEqual(1, sc.Errors.Count);
            Assert.AreEqual(1, sc.Errors[0].Count);
            Assert.IsTrue(sc.Errors[0][0].Contains("SharpTemplate.Test.Resources.Compilers.FailObject.NotImplemented"));

            AddFile(loadedAssembly);

            var content = File.ReadAllBytes(loadedAssembly);
            var compileSimpleObjectAsm = Assembly.Load(content);
            var instance = Activator.CreateInstance(compileSimpleObjectAsm.FullName, "SharpTemplate.Test.Resources.Compilers.CorrectObject").Unwrap();
            Assert.IsNotNull(instance);
            var method = instance.GetType().GetMethod("GetAssemblyName");
            Assert.IsNotNull(method);

            var result = method.Invoke(instance, new object[] { });
            Assert.AreEqual(dllName, result);
        }
        public void CompileSignedAssembly()
        {
            const string dllName = "CompileSignedAssemblyAd";
            var path = TestUtils.GetExecutionPath();
            var keyTemp = Path.Combine(path, "TestKey.snk");
            var keyContent = ResourceContentLoader.LoadBytes("TestKey.snk", Assembly.GetExecutingAssembly());
            File.WriteAllBytes(keyTemp, keyContent);
            AddFile(keyTemp);

            var sc = new SourceCompiler(dllName, path);
            sc.UseAppdomain = true;

            sc.Key = keyTemp;
            sc.LoadCurrentAssemblies();
            var loadedAssembly = sc.Compile();

            Assert.IsNotNull(loadedAssembly);
            Assert.IsFalse(sc.HasErrors);

            AddFile(loadedAssembly);

            var content = File.ReadAllBytes(loadedAssembly);
            var compileSimpleObjectAsm = Assembly.Load(content);
            Assert.IsNotNull(compileSimpleObjectAsm);

            var fullName = compileSimpleObjectAsm.FullName;
            Assert.IsTrue(fullName.Contains("3e48d4993a05c8f5"));
        }
예제 #4
0
		public static void Run(string path, string command)
		{
			if (!path.ToLowerInvariant().EndsWith(".cs"))
			{
				path = path + ".cs";
			}

			var loadedAssembly = string.Empty;
			var dllName = Guid.NewGuid().ToString().Replace("-", "");
			var className = dllName + "_class";
			var namespaceName = dllName + "_namespace";
			try
			{
				Type type;
				if (_runnables.ContainsKey(path))
				{
					var def = _runnables[path];
					if (def.Timestamp < File.GetLastWriteTime(path))
					{
						_runnables.Remove(path);
					}
				}

				if (!_runnables.ContainsKey(path))
				{
					var source = File.ReadAllText(path);
					var sc = new SourceCompiler(dllName, "tmp")
					{
						UseAppdomain = true
					};

					sc.AddFile(namespaceName, className, source);
					sc.LoadCurrentAssemblies();
					loadedAssembly = sc.Compile(0);

					if (sc.HasErrors)
					{
						var errs = new HashSet<string>();
						var compilationErrors = "Error compiling " + path;
						foreach (var errorList in sc.Errors)
						{
							var singleErrorList = errorList;
							foreach (var error in singleErrorList)
							{
								if (!errs.Contains(error))
								{
									compilationErrors += "\r\n" + error;
									errs.Add(error);
								}
							}
						}
						throw new Exception(compilationErrors.Replace("\t", "  "));
					}

					var content = File.ReadAllBytes(loadedAssembly);
					var compileSimpleObjectAsm = Assembly.Load(content);
					type =
						compileSimpleObjectAsm.GetTypes()
							.FirstOrDefault(a => a.GetMethods().Any(m => m.Name.ToLowerInvariant() == command.ToLowerInvariant()));
					_runnables.Add(path, new RunnableDefinition
					{
						Type = type,
						Timestamp = File.GetLastWriteTime(path)
					});
				}
				else
				{
					var def = _runnables[path];
					type = def.Type;
				}
				var instance = Activator.CreateInstance(type);
				var methodInfo = instance.GetType().GetMethods().First(m => m.Name.ToLowerInvariant() == command.ToLowerInvariant());
				methodInfo.Invoke(instance, new object[] { });
			}
			finally
			{
				if (File.Exists(loadedAssembly))
				{
					File.Delete(loadedAssembly);
				}
			}
		}
        private static string BuildParserClass(string dllName, string path, SharpClass sharpClass)
        {
            var sc = new SourceCompiler(dllName, path);
            sc.UseAppdomain = true;

            sc.AddFile(sharpClass);
            sc.LoadCurrentAssemblies();
            var loadedAssembly = sc.Compile();

            Assert.IsNotNull(loadedAssembly);
            Assert.IsFalse(sc.HasErrors);
            return loadedAssembly;
        }
        private static string BuildGenericClass(string dllName, string path,string nameSpace,string className,string source)
        {
            var sc = new SourceCompiler(dllName, path);
            sc.UseAppdomain = true;

            sc.AddFile(nameSpace,className,source);
            sc.LoadCurrentAssemblies();
            var loadedAssembly = sc.Compile();

            Assert.IsNotNull(loadedAssembly);
            Assert.IsFalse(sc.HasErrors);
            return loadedAssembly;
        }