Skip to content
This repository was archived by the owner on Jun 7, 2022. It is now read-only.

Commit a1ecdb8

Browse files
ianocianoc-stripe
authored andcommitted
Add optional support for bazel deps repos handling
1 parent 6c267ae commit a1ecdb8

File tree

1 file changed

+76
-11
lines changed

1 file changed

+76
-11
lines changed

src/jvm_indexer/jvm_indexer_app.rs

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use regex::Regex;
55

66
use lazy_static::lazy_static;
77

8+
use std::path::PathBuf;
89
use std::time::Instant;
9-
use std::{collections::HashMap, path::PathBuf};
1010

1111
use std::env;
1212
use std::sync::atomic::Ordering;
@@ -18,17 +18,17 @@ use bazelfe::bazel_runner;
1818
use bazelfe::build_events::build_event_server::bazel_event;
1919
use bazelfe::build_events::build_event_server::BuildEventAction;
2020
use bazelfe::build_events::hydrated_stream::HydratedInfo;
21-
use bazelfe::buildozer_driver;
2221
use bazelfe::jvm_indexer::bazel_query::BazelQuery;
2322
use dashmap::{DashMap, DashSet};
2423
use google::devtools::build::v1::publish_build_event_server::PublishBuildEventServer;
2524
use rand::Rng;
25+
use std::collections::{HashMap, HashSet};
2626
use std::io::Write;
2727
use std::sync::Arc;
2828
use tokio::sync::{broadcast, Mutex};
2929

3030
#[derive(Clap, Debug)]
31-
#[clap(name = "basic", setting = AppSettings::TrailingVarArg)]
31+
#[clap(name = "basic")]
3232
struct Opt {
3333
#[clap(long, env = "BIND_ADDRESS")]
3434
bind_address: Option<String>,
@@ -44,6 +44,9 @@ struct Opt {
4444

4545
#[clap(long)]
4646
extra_allowed_rule_kinds: Option<Vec<String>>,
47+
48+
#[clap(long)]
49+
bazel_deps_root: Option<String>,
4750
}
4851

4952
fn build_rule_queries(allowed_rule_kinds: &Vec<String>, target_roots: &Vec<String>) -> Vec<String> {
@@ -153,15 +156,76 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
153156
.chain(opt.extra_allowed_rule_kinds.unwrap_or_default().into_iter())
154157
.collect();
155158

156-
info!("Executing initial query to find all external repos in this bazel repository");
157159
let bazel_query = bazelfe::jvm_indexer::bazel_query::from_binary_path(opt.bazel_binary_path);
158160

161+
let bazel_deps_replacement_map: HashMap<String, String> = match &opt.bazel_deps_root {
162+
None => HashMap::default(),
163+
Some(bazel_deps_root) => {
164+
let targets_in_bazel_deps_root = bazel_query
165+
.execute(&vec![
166+
String::from("query"),
167+
format!("{}//3rdparty/jvm/...", bazel_deps_root),
168+
String::from("--keep_going"),
169+
])
170+
.await;
171+
172+
let bazel_deps_deps = bazel_query
173+
.execute(&vec![
174+
String::from("query"),
175+
format!("deps({}//3rdparty/jvm/...)", bazel_deps_root),
176+
String::from("--output"),
177+
String::from("graph"),
178+
String::from("--keep_going"),
179+
])
180+
.await;
181+
182+
let bazel_deps = {
183+
let mut bazel_deps = HashSet::new();
184+
for ln in targets_in_bazel_deps_root.stdout.lines().into_iter() {
185+
bazel_deps.insert(ln);
186+
}
187+
bazel_deps
188+
};
189+
let mut mapping = HashMap::new();
190+
for ln in bazel_deps_deps.stdout.lines().into_iter() {
191+
if ln.contains(" -> ") {
192+
let elements: Vec<&str> = ln.split(" -> ").collect();
193+
if elements.len() > 1 {
194+
let src = elements[0].trim();
195+
let dest = elements[1].trim();
196+
197+
let mut e = mapping
198+
.entry(src.replace("\"", "").to_string())
199+
.or_insert(Vec::default());
200+
e.push(dest.replace("\"", ""));
201+
}
202+
}
203+
}
204+
205+
let mut results_mapping = HashMap::new();
206+
for bazel_dep in bazel_deps {
207+
if let Some(values) = mapping.get(&bazel_dep.to_string()) {
208+
let mut values = values.clone();
209+
while !values.is_empty() {
210+
let e = values.pop().unwrap();
211+
if e.starts_with("@") {
212+
results_mapping.insert(e, bazel_dep.to_string());
213+
} else if e.starts_with("//external") {
214+
if let Some(r) = mapping.get(&e) {
215+
values.extend(r.clone().into_iter());
216+
}
217+
}
218+
}
219+
}
220+
}
221+
results_mapping
222+
}
223+
};
224+
225+
info!("Executing initial query to find all external repos in this bazel repository");
226+
159227
let res = bazel_query
160-
.execute(&vec![
161-
String::from("query"),
162-
String::from("--keep_going"),
163-
String::from("//external:*"),
164-
])
228+
.execute(&vec![String::from("query"), String::from("//external:*")])
165229
.await;
166230

167231
let mut target_roots = vec![String::from("//...")];
@@ -363,10 +427,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
363427
for kv in results_map.iter() {
364428
let key = kv.key();
365429
let value = kv.value();
366-
let freq: usize = ret.get(key).unwrap_or(&0).clone();
430+
let re = bazel_deps_replacement_map.get(key).unwrap_or(key).clone();
431+
let freq: usize = ret.get(&re).unwrap_or(&0).clone();
367432
for inner_v in value {
368433
let v = reverse_hashmap.entry(inner_v.clone()).or_insert(vec![]);
369-
v.push((freq, key.clone()))
434+
v.push((freq, re.clone()))
370435
}
371436
}
372437

0 commit comments

Comments
 (0)