Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stctl)!: use trunk as dependency of stctl #155

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
569 changes: 549 additions & 20 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions crates/stctl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ reqwest = { version = "0.11.20", features = [
], default-features = false }
tokio-stream = { version = "0.1.14", features = ["fs", "sync"] }
webbrowser = "0.8.11"
typed-builder = "0.16.0"

# Stellation Components
stellation-core = { version = "0.3.0", path = "../stellation-core" }
fs_extra = "1.3.0"

# Trunk
trunk-pipelines = { git = "https://github.com/futursolo/trunk", branch = "trunk-lib" }
trunk-util = { git = "https://github.com/futursolo/trunk", branch = "trunk-lib" }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "documenting"]
152 changes: 15 additions & 137 deletions crates/stctl/src/builder.rs → crates/stctl/src/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use std::path::{Path, PathBuf};
use std::pin::pin;
use std::process::Stdio;
use std::sync::Arc;

use anyhow::{bail, Context, Result};
use cargo_metadata::Metadata;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt};
use tokio::fs;
use tokio::sync::OnceCell;
use tokio::{fs, spawn};

use crate::builder::pipeline::{Pipeline, PipelineConfig};
use crate::env_file::EnvFile;
use crate::manifest::Manifest;
use crate::paths::Paths;
use crate::profile::Profile;
use crate::utils::random_str;
use crate::Stctl;

mod pipeline;

#[derive(Debug)]
pub(crate) struct Builder {
build_id: String,
Expand Down Expand Up @@ -98,111 +99,19 @@ impl Builder {
.map(|m| m.as_ref())
}

async fn transfer_to_file<R, P>(source: R, target: P) -> Result<()>
where
R: 'static + AsyncRead + Send,
P: Into<PathBuf>,
{
let target_path = target.into();
let mut target = fs::File::create(&target_path)
.await
.with_context(|| format!("failed to create {}", target_path.display()))?;

let inner = async move {
let mut source = pin!(source);

loop {
let mut buf = [0_u8; 8192];
let buf_len = source.read(&mut buf[..]).await?;

if buf_len == 0 {
break;
}
target.write_all(&buf[..buf_len]).await?;
}

Ok::<(), anyhow::Error>(())
};

spawn(async move {
if let Err(e) = inner
.await
.with_context(|| format!("failed to transfer logs to: {}", target_path.display()))
{
tracing::error!("{:#?}", e);
}
});

Ok(())
}

pub async fn build_frontend(&self) -> Result<&Path> {
use tokio::process::Command;

let frontend_logs_dir = self.paths.frontend_logs_dir().await?;
let frontend_build_dir = self.frontend_build_dir().await?;
let workspace_dir = self.paths.workspace_dir().await?;

let create_proc = || {
let mut proc = Command::new("trunk");
proc.arg("build")
.arg("--dist")
.arg(frontend_build_dir)
.arg(workspace_dir.join("index.html"))
.current_dir(workspace_dir)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());

if let Some(m) = self.profile.to_profile_argument() {
proc.arg(m);
}

let envs = self.env_file.load(workspace_dir);
proc.envs(envs);

if !self.is_watch_build {
proc.stdout(Stdio::inherit()).stderr(Stdio::inherit());
}
proc
};
let pipeline_config = PipelineConfig::builder()
.output_dir(frontend_build_dir)
.public_url("/")
.should_optimize(self.profile.should_optimize())
.build();

let mut child = create_proc().spawn()?;

if let Some(m) = child.stdout.take() {
Self::transfer_to_file(
m,
frontend_logs_dir.join(format!("log-stdout-{}", self.build_id)),
)
.await?;
}

if let Some(m) = child.stderr.take() {
Self::transfer_to_file(
m,
frontend_logs_dir.join(format!("log-stderr-{}", self.build_id)),
)
Pipeline::new(pipeline_config)
.build(workspace_dir.join("index.html"))
.await?;
}

let status = child.wait().await?;

// We try again with logs printed to console.
if !status.success() {
if !self.is_watch_build {
bail!("trunk failed with status {}", status);
}

let mut proc = create_proc();
proc.stdout(Stdio::inherit()).stderr(Stdio::inherit());

let mut child = proc.spawn()?;
let status = child.wait().await?;

if !status.success() {
bail!("trunk failed with status {}", status);
}
}

Ok(frontend_build_dir)
}
Expand All @@ -211,7 +120,6 @@ impl Builder {
use tokio::process::Command;

let frontend_build_dir = self.frontend_build_dir().await?;
let backend_logs_dir = self.paths.backend_logs_dir().await?;
let workspace_dir = self.paths.workspace_dir().await?;
let backend_build_dir = self.backend_build_dir().await?;

Expand All @@ -222,8 +130,8 @@ impl Builder {
.arg(&self.manifest.dev_server.bin_name)
.current_dir(workspace_dir)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.kill_on_drop(true);

if let Some(m) = self.profile.to_profile_argument() {
Expand All @@ -238,9 +146,7 @@ impl Builder {
proc.envs(envs);

if !self.is_watch_build {
proc.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.env("RUSTFLAGS", "--cfg stellation_embedded_frontend");
proc.env("RUSTFLAGS", "--cfg stellation_embedded_frontend");
}

proc.env("STELLATION_FRONTEND_BUILD_DIR", frontend_build_dir);
Expand All @@ -250,39 +156,11 @@ impl Builder {

let mut child = create_proc().spawn()?;

if let Some(m) = child.stdout.take() {
Self::transfer_to_file(
m,
backend_logs_dir.join(format!("log-stdout-{}", self.build_id)),
)
.await?;
}

if let Some(m) = child.stderr.take() {
Self::transfer_to_file(
m,
backend_logs_dir.join(format!("log-stderr-{}", self.build_id)),
)
.await?;
}

let status = child.wait().await?;

// We try again with logs printed to console.
if !status.success() {
if !self.is_watch_build {
bail!("trunk failed with status {}", status);
}

let mut proc = create_proc();
proc.stdout(Stdio::inherit()).stderr(Stdio::inherit());

let mut child = proc.spawn()?;
let status = child.wait().await?;

if !status.success() {
bail!("trunk failed with status {}", status);
}
bail!("cargo failed with status {}", status);
}

// Copy artifact from target directory.
Expand Down
Loading