Skip to content

Commit

Permalink
Demonstrate Configuring Exported Static Values via Bazel Config Flags
Browse files Browse the repository at this point in the history
This example now demonstrates how Claro programs can leverage Bazel's builtin functionality to satisfy arbitrary configuration needs without the language itself being burdened with syntax for such a thing. This is a demonstration of compile-time configurability that comes with no runtime overhead of checking flags and such as you'd find in other existing Guice-like dependency injection frameworks.

Now, if you'd like to point the Buggy Buggies AI demo server at a local instance of the Buggy Buggies server you can just run the following command to configure the dev build instead of the prod build (in a real project, defaults would probably be inverted):

```
$ bazel run //src/java/com/claro/claro_programs/demo_server/buggy_buggies:buggy_buggies_http_server --//src/java/com/claro/claro_programs/demo_server/buggy_buggies/buggy_buggies_service:env_flag=dev
```

See: https://bazel.build/docs/configurable-attributes.
  • Loading branch information
JasonSteving99 committed Oct 12, 2023
1 parent 6557728 commit 0c6bdce
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
3 changes: 3 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ load("@io_bazel_rules_closure//closure:repositories.bzl", "rules_closure_depende
rules_closure_dependencies()
rules_closure_toolchains()

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()

# See this documentation to understand how fetching Maven deps works in Bazel:
# https://github.com/bazelbuild/rules_jvm_external
# When you add a new maven dep run the following command to update new deps:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
load("//src/java/com/claro:claro_build_rules.bzl", "claro_module")
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

SRCS = ["buggy_buggies_move_response.claro"]

# This example now demonstrates how Claro programs can leverage Bazel's builtin functionality to satisfy arbitrary
# configuration needs without the language itself being burdened with syntax for such a thing. This is a demonstration
# of compile-time configurability that comes with no runtime overhead of checking flags and such as you'd find in other
# existing Guice-like dependency injection frameworks.
claro_module(
name = "buggy_buggies_client",
module_api_file = "buggy_buggies_client.claro_module_api",
srcs = [
"buggy_buggies_client.claro",
"buggy_buggies_move_response.claro",
],
srcs = select({
":dev": ["buggy_buggies_client_DEV.claro"] + SRCS,
":prod": ["buggy_buggies_client_PROD.claro"] + SRCS,
}),
optional_stdlib_deps = ["http"],
visibility = [
"//src/java/com/claro/claro_programs/demo_server/buggy_buggies:__pkg__",
"//src/java/com/claro/claro_programs/demo_server/buggy_buggies/endpoint_handlers:__pkg__",
],
)
)

# For demonstration purposes only, I'm defining a brand new flag here, so that it's obvious where all of the moving
# pieces in the above claro_module() target come from.
#
# If you want to actually override the default flag value, set following flag in your `bazel run` command:
# --//src/java/com/claro/claro_programs/demo_server/buggy_buggies/buggy_buggies_service:env_flag=dev
string_flag(
name = "env_flag",
values = [
"dev",
"prod",
],
build_setting_default = "prod",
)
config_setting(
name = "dev",
flag_values = {
":env_flag": "dev"
}
)
config_setting(
name = "prod",
flag_values = {
":env_flag": "prod"
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

provider static_HTTP_CLIENT() -> HttpClient<BuggyBuggies> {
# This now only needs to happen once throughout the entire program.
return http::getHttpClient("http://localhost:4000");
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ public Type getValidatedExprType(ScopedHeap scopedHeap) throws ClaroTypeExceptio

@Override
public StringBuilder generateJavaSourceBodyOutput(ScopedHeap scopedHeap) {
if (scopedHeap.getIdentifierData(this.identifier).isStaticValue) {
System.err.println(
"TESTING!!! FOUND STATIC ID REF: " + this.identifier + " " + this.optionalDefiningModuleDisambiguator);
}
ScopedHeap.IdentifierData identifierData = scopedHeap.getIdentifierData(this.identifier);
identifierData.used = true;
return new StringBuilder(
Expand Down

0 comments on commit 0c6bdce

Please sign in to comment.