Skip to content

Commit

Permalink
Write out AIR_CRATE_NAMES at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Dec 13, 2024
1 parent 6f93b9f commit e26893f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 15 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ biome_string_case = { git = "https://github.com/biomejs/biome", rev = "2648fa420
biome_text_size = { git = "https://github.com/biomejs/biome", rev = "2648fa4201be4afd26f44eca1a4e77aac0a67272" }
biome_unicode_table = { git = "https://github.com/biomejs/biome", rev = "2648fa4201be4afd26f44eca1a4e77aac0a67272" }
bytes = "1.8.0"
cargo_metadata = "0.19.1"
clap = { version = "4.5.20", features = ["derive"] }
crossbeam = "0.8.4"
dissimilar = "1.0.9"
Expand Down
9 changes: 6 additions & 3 deletions crates/lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ triomphe.workspace = true
url.workspace = true
uuid = { workspace = true, features = ["v4"] }

[lints]
workspace = true

[dev-dependencies]
assert_matches.workspace = true
bytes.workspace = true
Expand All @@ -54,3 +51,9 @@ lsp_test.workspace = true
memchr.workspace = true
tests_macros.workspace = true
tokio-util.workspace = true

[build-dependencies]
cargo_metadata.workspace = true

[lints]
workspace = true
34 changes: 34 additions & 0 deletions crates/lsp/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::env;
use std::fs;
use std::path::Path;

extern crate cargo_metadata;

fn main() {
write_workspace_crate_names();
}

/// Write out a constant array of air crate names as `AIR_CRATE_NAMES` at build time
fn write_workspace_crate_names() {
let dir = env::var_os("OUT_DIR").unwrap();
let path = Path::new(&dir).join("crates.rs");

// Equivalent to `cargo metadata --no-deps`
let mut cmd = cargo_metadata::MetadataCommand::new();
cmd.no_deps();
let metadata = cmd.exec().unwrap();

let packages: Vec<String> = metadata
.workspace_packages()
.iter()
.map(|package| package.name.clone())
.map(|package| String::from("\"") + package.as_str() + "\",")
.collect();

let packages = packages.join(" ");

let contents = format!("pub(crate) const AIR_CRATE_NAMES: &[&str] = &[{packages}];");

fs::write(&path, contents).unwrap();
println!("cargo::rerun-if-changed=build.rs");
}
3 changes: 3 additions & 0 deletions crates/lsp/src/crates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Generates `AIR_CRATE_NAMES`, a const array of the crate names in the air workspace,
// see `lsp/src/build.rs`
include!(concat!(env!("OUT_DIR"), "/crates.rs"));
1 change: 1 addition & 0 deletions crates/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub use tower_lsp::start_lsp;

pub mod config;
pub mod crates;
pub mod documents;
pub mod encoding;
pub mod from_proto;
Expand Down
15 changes: 3 additions & 12 deletions crates/lsp/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ use tracing_subscriber::{
Layer,
};

use crate::crates;

// TODO:
// - Add `air.logLevel` and `air.dependencyLogLevels` as VS Code extension options that set
// the log levels, and pass them through the arbitrary `initializationOptions` field of
Expand Down Expand Up @@ -226,17 +228,6 @@ fn is_test_client(client_info: Option<&ClientInfo>) -> bool {
client_info.map_or(false, |client_info| client_info.name == "AirTestClient")
}

// TODO: Is there a way to generate this at compile time?
const TARGETS: &[&str] = &[
"air_r_factory",
"air_r_formatter",
"air_r_parser",
"air_r_syntax",
"fs",
"line_ending",
"lsp",
];

fn log_filter(log_level: LogLevel, dependency_log_levels: Option<String>) -> filter::Targets {
// Initialize `filter` from dependency log levels.
// If nothing is supplied, dependency logs are completely off.
Expand All @@ -251,7 +242,7 @@ fn log_filter(log_level: LogLevel, dependency_log_levels: Option<String>) -> fil
let log_level = log_level.tracing_level();

// Apply the air log level to each air crate that logs
for target in TARGETS {
for target in crates::AIR_CRATE_NAMES {
filter = filter.with_target(*target, log_level);
}

Expand Down

0 comments on commit e26893f

Please sign in to comment.