Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rojo open #896

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aftman.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tools]
rojo = "rojo-rbx/rojo@7.3.0"
rojo = "rojo-rbx/rojo@7.4.1"
selene = "Kampfkarren/[email protected]"
stylua = "JohnnyMorganz/stylua@0.18.2"
stylua = "JohnnyMorganz/stylua@0.20.0"
run-in-roblox = "rojo-rbx/[email protected]"
21 changes: 19 additions & 2 deletions plugin/src/App/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ function App:init()
toolbarIcon = Assets.Images.PluginButton,
})

local rojoOpenValue = game:FindFirstChild("ROJO_OPEN")
if rojoOpenValue and rojoOpenValue:IsA("StringValue") then
local host, port = unpack(rojoOpenValue.Value:split(","))

if host ~= Config.defaultHost then
self.setHost(host)
end

if port ~= Config.defaultPort then
self.setPort(port)
end

rojoOpenValue:Destroy()

self:startSession(true)
end

if
RunService:IsEdit()
and self.serveSession == nil
Expand Down Expand Up @@ -407,7 +424,7 @@ function App:useRunningConnectionInfo()
self.setPort(port)
end

function App:startSession()
function App:startSession(skipInitialSync)
local claimedLock, priorOwner = self:claimSyncLock()
if not claimedLock then
local msg = string.format("Could not sync because user '%s' is already syncing", tostring(priorOwner))
Expand Down Expand Up @@ -623,7 +640,7 @@ function App:startSession()
return self.confirmationEvent:Wait()
end)

serveSession:start()
serveSession:start(not not skipInitialSync)

self.serveSession = serveSession
end
Expand Down
10 changes: 7 additions & 3 deletions plugin/src/ServeSession.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ function ServeSession:hookPostcommit(callback)
return self.__reconciler:hookPostcommit(callback)
end

function ServeSession:start()
function ServeSession:start(skipInitialSync)
self:__setStatus(Status.Connecting)

self.__apiContext
:connect()
:andThen(function(serverInfo)
self:__applyGameAndPlaceId(serverInfo)

return self:__initialSync(serverInfo):andThen(function()
return self:__initialSync(serverInfo, skipInitialSync):andThen(function()
self:__setStatus(Status.Connected, serverInfo.projectName)

return self:__mainSyncLoop()
Expand Down Expand Up @@ -208,7 +208,7 @@ function ServeSession:__onActiveScriptChanged(activeScript)
self.__apiContext:open(scriptId)
end

function ServeSession:__initialSync(serverInfo)
function ServeSession:__initialSync(serverInfo, skipInitialSync)
return self.__apiContext:read({ serverInfo.rootInstanceId }):andThen(function(readResponseBody)
-- Tell the API Context that we're up-to-date with the version of
-- the tree defined in this response.
Expand All @@ -219,6 +219,10 @@ function ServeSession:__initialSync(serverInfo)
Log.trace("Matching existing Roblox instances to Rojo IDs")
self.__reconciler:hydrate(readResponseBody.instances, serverInfo.rootInstanceId, game)

if skipInitialSync then
return Promise.resolve()
end

-- Calculate the initial patch to apply to the DataModel to catch us
-- up to what Rojo thinks the place should look like.
Log.trace("Computing changes that plugin needs to make to catch up to server...")
Expand Down
14 changes: 12 additions & 2 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl BuildCommand {

/// The different kinds of output that Rojo can build to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OutputKind {
pub(super) enum OutputKind {
/// An XML model file.
Rbxmx,

Expand All @@ -123,6 +123,16 @@ enum OutputKind {
}

impl OutputKind {
pub(super) fn from_place_path(output: &Path) -> Option<OutputKind> {
let extension = output.extension()?.to_str()?;

match extension {
"rbxlx" => Some(OutputKind::Rbxlx),
"rbxl" => Some(OutputKind::Rbxl),
_ => None,
}
}

fn from_output_path(output: &Path) -> Option<OutputKind> {
let extension = output.extension()?.to_str()?;

Expand Down Expand Up @@ -151,7 +161,7 @@ fn xml_encode_config() -> rbx_xml::EncodeOptions<'static> {
}

#[profiling::function]
fn write_model(
pub(super) fn write_model(
session: &ServeSession,
output: &Path,
output_kind: OutputKind,
Expand Down
4 changes: 4 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod build;
mod doc;
mod fmt_project;
mod init;
mod open;
mod plugin;
mod serve;
mod sourcemap;
Expand All @@ -18,6 +19,7 @@ pub use self::build::BuildCommand;
pub use self::doc::DocCommand;
pub use self::fmt_project::FmtProjectCommand;
pub use self::init::{InitCommand, InitKind};
use self::open::OpenCommand;
pub use self::plugin::{PluginCommand, PluginSubcommand};
pub use self::serve::ServeCommand;
pub use self::sourcemap::SourcemapCommand;
Expand All @@ -41,6 +43,7 @@ impl Options {
Subcommand::Init(subcommand) => subcommand.run(),
Subcommand::Serve(subcommand) => subcommand.run(self.global),
Subcommand::Build(subcommand) => subcommand.run(),
Subcommand::Open(subcommand) => subcommand.run(self.global),
Subcommand::Upload(subcommand) => subcommand.run(),
Subcommand::Sourcemap(subcommand) => subcommand.run(),
Subcommand::FmtProject(subcommand) => subcommand.run(),
Expand Down Expand Up @@ -114,6 +117,7 @@ pub enum Subcommand {
Init(InitCommand),
Serve(ServeCommand),
Build(BuildCommand),
Open(OpenCommand),
Upload(UploadCommand),
Sourcemap(SourcemapCommand),
FmtProject(FmtProjectCommand),
Expand Down
172 changes: 172 additions & 0 deletions src/cli/open.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
use std::{
fs::File,
io::{BufReader, BufWriter},
net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener},
path::{Path, PathBuf},
sync::Arc,
};

use anyhow::Context;
use clap::{IntoApp, Parser};
use memofs::Vfs;
use rbx_dom_weak::InstanceBuilder;

use crate::{serve_session::ServeSession, web::LiveServer};

use super::{
build::{write_model, OutputKind},
resolve_path,
serve::show_start_message,
GlobalOptions,
};

const UNKNOWN_OUTPUT_KIND_ERR: &str = "Could not detect what kind of file to build. \
Expected output file to end in .rbxl or .rbxlx.";
const DEFAULT_BIND_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
const DEFAULT_PORT: u16 = 34872;

/// TODO
#[derive(Debug, Parser)]
pub struct OpenCommand {
/// Path to the project to serve. Defaults to the current directory.
#[clap(default_value = "")]
pub project: PathBuf,

/// Where to output the result.
///
/// Should end in .rbxm, .rbxl.
#[clap(long, short, conflicts_with = "place", conflicts_with = "cloud")]
pub output: Option<PathBuf>,

/// TODO
#[clap(long, short, conflicts_with = "output", conflicts_with = "cloud")]
pub place: Option<PathBuf>,

/// TODO
#[clap(long, short, conflicts_with = "output", conflicts_with = "place")]
pub cloud: Option<u64>,

/// The IP address to listen on. Defaults to `127.0.0.1`.
#[clap(long)]
pub address: Option<IpAddr>,

/// The port to listen on. Defaults to the project's preference, or a random usable port if
/// it has none.
#[clap(long)]
pub port: Option<u16>,
}

// TODO: Support existing places.
impl OpenCommand {
pub fn run(self, global: GlobalOptions) -> anyhow::Result<()> {
if self.output.is_none() && self.place.is_none() && self.cloud.is_none() {
OpenCommand::command()
.error(
clap::ErrorKind::MissingRequiredArgument,
"one of the following arguments must be provided: \n --output <OUTPUT>\n --place <PLACE>\n --cloud <CLOUD>",
)
.exit();
}

let project = resolve_path(&self.project);

let vfs = Vfs::new_default();
let session = ServeSession::new(vfs, project)?;

let ip = self
.address
.or_else(|| session.serve_address())
.unwrap_or(DEFAULT_BIND_ADDRESS.into());

let port = self
.port
.or_else(|| session.project_port())
.or_else(|| {
if self.cloud.is_some() {
None
} else {
random_port(ip)
}
})
.unwrap_or(DEFAULT_PORT);

match self.cloud {
Some(place_id) => {
opener::open(format!(
"roblox-studio:1+task:EditPlace+universeId:0+placeId:{place_id}"
))?;
}
None => {
let is_existing_place = self.place.is_some();
let path = self.output.unwrap_or_else(|| self.place.unwrap());
let output_kind =
OutputKind::from_place_path(&path).context(UNKNOWN_OUTPUT_KIND_ERR)?;

if !is_existing_place {
write_model(&session, &path, OutputKind::Rbxl)?;
}

inject_rojo_open_string_value(&path, output_kind, ip, port)?;

opener::open(path)?;
}
}

let server = LiveServer::new(Arc::new(session));

let _ = show_start_message(ip, port, global.color.into());
server.start((ip, port).into());

Ok(())
}
}

fn random_port(ip: IpAddr) -> Option<u16> {
Some(
TcpListener::bind(SocketAddr::new(ip, 0))
.ok()?
.local_addr()
.ok()?
.port(),
)
}

fn inject_rojo_open_string_value(
path: &Path,
output_kind: OutputKind,
ip: IpAddr,
port: u16,
) -> anyhow::Result<()> {
let file = File::open(path).unwrap();

let mut dom = match output_kind {
OutputKind::Rbxl => rbx_binary::from_reader(BufReader::new(file)).unwrap(),
OutputKind::Rbxlx => rbx_xml::from_reader_default(BufReader::new(file)).unwrap(),
_ => unreachable!(),
};

let ip = if ip.is_loopback() {
"localhost".to_string()
} else {
ip.to_string()
};

dom.insert(
dom.root_ref(),
InstanceBuilder::new("StringValue")
.with_name("ROJO_OPEN")
.with_property("Value", format!("{ip},{port}",)),
);

let root_instance = dom.root();
let top_level_ids = root_instance.children();
let output = BufWriter::new(File::create(path).unwrap());

match output_kind {
OutputKind::Rbxl => rbx_binary::to_writer(output, &dom, top_level_ids)?,
OutputKind::Rbxlx => rbx_xml::to_writer_default(output, &dom, top_level_ids)?,
_ => unreachable!(),
}

Ok(())
}
6 changes: 5 additions & 1 deletion src/cli/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ impl ServeCommand {
}
}

fn show_start_message(bind_address: IpAddr, port: u16, color: ColorChoice) -> io::Result<()> {
pub(super) fn show_start_message(
bind_address: IpAddr,
port: u16,
color: ColorChoice,
) -> io::Result<()> {
let mut green = ColorSpec::new();
green.set_fg(Some(Color::Green)).set_bold(true);

Expand Down
Loading