static async void testc() { var srcfile = System.IO.Path.Combine("D:\\git\\nel\\SmartContractBrowser\\SmartContractBrowser\\sample_contract", "sample_contract" + ".csproj"); try { DateTime t01 = DateTime.Now; Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create(); DateTime t02 = DateTime.Now; var proj = await workspace.OpenProjectAsync(srcfile); DateTime t03 = DateTime.Now; var com = await proj.GetCompilationAsync(); DateTime t04 = DateTime.Now; var ms = new System.IO.MemoryStream(); var mspdb = new System.IO.MemoryStream(); com.Emit(ms, mspdb); DateTime t05 = DateTime.Now; Console.WriteLine("init rosyln time=" + (t02 - t01).TotalMilliseconds); Console.WriteLine("open project time=" + (t03 - t02).TotalMilliseconds); Console.WriteLine("build time=" + (t04 - t03).TotalMilliseconds); Console.WriteLine("gen dll & pdb time=" + (t05 - t04).TotalMilliseconds); List <string> errinfo = new List <string>(); var bytes = ms.ToArray(); var bytespdb = mspdb.ToArray(); var b = BuildNeon(new System.IO.MemoryStream(bytes), new System.IO.MemoryStream(bytespdb), errinfo); } catch (Exception err) { } }
public static async Task <bool> Build2(List <string> errorinfo) { var srcfile = System.IO.Path.Combine(BuildPath, "sample_contract" + ".csproj"); try { Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create(); var proj = await workspace.OpenProjectAsync(srcfile); var com = await proj.GetCompilationAsync(); } catch (Exception err) { } return(true); }
public async Task <buildResult> buildSrc(string src, string temppath) { Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create(); var path = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location); //build proj var bts = System.Text.Encoding.UTF8.GetBytes(src); var hashname = ToHexString(sha1.ComputeHash(bts)); var outpath = System.IO.Path.Combine(path, temppath, hashname); lock (lockobj)//不要同时搞目录 { if (System.IO.Directory.Exists(outpath)) { System.IO.Directory.Delete(outpath, true); } if (System.IO.Directory.Exists(outpath) == false) { System.IO.Directory.CreateDirectory(outpath); } System.IO.File.Copy(System.IO.Path.Combine(path, "sample_contract\\mscorlib.dll"), System.IO.Path.Combine(outpath, "mscorlib.dll"), true); System.IO.File.Copy(System.IO.Path.Combine(path, "sample_contract\\Neo.SmartContract.Framework.dll"), System.IO.Path.Combine(outpath, "Neo.SmartContract.Framework.dll"), true); System.IO.File.Copy(System.IO.Path.Combine(path, "sample_contract\\sample_contract.csproj"), System.IO.Path.Combine(outpath, "sample_contract.csproj"), true); System.IO.File.WriteAllText(System.IO.Path.Combine(outpath, "Contract.cs"), src); } //srcfile var projfile = System.IO.Path.Combine(outpath, "sample_contract.csproj"); Project proj = null; try { proj = await workspace.OpenProjectAsync(projfile); var com = await proj.GetCompilationAsync(); var diag = com.GetDiagnostics(); buildResult br = new buildResult(); foreach (var d in diag) { if (d.Severity == DiagnosticSeverity.Warning) { Log item = new Log(); item.msg = d.GetMessage(); item.id = d.Id; if (d.Location.IsInSource) { var span = d.Location.GetLineSpan(); item.line = span.Span.Start.Line; item.col = span.Span.Start.Character; } //item.line =d.Location br.warnings.Add(item); } else if (d.Severity == DiagnosticSeverity.Error) { Log item = new Log(); item.msg = d.GetMessage(); item.id = d.Id; if (d.Location.IsInSource) { var span = d.Location.GetLineSpan(); item.line = span.Span.Start.Line; item.col = span.Span.Start.Character; } br.errors.Add(item); } } var ms = new System.IO.MemoryStream(); var mspdb = new System.IO.MemoryStream(); com.Emit(ms, mspdb); br.dll = ms.ToArray(); br.pdb = mspdb.ToArray(); ms.Close(); mspdb.Close(); return(br); } catch (Exception err) { return(null); } }