Skip to content

Commit 96a1b88

Browse files
committed
centralize generated content
1 parent 2eda4f7 commit 96a1b88

17 files changed

Lines changed: 186 additions & 73 deletions

File tree

bazel-jdt-bridge/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"baseDir": ".bazel-jdt",
3+
"configFile": ".bazelproject",
4+
"projectsDir": "projects",
5+
"aspectsDir": "aspects"
6+
}

bazel-jdt-bridge/crates/bazel-jdt-core/src/aspect.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ pub enum AspectError {
1010
Utf8Error(#[from] std::string::FromUtf8Error),
1111
}
1212

13-
const ASPECT_DIR_NAME: &str = ".bazel-jdt/aspects";
13+
fn aspect_dir_name() -> String {
14+
crate::internal_config::aspects_dir()
15+
}
1416
const VERSION_FILE: &str = ".version";
1517

1618
const ASPECT_BUILD: &str = "";
@@ -113,7 +115,8 @@ pub fn version_hash(bazel_major: u32) -> String {
113115
/// what's already on disk. Returns the workspace-relative Bazel aspect label.
114116
pub fn extract_if_needed(workspace_root: &Path, bazel_path: &str) -> Result<String, AspectError> {
115117
let bazel_major = detect_bazel_major_version(bazel_path);
116-
let aspect_dir = workspace_root.join(ASPECT_DIR_NAME);
118+
let dir_name = aspect_dir_name();
119+
let aspect_dir = workspace_root.join(&dir_name);
117120
let version_path = aspect_dir.join(VERSION_FILE);
118121
let current_hash = version_hash(bazel_major);
119122

@@ -146,7 +149,7 @@ pub fn extract_if_needed(workspace_root: &Path, bazel_path: &str) -> Result<Stri
146149

147150
let label = format!(
148151
"//{}:intellij_info_bundled.bzl%intellij_info_aspect",
149-
ASPECT_DIR_NAME
152+
dir_name
150153
);
151154
Ok(label)
152155
}
@@ -156,18 +159,20 @@ pub fn extract_if_needed(workspace_root: &Path, bazel_path: &str) -> Result<Stri
156159
pub fn check_bazelignore(workspace_root: &Path) -> Option<String> {
157160
let bazelignore_path = workspace_root.join(".bazelignore");
158161
let contents = fs::read_to_string(&bazelignore_path).ok()?;
162+
let base_dir = crate::internal_config::base_dir();
163+
let dir_name = aspect_dir_name();
159164

160165
for line in contents.lines() {
161166
let line = line.trim();
162167
if line.is_empty() || line.starts_with('#') {
163168
continue;
164169
}
165-
if line == ".bazel-jdt" || line == ".bazel-jdt/" || line == ".bazel-jdt/**" || line == "/" {
170+
if line == base_dir || line == format!("{}/", base_dir) || line == format!("{}/**", base_dir) || line == "/" {
166171
return Some(format!(
167172
"Aspect directory '{}' is covered by .bazelignore \
168173
(pattern: '{}') — aspects may not be found by Bazel. \
169174
Please remove this pattern from .bazelignore.",
170-
ASPECT_DIR_NAME, line
175+
dir_name, line
171176
));
172177
}
173178
}
@@ -201,9 +206,9 @@ mod tests {
201206
let label = extract_if_needed(tmp.path(), "bazel").unwrap();
202207

203208
assert!(label.contains("intellij_info_bundled.bzl%intellij_info_aspect"));
204-
assert!(label.starts_with("//.bazel-jdt/aspects:intellij_info_bundled.bzl"));
209+
assert!(label.starts_with(&format!("//{}:intellij_info_bundled.bzl", aspect_dir_name())));
205210

206-
let aspect_dir = tmp.path().join(ASPECT_DIR_NAME);
211+
let aspect_dir = tmp.path().join(aspect_dir_name());
207212
assert!(aspect_dir.join("BUILD").exists());
208213
assert!(aspect_dir.join("intellij_info_bundled.bzl").exists());
209214
assert!(aspect_dir.join("intellij_info_impl_bundled.bzl").exists());
@@ -223,7 +228,7 @@ mod tests {
223228
let tmp = tempfile::tempdir().unwrap();
224229
extract_if_needed(tmp.path(), "bazel").unwrap();
225230

226-
let version_path = tmp.path().join(ASPECT_DIR_NAME).join(VERSION_FILE);
231+
let version_path = tmp.path().join(aspect_dir_name()).join(VERSION_FILE);
227232
std::thread::sleep(std::time::Duration::from_millis(10));
228233

229234
extract_if_needed(tmp.path(), "bazel").unwrap();
@@ -235,7 +240,7 @@ mod tests {
235240
#[test]
236241
fn test_extract_updates_when_version_differs() {
237242
let tmp = tempfile::tempdir().unwrap();
238-
let aspect_dir = tmp.path().join(ASPECT_DIR_NAME);
243+
let aspect_dir = tmp.path().join(aspect_dir_name());
239244

240245
extract_if_needed(tmp.path(), "bazel").unwrap();
241246

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const CONFIG_JSON: &str = include_str!("../../../config.json");
2+
3+
fn extract_json_str<'a>(json: &'a str, key: &str) -> Option<&'a str> {
4+
let needle = format!("\"{}\"", key);
5+
let after_key = json.split(&needle).nth(1)?;
6+
let after_colon = after_key.splitn(2, ':').nth(1)?.trim();
7+
let after_quote = after_colon.strip_prefix('"')?;
8+
let value = after_quote.split('"').next()?;
9+
Some(value)
10+
}
11+
12+
pub fn base_dir() -> &'static str {
13+
extract_json_str(CONFIG_JSON, "baseDir").unwrap_or(".bazel-jdt")
14+
}
15+
16+
pub fn config_file() -> &'static str {
17+
extract_json_str(CONFIG_JSON, "configFile").unwrap_or(".bazelproject")
18+
}
19+
20+
pub fn aspects_dir() -> String {
21+
format!("{}/{}", base_dir(), extract_json_str(CONFIG_JSON, "aspectsDir").unwrap_or("aspects"))
22+
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
28+
#[test]
29+
fn test_config_reads_from_root_config_json() {
30+
assert_eq!(base_dir(), ".bazel-jdt");
31+
assert_eq!(config_file(), ".bazelproject");
32+
assert_eq!(aspects_dir(), ".bazel-jdt/aspects");
33+
}
34+
}

bazel-jdt-bridge/crates/bazel-jdt-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod aspect;
2+
pub mod internal_config;
23
pub mod change_detector;
34
pub mod jni_exports;
45
pub mod state;

bazel-jdt-bridge/crates/bazel-jdt-core/src/watcher.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl BuildFileWatcher {
3737
let mut debouncer =
3838
notify_debouncer_full::new_debouncer(Duration::from_millis(500), None, tx)?;
3939

40-
// Watch workspace root non-recursively for WORKSPACE and .bazelproject changes
40+
// Watch workspace root non-recursively for WORKSPACE changes
4141
if let Err(e) = debouncer.watch(&workspace_root, RecursiveMode::NonRecursive) {
4242
if cfg!(target_os = "linux") {
4343
log::warn!(
@@ -48,6 +48,14 @@ impl BuildFileWatcher {
4848
return Err(WatcherError::NotifyError(e));
4949
}
5050

51+
// Watch base dir non-recursively for .bazelproject changes
52+
let bazel_jdt_dir = workspace_root.join(crate::internal_config::base_dir());
53+
if bazel_jdt_dir.is_dir() {
54+
if let Err(e) = debouncer.watch(&bazel_jdt_dir, RecursiveMode::NonRecursive) {
55+
log::warn!("Failed to watch {} directory: {}", crate::internal_config::base_dir(), e);
56+
}
57+
}
58+
5159
// Watch each user-selected directory recursively for BUILD file changes
5260
for rel_path in &watch_paths {
5361
let abs_path = workspace_root.join(rel_path);
@@ -196,17 +204,26 @@ pub fn is_watched_file(path: &Path) -> bool {
196204
.map(|name| {
197205
matches!(
198206
name,
199-
"BUILD" | "BUILD.bazel" | "WORKSPACE" | "WORKSPACE.bazel" | ".bazelproject"
200-
)
207+
"BUILD" | "BUILD.bazel" | "WORKSPACE" | "WORKSPACE.bazel"
208+
) || is_bazelproject_file(path)
201209
})
202210
.unwrap_or(false)
203211
}
204212

213+
fn is_in_bazel_jdt_dir(path: &Path) -> bool {
214+
path.parent()
215+
.and_then(|p| p.file_name())
216+
.and_then(|n| n.to_str())
217+
.map(|n| n == crate::internal_config::base_dir())
218+
.unwrap_or(false)
219+
}
220+
205221
pub fn is_bazelproject_file(path: &Path) -> bool {
206222
path.file_name()
207223
.and_then(|name| name.to_str())
208-
.map(|name| name == ".bazelproject")
224+
.map(|name| name == crate::internal_config::config_file())
209225
.unwrap_or(false)
226+
&& is_in_bazel_jdt_dir(path)
210227
}
211228

212229
impl Drop for BuildFileWatcher {
@@ -240,8 +257,8 @@ mod tests {
240257
assert!(is_watched_file(Path::new("WORKSPACE.bazel")));
241258
assert!(is_watched_file(Path::new("/some/path/BUILD")));
242259
assert!(is_watched_file(Path::new("/some/path/BUILD.bazel")));
243-
assert!(is_watched_file(Path::new(".bazelproject")));
244-
assert!(is_watched_file(Path::new("/some/path/.bazelproject")));
260+
assert!(is_watched_file(Path::new(".bazel-jdt/.bazelproject")));
261+
assert!(is_watched_file(Path::new("/some/path/.bazel-jdt/.bazelproject")));
245262
}
246263

247264
#[test]
@@ -252,12 +269,17 @@ mod tests {
252269
assert!(!is_watched_file(Path::new("Cargo.toml")));
253270
assert!(!is_watched_file(Path::new("src/main.rs")));
254271
assert!(!is_watched_file(Path::new("")));
272+
// .bazelproject at root (wrong location) should not be watched
273+
assert!(!is_watched_file(Path::new(".bazelproject")));
274+
assert!(!is_watched_file(Path::new("/workspace/.bazelproject")));
255275
}
256276

257277
#[test]
258278
fn test_is_bazelproject_file() {
259-
assert!(is_bazelproject_file(Path::new(".bazelproject")));
260-
assert!(is_bazelproject_file(Path::new("/workspace/.bazelproject")));
279+
assert!(is_bazelproject_file(Path::new(".bazel-jdt/.bazelproject")));
280+
assert!(is_bazelproject_file(Path::new("/workspace/.bazel-jdt/.bazelproject")));
281+
assert!(!is_bazelproject_file(Path::new(".bazelproject")));
282+
assert!(!is_bazelproject_file(Path::new("/workspace/.bazelproject")));
261283
assert!(!is_bazelproject_file(Path::new("BUILD")));
262284
assert!(!is_bazelproject_file(Path::new("WORKSPACE")));
263285
assert!(!is_bazelproject_file(Path::new("")));

bazel-jdt-bridge/java-bridge/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@
7171
</dependencies>
7272

7373
<build>
74+
<resources>
75+
<resource>
76+
<directory>${project.basedir}/..</directory>
77+
<includes>
78+
<include>config.json</include>
79+
</includes>
80+
</resource>
81+
<resource>
82+
<directory>src/main/resources</directory>
83+
</resource>
84+
</resources>
7485
<plugins>
7586
<plugin>
7687
<groupId>org.apache.maven.plugins</groupId>

bazel-jdt-bridge/java-bridge/src/main/java/com/bazel/jdt/BazelBuildSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class BazelBuildSupport implements IBuildSupport {
2424
"**/BUILD.bazel",
2525
"**/WORKSPACE",
2626
"**/WORKSPACE.bazel",
27-
"**/.bazelproject"
27+
"**/" + InternalConfig.bazelprojectRelPath()
2828
);
2929

3030
private static final ConcurrentLinkedQueue<String> pendingChangedFiles = new ConcurrentLinkedQueue<>();

bazel-jdt-bridge/java-bridge/src/main/java/com/bazel/jdt/BazelProjectCreator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static IProject createProjectForPackage(
5555
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
5656
IProject project = workspaceRoot.getProject(projectName);
5757

58-
File bazelProjectDir = new File(workspacePath, ".bazel-projects/" + projectName);
58+
File bazelProjectDir = new File(workspacePath, InternalConfig.projectsDirRelPath() + "/" + projectName);
5959
IPath expectedLocation = new Path(bazelProjectDir.getAbsolutePath());
6060

6161
if (project.exists()) {
@@ -66,17 +66,17 @@ public static IProject createProjectForPackage(
6666
TargetProjectMapping.appendTargets(project, Collections.singletonList(targetLabel));
6767
}
6868
LOG.log(new Status(IStatus.INFO, "com.bazel.jdt",
69-
"Project '" + projectName + "' already at .bazel-projects/, skipping rebuild"));
69+
"Project '" + projectName + "' already at " + InternalConfig.projectsDirRelPath() + "/, skipping rebuild"));
7070
return project;
7171
}
7272
LOG.log(new Status(IStatus.INFO, "com.bazel.jdt",
73-
"Migrating project '" + projectName + "' to .bazel-projects/ location"));
73+
"Migrating project '" + projectName + "' to " + InternalConfig.projectsDirRelPath() + "/ location"));
7474
project.delete(false, true, monitor);
7575
}
7676

7777
if (!bazelProjectDir.exists() && !bazelProjectDir.mkdirs()) {
7878
LOG.log(new Status(IStatus.ERROR, "com.bazel.jdt",
79-
"Failed to create .bazel-projects directory: " + bazelProjectDir.getAbsolutePath()));
79+
"Failed to create projects directory: " + bazelProjectDir.getAbsolutePath()));
8080
return null;
8181
}
8282

bazel-jdt-bridge/java-bridge/src/main/java/com/bazel/jdt/BazelProjectImporter.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public boolean applies(IProgressMonitor monitor) {
3030
boolean hasWorkspace = new File(rootFolder, "WORKSPACE").exists()
3131
|| new File(rootFolder, "WORKSPACE.bazel").exists();
3232
if (!hasWorkspace) return false;
33-
return new File(rootFolder, ".bazelproject").exists();
33+
return new File(rootFolder, InternalConfig.bazelprojectRelPath()).exists();
3434
}
3535

3636
@Override
@@ -65,8 +65,6 @@ public void importToWorkspace(IProgressMonitor monitor) throws CoreException {
6565
LOG.log(new Status(IStatus.INFO, "com.bazel.jdt",
6666
"Importing Bazel workspace: " + workspacePath));
6767

68-
ensureBazelProjectsGitignore(workspacePath);
69-
7068
if (projectView != null && !projectView.getDirectories().isEmpty()) {
7169
String[] watchDirs = projectView.getDirectories().toArray(new String[0]);
7270
bridge.updateWatchPaths(watchDirs);
@@ -273,8 +271,6 @@ private boolean tryFastReload(IProgressMonitor monitor) throws CoreException {
273271
bridge.setProjectView(projectView);
274272
}
275273

276-
ensureBazelProjectsGitignore(workspacePath);
277-
278274
if (projectView != null && !projectView.getDirectories().isEmpty()) {
279275
String[] watchDirs = projectView.getDirectories().toArray(new String[0]);
280276
bridge.updateWatchPaths(watchDirs);
@@ -325,34 +321,6 @@ public void run(IProgressMonitor pm) throws CoreException {
325321
return true;
326322
}
327323

328-
private static void ensureBazelProjectsGitignore(String workspacePath) {
329-
File gitignore = new File(workspacePath, ".gitignore");
330-
String entry = ".bazel-projects/";
331-
try {
332-
if (gitignore.exists()) {
333-
String content = new String(java.nio.file.Files.readAllBytes(gitignore.toPath()),
334-
java.nio.charset.StandardCharsets.UTF_8);
335-
for (String line : content.split("\n")) {
336-
if (line.trim().equals(entry)) {
337-
return;
338-
}
339-
}
340-
String separator = content.endsWith("\n") ? "" : "\n";
341-
java.nio.file.Files.write(gitignore.toPath(),
342-
(separator + entry + "\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),
343-
java.nio.file.StandardOpenOption.APPEND);
344-
} else {
345-
java.nio.file.Files.write(gitignore.toPath(),
346-
(entry + "\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
347-
}
348-
LOG.log(new Status(IStatus.INFO, "com.bazel.jdt",
349-
"Added .bazel-projects/ to .gitignore"));
350-
} catch (Exception e) {
351-
LOG.log(new Status(IStatus.WARNING, "com.bazel.jdt",
352-
"Failed to update .gitignore: " + e.getMessage()));
353-
}
354-
}
355-
356324
@Override
357325
public void reset() {
358326
// No-op: BazelBridge.initialize() in importToWorkspace() handles native handle

bazel-jdt-bridge/java-bridge/src/main/java/com/bazel/jdt/BazelProjectView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
public final class BazelProjectView {
2222
private static final ILog LOG = Platform.getLog(BazelProjectView.class);
23-
private static final String BAZELPROJECT_FILE = ".bazelproject";
23+
private static final String BAZELPROJECT_FILE = InternalConfig.bazelprojectRelPath();
2424

2525
private final List<String> directories = new ArrayList<>();
2626
private final List<String> targets = new ArrayList<>();

0 commit comments

Comments
 (0)