mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-25 07:06:12 +00:00
Port commands to use common setup code
Initialization logic needed for serve, build, and upload is now much more clear than it was when these functions were written. This commit refactors all of them to use a new common_setup module for all of their initialization that's the same.
This commit is contained in:
@@ -1,18 +1,15 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, BufWriter, Write},
|
io::{self, BufWriter, Write},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use failure::Fail;
|
use failure::Fail;
|
||||||
use rbx_dom_weak::RbxInstanceProperties;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
common_setup,
|
||||||
imfs::{FsError, Imfs, RealFetcher, WatchMode},
|
imfs::{FsError, Imfs, RealFetcher, WatchMode},
|
||||||
project::{Project, ProjectLoadError},
|
project::ProjectLoadError,
|
||||||
snapshot::{apply_patch_set, compute_patch_set, InstancePropertiesWithMeta, RojoTree},
|
|
||||||
snapshot_middleware::{snapshot_from_imfs, InstanceSnapshotContext, SnapshotPluginContext},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
@@ -83,52 +80,11 @@ pub fn build(options: &BuildOptions) -> Result<(), BuildError> {
|
|||||||
|
|
||||||
log::debug!("Hoping to generate file of type {:?}", output_kind);
|
log::debug!("Hoping to generate file of type {:?}", output_kind);
|
||||||
|
|
||||||
let maybe_project = match Project::load_fuzzy(&options.fuzzy_project_path) {
|
|
||||||
Ok(project) => Some(project),
|
|
||||||
Err(ProjectLoadError::NotFound) => None,
|
|
||||||
Err(other) => return Err(other.into()),
|
|
||||||
};
|
|
||||||
log::trace!("Using project file {:#?}", maybe_project);
|
|
||||||
|
|
||||||
let mut tree = RojoTree::new(InstancePropertiesWithMeta {
|
|
||||||
properties: RbxInstanceProperties {
|
|
||||||
name: "ROOT".to_owned(),
|
|
||||||
class_name: "Folder".to_owned(),
|
|
||||||
properties: HashMap::new(),
|
|
||||||
},
|
|
||||||
metadata: Default::default(),
|
|
||||||
});
|
|
||||||
let root_id = tree.get_root_id();
|
|
||||||
|
|
||||||
log::trace!("Constructing in-memory filesystem");
|
log::trace!("Constructing in-memory filesystem");
|
||||||
let mut imfs = Imfs::new(RealFetcher::new(WatchMode::Disabled));
|
let mut imfs = Imfs::new(RealFetcher::new(WatchMode::Disabled));
|
||||||
|
|
||||||
log::trace!("Reading project root");
|
let (_maybe_project, tree) = common_setup::start(&options.fuzzy_project_path, &mut imfs);
|
||||||
let entry = imfs
|
let root_id = tree.get_root_id();
|
||||||
.get(&options.fuzzy_project_path)
|
|
||||||
.expect("could not get project path");
|
|
||||||
|
|
||||||
let mut snapshot_context = InstanceSnapshotContext::default();
|
|
||||||
|
|
||||||
if let Some(project) = maybe_project {
|
|
||||||
// If the project file defines no plugins, then there's no need to
|
|
||||||
// initialize the snapshot plugin context.
|
|
||||||
if !project.plugins.is_empty() {
|
|
||||||
snapshot_context.plugin_context =
|
|
||||||
Some(SnapshotPluginContext::new(project.plugins.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log::trace!("Generating snapshot of instances from IMFS");
|
|
||||||
let snapshot = snapshot_from_imfs(&mut snapshot_context, &mut imfs, &entry)
|
|
||||||
.expect("snapshot failed")
|
|
||||||
.expect("snapshot did not return an instance");
|
|
||||||
|
|
||||||
log::trace!("Computing patch set");
|
|
||||||
let patch_set = compute_patch_set(&snapshot, &tree, root_id);
|
|
||||||
|
|
||||||
log::trace!("Applying patch set");
|
|
||||||
apply_patch_set(&mut tree, patch_set);
|
|
||||||
|
|
||||||
log::trace!("Opening output file for write");
|
log::trace!("Opening output file for write");
|
||||||
let mut file = BufWriter::new(File::create(&options.output_file)?);
|
let mut file = BufWriter::new(File::create(&options.output_file)?);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
imfs::{Imfs, RealFetcher, WatchMode},
|
imfs::{Imfs, RealFetcher, WatchMode},
|
||||||
project::{Project, ProjectLoadError},
|
project::ProjectLoadError,
|
||||||
serve_session::ServeSession,
|
serve_session::ServeSession,
|
||||||
web::LiveServer,
|
web::LiveServer,
|
||||||
};
|
};
|
||||||
@@ -33,33 +33,18 @@ impl_from!(ServeError {
|
|||||||
});
|
});
|
||||||
|
|
||||||
pub fn serve(options: &ServeOptions) -> Result<(), ServeError> {
|
pub fn serve(options: &ServeOptions) -> Result<(), ServeError> {
|
||||||
let maybe_project = match Project::load_fuzzy(&options.fuzzy_project_path) {
|
let imfs = Imfs::new(RealFetcher::new(WatchMode::Enabled));
|
||||||
Ok(project) => Some(project),
|
|
||||||
Err(ProjectLoadError::NotFound) => None,
|
let session = Arc::new(ServeSession::new(imfs, &options.fuzzy_project_path));
|
||||||
Err(other) => return Err(other.into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let port = options
|
let port = options
|
||||||
.port
|
.port
|
||||||
.or_else(|| {
|
.or_else(|| session.project_port())
|
||||||
maybe_project
|
|
||||||
.as_ref()
|
|
||||||
.and_then(|project| project.serve_port)
|
|
||||||
})
|
|
||||||
.unwrap_or(DEFAULT_PORT);
|
.unwrap_or(DEFAULT_PORT);
|
||||||
|
|
||||||
let _ = show_start_message(port);
|
|
||||||
|
|
||||||
let imfs = Imfs::new(RealFetcher::new(WatchMode::Enabled));
|
|
||||||
|
|
||||||
let session = Arc::new(ServeSession::new(
|
|
||||||
imfs,
|
|
||||||
&options.fuzzy_project_path,
|
|
||||||
maybe_project,
|
|
||||||
));
|
|
||||||
|
|
||||||
let server = LiveServer::new(session);
|
let server = LiveServer::new(session);
|
||||||
|
|
||||||
|
let _ = show_start_message(port);
|
||||||
server.start(port);
|
server.start(port);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
use std::{collections::HashMap, path::PathBuf};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use failure::Fail;
|
use failure::Fail;
|
||||||
use rbx_dom_weak::RbxInstanceProperties;
|
|
||||||
use reqwest::header::{ACCEPT, CONTENT_TYPE, COOKIE, USER_AGENT};
|
use reqwest::header::{ACCEPT, CONTENT_TYPE, COOKIE, USER_AGENT};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
auth_cookie::get_auth_cookie,
|
auth_cookie::get_auth_cookie,
|
||||||
|
common_setup,
|
||||||
imfs::{Imfs, RealFetcher, WatchMode},
|
imfs::{Imfs, RealFetcher, WatchMode},
|
||||||
snapshot::{apply_patch_set, compute_patch_set, InstancePropertiesWithMeta, RojoTree},
|
|
||||||
snapshot_middleware::{snapshot_from_imfs, InstanceSnapshotContext},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Fail)]
|
#[derive(Debug, Fail)]
|
||||||
@@ -45,40 +43,15 @@ pub fn upload(options: UploadOptions) -> Result<(), UploadError> {
|
|||||||
.or_else(get_auth_cookie)
|
.or_else(get_auth_cookie)
|
||||||
.ok_or(UploadError::NeedAuthCookie)?;
|
.ok_or(UploadError::NeedAuthCookie)?;
|
||||||
|
|
||||||
let mut tree = RojoTree::new(InstancePropertiesWithMeta {
|
|
||||||
properties: RbxInstanceProperties {
|
|
||||||
name: "ROOT".to_owned(),
|
|
||||||
class_name: "Folder".to_owned(),
|
|
||||||
properties: HashMap::new(),
|
|
||||||
},
|
|
||||||
metadata: Default::default(),
|
|
||||||
});
|
|
||||||
let root_id = tree.get_root_id();
|
|
||||||
|
|
||||||
log::trace!("Constructing in-memory filesystem");
|
log::trace!("Constructing in-memory filesystem");
|
||||||
let mut imfs = Imfs::new(RealFetcher::new(WatchMode::Disabled));
|
let mut imfs = Imfs::new(RealFetcher::new(WatchMode::Disabled));
|
||||||
|
|
||||||
log::trace!("Reading project root");
|
let (_maybe_project, tree) = common_setup::start(&options.fuzzy_project_path, &mut imfs);
|
||||||
let entry = imfs
|
|
||||||
.get(&options.fuzzy_project_path)
|
|
||||||
.expect("could not get project path");
|
|
||||||
|
|
||||||
// TODO: Compute snapshot context from project.
|
|
||||||
log::trace!("Generating snapshot of instances from IMFS");
|
|
||||||
let snapshot = snapshot_from_imfs(&mut InstanceSnapshotContext::default(), &mut imfs, &entry)
|
|
||||||
.expect("snapshot failed")
|
|
||||||
.expect("snapshot did not return an instance");
|
|
||||||
|
|
||||||
log::trace!("Computing patch set");
|
|
||||||
let patch_set = compute_patch_set(&snapshot, &tree, root_id);
|
|
||||||
|
|
||||||
log::trace!("Applying patch set");
|
|
||||||
apply_patch_set(&mut tree, patch_set);
|
|
||||||
|
|
||||||
let root_id = tree.get_root_id();
|
let root_id = tree.get_root_id();
|
||||||
|
|
||||||
let mut buffer = Vec::new();
|
let mut buffer = Vec::new();
|
||||||
|
|
||||||
|
log::trace!("Encoding XML model");
|
||||||
let config = rbx_xml::EncodeOptions::new()
|
let config = rbx_xml::EncodeOptions::new()
|
||||||
.property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown);
|
.property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown);
|
||||||
rbx_xml::to_writer(&mut buffer, tree.inner(), &[root_id], config)?;
|
rbx_xml::to_writer(&mut buffer, tree.inner(), &[root_id], config)?;
|
||||||
@@ -88,6 +61,7 @@ pub fn upload(options: UploadOptions) -> Result<(), UploadError> {
|
|||||||
options.asset_id
|
options.asset_id
|
||||||
);
|
);
|
||||||
|
|
||||||
|
log::trace!("POSTing to {}", url);
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let mut response = client
|
let mut response = client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
|
|||||||
66
src/common_setup.rs
Normal file
66
src/common_setup.rs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
//! Initialization routines that are used by more than one Rojo command or
|
||||||
|
//! utility.
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use rbx_dom_weak::RbxInstanceProperties;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
imfs::{Imfs, ImfsFetcher},
|
||||||
|
project::{Project, ProjectLoadError},
|
||||||
|
snapshot::{apply_patch_set, compute_patch_set, InstancePropertiesWithMeta, RojoTree},
|
||||||
|
snapshot_middleware::{snapshot_from_imfs, InstanceSnapshotContext, SnapshotPluginContext},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn start<F: ImfsFetcher>(
|
||||||
|
fuzzy_project_path: &Path,
|
||||||
|
imfs: &mut Imfs<F>,
|
||||||
|
) -> (Option<Project>, RojoTree) {
|
||||||
|
log::trace!("Loading project file from {}", fuzzy_project_path.display());
|
||||||
|
let maybe_project = match Project::load_fuzzy(fuzzy_project_path) {
|
||||||
|
Ok(project) => Some(project),
|
||||||
|
Err(ProjectLoadError::NotFound) => None,
|
||||||
|
Err(other) => panic!("{}", other), // TODO: return error upward
|
||||||
|
};
|
||||||
|
|
||||||
|
log::trace!("Constructing initial tree");
|
||||||
|
let mut tree = RojoTree::new(InstancePropertiesWithMeta {
|
||||||
|
properties: RbxInstanceProperties {
|
||||||
|
name: "ROOT".to_owned(),
|
||||||
|
class_name: "Folder".to_owned(),
|
||||||
|
properties: Default::default(),
|
||||||
|
},
|
||||||
|
metadata: Default::default(),
|
||||||
|
});
|
||||||
|
|
||||||
|
let root_id = tree.get_root_id();
|
||||||
|
|
||||||
|
log::trace!("Constructing snapshot context");
|
||||||
|
let mut snapshot_context = InstanceSnapshotContext::default();
|
||||||
|
if let Some(project) = &maybe_project {
|
||||||
|
// If the project file defines no plugins, then there's no need to
|
||||||
|
// initialize the snapshot plugin context.
|
||||||
|
if !project.plugins.is_empty() {
|
||||||
|
snapshot_context.plugin_context =
|
||||||
|
Some(SnapshotPluginContext::new(project.plugins.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log::trace!("Reading project root");
|
||||||
|
let entry = imfs
|
||||||
|
.get(fuzzy_project_path)
|
||||||
|
.expect("could not get project path");
|
||||||
|
|
||||||
|
log::trace!("Generating snapshot of instances from IMFS");
|
||||||
|
let snapshot = snapshot_from_imfs(&mut snapshot_context, imfs, &entry)
|
||||||
|
.expect("snapshot failed")
|
||||||
|
.expect("snapshot did not return an instance");
|
||||||
|
|
||||||
|
log::trace!("Computing patch set");
|
||||||
|
let patch_set = compute_patch_set(&snapshot, &tree, root_id);
|
||||||
|
|
||||||
|
log::trace!("Applying patch set");
|
||||||
|
apply_patch_set(&mut tree, patch_set);
|
||||||
|
|
||||||
|
(maybe_project, tree)
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ mod tree_view;
|
|||||||
|
|
||||||
mod auth_cookie;
|
mod auth_cookie;
|
||||||
mod change_processor;
|
mod change_processor;
|
||||||
|
mod common_setup;
|
||||||
mod imfs;
|
mod imfs;
|
||||||
mod message_queue;
|
mod message_queue;
|
||||||
mod multimap;
|
mod multimap;
|
||||||
|
|||||||
@@ -5,18 +5,14 @@ use std::{
|
|||||||
time::Instant,
|
time::Instant,
|
||||||
};
|
};
|
||||||
|
|
||||||
use rbx_dom_weak::RbxInstanceProperties;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
change_processor::ChangeProcessor,
|
change_processor::ChangeProcessor,
|
||||||
|
common_setup,
|
||||||
imfs::{Imfs, ImfsFetcher},
|
imfs::{Imfs, ImfsFetcher},
|
||||||
message_queue::MessageQueue,
|
message_queue::MessageQueue,
|
||||||
project::Project,
|
project::Project,
|
||||||
session_id::SessionId,
|
session_id::SessionId,
|
||||||
snapshot::{
|
snapshot::{AppliedPatchSet, RojoTree},
|
||||||
apply_patch_set, compute_patch_set, AppliedPatchSet, InstancePropertiesWithMeta, RojoTree,
|
|
||||||
},
|
|
||||||
snapshot_middleware::{snapshot_from_imfs, InstanceSnapshotContext},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Contains all of the state for a Rojo serve session.
|
/// Contains all of the state for a Rojo serve session.
|
||||||
@@ -82,47 +78,14 @@ impl<F: ImfsFetcher + Send + 'static> ServeSession<F> {
|
|||||||
/// The project file is expected to be loaded out-of-band since it's
|
/// The project file is expected to be loaded out-of-band since it's
|
||||||
/// currently loaded from the filesystem directly instead of through the
|
/// currently loaded from the filesystem directly instead of through the
|
||||||
/// in-memory filesystem layer.
|
/// in-memory filesystem layer.
|
||||||
pub fn new<P: AsRef<Path>>(
|
pub fn new<P: AsRef<Path>>(mut imfs: Imfs<F>, start_path: P) -> Self {
|
||||||
mut imfs: Imfs<F>,
|
|
||||||
start_path: P,
|
|
||||||
root_project: Option<Project>,
|
|
||||||
) -> Self {
|
|
||||||
let start_path = start_path.as_ref();
|
let start_path = start_path.as_ref();
|
||||||
|
|
||||||
log::trace!(
|
log::trace!("Starting new ServeSession at path {}", start_path.display(),);
|
||||||
"Starting new ServeSession at path {} with project {:#?}",
|
|
||||||
start_path.display(),
|
|
||||||
root_project
|
|
||||||
);
|
|
||||||
|
|
||||||
let start_time = Instant::now();
|
let start_time = Instant::now();
|
||||||
|
|
||||||
log::trace!("Constructing initial tree");
|
let (root_project, tree) = common_setup::start(start_path, &mut imfs);
|
||||||
let mut tree = RojoTree::new(InstancePropertiesWithMeta {
|
|
||||||
properties: RbxInstanceProperties {
|
|
||||||
name: "ROOT".to_owned(),
|
|
||||||
class_name: "Folder".to_owned(),
|
|
||||||
properties: Default::default(),
|
|
||||||
},
|
|
||||||
metadata: Default::default(),
|
|
||||||
});
|
|
||||||
let root_id = tree.get_root_id();
|
|
||||||
|
|
||||||
log::trace!("Loading start path: {}", start_path.display());
|
|
||||||
let entry = imfs.get(start_path).expect("could not get project path");
|
|
||||||
|
|
||||||
// TODO: Compute snapshot context from project.
|
|
||||||
log::trace!("Snapshotting start path");
|
|
||||||
let snapshot =
|
|
||||||
snapshot_from_imfs(&mut InstanceSnapshotContext::default(), &mut imfs, &entry)
|
|
||||||
.expect("snapshot failed")
|
|
||||||
.expect("snapshot did not return an instance");
|
|
||||||
|
|
||||||
log::trace!("Computing initial patch set");
|
|
||||||
let patch_set = compute_patch_set(&snapshot, &tree, root_id);
|
|
||||||
|
|
||||||
log::trace!("Applying initial patch set");
|
|
||||||
apply_patch_set(&mut tree, patch_set);
|
|
||||||
|
|
||||||
let session_id = SessionId::new();
|
let session_id = SessionId::new();
|
||||||
let message_queue = MessageQueue::new();
|
let message_queue = MessageQueue::new();
|
||||||
@@ -177,6 +140,12 @@ impl<F: ImfsFetcher> ServeSession<F> {
|
|||||||
.map(|project| project.name.as_str())
|
.map(|project| project.name.as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn project_port(&self) -> Option<u16> {
|
||||||
|
self.root_project
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|project| project.serve_port)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn start_time(&self) -> Instant {
|
pub fn start_time(&self) -> Instant {
|
||||||
self.start_time
|
self.start_time
|
||||||
}
|
}
|
||||||
@@ -214,7 +183,7 @@ mod serve_session {
|
|||||||
|
|
||||||
imfs.debug_load_snapshot("/foo", ImfsSnapshot::empty_dir());
|
imfs.debug_load_snapshot("/foo", ImfsSnapshot::empty_dir());
|
||||||
|
|
||||||
let session = ServeSession::new(imfs, "/foo", None);
|
let session = ServeSession::new(imfs, "/foo");
|
||||||
|
|
||||||
let mut rm = RedactionMap::new();
|
let mut rm = RedactionMap::new();
|
||||||
assert_yaml_snapshot!(view_tree(&session.tree(), &mut rm));
|
assert_yaml_snapshot!(view_tree(&session.tree(), &mut rm));
|
||||||
@@ -241,7 +210,7 @@ mod serve_session {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
let session = ServeSession::new(imfs, "/foo", None);
|
let session = ServeSession::new(imfs, "/foo");
|
||||||
|
|
||||||
let mut rm = RedactionMap::new();
|
let mut rm = RedactionMap::new();
|
||||||
assert_yaml_snapshot!(view_tree(&session.tree(), &mut rm));
|
assert_yaml_snapshot!(view_tree(&session.tree(), &mut rm));
|
||||||
@@ -259,7 +228,7 @@ mod serve_session {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
let session = ServeSession::new(imfs, "/root", None);
|
let session = ServeSession::new(imfs, "/root");
|
||||||
|
|
||||||
let mut rm = RedactionMap::new();
|
let mut rm = RedactionMap::new();
|
||||||
assert_yaml_snapshot!(view_tree(&session.tree(), &mut rm));
|
assert_yaml_snapshot!(view_tree(&session.tree(), &mut rm));
|
||||||
@@ -278,7 +247,7 @@ mod serve_session {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let imfs = Imfs::new(fetcher);
|
let imfs = Imfs::new(fetcher);
|
||||||
let session = ServeSession::new(imfs, "/root", None);
|
let session = ServeSession::new(imfs, "/root");
|
||||||
|
|
||||||
let mut redactions = RedactionMap::new();
|
let mut redactions = RedactionMap::new();
|
||||||
assert_yaml_snapshot!(
|
assert_yaml_snapshot!(
|
||||||
@@ -317,7 +286,7 @@ mod serve_session {
|
|||||||
state.load_snapshot("/foo.txt", ImfsSnapshot::file("Hello!"));
|
state.load_snapshot("/foo.txt", ImfsSnapshot::file("Hello!"));
|
||||||
|
|
||||||
let imfs = Imfs::new(fetcher);
|
let imfs = Imfs::new(fetcher);
|
||||||
let session = ServeSession::new(imfs, "/foo.txt", None);
|
let session = ServeSession::new(imfs, "/foo.txt");
|
||||||
|
|
||||||
let mut redactions = RedactionMap::new();
|
let mut redactions = RedactionMap::new();
|
||||||
assert_yaml_snapshot!(
|
assert_yaml_snapshot!(
|
||||||
|
|||||||
Reference in New Issue
Block a user