mirror of
https://github.com/Noratrieb/viewstrap.git
synced 2026-01-14 16:45:10 +01:00
implement more
This commit is contained in:
parent
c5c87feef2
commit
7afcfa727b
3 changed files with 142 additions and 30 deletions
|
|
@ -28,6 +28,21 @@
|
|||
display: flex;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
#compilers-wrapper {
|
||||
display: flex;
|
||||
height: 100px;
|
||||
}
|
||||
|
||||
.compiler {
|
||||
height: 70px;
|
||||
width: 150px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border: 1px black solid;
|
||||
background: rgb(185, 210, 181);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
|
@ -36,7 +51,14 @@
|
|||
<div class="source-box"><div class="source-box-text">LIBRARY</div></div>
|
||||
</div>
|
||||
|
||||
<button onclick="startWs()">build me a compiler</button>
|
||||
<div id="compilers-wrapper"></div>
|
||||
|
||||
<button class="compile-action" disabled onclick="compile()">
|
||||
build me a compiler
|
||||
</button>
|
||||
<button class="compile-action" disabled onclick="updateCompilers()">
|
||||
update compilers
|
||||
</button>
|
||||
|
||||
<br />
|
||||
<br />
|
||||
|
|
@ -45,18 +67,21 @@
|
|||
<textarea id="stderr" rows="30" cols="80"></textarea>
|
||||
|
||||
<script>
|
||||
function startWs() {
|
||||
console.log("starting ws");
|
||||
const stdout = document.getElementById("stdout");
|
||||
const stderr = document.getElementById("stderr");
|
||||
|
||||
stdout.value = "";
|
||||
stderr.value = "";
|
||||
const compilersWrapper = document.getElementById("compilers-wrapper");
|
||||
|
||||
const compileActions = document.querySelectorAll(".compile-action");
|
||||
|
||||
const ws = new WebSocket("ws://localhost:3000/ws");
|
||||
|
||||
ws.addEventListener("open", () => {
|
||||
ws.send("bootstrap me");
|
||||
for (elem of compileActions) {
|
||||
elem.disabled = false;
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify("ListCompilers"));
|
||||
});
|
||||
|
||||
ws.addEventListener("message", (event) => {
|
||||
|
|
@ -71,7 +96,37 @@
|
|||
if ("Stderr" in msg) {
|
||||
stdout.value += msg.Stderr;
|
||||
}
|
||||
if ("AvailableCompilers" in msg) {
|
||||
console.log("compilers");
|
||||
const compilers = msg.AvailableCompilers;
|
||||
|
||||
compilersWrapper.innerHTML = "";
|
||||
|
||||
for (const compiler of compilers) {
|
||||
const inner = document.createElement("div");
|
||||
inner.innerText = compiler;
|
||||
inner.classList.add("source-box-text");
|
||||
const elem = document.createElement("div");
|
||||
elem.classList.add("compiler");
|
||||
elem.appendChild(inner);
|
||||
compilersWrapper.appendChild(elem);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function compile() {
|
||||
console.log("compiling");
|
||||
|
||||
ws.send(JSON.stringify("Compile"));
|
||||
|
||||
stdout.value = "";
|
||||
stderr.value = "";
|
||||
}
|
||||
|
||||
function updateCompilers() {
|
||||
console.log("list compilers");
|
||||
|
||||
ws.send(JSON.stringify("ListCompilers"));
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ use std::{path::Path, pin::Pin, process::Stdio};
|
|||
use axum::extract::ws::{self, WebSocket};
|
||||
use color_eyre::{eyre::Context, Result};
|
||||
use futures::{stream::SplitSink, SinkExt};
|
||||
use serde::Serialize;
|
||||
use tokio::{
|
||||
io::{AsyncRead, AsyncReadExt},
|
||||
process::{Child, Command},
|
||||
|
|
@ -36,6 +37,29 @@ pub async fn build_a_compiler(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Compiler {
|
||||
Bootstrap,
|
||||
Dev,
|
||||
Dist,
|
||||
}
|
||||
|
||||
pub async fn list_compilers(entrypoint: &Path) -> Vec<Compiler> {
|
||||
let mut compilers = vec![Compiler::Bootstrap];
|
||||
|
||||
let build = entrypoint.parent().unwrap().join("build/host");
|
||||
|
||||
if let Ok(true) = tokio::fs::try_exists(build.join("stage1").join("bin").join("rustc")).await {
|
||||
compilers.push(Compiler::Dev);
|
||||
}
|
||||
|
||||
if let Ok(true) = tokio::fs::try_exists(build.join("stage2").join("bin").join("rustc")).await {
|
||||
compilers.push(Compiler::Dist);
|
||||
}
|
||||
|
||||
compilers
|
||||
}
|
||||
|
||||
async fn handle_stdouts(
|
||||
mut stdout: Pin<Box<impl AsyncRead>>,
|
||||
mut stderr: Pin<Box<impl AsyncRead>>,
|
||||
|
|
|
|||
43
src/main.rs
43
src/main.rs
|
|
@ -5,15 +5,16 @@ use std::{net::SocketAddr, path::PathBuf};
|
|||
|
||||
use axum::{
|
||||
extract::{
|
||||
ws::{Message, WebSocket},
|
||||
ws::{CloseFrame, Message, WebSocket},
|
||||
ConnectInfo, TypedHeader, WebSocketUpgrade,
|
||||
},
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use futures::stream::StreamExt;
|
||||
use serde::Serialize;
|
||||
use bootstrap::Compiler;
|
||||
use futures::{stream::StreamExt, SinkExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tower_http::{
|
||||
services::ServeDir,
|
||||
trace::{DefaultMakeSpan, TraceLayer},
|
||||
|
|
@ -94,6 +95,13 @@ async fn ws_handler(
|
|||
enum ServerMessage<'a> {
|
||||
Stdout(&'a str),
|
||||
Stderr(&'a str),
|
||||
AvailableCompilers(Vec<Compiler>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
enum ClientMessage {
|
||||
Compile,
|
||||
ListCompilers,
|
||||
}
|
||||
|
||||
impl<'a> From<ServerMessage<'a>> for Message {
|
||||
|
|
@ -136,12 +144,37 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr, entrypoint: PathB
|
|||
while let Some(Ok(msg)) = receiver.next().await {
|
||||
info!(?msg);
|
||||
|
||||
if let Message::Text(msg) = msg {
|
||||
if msg == "bootstrap me" {
|
||||
if let Message::Text(msg_str) = msg {
|
||||
let msg = serde_json::from_str::<ClientMessage>(&msg_str);
|
||||
|
||||
match msg {
|
||||
Ok(ClientMessage::Compile) => {
|
||||
if let Err(err) = bootstrap::build_a_compiler(&mut sender, &entrypoint).await {
|
||||
error!(%err);
|
||||
}
|
||||
}
|
||||
Ok(ClientMessage::ListCompilers) => {
|
||||
let compilers = bootstrap::list_compilers(&entrypoint).await;
|
||||
if let Err(err) = sender
|
||||
.send(ServerMessage::AvailableCompilers(compilers).into())
|
||||
.await
|
||||
{
|
||||
error!(%err);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
error!(?err, ?msg_str, "invalid client message");
|
||||
if let Err(err) = sender
|
||||
.send(Message::Close(Some(CloseFrame {
|
||||
code: 0,
|
||||
reason: "invalid message, you naughty".into(),
|
||||
})))
|
||||
.await
|
||||
{
|
||||
error!(%err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue