blob: 0ca0c3eee9c5c71084473a686660be4211c2c7d6 [file] [log] [blame]
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +01001# Copyright 2017 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Quick rule to build a Jekyll site."""
15
16def _bucket_from_workspace_name(wname):
vladmos20a042f2018-06-01 04:51:21 -070017 """Try to assert the bucket name from the workspace name.
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +010018
vladmos20a042f2018-06-01 04:51:21 -070019 E.g. it will answer www.bazel.build if the workspace name is build_bazel_www.
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +010020
vladmos20a042f2018-06-01 04:51:21 -070021 Args:
22 wname: workspace name
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +010023
vladmos20a042f2018-06-01 04:51:21 -070024 Returns:
25 the guessed name of the bucket for this workspace.
26 """
27 revlist = []
28 for part in wname.split("_"):
29 revlist.insert(0, part)
30 return ".".join(revlist)
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +010031
32def _impl(ctx):
vladmos20a042f2018-06-01 04:51:21 -070033 """Quick and non-hermetic rule to build a Jekyll site."""
34 source = ctx.actions.declare_directory(ctx.attr.name + "-srcs")
35 output = ctx.actions.declare_directory(ctx.attr.name + "-build")
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +010036
vladmos20a042f2018-06-01 04:51:21 -070037 ctx.actions.run_shell(
38 inputs = ctx.files.srcs,
39 outputs = [source],
40 command = ("mkdir -p %s\n" % (source.path)) +
41 "\n".join([
42 "tar xf %s -C %s" % (src.path, source.path)
43 for src in ctx.files.srcs
44 ]),
45 )
46 ctx.actions.run(
47 inputs = [source],
48 outputs = [output],
49 executable = "jekyll",
50 use_default_shell_env = True,
51 arguments = ["build", "-q", "-s", source.path, "-d", output.path],
52 )
53 ctx.actions.run(
54 inputs = [output],
55 outputs = [ctx.outputs.out],
56 executable = "tar",
57 arguments = ["cf", ctx.outputs.out.path, "-C", output.path, "."],
58 )
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +010059
vladmos20a042f2018-06-01 04:51:21 -070060 # Create a shell script to serve the site locally or push with the --push
61 # flag.
62 bucket = ctx.attr.bucket if ctx.attr.bucket else _bucket_from_workspace_name(ctx.workspace_name)
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +010063
vladmos20a042f2018-06-01 04:51:21 -070064 ctx.actions.expand_template(
65 template = ctx.file._jekyll_build_tpl,
66 output = ctx.outputs.executable,
67 substitutions = {
68 "%{workspace_name}": ctx.workspace_name,
69 "%{source_dir}": source.short_path,
70 "%{prod_dir}": output.short_path,
71 "%{bucket}": bucket,
72 },
73 is_executable = True,
74 )
75 return [DefaultInfo(runfiles = ctx.runfiles(files = [source, output]))]
Damien Martin-Guillerez49a3e4b2017-11-03 15:30:52 +010076
77jekyll_build = rule(
vladmos20a042f2018-06-01 04:51:21 -070078 implementation = _impl,
79 executable = True,
80 attrs = {
81 "srcs": attr.label_list(allow_empty = False),
82 "bucket": attr.string(),
83 "_jekyll_build_tpl": attr.label(
84 default = ":jekyll_build.sh.tpl",
85 allow_files = True,
86 single_file = True,
87 ),
88 },
89 outputs = {"out": "%{name}.tar"},
90)