Reorganize some of the unwieldly parts of the codebase

* Moved commands into their own folder to reduce `bin.rs`'s size
* Moved all of the VFS-related functionality into its own folder
* Documented a lot of functions, including a few very obscure ones
This commit is contained in:
Lucien Greathouse
2018-01-03 16:45:46 -08:00
parent d8bcbee463
commit 58b244b7e9
14 changed files with 267 additions and 212 deletions

16
src/commands/init.rs Normal file
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);
},
}
}

5
src/commands/mod.rs Normal file
View File

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

114
src/commands/serve.rs Normal file
View File

@@ -0,0 +1,114 @@
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::thread;
use std::process;
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()),
]);
}
if verbose {
println!("Loading VFS...");
}
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)
}
};
if verbose {
println!(
"Partition '{}': {} @ {}",
name,
project_partition.target,
project_partition.path
);
}
vfs.partitions.insert(name.clone(), path);
}
Arc::new(Mutex::new(vfs))
};
{
let vfs = vfs.clone();
thread::spawn(move || {
VfsWatcher::new(vfs).start();
});
}
println!("Server listening on port {}", web_config.port);
web::start(web_config, project.clone(), &PLUGIN_CHAIN, vfs.clone());
}