private string GetCompilerArgString(CSharpCompilerArgs args) { var sb = new StringBuilder(); var fixedSourceFiles = args.SourceFiles.Select(PathHelper.FixSlashesToOSFormat).ToList(); var fixedReferenceFiles = args.ReferenceFiles.Select(PathHelper.FixSlashesToOSFormat).ToList(); var output = this.GetOutputSwitch(args.OutputFilepath); var target = this.GetTargetSwitch(); var optimize = this.GetOptimizeSwitch(); var referenceFiles = this.GetReferenceFiles(fixedReferenceFiles); var sourceFiles = this.GetSourceFiles(fixedSourceFiles); sb.Append(output) .Append(" ") .Append(target) .Append(" ") .Append(optimize); if (!string.IsNullOrWhiteSpace(referenceFiles)) sb.Append(" ").Append(referenceFiles); if (!string.IsNullOrWhiteSpace(sourceFiles)) sb.Append(" ").Append(sourceFiles); var text = sb.ToString(); return text; }
public CSharpCompilerResult Compile(CSharpCompilerArgs args) { if (args == null) throw new ArgumentNullException("args"); var exitCode = 0; var outputText = string.Empty; var invocationError = null as Exception; var warnings = new List<string>(); var errors = new List<string>(); var framework = new FrameworkVersion40Info(); var compilerArgs = this.GetCompilerArgString(args); var psi = new ProcessStartInfo(framework.CSharpCompilerBinFilepath, compilerArgs); psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.WorkingDirectory = args.WorkDir; psi.CreateNoWindow = true; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = false; var process = new Process(); process.StartInfo = psi; process.ErrorDataReceived += (s, e) => { Console.WriteLine(e.Data); }; process.OutputDataReceived += (s, e) => { Console.WriteLine(e.Data); }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); process.CancelErrorRead(); process.CancelOutputRead(); exitCode = process.ExitCode; process.Dispose(); process = null; return new CSharpCompilerResult(exitCode, invocationError, outputText, args.OutputFilepath, warnings, errors); }
public ProjectCompilerResult Compile(ZMFile projectFile) { var filename = Path.GetFileNameWithoutExtension(projectFile.FileName); var outputFilename = string.Format("{0}.dll", filename); var outputFilepath = Path.Combine(projectFile.BuildDir, outputFilename); var workDir = projectFile.FileDir; if (!Directory.Exists(projectFile.BuildDir)) Directory.CreateDirectory(projectFile.BuildDir); var args = new CSharpCompilerArgs(workDir, outputFilepath, projectFile.SourceFiles, projectFile.ReferenceFiles); var csc = new CSharpCompiler(); var cscResult = csc.Compile(args); var result = new ProjectCompilerResult(cscResult.OutputAssemblyFilepath); return result; }