Skip to content

Commit

Permalink
build: add compile_typescript to Workspace provider
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Oct 6, 2023
1 parent 0c14d21 commit 1e9dee9
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/api/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ filegroup(

tsc_build(
name = "api",
tsconfig = "tsconfig-build.json",
srcs = [":src"],
)

Expand Down
5 changes: 5 additions & 0 deletions toolchains/workspace-pnpm/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export_file(
name = "prepare_build_context.py",
visibility = ["PUBLIC"],
)

export_file(
name = "compile_typescript.py",
visibility = ["PUBLIC"],
)
64 changes: 64 additions & 0 deletions toolchains/workspace-pnpm/compile_typescript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
"""
Runs a tsc compilation.
"""
import argparse
import subprocess
import sys
import os

def compute_path(arg: str, cwd: str) -> str:
return os.path.relpath(
os.path.abspath(arg),
cwd,
)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--package-dir",
help="Directory of the package",
)
parser.add_argument(
"--tsc-bin",
help="Path to the tsc executable",
)
parser.add_argument(
"--tsconfig",
help="Path to tsconfig",
)
parser.add_argument(
"--tscpaths-bin",
help="Path to the tscpaths executable",
)
parser.add_argument(
"out_path",
help="Path to output directory",
)

args = parser.parse_args()

tsc_cmd = [
os.path.abspath(args.tsc_bin),
"-p",
os.path.relpath(args.tsconfig),
"--outDir",
os.path.abspath(args.out_path),
]

exit_code = subprocess.call(tsc_cmd, cwd=args.package_dir)
if exit_code != 0:
sys.exit(exit_code)

tscpaths_cmd = [
os.path.abspath(args.tscpaths_bin),
"-p",
os.path.relpath(args.tsconfig),
"-s",
os.path.relpath("src"),
"-o",
os.path.abspath(args.out_path),
]
exit_code = subprocess.call(tscpaths_cmd, cwd=args.package_dir)

sys.exit(exit_code)
55 changes: 51 additions & 4 deletions toolchains/workspace-pnpm/macros.bzl
Original file line number Diff line number Diff line change
@@ -1,24 +1,71 @@
# move to workspace toolchain
load("@toolchains//simple-pnpm:macros.bzl", "npm_bin")

load("@prelude//python:toolchain.bzl", "PythonToolchainInfo",)
load(":toolchain.bzl", "WorkspacePnpmToolchainInfo",)

def tsc_build_impl(ctx: AnalysisContext) -> list[[DefaultInfo, RunInfo]]:
def tsc_build_impl(ctx: AnalysisContext) -> list[DefaultInfo]:
build_context = prepare_build_context(ctx)

out = ctx.actions.declare_output("dist", dir = True)
pnpm_toolchain = ctx.attrs._workspace_pnpm_toolchain[WorkspacePnpmToolchainInfo]

cmd = cmd_args(
ctx.attrs._python_toolchain[PythonToolchainInfo].interpreter,
pnpm_toolchain.compile_typescript[DefaultInfo].default_outputs,
"--package-dir",
cmd_args([build_context.workspace_root, ctx.label.package], delimiter = "/"),
"--tsc-bin",
cmd_args(ctx.attrs.tsc[RunInfo]),
"--tsconfig",
cmd_args(ctx.attrs.tsconfig),
"--tscpaths-bin",
cmd_args(ctx.attrs.tscpaths[RunInfo]),
cmd_args(out.as_output()),
)

ctx.actions.run(cmd, category = "tsc")

return [
DefaultInfo(build_context.workspace_root),
RunInfo("dummy"),
DefaultInfo(default_output = out),
]

def tsc_build(
node_modules = ":node_modules",
**kwargs):
tsc_bin = "tsc_bin"
if not rule_exists(tsc_bin):
npm_bin(
name = tsc_bin,
bin_name = "tsc",
)
tscpaths_bin = "tscpaths_bin"
if not rule_exists(tscpaths_bin):
npm_bin(
name = tscpaths_bin,
bin_name = "tscpaths",
)
_tsc_build(
tsc = ":{}".format(tsc_bin),
tscpaths = ":{}".format(tscpaths_bin),
node_modules = node_modules,
**kwargs,
)

_tsc_build = rule(
impl = tsc_build_impl,
attrs = {
"tsc": attrs.dep(
providers = [RunInfo],
doc = """TypeScript compiler dependency.""",
),
"tsconfig": attrs.string(
doc = """Target which builds `tsconfig.json`.""",
),
"tscpaths": attrs.dep(
providers = [RunInfo],
doc = """tscpaths dependency.""",
),
"srcs": attrs.list(
attrs.source(),
default = [],
Expand All @@ -44,7 +91,7 @@ BuildContext = record(


def prepare_build_context(ctx: AnalysisContext) -> BuildContext:
workspace_root = ctx.actions.declare_output("__workspace")
workspace_root = ctx.actions.declare_output("__workspace", dir = True)

pnpm_toolchain = ctx.attrs._workspace_pnpm_toolchain[WorkspacePnpmToolchainInfo]
package_dir = cmd_args(ctx.label.package).relative_to(ctx.label.cell_root)
Expand Down
5 changes: 5 additions & 0 deletions toolchains/workspace-pnpm/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
WorkspacePnpmToolchainInfo = provider(fields = [
"prepare_build_context",
"compile_typescript",
])

def workspace_pnpm_toolchain_impl(ctx) -> list[[DefaultInfo, WorkspacePnpmToolchainInfo]]:
Expand All @@ -10,6 +11,7 @@ def workspace_pnpm_toolchain_impl(ctx) -> list[[DefaultInfo, WorkspacePnpmToolch
DefaultInfo(),
WorkspacePnpmToolchainInfo(
prepare_build_context = ctx.attrs._prepare_build_context,
compile_typescript = ctx.attrs._compile_typescript,
)
]

Expand All @@ -19,6 +21,9 @@ workspace_pnpm_toolchain = rule(
"_prepare_build_context": attrs.dep(
default = "toolchains//workspace-pnpm:prepare_build_context.py",
),
"_compile_typescript": attrs.dep(
default = "toolchains//workspace-pnpm:compile_typescript.py",
),
},
is_toolchain_rule = True,
)

0 comments on commit 1e9dee9

Please sign in to comment.