Skip to content

Commit

Permalink
refactor: change tilt buck target to be runnable from any dir (#3435)
Browse files Browse the repository at this point in the history
* refactor: de-duplicate tilt rule

* refactor: allow tilt to be run from any dir

* build: re-introduce 'args' attr to tilt rule
  • Loading branch information
vindard authored Nov 8, 2023
1 parent 5db6491 commit 457d3af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
8 changes: 5 additions & 3 deletions dev/BUCK
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
load("@toolchains//rover:macros.bzl", "supergraph", "diff_check", "dev_update_file")
load( ":tilt.bzl", "tilt_down", "tilt_up",)
load( ":tilt.bzl", "tilt",)

# Bring up the full set of services for development
tilt_up(
tilt(
name = "up",
subcmd = "up",
)

# Bring down any remaining/running services
tilt_down(
tilt(
name = "down",
subcmd = "down",
)

python_bootstrap_binary(
Expand Down
56 changes: 19 additions & 37 deletions dev/tilt.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
def tilt_up_impl(ctx: AnalysisContext) -> list[[DefaultInfo, RunInfo]]:
return _invoke_tilt(ctx, "up")
def tilt_impl(ctx: AnalysisContext) -> list[[DefaultInfo, RunInfo]]:
return _invoke_tilt(ctx, ctx.attrs.subcmd)

tilt_up = rule(
impl = tilt_up_impl,
tilt = rule(
impl = tilt_impl,
attrs = {
"subcmd": attrs.enum(["up", "down"]),
"tiltfile": attrs.string(
default = "Tiltfile",
doc = """The Tiltfile to run.""",
Expand All @@ -13,34 +14,6 @@ tilt_up = rule(
default = [],
doc = """Additional arguments passed as <Tiltfile args>.""",
),
"tilt_args": attrs.list(
attrs.string(),
default = [],
doc = """Additional arguments passed as `tilt` arguments.""",
),
},
)

def tilt_down_impl(ctx: AnalysisContext) -> list[[DefaultInfo, RunInfo]]:
return _invoke_tilt(ctx, "down")

tilt_down = rule(
impl = tilt_down_impl,
attrs = {
"tiltfile": attrs.string(
default = "Tiltfile",
doc = """The Tiltfile to run.""",
),
"args": attrs.list(
attrs.string(),
default = [],
doc = """Additional arguments passed as <Tiltfile args>.""",
),
"tilt_args": attrs.list(
attrs.string(),
default = [],
doc = """Additional arguments passed as `tilt` arguments.""",
),
},
)

Expand All @@ -50,15 +23,24 @@ def _invoke_tilt(ctx: AnalysisContext, subcmd: str) -> list[[DefaultInfo, RunInf
ctx.attrs.tiltfile,
)

script = ctx.actions.write("tilt-run.sh", """\
#!/usr/bin/env bash
set -euo pipefail
rootpath="$(git rev-parse --show-toplevel)"
subcmd="$1"
tiltfile="$2"
args=("${@:3}")
tilt "$subcmd" --file "$rootpath"/"$tiltfile" -- "${args[@]}"
""", is_executable = True)

run_cmd_args = cmd_args([
"tilt",
script,
subcmd,
"--file",
tiltfile,
ctx.attrs.args
])
run_cmd_args.add(ctx.attrs.tilt_args)
run_cmd_args.add("--")
run_cmd_args.add(ctx.attrs.args)

args_file = ctx.actions.write("tilt-args.txt", run_cmd_args)

Expand Down

0 comments on commit 457d3af

Please sign in to comment.