📄 csharpbindingcompilermanager.cs
字号:
string outstr = String.Concat(compilerFileName, compilerparameters.NoConfig ? " /noconfig" : String.Empty, " \"@", responseFileName, "\"");
TempFileCollection tf = new TempFileCollection();
Executor.ExecWaitWithCapture(outstr, tf, ref output, ref error);
ICompilerResult result = null;
if (compilerparameters.CsharpCompiler == CsharpCompiler.Csc) {
result = ParseOutput(tf, output, true);
} else {
// Using Mono so check the error file and parse it if it contains any
// content (Mono 1.1 sends errors to the error file).
FileInfo info = new FileInfo(error);
if (info.Length > 0) {
result = ParseOutput(tf, error, false);
} else {
result = ParseOutput(tf, output, true);
}
}
project.CopyReferencesToOutputPath(false);
File.Delete(responseFileName);
File.Delete(output);
File.Delete(error);
if (compilerparameters.CompileTarget == CompileTarget.Exe || compilerparameters.CompileTarget == CompileTarget.WinExe) {
WriteManifestFile(exe);
}
return result;
}
// code duplication: see VB.NET backend : VBBindingCompilerManager
void WriteManifestFile(string fileName)
{
string manifestFile = String.Concat(fileName, ".manifest");
if (File.Exists(manifestFile)) {
return;
}
StreamWriter sw = new StreamWriter(manifestFile);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
sw.WriteLine("");
sw.WriteLine("<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">");
sw.WriteLine(" <dependency>");
sw.WriteLine(" <dependentAssembly>");
sw.WriteLine(" <assemblyIdentity");
sw.WriteLine(" type=\"win32\"");
sw.WriteLine(" name=\"Microsoft.Windows.Common-Controls\"");
sw.WriteLine(" version=\"6.0.0.0\"");
sw.WriteLine(" processorArchitecture=\"X86\"");
sw.WriteLine(" publicKeyToken=\"6595b64144ccf1df\"");
sw.WriteLine(" language=\"*\"");
sw.WriteLine(" />");
sw.WriteLine(" </dependentAssembly>");
sw.WriteLine(" </dependency>");
sw.WriteLine("</assembly>");
sw.Close();
}
string GetMsCompilerFileName(string compilerVersion)
{
string runtimeDirectory = Path.Combine(fileUtilityService.NETFrameworkInstallRoot, compilerVersion);
if (compilerVersion.Length == 0 || compilerVersion == "Standard" || !Directory.Exists(runtimeDirectory)) {
runtimeDirectory = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
}
return String.Concat('"', Path.Combine(runtimeDirectory, "csc.exe"), '"');
}
string GetCompilerFileName(CSharpCompilerParameters parameters)
{
string name = String.Empty;
switch (parameters.CsharpCompiler) {
case CsharpCompiler.Csc:
name = GetMsCompilerFileName(parameters.CSharpCompilerVersion);
break;
case CsharpCompiler.Mcs:
name = System.Environment.GetEnvironmentVariable("ComSpec") + " /c mcs";
break;
case CsharpCompiler.Gmcs:
name = System.Environment.GetEnvironmentVariable("ComSpec") + " /c gmcs";
break;
}
return name;
}
ICompilerResult ParseOutput(TempFileCollection tf, string file, bool skipFirstLine)
{
StringBuilder compilerOutput = new StringBuilder();
StreamReader sr = File.OpenText(file);
// skip first whitespace line
if (skipFirstLine) {
sr.ReadLine();
}
CompilerResults cr = new CompilerResults(tf);
while (true) {
string curLine = sr.ReadLine();
compilerOutput.Append(curLine);
compilerOutput.Append('\n');
if (curLine == null) {
break;
}
curLine = curLine.Trim();
if (curLine.Length == 0) {
continue;
}
CompilerError error = new CompilerError();
// try to match standard errors
Match match = normalError.Match(curLine);
if (match.Success) {
error.Column = Int32.Parse(match.Result("${column}"));
error.Line = Int32.Parse(match.Result("${line}"));
error.FileName = Path.GetFullPath(match.Result("${file}"));
error.IsWarning = match.Result("${error}") == "warning";
error.ErrorNumber = match.Result("${number}");
error.ErrorText = match.Result("${message}");
} else {
match = monoNormalError.Match(curLine); // try to match standard mcs errors
if (match.Success) {
error.Column = 0; // no column info :/
error.Line = Int32.Parse(match.Result("${line}"));
error.FileName = Path.GetFullPath(match.Result("${file}"));
error.IsWarning = match.Result("${error}") == "warning";
error.ErrorNumber = match.Result("${number}");
error.ErrorText = match.Result("${message}");
} else {
match = generalError.Match(curLine); // try to match general csc errors
if (match.Success) {
error.IsWarning = match.Result("${error}") == "warning";
error.ErrorNumber = match.Result("${number}");
error.ErrorText = match.Result("${message}");
} else { // give up and skip the line
continue;
// error.IsWarning = false;
// error.ErrorText = curLine;
}
}
}
cr.Errors.Add(error);
}
sr.Close();
return new DefaultCompilerResult(cr, compilerOutput.ToString());
}
string GenerateMonoOptions(CSharpCompilerParameters compilerparameters, string outputFileName)
{
StringBuilder sb = new StringBuilder();
sb.Append(String.Concat("-out:\"", outputFileName, "\""));
sb.Append(Environment.NewLine);
if (compilerparameters.UnsafeCode) {
sb.Append("-unsafe");
sb.Append(Environment.NewLine);
}
sb.Append(String.Concat("-warn:", compilerparameters.WarningLevel));
sb.Append(Environment.NewLine);
if (compilerparameters.NoWarnings != null && compilerparameters.NoWarnings.Trim().Length > 0) {
sb.Append("-nowarn:");sb.Append(compilerparameters.NoWarnings.Trim());sb.Append(Environment.NewLine);
}
if (compilerparameters.Debugmode) {
sb.Append("-debug");sb.Append(Environment.NewLine);
}
if (compilerparameters.Optimize) {
sb.Append("-optimize");sb.Append(Environment.NewLine);
}
if (compilerparameters.UnsafeCode) {
sb.Append("-unsafe");sb.Append(Environment.NewLine);
}
if (compilerparameters.NoStdLib) {
sb.Append("-nostdlib");sb.Append(Environment.NewLine);
}
if (compilerparameters.DefineSymbols != null && compilerparameters.DefineSymbols.Trim().Length > 0) {
sb.Append("-define:");sb.Append('"');sb.Append(compilerparameters.DefineSymbols);sb.Append('"');sb.Append(Environment.NewLine);
}
if (compilerparameters.MainClass != null && compilerparameters.MainClass.Trim().Length > 0) {
sb.Append("-main:");sb.Append(compilerparameters.MainClass.Trim());sb.Append(Environment.NewLine);
}
switch (compilerparameters.CompileTarget) {
case CompileTarget.Exe:
sb.Append("-target:exe");
break;
case CompileTarget.WinExe:
sb.Append("-target:winexe");
break;
case CompileTarget.Library:
sb.Append("-target:library");
break;
case CompileTarget.Module:
sb.Append("-target:module");
break;
}
sb.Append(Environment.NewLine);
if (compilerparameters.GenerateXmlDocumentation) {
sb.Append("\"-doc:");sb.Append(Path.ChangeExtension(outputFileName, ".xml"));sb.Append('"');sb.Append(Environment.NewLine);
}
return sb.ToString();
}
string GetCompiledOutputName(CSharpCompilerParameters compilerparameters)
{
string extension = String.Empty;
switch (compilerparameters.CompileTarget) {
case CompileTarget.Exe:
case CompileTarget.WinExe:
extension = ".exe";
break;
case CompileTarget.Library:
extension = ".dll";
break;
case CompileTarget.Module:
extension = ".netmodule";
break;
}
return fileUtilityService.GetDirectoryNameWithSeparator(compilerparameters.OutputDirectory) + compilerparameters.OutputAssembly + extension;
}
bool IsNetModule(string fileName)
{
return Path.GetExtension(fileName).ToLower() == ".netmodule";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -