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

stream progress as JSON to a given file #34

Draft
wants to merge 3 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
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ getopts = "0.2"
anyhow = "1.0"
libc = "0.2"
lazy_static = "1.4.0"
json = "0.12.4"

[target.'cfg(windows)'.dependencies]
kernel32-sys = "0.2.2"
Expand Down
65 changes: 65 additions & 0 deletions src/json_progress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! Writes progress updates as JSON messages to a stream.

extern crate json;

use std::io::Write;

use crate::densemap::Index;
use crate::graph::{Build, BuildId};
use crate::progress::Progress;
use crate::task::TaskResult;
use crate::work::{BuildState, StateCounts};

/// Implements progress::Progress by forwarding messages as JSON to a stream.
pub struct JSONProgress {
stream: Option<Box<dyn Write>>,
}

impl JSONProgress {
pub fn new(path: &str) -> anyhow::Result<Self> {
let stream = Box::new(std::fs::OpenOptions::new().append(true).open(path)?);
Ok(JSONProgress {
stream: Some(stream),
})
}

fn write(&mut self, val: json::JsonValue) {
if let Some(stream) = &mut self.stream {
let mut buf = json::stringify(val);
buf.push('\n');
if stream.write_all(buf.as_bytes()).is_err() {
self.stream = None;
}
}
}
}

impl Progress for JSONProgress {
fn update(&mut self, counts: &StateCounts) {
self.write(json::object! {
counts: {
want: counts.get(BuildState::Want),
ready: counts.get(BuildState::Ready),
queued: counts.get(BuildState::Queued),
running: counts.get(BuildState::Running),
done: counts.get(BuildState::Done),
failed: counts.get(BuildState::Failed),
}
});
}

fn flush(&mut self) {}

fn task_state(&mut self, id: BuildId, _build: &Build, _result: Option<&TaskResult>) {
self.write(json::object! {
task: {
id: id.index(),
}
});
}

fn finish(&mut self) {
// TODO a build finish()es multiple times due to updating build.ninja,
// so this isn't so useful for giving a status message.
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod densemap;
mod depfile;
mod eval;
mod graph;
mod json_progress;
mod load;
mod parse;
mod progress;
Expand Down
34 changes: 34 additions & 0 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@ pub trait Progress {
fn finish(&mut self);
}

pub struct MultiProgress {
progress: Vec<Box<dyn Progress>>,
}
impl MultiProgress {
pub fn new(progress: Vec<Box<dyn Progress>>) -> Self {
MultiProgress { progress }
}
}
impl Progress for MultiProgress {
fn update(&mut self, counts: &StateCounts) {
for p in self.progress.iter_mut() {
p.update(counts);
}
}

fn flush(&mut self) {
for p in self.progress.iter_mut() {
p.flush();
}
}

fn task_state(&mut self, id: BuildId, build: &Build, result: Option<&TaskResult>) {
for p in self.progress.iter_mut() {
p.task_state(id, build, result);
}
}

fn finish(&mut self) {
for p in self.progress.iter_mut() {
p.finish();
}
}
}

/// Currently running build task, as tracked for progress updates.
struct Task {
id: BuildId,
Expand Down
22 changes: 17 additions & 5 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ extern crate getopts;
use anyhow::anyhow;
use std::path::Path;

use crate::{load, progress::ConsoleProgress, trace, work};
use crate::json_progress::JSONProgress;
use crate::load;
use crate::progress::{ConsoleProgress, MultiProgress, Progress};
use crate::trace;
use crate::work;

// The result of starting a build.
enum BuildResult {
Expand All @@ -28,7 +32,7 @@ struct BuildParams<'a> {
// possible, and if that build changes build.ninja, then return
// BuildResult::Regen to signal to the caller that we need to start the whole
// build over.
fn build(progress: &mut ConsoleProgress, params: &BuildParams) -> anyhow::Result<BuildResult> {
fn build(progress: &mut dyn Progress, params: &BuildParams) -> anyhow::Result<BuildResult> {
let mut state = trace::scope("load::read", || load::read(params.build_filename))?;

let mut work = work::Work::new(
Expand Down Expand Up @@ -193,7 +197,15 @@ fn run_impl() -> anyhow::Result<i32> {
build_filename = name;
}

let mut progress = ConsoleProgress::new(matches.opt_present("v"), use_fancy_terminal());
let mut progress: Box<dyn Progress> = Box::new(ConsoleProgress::new(
matches.opt_present("v"),
use_fancy_terminal(),
));

if let Ok(stream) = std::env::var("N2_PROGRESS_STREAM") {
let json_progress = Box::new(JSONProgress::new(&stream)?);
progress = Box::new(MultiProgress::new(vec![progress, json_progress]));
}

// Build once with regen=true, and if the result says we regenerated the
// build file, reload and build everything a second time.
Expand All @@ -205,10 +217,10 @@ fn run_impl() -> anyhow::Result<i32> {
target_names: &matches.free,
build_filename: &build_filename,
};
let mut result = build(&mut progress, &params)?;
let mut result = build(progress.as_mut(), &params)?;
if let BuildResult::Regen = result {
params.regen = false;
result = build(&mut progress, &params)?;
result = build(progress.as_mut(), &params)?;
}

match result {
Expand Down
70 changes: 0 additions & 70 deletions vscode/README.md

This file was deleted.

14 changes: 0 additions & 14 deletions vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "n2" is now active!');

// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('n2.helloWorld', () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage('Hello World from n2!');
});

context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
export function deactivate() {}