blob: 4819f14983093a9ca74c92fa94c13bde8dd67bb1 [file] [log] [blame]
def _gostatic_tarball_impl(ctx):
out = ctx.actions.declare_directory("out")
tarball_name = ctx.attr.name + ".tar"
tarball = ctx.actions.declare_file(tarball_name)
config_name = ctx.attr.name + ".config"
config = ctx.actions.declare_file(config_name)
# Build path to root of sources, based on source_dir
# and location of the instantiating BUILDfile
# (source_dir is defined as relative to the BUILD file).
source_dir = "/".join(ctx.build_file_path.split("/")[:-1]) + "/" + ctx.attr.source_dir
# Relative path to go up from generated config to build
# root. This is because gostatic is magical and really
# wants the config to be alongside the source.
up = "../" * (config.path.count("/"))
templates = " ".join([up + f.path for f in ctx.files.templates])
config_lines = [
"OUTPUT = {}".format(up + out.path),
"TEMPLATES = {}".format(templates),
"SOURCE = {}".format(up + source_dir),
]
config_content = "\n".join(config_lines)
config_content += ctx.attr.extra_config
ctx.actions.write(config, config_content)
ctx.actions.run(
outputs = [out],
inputs = [config] + ctx.files.templates + ctx.files.srcs,
executable = ctx.file._gostatic,
arguments = [config.path],
)
ctx.actions.run(
outputs = [tarball],
inputs = [out],
executable = ctx.file._tarify,
arguments = [
"-site",
out.path,
"-tarball",
tarball.path,
],
)
return [DefaultInfo(files = depset([tarball]))]
gostatic_tarball = rule(
implementation = _gostatic_tarball_impl,
attrs = {
"extra_config": attr.string(
mandatory = True,
doc = """
Gostatic configuration (rules, etc). Do not specify OUTPUT, TEMPLATES
or SOURCES - these are automatically generated.
""",
),
"source_dir": attr.string(
mandatory = True,
doc = "Root of site sources. Relative to BUILDfile.",
),
"srcs": attr.label_list(
allow_files = True,
doc = "Site sources, all must be contained within source_dir",
),
"templates": attr.label_list(
allow_files = True,
doc = "Templates to use (passed to TEMPLATES in gostatic config).",
),
"_gostatic": attr.label(
default = Label("//tools/gostatic"),
allow_single_file = True,
executable = True,
cfg = "exec",
doc = "Path to gostatic binary.",
),
"_tarify": attr.label(
default = Label("//tools/gostatic/tarify"),
allow_single_file = True,
executable = True,
cfg = "exec",
doc = "Path to tarify binary.",
),
},
)