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 referenceFiles = this.GetReferenceFiles(fixedReferenceFiles); var sourceFiles = this.GetSourceFiles(fixedSourceFiles); sb.Append(output) .AppendLine() .Append(TargetSwitch) .AppendLine() .Append(NoLogoSwitch) .AppendLine(); if(args.Optimize) { sb.Append(OptimizeSwitch) .Append(args.Optimize ? "+" : "-") .AppendLine(); } if(args.Debug) { sb.Append(DebugSwitch) .Append(args.Debug ? "+" : "-") .AppendLine(); } if (!string.IsNullOrWhiteSpace(referenceFiles)) sb.AppendLine().Append(referenceFiles); if (!string.IsNullOrWhiteSpace(sourceFiles)) sb.AppendLine().Append(sourceFiles); var text = sb.ToString(); return text; }
public CSharpCompilerResult Compile(CSharpCompilerArgs args) { if (args == null) throw new ArgumentNullException("args"); var command = string.Empty; var commandArgs = string.Empty; var sw = new Stopwatch(); var responseFilename = string.Empty; try { sw.Start(); var framework = DotNetFramework.Version40; responseFilename = Path.GetTempFileName(); commandArgs = string.Format("@{0}", responseFilename); command = framework.CSharpCompilerBinFilePath; var responseFileText = this.GetCompilerArgString(args); File.WriteAllText(responseFilename, responseFileText); var processResult = null as ProcessRunnerResult; using(var processRunner = new ProcessRunner(command, commandArgs, redirectStreams: true)) { processRunner.OutputRecevied += this.Process_OnOutputReceived; processRunner.Start(); processResult = processRunner.WaitForExit(); } if(processResult == null) throw new InvalidOperationException("wait for exit returned null"); var exitCode = processResult.ExitCode; var isSuccess = exitCode == 0 && this.errorTextLines.Count == 0; var compilerResult = null as CSharpCompilerResult; sw.Stop(); if (isSuccess) { compilerResult = CSharpCompilerResult.GetSuccess(exitCode, sw.Elapsed, this.outputTextLines, this.warningTextLines, args.OutputFilepath); } else { compilerResult = CSharpCompilerResult.GetFailure(exitCode, sw.Elapsed, null, this.outputTextLines, this.warningTextLines, this.errorTextLines); } return compilerResult; } catch (Exception ex) { throw CSharpCompilerInvocationException.Get(command, commandArgs, ex); } finally { if(File.Exists(responseFilename)) { try { File.Delete(responseFilename); } catch(Exception) { } } this.outputTextLines.Clear(); this.warningTextLines.Clear(); this.errorTextLines.Clear(); } }
private CSharpCompilerResult CompileCore(DroneEnv env) { try { if (env == null) throw new ArgumentNullException("env"); var compiler = new CSharpCompiler(); this.log.Debug("checking if output dir exists '{0}'", env.Config.BuildDirPath); if (!Directory.Exists(env.Config.BuildDirPath)) { this.log.Debug("output dir not found. creating output bin dir"); Directory.CreateDirectory(env.Config.BuildDirPath); } var configDirpath = Path.GetDirectoryName(env.Config.FilePath); var resolvedBaseReferences = this.GetBaseReferenceFiles(env.Config.DroneReferencesDirPath).ToList(); var resolvedConfigReferences = this.ResolveReferenceFiles(env.Config.ReferenceFiles, configDirpath).ToList(); var referenceFiles = resolvedBaseReferences.Concat(resolvedConfigReferences).Distinct().ToList(); var sourceFiles = this.ResolveSourceFiles(env.Config.SourceFiles, configDirpath).ToList(); this.log.Debug("creating csharp compiler args"); var args = new CSharpCompilerArgs( env.Config.DirPath, env.Config.AssemblyFilePath, sourceFiles, referenceFiles); if (env.Flags.IsDebugEnabled) { args.Debug = true; args.Optimize = false; } else { args.Debug = false; args.Optimize = true; } if (this.log.IsDebugEnabled) { this.log.Debug("csharp args"); this.log.Debug("work dir: '{0}'", args.WorkDir); this.log.Debug("output filepath: '{0}'", args.OutputFilepath); this.log.Debug("source files:"); foreach (var file in sourceFiles) this.log.Debug(file); this.log.Debug("reference files:"); foreach (var file in referenceFiles) this.log.Debug(file); } this.log.Debug("calling csc compiler '{0}'...", env.Config.FileName); var result = compiler.Compile(args); this.log.Debug("csc result: {0}", result.IsSuccess); this.log.Debug("exit code: {0}", result.ExiteCode); if (result.IsSuccess) { this.log.Debug("output assembly filepath: '{0}'", result.Success.OutputAssemblyFilepath); var outputPath = Path.GetDirectoryName(result.Success.OutputAssemblyFilepath); foreach (var refFile in resolvedConfigReferences) File.Copy(refFile, Path.Combine(outputPath, Path.GetFileName(refFile)), true); } return result; } catch (Exception ex) { return CSharpCompilerResult.GetFailure(-1, TimeSpan.Zero, ex, Enumerable.Empty<string>(), Enumerable.Empty<string>(), Enumerable.Empty<string>()); } }