// UnitTest // Make Surrogate scenario to compile conditionally // + check and delete the exe before building // + set Appartment state // + update all ExecutionClients incliding csslib // + when starting remove css and //x args //+ try to solve limitations with console Input redurectionlimi //+ ensure launcher is not build when building dll/exe without execution public string BuildSurrogateLauncher(string scriptAssembly, string tragetFramework, CompilerParameters compilerParams, ApartmentState appartmentState, string consoleEncoding) { //Debug.Assert(false); #if !net4 throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR."); #else var provider = CodeDomProvider.CreateProvider("C#", new Dictionary <string, string> { { "CompilerVersion", tragetFramework } }); compilerParams.OutputAssembly = GetLauncherName(scriptAssembly); compilerParams.GenerateExecutable = true; compilerParams.GenerateInMemory = false; compilerParams.IncludeDebugInformation = false; try { Utils.FileDelete(compilerParams.OutputAssembly, true); } catch (Exception e) { throw new ApplicationException("Cannot build surrogate host application", e); } if (compilerParams.CompilerOptions != null) { compilerParams.CompilerOptions = compilerParams.CompilerOptions.Replace("/d:TRACE", "") .Replace("/d:DEBUG", ""); } if (!AppInfo.appConsole) { compilerParams.CompilerOptions += " /target:winexe"; } string refAssemblies = ""; string appartment = "[STAThread]"; if (appartmentState == ApartmentState.MTA) { appartment = "[" + appartmentState + "Thread]"; } else if (appartmentState == ApartmentState.Unknown) { appartment = ""; } foreach (string asm in compilerParams.ReferencedAssemblies) { if (File.Exists(asm)) //ignore GAC (not full path) assemblies { refAssemblies += Assembly.ReflectionOnlyLoadFrom(asm).FullName + ":" + asm + ";"; } } compilerParams.ReferencedAssemblies.Clear(); //it is important to remove all asms as they can have absolute path to the wrong CLR asms branch compilerParams.ReferencedAssemblies.Add("System.dll"); foreach (var item in compilerParams.ReferencedAssemblies) { Debug.WriteLine(item); } string setEncodingSatement = ""; if (string.Compare(consoleEncoding, Settings.DefaultEncodingName, true) != 0) { setEncodingSatement = "try { Console.OutputEncoding = System.Text.Encoding.GetEncoding(\"" + consoleEncoding + "\"); } catch {}"; } string code = launcherCode .Replace("${REF_ASSEMBLIES}", refAssemblies) .Replace("${APPARTMENT}", appartment) .Replace("${CONSOLE_ENCODING}", setEncodingSatement) .Replace("${ASM_MANE}", Path.GetFileName(scriptAssembly)); CompilerResults retval; compilerParams.IncludeDebugInformation = true; bool debugLauncher = false; if (debugLauncher) { compilerParams.CompilerOptions += " /d:DEBUG"; string launcherFile = Environment.ExpandEnvironmentVariables(@"C:\Users\%USERNAME%\Desktop\New folder\script.launcher.cs"); File.WriteAllText(launcherFile, code); retval = provider.CompileAssemblyFromFile(compilerParams, launcherFile); } else { retval = provider.CompileAssemblyFromSource(compilerParams, code); } foreach (CompilerError err in retval.Errors) { if (!err.IsWarning) { throw CompilerException.Create(retval.Errors, true, false); } } CSSUtils.SetTimestamp(compilerParams.OutputAssembly, scriptAssembly); return(compilerParams.OutputAssembly); #endif }
// UnitTest // Make Surrogate scenario to compile conditionally // + check and delete the exe before building // + set Appartment state // + update all ExecutionClients incliding csslib // + when starting remove css and //x args //+ try to solve limitations with console Input redurectionlimi //+ ensure launcher is not build when building dll/exe without execution public string BuildSurrogateLauncher(string scriptAssembly, string tragetFramework, CompilerParameters compilerParams, ApartmentState appartmentState) { //string #if !net4 throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR."); #else var provider = CodeDomProvider.CreateProvider("C#", new Dictionary <string, string> { { "CompilerVersion", tragetFramework } }); compilerParams.OutputAssembly = GetLauncherName(scriptAssembly); compilerParams.GenerateExecutable = true; compilerParams.GenerateInMemory = false; compilerParams.IncludeDebugInformation = false; try { if (File.Exists(compilerParams.OutputAssembly)) { File.Delete(compilerParams.OutputAssembly); } } catch (Exception e) { throw new ApplicationException("Cannot build surrogate host application", e); } if (compilerParams.CompilerOptions != null) { compilerParams.CompilerOptions = compilerParams.CompilerOptions.Replace("/d:TRACE", "") .Replace("/d:DEBUG", ""); } if (!AppInfo.appConsole) { compilerParams.CompilerOptions += " /target:winexe"; } string refAssemblies = ""; string appartment = "[STAThread]"; if (appartmentState == ApartmentState.MTA) { appartment = "[" + appartmentState + "Thread]"; } else if (appartmentState == ApartmentState.Unknown) { appartment = ""; } foreach (string asm in compilerParams.ReferencedAssemblies) { if (File.Exists(asm)) //ignore GAC (not full path) assemblies { refAssemblies += Assembly.ReflectionOnlyLoadFrom(asm).FullName + ":" + asm + ";"; } } string code = launcherCode .Replace("${REF_ASSEMBLIES}", refAssemblies) .Replace("${APPARTMENT}", appartment) .Replace("${ASM_MANE}", Path.GetFileName(scriptAssembly)); CompilerResults retval; bool debugLauncher = false; if (debugLauncher) { compilerParams.IncludeDebugInformation = true; compilerParams.CompilerOptions += " /d:DEBUG"; //string launcherFile = @"C:\Users\OSH\Desktop\New folder (2)\script.launcher.cs"; string launcherFile = Path.GetTempFileName(); File.WriteAllText(launcherFile, code); retval = provider.CompileAssemblyFromFile(compilerParams, launcherFile); } else { retval = provider.CompileAssemblyFromSource(compilerParams, code); } if (retval.Errors.Count != 0) { throw CompilerException.Create(retval.Errors, true); } CSSUtils.SetTimestamp(compilerParams.OutputAssembly, scriptAssembly); return(compilerParams.OutputAssembly); #endif }