public (PreviewerSession session, string error) GetPreviewerSession() { if (_previewer != null && _previewer.IsAlive) { return(_previewer, null); } var project = _workspace.FindProjectWithXamlFile(_path); if (project == null) { return(null, $"Can't resolve C# project for file {_path}"); } if (project.TargetPath == null) { return(null, $"Can't resolve TargetPath for {project.Name}"); } if (!File.Exists(project.TargetPath)) { return(null, $"Can't find {project.TargetPath}, make sure to compile your project"); } var s = PreviewerSessionConnector.Start(project.TargetPath); if (s.session == null) { return(s); } _previewer = s.session; _previewerAssemblyPath = project.TargetPath; s.session.UpdateXaml(_xaml, project.TargetPath); return(s); }
public static (PreviewerSession session, string error) Start(string targetAssembly) { targetAssembly = "/home/kekekeks/Projects/AvaloniaMaster/samples/ControlCatalog.NetCore/bin/Debug/netcoreapp2.0/ControlCatalog.NetCore.dll"; string targetDir = Path.GetDirectoryName(targetAssembly); string targetBasePath = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(targetAssembly)); var previewerPath = Path.Combine(targetDir, PreviewerDllName); if (!File.Exists(previewerPath)) { previewerPath = null; var depsJsonPath = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(targetAssembly) + ".deps.json"); if (File.Exists(depsJsonPath)) { var avaloniaDll = DepsJsonAssemblyListLoader.ParseFile(depsJsonPath) .FirstOrDefault(x => x.EndsWith("Avalonia.dll")); if (avaloniaDll != null) { previewerPath = Path.Combine(Path.GetDirectoryName(avaloniaDll), PreviewerPackagePath); if (!File.Exists(previewerPath)) { previewerPath = null; } } } } if (previewerPath == null) { return(null, "Unable to locate Avalonia.Designer.HostApp.dll"); } Process proc = null; try { using (var l = new OneShotTcpServer()) { var cmdline = $@"exec --runtimeconfig {targetBasePath}.runtimeconfig.json --depsfile { targetBasePath }.deps.json {previewerPath} --transport tcp-bson://127.0.0.1:{l.Port}/ {targetAssembly}"; proc = Process.Start(new ProcessStartInfo("dotnet", cmdline) { UseShellExecute = false }); var client = l.WaitForOneConnection(new TimeSpan(0, 0, 30)); PreviewerSession session = null; var transport = BsonTransportHack.CreateBsonTransport(client.GetStream(), () => { client.Dispose(); session?.OnTransportDisconnected(); }); session = new PreviewerSession(transport, () => { try { proc.Kill(); } catch { } }); return(session, null); } } catch (Exception e) { try { proc?.Kill(); } catch { //Ignore } proc?.Dispose(); return(null, "Unable to establish previewer transport connection: " + e); } return(null, null); }