/// <summary> /// Compiles <paramref name="unit"/> and returns generated assembly. /// </summary> /// <param name="unit">Source code compile unit.</param> /// <returns>Generated assembly.</returns> private Assembly CompileCodeUnit(CodeCompileUnit unit) { IStaticCompiler compiler = compilerFactory.CreateStatic(); string assemblyFilePath = Path.Combine(configuration.TempDirectory(), nameFormatter.FormatAssemblyFileName()); ICompilerResult result = compiler.FromUnit(unit, assemblyFilePath); if (!result.IsSuccess) { // Save source code if compilation was not successfull. CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); string sourceCodePath = Path.Combine(configuration.TempDirectory(), nameFormatter.FormatSourceCodeFileName()); using (StreamWriter writer = new StreamWriter(sourceCodePath)) { provider.GenerateCodeFromCompileUnit(unit, writer, new CodeGeneratorOptions()); } Ensure.Exception.UnCompilableSource(sourceCodePath); } // Load compiled assembly. return(ReflectionFactory.FromCurrentAppDomain().LoadAssembly(assemblyFilePath)); }
public SharpKitTempProvider(IUniqueNameProvider tempNameProvider, ICompilerConfiguration configuration) { if (!Directory.Exists(configuration.TempDirectory())) { Directory.CreateDirectory(configuration.TempDirectory()); } TempDirectory = Path.Combine(configuration.TempDirectory(), tempNameProvider.Next()); if (!Directory.Exists(TempDirectory)) { Directory.CreateDirectory(TempDirectory); } string key = Guid.NewGuid().ToString("N"); InputCsFileName = String.Format("{0}.cs", key); InputCsFilePath = Path.Combine(TempDirectory, InputCsFileName); if (File.Exists(InputCsFilePath)) { File.Delete(InputCsFilePath); } OutputDllFileName = String.Format("{0}.dll", key); OutputDllFilePath = Path.Combine(TempDirectory, OutputDllFileName); if (File.Exists(OutputDllFilePath)) { File.Delete(OutputDllFilePath); } OutputJsFileName = String.Format("{0}.js", key); OutputJsFilePath = Path.Combine(TempDirectory, OutputJsFileName); if (File.Exists(OutputJsFilePath)) { File.Delete(OutputJsFilePath); } ManifestFileName = String.Format("{0}.skccache", key); ManifestFilePath = Path.Combine(TempDirectory, ManifestFileName); if (File.Exists(ManifestFilePath)) { File.Delete(ManifestFilePath); } }