Expose more project stuff via the API

This commit is contained in:
Lucien Greathouse
2018-11-17 01:14:07 -08:00
parent 38e0f82812
commit 729ab25581
4 changed files with 73 additions and 33 deletions

View File

@@ -137,13 +137,15 @@ impl fmt::Display for ProjectSaveError {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ProjectNode { pub enum ProjectNode {
Instance(InstanceProjectNode), Instance(InstanceProjectNode),
SyncPoint(SyncPointProjectNode), SyncPoint(SyncPointProjectNode),
} }
#[derive(Debug)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InstanceProjectNode { pub struct InstanceProjectNode {
pub class_name: String, pub class_name: String,
pub children: HashMap<String, ProjectNode>, pub children: HashMap<String, ProjectNode>,
@@ -151,12 +153,13 @@ pub struct InstanceProjectNode {
// ignore_unknown: bool, // ignore_unknown: bool,
} }
#[derive(Debug)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncPointProjectNode { pub struct SyncPointProjectNode {
pub path: PathBuf, pub path: PathBuf,
} }
#[derive(Debug)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Project { pub struct Project {
pub name: String, pub name: String,
pub tree: ProjectNode, pub tree: ProjectNode,

View File

@@ -16,6 +16,7 @@ use crate::{
pub struct RbxSession { pub struct RbxSession {
tree: RbxTree, tree: RbxTree,
paths_to_ids: HashMap<PathBuf, RbxId>, paths_to_ids: HashMap<PathBuf, RbxId>,
ids_to_project_paths: HashMap<RbxId, String>,
message_queue: Arc<MessageQueue>, message_queue: Arc<MessageQueue>,
vfs: Arc<Mutex<Vfs>>, vfs: Arc<Mutex<Vfs>>,
project: Arc<Project>, project: Arc<Project>,
@@ -23,7 +24,7 @@ pub struct RbxSession {
impl RbxSession { impl RbxSession {
pub fn new(project: Arc<Project>, vfs: Arc<Mutex<Vfs>>, message_queue: Arc<MessageQueue>) -> RbxSession { pub fn new(project: Arc<Project>, vfs: Arc<Mutex<Vfs>>, message_queue: Arc<MessageQueue>) -> RbxSession {
let (tree, paths_to_ids) = { let (tree, paths_to_ids, ids_to_project_paths) = {
let temp_vfs = vfs.lock().unwrap(); let temp_vfs = vfs.lock().unwrap();
construct_initial_tree(&project, &temp_vfs) construct_initial_tree(&project, &temp_vfs)
}; };
@@ -31,6 +32,7 @@ impl RbxSession {
RbxSession { RbxSession {
tree, tree,
paths_to_ids, paths_to_ids,
ids_to_project_paths,
message_queue, message_queue,
vfs, vfs,
project, project,
@@ -56,14 +58,19 @@ impl RbxSession {
pub fn get_tree(&self) -> &RbxTree { pub fn get_tree(&self) -> &RbxTree {
&self.tree &self.tree
} }
pub fn get_project_path_map(&self) -> &HashMap<RbxId, String> {
&self.ids_to_project_paths
}
} }
fn construct_initial_tree( fn construct_initial_tree(
project: &Project, project: &Project,
vfs: &Vfs, vfs: &Vfs,
) -> (RbxTree, HashMap<PathBuf, RbxId>) { ) -> (RbxTree, HashMap<PathBuf, RbxId>, HashMap<RbxId, String>) {
let mut paths_to_ids = HashMap::new(); let paths_to_ids = HashMap::new();
let mut tree = RbxTree::new(RbxInstance { let ids_to_project_paths = HashMap::new();
let tree = RbxTree::new(RbxInstance {
name: "this isn't supposed to be here".to_string(), name: "this isn't supposed to be here".to_string(),
class_name: "ahhh, help me".to_string(), class_name: "ahhh, help me".to_string(),
properties: HashMap::new(), properties: HashMap::new(),
@@ -71,59 +78,80 @@ fn construct_initial_tree(
let root_id = tree.get_root_id(); let root_id = tree.get_root_id();
construct_initial_tree_node(&mut tree, vfs, &mut paths_to_ids, root_id, "<<<ROOT>>>", &project.tree); let mut context = ConstructContext {
tree,
vfs,
paths_to_ids,
ids_to_project_paths,
};
(tree, paths_to_ids) construct_project_node(
&mut context,
root_id,
"<<<ROOT>>>".to_string(),
"<<<ROOT>>>",
&project.tree,
);
(context.tree, context.paths_to_ids, context.ids_to_project_paths)
} }
fn construct_initial_tree_node( struct ConstructContext<'a> {
tree: &mut RbxTree, tree: RbxTree,
vfs: &Vfs, vfs: &'a Vfs,
paths_to_ids: &mut HashMap<PathBuf, RbxId>, paths_to_ids: HashMap<PathBuf, RbxId>,
ids_to_project_paths: HashMap<RbxId, String>,
}
fn construct_project_node(
context: &mut ConstructContext,
parent_instance_id: RbxId, parent_instance_id: RbxId,
instance_path: String,
instance_name: &str, instance_name: &str,
project_node: &ProjectNode, project_node: &ProjectNode,
) { ) {
match project_node { match project_node {
ProjectNode::Instance(node) => { ProjectNode::Instance(node) => {
construct_instance_node(tree, vfs, paths_to_ids, parent_instance_id, instance_name, node); let id = construct_instance_node(context, parent_instance_id, &instance_path, instance_name, node);
context.ids_to_project_paths.insert(id, instance_path.to_string());
}, },
ProjectNode::SyncPoint(node) => { ProjectNode::SyncPoint(node) => {
construct_sync_point_node(tree, vfs, paths_to_ids, parent_instance_id, instance_name, &node.path); let id = construct_sync_point_node(context, parent_instance_id, instance_name, &node.path);
context.ids_to_project_paths.insert(id, instance_path.to_string());
}, },
} }
} }
fn construct_instance_node( fn construct_instance_node(
tree: &mut RbxTree, context: &mut ConstructContext,
vfs: &Vfs,
paths_to_ids: &mut HashMap<PathBuf, RbxId>,
parent_instance_id: RbxId, parent_instance_id: RbxId,
instance_path: &str,
instance_name: &str, instance_name: &str,
project_node: &InstanceProjectNode, project_node: &InstanceProjectNode,
) { ) -> RbxId {
let instance = RbxInstance { let instance = RbxInstance {
class_name: project_node.class_name.clone(), class_name: project_node.class_name.clone(),
name: instance_name.to_string(), name: instance_name.to_string(),
properties: HashMap::new(), properties: HashMap::new(),
}; };
let id = tree.insert_instance(instance, parent_instance_id); let id = context.tree.insert_instance(instance, parent_instance_id);
for (child_name, child_project_node) in &project_node.children { for (child_name, child_project_node) in &project_node.children {
construct_initial_tree_node(tree, vfs, paths_to_ids, id, child_name, child_project_node); let child_path = format!("{}/{}", instance_path, child_name);
construct_project_node(context, id, child_path, child_name, child_project_node);
} }
id
} }
fn construct_sync_point_node( fn construct_sync_point_node(
tree: &mut RbxTree, context: &mut ConstructContext,
vfs: &Vfs,
paths_to_ids: &mut HashMap<PathBuf, RbxId>,
parent_instance_id: RbxId, parent_instance_id: RbxId,
instance_name: &str, instance_name: &str,
file_path: &Path, file_path: &Path,
) { ) -> RbxId {
match vfs.get(&file_path) { match context.vfs.get(&file_path) {
Some(VfsItem::File(file)) => { Some(VfsItem::File(file)) => {
let contents = str::from_utf8(&file.contents).unwrap(); let contents = str::from_utf8(&file.contents).unwrap();
@@ -136,8 +164,10 @@ fn construct_sync_point_node(
properties, properties,
}; };
let id = tree.insert_instance(instance, parent_instance_id); let id = context.tree.insert_instance(instance, parent_instance_id);
paths_to_ids.insert(file.path.clone(), id); context.paths_to_ids.insert(file.path.clone(), id);
id
}, },
Some(VfsItem::Directory(directory)) => { Some(VfsItem::Directory(directory)) => {
let instance = RbxInstance { let instance = RbxInstance {
@@ -146,13 +176,15 @@ fn construct_sync_point_node(
properties: HashMap::new(), properties: HashMap::new(),
}; };
let id = tree.insert_instance(instance, parent_instance_id); let id = context.tree.insert_instance(instance, parent_instance_id);
paths_to_ids.insert(directory.path.clone(), id); context.paths_to_ids.insert(directory.path.clone(), id);
for child_path in &directory.children { for child_path in &directory.children {
let child_instance_name = child_path.file_name().unwrap().to_str().unwrap(); let child_instance_name = child_path.file_name().unwrap().to_str().unwrap();
construct_sync_point_node(tree, vfs, paths_to_ids, id, child_instance_name, child_path); construct_sync_point_node(context, id, child_instance_name, child_path);
} }
id
}, },
None => panic!("Couldn't read {} from disk", file_path.display()), None => panic!("Couldn't read {} from disk", file_path.display()),
} }

View File

@@ -24,7 +24,7 @@ use crate::{
const WATCH_TIMEOUT_MS: u64 = 100; const WATCH_TIMEOUT_MS: u64 = 100;
pub struct Session { pub struct Session {
project: Arc<Project>, pub project: Arc<Project>,
pub session_id: SessionId, pub session_id: SessionId,
pub message_queue: Arc<MessageQueue>, pub message_queue: Arc<MessageQueue>,
pub rbx_session: Arc<Mutex<RbxSession>>, pub rbx_session: Arc<Mutex<RbxSession>>,

View File

@@ -11,6 +11,7 @@ use crate::{
message_queue::Message, message_queue::Message,
session::Session, session::Session,
session_id::SessionId, session_id::SessionId,
project::Project,
}; };
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@@ -20,6 +21,8 @@ pub struct ServerInfoResponse<'a> {
pub server_version: &'a str, pub server_version: &'a str,
pub protocol_version: u64, pub protocol_version: u64,
pub root_instance_id: RbxId, pub root_instance_id: RbxId,
pub project: Cow<'a, Project>,
pub project_paths_to_ids: Cow<'a, HashMap<RbxId, String>>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@@ -71,6 +74,8 @@ impl Server {
protocol_version: 2, protocol_version: 2,
session_id: self.session.session_id, session_id: self.session.session_id,
root_instance_id: tree.get_root_id(), root_instance_id: tree.get_root_id(),
project: Cow::Borrowed(&self.session.project),
project_paths_to_ids: Cow::Borrowed(rbx_session.get_project_path_map()),
}) })
}, },