internal static FileStream CreateFile(string filePath, TextWriter outputWriter) { FileStream result; try { result = File.Create(filePath, 1024); } catch (Exception ex) { outputWriter?.WriteLine(CommonCompiler.GetErrorOutput(null, string.Format(Res.Compiler_Failed_To_Create_File, filePath, ex.Message))); result = null; } return(result); }
private static bool WriteMemoryStreamToFile(MemoryStream memoryStream, string filename, TextWriter outputWriter) { bool result; using (FileStream fileStream = CommonCompiler.CreateFile(filename, outputWriter)) { if (fileStream == null) { result = false; } else { memoryStream.Position = 0L; memoryStream.CopyTo(fileStream); result = true; } } return(result); }
internal CompilerResults Compile(CompilerParameters parameters, IEnumerable <SyntaxTree> syntaxTrees) { CompilerResults result; using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture)) { CompilerResults compilerResults = this.Compile(parameters, syntaxTrees, stringWriter); using (StringReader stringReader = new StringReader(stringWriter.ToString())) { string[] array = CommonCompiler.ReadAllLines(stringReader, Encoding.UTF8); string[] array2 = array; for (int i = 0; i < array2.Length; i++) { string value = array2[i]; compilerResults.Output.Add(value); } } result = compilerResults; } return(result); }
internal static string GetErrorOutput(string errorNumber, string errorText) { return(CommonCompiler.GetErrorOutput(null, 0, 0, errorNumber, errorText)); }
internal CompilerResults Compile(CompilerParameters parameters, IEnumerable <SyntaxTree> syntaxTrees, TextWriter outputWriter) { var compilerResults = new CompilerResults(parameters.TempFiles) { NativeCompilerReturnValue = 1 }; var commonCompilationArguments = this.ArgumentsFromParametersNoThrow(parameters, outputWriter); if (commonCompilationArguments == null) { return(compilerResults); } if (commonCompilationArguments.CmdArguments.DisplayLogo) { this.PrintLogo(outputWriter); } if (commonCompilationArguments.CmdArguments.DisplayHelp) { this.PrintHelp(outputWriter); return(null); } if (this.ErrorInDiagnostics(commonCompilationArguments.CmdArguments.Errors, compilerResults, outputWriter)) { return(compilerResults); } Compilation compilation = this.CreateCompilation(commonCompilationArguments, syntaxTrees, outputWriter); if (compilation == null || this.ErrorInDiagnostics(compilation.GetParseDiagnostics(default(CancellationToken)), compilerResults, outputWriter) || this.ErrorInDiagnostics(compilation.GetDeclarationDiagnostics(default(CancellationToken)), compilerResults, outputWriter)) { return(compilerResults); } SecurityPermission securityPermission = new SecurityPermission(SecurityPermissionFlag.ControlEvidence); securityPermission.Assert(); try { compilerResults.Evidence = parameters.Evidence; } finally { CodeAccessPermission.RevertAssert(); } bool flag = false; MemoryStream outputStream = new MemoryStream(); MemoryStream pdbStream = null; if (commonCompilationArguments.FinalPdbFilePath != null) { pdbStream = new MemoryStream(); } CompilerResults result; using (outputStream) { using (pdbStream) { try { FileStream documentationStream = null; string documentationPath = commonCompilationArguments.CmdArguments.DocumentationPath; if (documentationPath != null) { documentationStream = OpenFile(documentationPath, outputWriter, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Write | FileShare.Delete); if (documentationStream == null) { result = compilerResults; return(result); } documentationStream.SetLength(0L); } Stream win32resourceFileStream = null; if (!string.IsNullOrWhiteSpace(commonCompilationArguments.Win32ResourceFile)) { win32resourceFileStream = OpenFile(commonCompilationArguments.Win32ResourceFile, outputWriter, FileMode.Open, FileAccess.ReadWrite, FileShare.None); } EmitResult emitResult; using (win32resourceFileStream) { using (documentationStream) { //// Obsolete //emitResult = compilation.Emit(outputStream, // commonCompilationArguments.OutputFileName, // commonCompilationArguments.FinalPdbFilePath, // pdbStream, documentationStream, default(CancellationToken), // win32resourceFileStream, commonCompilationArguments.CmdArguments.ManifestResources); //var emitOptions = new EmitOptions(); emitResult = compilation.Emit(outputStream, pdbStream, null, win32resourceFileStream, commonCompilationArguments.CmdArguments.ManifestResources, null, null); } } if (this.ErrorInDiagnostics(emitResult.Diagnostics, compilerResults, outputWriter)) { result = compilerResults; } else { if (!parameters.GenerateInMemory) { if (!CommonCompiler.WriteMemoryStreamToFile(outputStream, commonCompilationArguments.FinalOutputPath, outputWriter)) { flag = true; result = compilerResults; return(result); } if (commonCompilationArguments.FinalPdbFilePath != null && !CommonCompiler.WriteMemoryStreamToFile(pdbStream, commonCompilationArguments.FinalPdbFilePath, outputWriter)) { flag = true; result = compilerResults; return(result); } compilerResults.PathToAssembly = parameters.OutputAssembly; } else { byte[] rawAssembly = outputStream.ToArray(); byte[] rawSymbolStore = null; if (pdbStream != null) { try { rawSymbolStore = pdbStream.ToArray(); } catch { } } SecurityPermission securityPermission2 = new SecurityPermission(SecurityPermissionFlag.ControlEvidence); securityPermission2.Assert(); try { compilerResults.CompiledAssembly = Assembly.Load(rawAssembly, rawSymbolStore, parameters.Evidence); } finally { CodeAccessPermission.RevertAssert(); } compilerResults.CompiledAssembly = Assembly.Load(rawAssembly, rawSymbolStore); } compilerResults.NativeCompilerReturnValue = 0; result = compilerResults; } } finally { if (flag) { if (commonCompilationArguments.FinalOutputPath != null) { CommonCompiler.TryDeleteFile(commonCompilationArguments.FinalOutputPath); } if (commonCompilationArguments.FinalPdbFilePath != null) { CommonCompiler.TryDeleteFile(commonCompilationArguments.FinalPdbFilePath); } } } } } return(result); }