-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[change]
bazel info
call caching mechanism (#228)
Co-authored-by: Lukasz Wawrzyk <uwawrzyk@twitter.com>
- Loading branch information
1 parent
0bdfe15
commit addca71
Showing
11 changed files
with
186 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
bazelrunner/src/main/java/org/jetbrains/bsp/bazel/bazelrunner/BasicBazelInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jetbrains.bsp.bazel.bazelrunner; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
public class BasicBazelInfo implements BazelInfo { | ||
private final String execRoot; | ||
private final Path workspaceRoot; | ||
|
||
public BasicBazelInfo( | ||
@JsonProperty("execRoot") String execRoot, | ||
@JsonProperty("workspaceRoot") Path workspaceRoot) { | ||
this.execRoot = execRoot; | ||
this.workspaceRoot = workspaceRoot; | ||
} | ||
|
||
@Override | ||
public String execRoot() { | ||
return execRoot; | ||
} | ||
|
||
@Override | ||
public Path workspaceRoot() { | ||
return workspaceRoot; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
BasicBazelInfo that = (BasicBazelInfo) o; | ||
return Objects.equals(execRoot, that.execRoot) | ||
&& Objects.equals(workspaceRoot, that.workspaceRoot); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(execRoot, workspaceRoot); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
bazelrunner/src/main/java/org/jetbrains/bsp/bazel/bazelrunner/BazelInfoStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.jetbrains.bsp.bazel.bazelrunner; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.vavr.control.Option; | ||
import io.vavr.control.Try; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.function.Function; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.jetbrains.bsp.bazel.server.bsp.info.BspInfo; | ||
|
||
public class BazelInfoStorage { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(BazelInfoStorage.class); | ||
private final ObjectMapper mapper; | ||
private final Path path; | ||
|
||
public BazelInfoStorage(BspInfo bspInfo) { | ||
this(bspInfo.bazelBspDir().resolve("bazel-info-cache.json")); | ||
} | ||
|
||
public BazelInfoStorage(Path path) { | ||
this.path = path; | ||
mapper = createMapper(); | ||
} | ||
|
||
private ObjectMapper createMapper() { | ||
var mapper = new ObjectMapper(); | ||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); | ||
return mapper; | ||
} | ||
|
||
public Option<BazelInfo> load() { | ||
return Option.when(Files.exists(path), this::read).flatMap(Function.identity()); | ||
} | ||
|
||
private Option<BasicBazelInfo> read() { | ||
return Try.of(() -> mapper.readValue(path.toFile(), BasicBazelInfo.class)).toOption(); | ||
} | ||
|
||
public void store(BasicBazelInfo bazelInfo) { | ||
try { | ||
mapper.writeValue(path.toFile(), bazelInfo); | ||
} catch (IOException e) { | ||
LOGGER.error("Could not store bazel info", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 5 additions & 21 deletions
26
bazelrunner/src/main/java/org/jetbrains/bsp/bazel/bazelrunner/LazyBazelInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,22 @@ | ||
package org.jetbrains.bsp.bazel.bazelrunner; | ||
|
||
import io.vavr.Lazy; | ||
import io.vavr.collection.Map; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public class LazyBazelInfo implements BazelInfo { | ||
private final Lazy<Map<String, String>> bazelInfoOutput; | ||
private final Lazy<String> execRoot; | ||
private final Lazy<Path> workspaceRoot; | ||
private final Lazy<BazelInfo> bazelInfo; | ||
|
||
public LazyBazelInfo(Lazy<Map<String, String>> bazelInfoOutput) { | ||
this.bazelInfoOutput = bazelInfoOutput; | ||
this.execRoot = extract("execution_root"); | ||
this.workspaceRoot = extract("workspace").map(Paths::get); | ||
public LazyBazelInfo(Lazy<BazelInfo> bazelInfo) { | ||
this.bazelInfo = bazelInfo; | ||
} | ||
|
||
@Override | ||
public String execRoot() { | ||
return execRoot.get(); | ||
return bazelInfo.get().execRoot(); | ||
} | ||
|
||
@Override | ||
public Path workspaceRoot() { | ||
return workspaceRoot.get(); | ||
} | ||
|
||
private Lazy<String> extract(String name) { | ||
return bazelInfoOutput.map( | ||
map -> | ||
map.get(name) | ||
.getOrElseThrow( | ||
() -> | ||
new RuntimeException( | ||
String.format("Failed to resolve %s from bazel info", name)))); | ||
return bazelInfo.get().workspaceRoot(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
bazelrunner/src/test/java/org/jetbrains/bsp/bazel/bazelrunner/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("//:junit5.bzl", "kt_junit5_test") | ||
|
||
kt_junit5_test( | ||
name = "bazelrunner", | ||
size = "small", | ||
srcs = glob(["*.kt"]), | ||
test_package = "org.jetbrains.bsp.bazel.bazelrunner", | ||
deps = [ | ||
"//bazelrunner/src/main/java/org/jetbrains/bsp/bazel/bazelrunner", | ||
"@maven//:io_vavr_vavr", | ||
], | ||
) |
29 changes: 29 additions & 0 deletions
29
bazelrunner/src/test/java/org/jetbrains/bsp/bazel/bazelrunner/StoredBazelInfoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.jetbrains.bsp.bazel.bazelrunner; | ||
|
||
import org.assertj.core.api.Assertions | ||
import org.assertj.core.api.AssertionsForClassTypes | ||
import org.junit.jupiter.api.Test | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
|
||
class ProjectStorageTest { | ||
@Test | ||
fun shouldStoreAndLoadProject() { | ||
val path = Paths.get(System.getProperty("java.io.tmpdir"), "bazel-info-cache-test.json") | ||
.also { Files.deleteIfExists(it) } | ||
|
||
val storage = BazelInfoStorage(path) | ||
|
||
val empty = storage.load() | ||
Assertions.assertThat(empty).isEmpty() | ||
|
||
val bazelInfo = BasicBazelInfo( | ||
"/private/var/tmp/_bazel/125c7a6ca879ed16a4b4b1a74bc5f27b/execroot/bazel_bsp", | ||
Paths.get("/Users/user/workspace/bazel-bsp")) | ||
|
||
storage.store(bazelInfo) | ||
val loaded = storage.load() | ||
loaded.forEach { Assertions.assertThat(it).isEqualTo(bazelInfo) } | ||
loaded.onEmpty { AssertionsForClassTypes.fail("bazel info not loaded") } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters