forked from rojo-rbx/rojo
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::{
|
||||
collections::HashMap,
|
||||
fs::File,
|
||||
io::{self, BufWriter, Write},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use failure::Fail;
|
||||
use rbx_dom_weak::RbxInstanceProperties;
|
||||
|
||||
use crate::{
|
||||
common_setup,
|
||||
imfs::{FsError, Imfs, RealFetcher, WatchMode},
|
||||
project::{Project, ProjectLoadError},
|
||||
snapshot::{apply_patch_set, compute_patch_set, InstancePropertiesWithMeta, RojoTree},
|
||||
snapshot_middleware::{snapshot_from_imfs, InstanceSnapshotContext, SnapshotPluginContext},
|
||||
project::ProjectLoadError,
|
||||
};
|
||||
|
||||
#[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);
|
||||
|
||||
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");
|
||||
let mut imfs = Imfs::new(RealFetcher::new(WatchMode::Disabled));
|
||||
|
||||
log::trace!("Reading project root");
|
||||
let entry = imfs
|
||||
.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);
|
||||
let (_maybe_project, tree) = common_setup::start(&options.fuzzy_project_path, &mut imfs);
|
||||
let root_id = tree.get_root_id();
|
||||
|
||||
log::trace!("Opening output file for write");
|
||||
let mut file = BufWriter::new(File::create(&options.output_file)?);
|
||||
|
||||
@@ -9,7 +9,7 @@ use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
|
||||
|
||||
use crate::{
|
||||
imfs::{Imfs, RealFetcher, WatchMode},
|
||||
project::{Project, ProjectLoadError},
|
||||
project::ProjectLoadError,
|
||||
serve_session::ServeSession,
|
||||
web::LiveServer,
|
||||
};
|
||||
@@ -33,33 +33,18 @@ impl_from!(ServeError {
|
||||
});
|
||||
|
||||
pub fn serve(options: &ServeOptions) -> Result<(), ServeError> {
|
||||
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()),
|
||||
};
|
||||
let imfs = Imfs::new(RealFetcher::new(WatchMode::Enabled));
|
||||
|
||||
let session = Arc::new(ServeSession::new(imfs, &options.fuzzy_project_path));
|
||||
|
||||
let port = options
|
||||
.port
|
||||
.or_else(|| {
|
||||
maybe_project
|
||||
.as_ref()
|
||||
.and_then(|project| project.serve_port)
|
||||
})
|
||||
.or_else(|| session.project_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 _ = show_start_message(port);
|
||||
server.start(port);
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use failure::Fail;
|
||||
use rbx_dom_weak::RbxInstanceProperties;
|
||||
use reqwest::header::{ACCEPT, CONTENT_TYPE, COOKIE, USER_AGENT};
|
||||
|
||||
use crate::{
|
||||
auth_cookie::get_auth_cookie,
|
||||
common_setup,
|
||||
imfs::{Imfs, RealFetcher, WatchMode},
|
||||
snapshot::{apply_patch_set, compute_patch_set, InstancePropertiesWithMeta, RojoTree},
|
||||
snapshot_middleware::{snapshot_from_imfs, InstanceSnapshotContext},
|
||||
};
|
||||
|
||||
#[derive(Debug, Fail)]
|
||||
@@ -45,40 +43,15 @@ pub fn upload(options: UploadOptions) -> Result<(), UploadError> {
|
||||
.or_else(get_auth_cookie)
|
||||
.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");
|
||||
let mut imfs = Imfs::new(RealFetcher::new(WatchMode::Disabled));
|
||||
|
||||
log::trace!("Reading project root");
|
||||
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 (_maybe_project, tree) = common_setup::start(&options.fuzzy_project_path, &mut imfs);
|
||||
let root_id = tree.get_root_id();
|
||||
|
||||
let mut buffer = Vec::new();
|
||||
|
||||
log::trace!("Encoding XML model");
|
||||
let config = rbx_xml::EncodeOptions::new()
|
||||
.property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown);
|
||||
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
|
||||
);
|
||||
|
||||
log::trace!("POSTing to {}", url);
|
||||
let client = reqwest::Client::new();
|
||||
let mut response = client
|
||||
.post(&url)
|
||||
|
||||
Reference in New Issue
Block a user