Damien Martin-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 1 | # 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 | |
| 16 | def _bucket_from_workspace_name(wname): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 17 | """Try to assert the bucket name from the workspace name. |
Damien Martin-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 18 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 19 | E.g. it will answer www.bazel.build if the workspace name is build_bazel_www. |
Damien Martin-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 20 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 21 | Args: |
| 22 | wname: workspace name |
Damien Martin-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 23 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 24 | 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-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 31 | |
| 32 | def _impl(ctx): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 33 | """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-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 36 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 37 | 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-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 59 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 60 | # 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-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 63 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 64 | 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-Guillerez | 49a3e4b | 2017-11-03 15:30:52 +0100 | [diff] [blame] | 76 | |
| 77 | jekyll_build = rule( |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 78 | 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 | ) |