Skip to content

Commit

Permalink
feat: finish git extension feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Mar 17, 2024
1 parent 4fa3381 commit aa729e4
Show file tree
Hide file tree
Showing 37 changed files with 510 additions and 634 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions crates/core/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl Graph {
}

pub fn execute(&mut self, command: GraphCommand) {
let skip = vec!["git", "git-checkout", "git-last-commit", "tree", "http"];
match command {
GraphCommand::AddVertex(id, label, command, needs) => {
if let Some(vertex) = self.vertices.iter_mut().find(|v| v.id == id) {
Expand All @@ -63,6 +64,7 @@ impl Graph {
}
}
while let Some(i) = stack.pop() {
let label = &self.vertices[i].label.as_str();
if visited[i] {
continue;
}
Expand All @@ -71,6 +73,10 @@ impl Graph {
stack.push(edge.to);
}

if skip.contains(&label) {
continue;
}

let (tx, rx) = mpsc::channel();

if self.vertices[i].label == "withWorkdir" {
Expand Down Expand Up @@ -118,14 +124,21 @@ impl Graph {
stack.push(i);
}
}

while let Some(i) = stack.pop() {
let label = &self.vertices[i].label.as_str();
if visited[i] {
continue;
}
visited[i] = true;
for edge in self.edges.iter().filter(|e| e.from == i) {
stack.push(edge.to);
}

if skip.contains(&label) {
continue;
}

let (tx, rx) = mpsc::channel();

if self.vertices[i].label == "withWorkdir" {
Expand Down
1 change: 1 addition & 0 deletions crates/ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version = "0.1.0"
anyhow = "1.0.80"
fluentci-types = {path = "../types"}
owo-colors = "4.0.0"
regex = "1.10.3"
reqwest = {version = "0.11.26", features = ["rustls-tls", "blocking"], default-features = false}
sha256 = "1.5.0"
users = "0.11.0"
65 changes: 4 additions & 61 deletions crates/ext/src/devbox.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{
io::{BufRead, BufReader},
process::{Command, ExitStatus, Stdio},
sync::mpsc::{self, Receiver, Sender},
thread,
sync::mpsc::Sender,
};

use crate::{nix::Nix, Extension};
use crate::{exec, nix::Nix, Extension};
use anyhow::Error;
use fluentci_types::Output;

Expand Down Expand Up @@ -41,9 +39,6 @@ impl Extension for Devbox {
return Ok(ExitStatus::default());
}

let (stdout_tx, stdout_rx): (Sender<String>, Receiver<String>) = mpsc::channel();
let (stderr_tx, stderr_rx): (Sender<String>, Receiver<String>) = mpsc::channel();

Command::new("bash")
.arg("-c")
.arg("[ -f devbox.json ] || devbox init")
Expand All @@ -53,60 +48,8 @@ impl Extension for Devbox {
.spawn()?
.wait()?;

let mut child = Command::new("bash")
.arg("-c")
.arg(format!("devbox run {}", cmd))
.current_dir(work_dir)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;

let stdout_tx_clone = stdout_tx.clone();
let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();

let out_clone = out.clone();
let tx_clone = tx.clone();

thread::spawn(move || {
let mut stdout = String::new();
while let Ok(line) = stdout_rx.recv() {
println!("{}", line);
stdout.push_str(&line);
stdout.push_str("\n");
}
if out_clone == Output::Stdout && last_cmd {
tx_clone.send(stdout).unwrap();
}
});

thread::spawn(move || {
let mut stderr = String::new();
while let Ok(line) = stderr_rx.recv() {
println!("{}", line);
stderr.push_str(&line);
stderr.push_str("\n");
}
if out == Output::Stderr && last_cmd {
tx.send(stderr).unwrap();
}
});

thread::spawn(move || {
let reader = BufReader::new(stdout);
for line in reader.lines() {
stdout_tx_clone.send(line.unwrap()).unwrap();
}
});

thread::spawn(move || {
let reader = BufReader::new(stderr);
for line in reader.lines() {
stderr_tx.send(line.unwrap()).unwrap();
}
});

child.wait().map_err(Error::from)
let cmd = format!("devbox run {}", cmd);
exec(&cmd, tx, out, last_cmd, work_dir)
}

fn setup(&self) -> Result<(), Error> {
Expand Down
65 changes: 4 additions & 61 deletions crates/ext/src/devenv.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{
io::{BufRead, BufReader},
process::{Command, ExitStatus, Stdio},
sync::mpsc::{self, Receiver, Sender},
thread,
sync::mpsc::Sender,
};

use crate::{nix::Nix, pkgx::Pkgx, Extension};
use crate::{exec, nix::Nix, pkgx::Pkgx, Extension};
use anyhow::Error;
use fluentci_types::Output;

Expand All @@ -29,9 +27,6 @@ impl Extension for Devenv {

Pkgx::default().install(vec!["direnv"])?;

let (stdout_tx, stdout_rx): (Sender<String>, Receiver<String>) = mpsc::channel();
let (stderr_tx, stderr_rx): (Sender<String>, Receiver<String>) = mpsc::channel();

Command::new("bash")
.arg("-c")
.arg("[ -f devenv.nix ] || devenv init")
Expand All @@ -41,60 +36,8 @@ impl Extension for Devenv {
.spawn()?
.wait()?;

let mut child = Command::new("bash")
.arg("-c")
.arg(format!("devenv shell {}", cmd))
.current_dir(work_dir)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;

let stdout_tx_clone = stdout_tx.clone();
let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();

let out_clone = out.clone();
let tx_clone = tx.clone();

thread::spawn(move || {
let mut stdout = String::new();
while let Ok(line) = stdout_rx.recv() {
println!("{}", line);
stdout.push_str(&line);
stdout.push_str("\n");
}
if out_clone == Output::Stdout && last_cmd {
tx_clone.send(stdout).unwrap();
}
});

thread::spawn(move || {
let mut stderr = String::new();
while let Ok(line) = stderr_rx.recv() {
println!("{}", line);
stderr.push_str(&line);
stderr.push_str("\n");
}
if out == Output::Stderr && last_cmd {
tx.send(stderr).unwrap();
}
});

thread::spawn(move || {
let reader = BufReader::new(stdout);
for line in reader.lines() {
stdout_tx_clone.send(line.unwrap()).unwrap();
}
});

thread::spawn(move || {
let reader = BufReader::new(stderr);
for line in reader.lines() {
stderr_tx.send(line.unwrap()).unwrap();
}
});

child.wait().map_err(Error::from)
let cmd = format!("devenv shell {}", cmd);
exec(&cmd, tx, out, last_cmd, work_dir)
}

fn setup(&self) -> Result<(), Error> {
Expand Down
64 changes: 3 additions & 61 deletions crates/ext/src/envhub.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{
io::{BufRead, BufReader},
process::{Command, ExitStatus, Stdio},
sync::mpsc::{self, Receiver, Sender},
thread,
sync::mpsc::Sender,
};

use crate::{nix::Nix, pkgx::Pkgx, Extension};
use crate::{exec, nix::Nix, pkgx::Pkgx, Extension};
use anyhow::Error;
use fluentci_types::Output;

Expand All @@ -27,63 +25,7 @@ impl Extension for Envhub {
return Ok(ExitStatus::default());
}

let (stdout_tx, stdout_rx): (Sender<String>, Receiver<String>) = mpsc::channel();
let (stderr_tx, stderr_rx): (Sender<String>, Receiver<String>) = mpsc::channel();

let mut child = Command::new("bash")
.arg("-c")
.arg(cmd)
.current_dir(work_dir)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;

let stdout_tx_clone = stdout_tx.clone();
let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();

let out_clone = out.clone();
let tx_clone = tx.clone();

thread::spawn(move || {
let mut stdout = String::new();
while let Ok(line) = stdout_rx.recv() {
println!("{}", line);
stdout.push_str(&line);
stdout.push_str("\n");
}
if out_clone == Output::Stdout && last_cmd {
tx_clone.send(stdout).unwrap();
}
});

thread::spawn(move || {
let mut stderr = String::new();
while let Ok(line) = stderr_rx.recv() {
println!("{}", line);
stderr.push_str(&line);
stderr.push_str("\n");
}
if out == Output::Stderr && last_cmd {
tx.send(stderr).unwrap();
}
});

thread::spawn(move || {
let reader = BufReader::new(stdout);
for line in reader.lines() {
stdout_tx_clone.send(line.unwrap()).unwrap();
}
});

thread::spawn(move || {
let reader = BufReader::new(stderr);
for line in reader.lines() {
stderr_tx.send(line.unwrap()).unwrap();
}
});

child.wait().map_err(Error::from)
exec(cmd, tx, out, last_cmd, work_dir)
}

fn setup(&self) -> Result<(), Error> {
Expand Down
Loading

0 comments on commit aa729e4

Please sign in to comment.