Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
refactor(rome_bin): merge the CLI and Language Server (#3044)
Browse files Browse the repository at this point in the history
* refactor(rome_bin): merge the CLI and Language Server

* address PR review

* rename `run_cli` to `CliSession::run`

* fix CI

* address PR review

* improve the printing of transport errors
  • Loading branch information
leops committed Aug 12, 2022
1 parent 7d15325 commit c2927dc
Show file tree
Hide file tree
Showing 51 changed files with 1,589 additions and 821 deletions.
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ bench_parser = "run -p xtask_bench --release -- --feature parser"
bench_formatter = "run -p xtask_bench --release -- --feature formatter"
bench_analyzer = "run -p xtask_bench --release -- --feature analyzer"
coverage = "run -p xtask_coverage --profile=release-with-debug --"
rome-cli = "run -p rome_cli --release --"
rome-cli-dev = "run -p rome_cli --"
rome-cli = "run -p rome_bin --release --"
rome-cli-dev = "run -p rome_bin --"

[profile.release]
lto = true
2 changes: 1 addition & 1 deletion .github/workflows/release_cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:

# Build the CLI binary
- name: Build binaries
run: cargo build -p rome_cli --release --target ${{ matrix.target }}
run: cargo build -p rome_bin --release --target ${{ matrix.target }}
env:
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
# Strip all debug symbols from the resulting binaries
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release_lsp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:

# Build the LSP binary
- name: Build binaries
run: cargo build -p rome_lsp --release --target ${{ matrix.target }}
run: cargo build -p rome_bin --release --target ${{ matrix.target }}
env:
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
# Strip all debug symbols from the resulting binaries
Expand All @@ -113,13 +113,13 @@ jobs:
run: |
mkdir dist
mkdir editors/vscode/server
cp target/${{ matrix.target }}/release/rome_lsp.exe editors/vscode/server/rome_lsp.exe
cp target/${{ matrix.target }}/release/rome.exe editors/vscode/server/rome.exe
- name: Copy LSP binary
if: matrix.os != 'windows-2022'
run: |
mkdir dist
mkdir editors/vscode/server
cp target/${{ matrix.target }}/release/rome_lsp editors/vscode/server/rome_lsp
cp target/${{ matrix.target }}/release/rome editors/vscode/server/rome
- name: Install Node.js
uses: actions/setup-node@v3
Expand Down
62 changes: 55 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions crates/rome_bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "rome_bin"
version = "0.0.0"
edition = "2021"
authors = ["Rome Tools Developers and Contributors"]
license = "MIT"
repository = "https://github.com/rome/tools"
description = "Rome's main binary distribution"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "rome"
path = "src/main.rs"

[dependencies]
rome_cli = { path = "../rome_cli" }
rome_lsp = { path = "../rome_lsp" }
rome_service = { path = "../rome_service" }

tokio = { version = "1.15.0", features = ["io-std", "io-util", "net", "time", "rt", "rt-multi-thread"] }
tower-lsp = { version = "0.17.0" }
anyhow = "1.0.52"

tracing = { version = "0.1.31", default-features = false, features = ["std"] }
tracing-tree = "0.2.0"
tracing-subscriber = { version = "0.3.5", features = ["env-filter"] }
tracing-appender = "0.2"

[target.'cfg(unix)'.dependencies]
libc = "0.2.127"
tokio = { version = "1.15.0", features = ["io-std", "io-util", "net", "time", "process", "rt", "rt-multi-thread", "macros"] }

[target.'cfg(windows)'.dependencies]
mimalloc = "0.1.29"
11 changes: 11 additions & 0 deletions crates/rome_bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `rome_bin`

Rome's main binary distribution, exposes the command line interface defined in
`rome_cli`, and the language server interface defined in `rome_lsp` and used by
the `rome` VSCode extension

# Logs

When the server is run in daemon mode, it will output logs to a file created in
a `rome-logs` directory inside the system temporary directory. The log file
will be rotated on a hourly basis.
19 changes: 19 additions & 0 deletions crates/rome_bin/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rome_cli::{setup_panic_handler, Arguments, CliSession, Termination};
use rome_service::workspace;
use tokio::runtime::Runtime;

use crate::service::open_transport;

pub fn run_cli_session(args: Arguments) -> Result<(), Termination> {
setup_panic_handler();

// Try to open a connection to an existing Rome server socket, or create an
// in-process Workspace server instance if no daemon process is found
let runtime = Runtime::new()?;
let workspace = match open_transport(runtime)? {
Some(transport) => workspace::client(transport)?,
None => workspace::server(),
};

CliSession::new(&*workspace, args).run()
}
9 changes: 9 additions & 0 deletions crates/rome_bin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![doc = include_str!("../README.md")]

mod cli;
mod server;
mod service;

pub use cli::run_cli_session;
pub use server::{print_server_socket, run_server_session};
pub use service::SocketTransport;
29 changes: 29 additions & 0 deletions crates/rome_bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! This is the main binary of Rome.
//!
//! If you're curios about how to use it, check Rome's [website]
//!
//! [website]: https://rome.tools

use cli::run_cli_session;
use rome_cli::{Arguments, Termination};
use server::{print_server_socket, run_server_session};

mod cli;
mod server;
mod service;

#[cfg(target_os = "windows")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

fn main() -> Result<(), Termination> {
let mut args = Arguments::from_env();

if args.contains("__print_socket") {
print_server_socket()
} else if args.contains("__run_server") {
run_server_session()
} else {
run_cli_session(args)
}
}
56 changes: 56 additions & 0 deletions crates/rome_bin/src/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::env;

use rome_cli::Termination;
use rome_lsp::ServerFactory;
use tokio::runtime::Runtime;
use tracing::{debug_span, Instrument};
use tracing_subscriber::{prelude::*, registry, EnvFilter, Layer};
use tracing_tree::HierarchicalLayer;

use crate::service::{print_socket, run_daemon};

pub fn run_server_session() -> Result<(), Termination> {
setup_tracing_subscriber();

let rt = Runtime::new()?;
let factory = ServerFactory::default();
let span = debug_span!("Running Server", pid = std::process::id());
rt.block_on(run_daemon(factory).instrument(span))?;

Ok(())
}

pub fn print_server_socket() -> Result<(), Termination> {
let rt = Runtime::new()?;
rt.block_on(print_socket())?;
Ok(())
}

/// Setup the [tracing]-based logging system for the server
/// The events received by the subscriber are filtered at the `info` level,
/// then printed using the [HierarchicalLayer] layer, and the resulting text
/// is written to log files rotated on a hourly basis (in
/// `rome-logs/server.log.yyyy-MM-dd-HH` files inside the system temporary
/// directory)
fn setup_tracing_subscriber() {
/// This filter enables:
/// - All spans and events at level info or higher
/// - All spans and events in the `rome_lsp` and `rome_js_parser` crates
const LOGGING_FILTER: &str = "info,rome_lsp=trace,rome_js_parser=trace";

let logs_dir = env::temp_dir().join("rome-logs");
let file_appender = tracing_appender::rolling::hourly(logs_dir, "server.log");

registry()
.with(
HierarchicalLayer::default()
.with_indent_lines(true)
.with_indent_amount(2)
.with_bracketed_fields(true)
.with_targets(true)
.with_ansi(false)
.with_writer(file_appender)
.with_filter(EnvFilter::new(LOGGING_FILTER)),
)
.init();
}
Loading

0 comments on commit c2927dc

Please sign in to comment.