Skip to content

Commit

Permalink
Merge pull request #107 from dherman/events-plugin-config
Browse files Browse the repository at this point in the history
Move events reporting plugin to the user's config file
  • Loading branch information
dherman authored Aug 7, 2018
2 parents 2c974c2 + 33c8710 commit aa6939f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 53 deletions.
7 changes: 7 additions & 0 deletions crates/notion-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl LazyConfig {
pub struct Config {
pub node: Option<ToolConfig<NodeDistro>>,
pub yarn: Option<ToolConfig<YarnDistro>>,
pub events: Option<EventsConfig>,
}

/// Notion configuration settings relating to the Node executable.
Expand Down Expand Up @@ -68,3 +69,9 @@ impl FromStr for Config {
Ok(serial.into_config()?)
}
}

/// Notion configuration settings related to events.
pub struct EventsConfig {
/// The plugin for publishing events, if any.
pub publish: Option<plugin::Publish>,
}
17 changes: 11 additions & 6 deletions crates/notion-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use monitor::LazyMonitor;
use notion_fail::{Fallible, NotionError};
use session::ActivityKind;
use plugin::Publish;

// the Event data that is serialized to JSON and sent the plugin
#[derive(Serialize)]
Expand Down Expand Up @@ -121,11 +122,15 @@ impl EventLog {
self.events.push(event);
}

// send the events from this session to the monitor
pub fn send_events(&mut self, command: Option<String>) {
self.monitor
.get_mut(command)
.unwrap()
.send_events(&self.events);
pub fn publish(&mut self, plugin: Option<&Publish>) {
match plugin {
Some(&Publish::Url(_)) => { unimplemented!() }
Some(&Publish::Bin(ref command)) => {
self.monitor
.get_mut(command)
.send_events(&self.events);
}
None => { }
}
}
}
2 changes: 0 additions & 2 deletions crates/notion-core/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ pub struct Manifest {
pub dependencies: HashMap<String, String>,
/// The `devDependencies` section.
pub dev_dependencies: HashMap<String, String>,
/// The command to run a plugin for events, under the `notion.events_plugin` key.
pub events_plugin: Option<String>,
}

impl Manifest {
Expand Down
43 changes: 20 additions & 23 deletions crates/notion-core/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use lazycell::LazyCell;
use serde_json;

use event::Event;
use notion_fail::Fallible;

pub struct Monitor {
monitor_process: Option<Child>,
}

impl Monitor {
/// Returns the current monitor.
pub fn new(command: Option<String>) -> Monitor {
pub fn new(command: &str) -> Monitor {
Monitor {
monitor_process: spawn_process(command),
}
Expand Down Expand Up @@ -54,33 +53,31 @@ impl LazyMonitor {
}

/// Forces creating a monitor and returns an immutable reference to it.
pub fn get(&self, command: Option<String>) -> Fallible<&Monitor> {
self.monitor.try_borrow_with(|| Ok(Monitor::new(command)))
pub fn get(&self, command: &str) -> &Monitor {
self.monitor.borrow_with(|| Monitor::new(command))
}

/// Forces creating a monitor and returns a mutable reference to it.
pub fn get_mut(&mut self, command: Option<String>) -> Fallible<&mut Monitor> {
pub fn get_mut(&mut self, command: &str) -> &mut Monitor {
self.monitor
.try_borrow_mut_with(|| Ok(Monitor::new(command)))
.borrow_mut_with(|| Monitor::new(command))
}
}

fn spawn_process(command: Option<String>) -> Option<Child> {
command.as_ref().and_then(|full_cmd| {
return full_cmd.split(" ").take(1).next().and_then(|executable| {
let child = Command::new(executable)
.args(full_cmd.split(" ").skip(1))
.stdin(Stdio::piped()) // JSON data is sent over stdin
// .stdout(Stdio::piped()) // let the plugin write to stdout for now
.spawn();
return match child {
Err(err) => {
eprintln!("Error running plugin command: '{}'", full_cmd);
eprintln!("{}", err);
None
}
Ok(c) => Some(c),
};
});
fn spawn_process(command: &str) -> Option<Child> {
command.split(" ").take(1).next().and_then(|executable| {
let child = Command::new(executable)
.args(command.split(" ").skip(1))
.stdin(Stdio::piped()) // JSON data is sent over stdin
// .stdout(Stdio::piped()) // let the plugin write to stdout for now
.spawn();
match child {
Err(err) => {
eprintln!("Error running plugin command: '{}'", command);
eprintln!("{}", err);
None
}
Ok(c) => Some(c),
}
})
}
9 changes: 9 additions & 0 deletions crates/notion-core/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@ pub enum LsRemote {
Url(String),
Bin(String),
}

/// A plugin for publishing Notion events.
pub enum Publish {
/// Reports an event by sending a POST request to a URL.
Url(String),

/// Reports an event by forking a process and sending the event by IPC.
Bin(String),
}
24 changes: 24 additions & 0 deletions crates/notion-core/src/serial/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ use notion_fail::Fallible;
pub struct Config {
pub node: Option<ToolConfig<NodeDistro>>,
pub yarn: Option<ToolConfig<YarnDistro>>,
pub events: Option<EventsConfig>,
}

#[derive(Serialize, Deserialize)]
#[serde(rename = "events")]
pub struct EventsConfig {
pub publish: Option<Plugin>,
}

impl EventsConfig {
pub fn into_events_config(self) -> Fallible<config::EventsConfig> {
Ok(config::EventsConfig {
publish: if let Some(p) = self.publish {
Some(p.into_publish()?)
} else {
None
}
})
}
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -39,6 +58,11 @@ impl Config {
} else {
None
},
events: if let Some(e) = self.events {
Some(e.into_events_config()?)
} else {
None
},
})
}
}
Expand Down
7 changes: 0 additions & 7 deletions crates/notion-core/src/serial/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub struct Manifest {
pub struct ToolchainManifest {
pub node: String,
pub yarn: Option<String>,
// FIXME: this should be in the notion config file
pub events_plugin: Option<String>,
}

impl Manifest {
Expand All @@ -42,11 +40,6 @@ impl Manifest {
},
dependencies: self.dependencies,
dev_dependencies: self.dev_dependencies,
events_plugin: if let Some(plugin) = notion.events_plugin {
Some(plugin)
} else {
None
},
}));
}

Expand Down
4 changes: 4 additions & 0 deletions crates/notion-core/src/serial/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl Plugin {
pub fn into_ls_remote(self) -> Fallible<plugin::LsRemote> {
self.into_plugin(plugin::LsRemote::Url, plugin::LsRemote::Bin)
}

pub fn into_publish(self) -> Fallible<plugin::Publish> {
self.into_plugin(plugin::Publish::Url, plugin::Publish::Bin)
}
}

#[derive(Serialize, Deserialize)]
Expand Down
29 changes: 14 additions & 15 deletions crates/notion-core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::env::{self, VarError};
use catalog::{Catalog, LazyCatalog};
use config::{Config, LazyConfig};
use distro::Fetched;
use plugin::Publish;
use project::Project;
use std::fmt::{self, Display, Formatter};
use std::process::exit;
Expand Down Expand Up @@ -190,22 +191,20 @@ impl Session {
self.event_log.add_event_error(activity_kind, error)
}

// send the events from this session to the monitor
pub fn send_events(&mut self) {
let command = self.events_command();
self.event_log.send_events(command)
}

// get the .notion.events_plugin string from package.json
pub fn events_command(&self) -> Option<String> {
self.project
.as_ref()
.and_then(|project| project.manifest().events_plugin.as_ref())
.map(|plugin| plugin.to_string())
}

pub fn exit(mut self, code: i32) -> ! {
self.send_events();
match publish_plugin(&self.config) {
Ok(plugin) => {
self.event_log.publish(plugin);
}
Err(e) => {
eprintln!("Warning: invalid config file ({})", e);
}
}
exit(code);
}
}

fn publish_plugin(config: &LazyConfig) -> Fallible<Option<&Publish>> {
let config = config.get()?;
Ok(config.events.as_ref().and_then(|events| events.publish.as_ref()))
}

0 comments on commit aa6939f

Please sign in to comment.