Merge plugin back into main repository (#49)

This commit is contained in:
Lucien Greathouse
2018-04-01 23:22:04 -07:00
committed by GitHub
parent c8f837d726
commit 6fa925a402
55 changed files with 1742 additions and 36 deletions

View File

@@ -0,0 +1,16 @@
use std::path::PathBuf;
use std::process;
use project::Project;
pub fn init(project_path: &PathBuf) {
match Project::init(project_path) {
Ok(_) => {
println!("Created new empty project at {}", project_path.display());
},
Err(e) => {
eprintln!("Failed to create new project.\n{}", e);
process::exit(1);
},
}
}

View File

@@ -0,0 +1,5 @@
mod serve;
mod init;
pub use self::serve::*;
pub use self::init::*;

View File

@@ -0,0 +1,99 @@
use std::path::{Path, PathBuf};
use std::process;
use std::sync::{Arc, Mutex};
use std::thread;
use rand;
use project::{Project, ProjectLoadError};
use plugin::{PluginChain};
use plugins::{DefaultPlugin, JsonModelPlugin, ScriptPlugin};
use vfs::{VfsSession, VfsWatcher};
use web;
pub fn serve(project_path: &PathBuf, verbose: bool, port: Option<u64>) {
let server_id = rand::random::<u64>();
if verbose {
println!("Attempting to locate project at {}...", project_path.display());
}
let project = match Project::load(project_path) {
Ok(v) => {
println!("Using project from {}", project_path.display());
v
},
Err(err) => {
match err {
ProjectLoadError::InvalidJson(serde_err) => {
eprintln!(
"Found invalid JSON!\nProject in: {}\nError: {}",
project_path.display(),
serde_err,
);
process::exit(1);
},
ProjectLoadError::FailedToOpen | ProjectLoadError::FailedToRead => {
eprintln!("Found project file, but failed to read it!");
eprintln!(
"Check the permissions of the project file at\n{}",
project_path.display(),
);
process::exit(1);
},
_ => {
// Any other error is fine; use the default project.
println!("Found no project file, using default project...");
Project::default()
},
}
},
};
let web_config = web::WebConfig {
verbose,
port: port.unwrap_or(project.serve_port),
server_id,
};
lazy_static! {
static ref PLUGIN_CHAIN: PluginChain = PluginChain::new(vec![
Box::new(ScriptPlugin::new()),
Box::new(JsonModelPlugin::new()),
Box::new(DefaultPlugin::new()),
]);
}
let vfs = {
let mut vfs = VfsSession::new(&PLUGIN_CHAIN);
for (name, project_partition) in &project.partitions {
let path = {
let given_path = Path::new(&project_partition.path);
if given_path.is_absolute() {
given_path.to_path_buf()
} else {
project_path.join(given_path)
}
};
vfs.insert_partition(name, path);
}
Arc::new(Mutex::new(vfs))
};
println!("Server listening on port {}", web_config.port);
{
let vfs = vfs.clone();
thread::spawn(move || {
VfsWatcher::new(vfs).start();
});
}
web::start(web_config, project.clone(), &PLUGIN_CHAIN, vfs.clone());
}