aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs/sass/sass.bzl
blob: 4ae978a1a1797e9558135dd0f8cfa37872ebc16f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//tools/build_rules:deprecation.bzl", "deprecated")

def warning(rule):
  print(deprecated(
      "sass",
      rule,
      "@bazel_tools//tools/build_defs/sass:sass.bzl",
      "@io_bazel_rules_sass//sass:sass.bzl"))

SASS_FILETYPES = FileType([
    ".sass",
    ".scss",
])

def collect_transitive_sources(ctx):
    source_files = set(order="compile")
    for dep in ctx.attr.deps:
        source_files += dep.transitive_sass_files
    return source_files

def _sass_library_impl(ctx):
    warning("sass_library")
    transitive_sources = collect_transitive_sources(ctx)
    transitive_sources += SASS_FILETYPES.filter(ctx.files.srcs)
    return struct(
        files = set(),
        transitive_sass_files = transitive_sources)

def _sass_binary_impl(ctx):
    # Reference the sass compiler and define the default options
    # that sass_binary uses.
    warning("sass_binary")
    sassc = ctx.file._sassc
    options = [
        "--style={0}".format(ctx.attr.output_style),
        "--sourcemap",
    ]

    # Load up all the transitive sources as dependent includes.
    transitive_sources = collect_transitive_sources(ctx)
    for src in transitive_sources:
        options += ["-I={0}".format(src)]

    ctx.action(
        inputs = [sassc, ctx.file.src] + list(transitive_sources),
        executable = sassc,
        arguments = options + [ctx.file.src.path, ctx.outputs.css_file.path],
        mnemonic = "SassCompiler",
        outputs = [ctx.outputs.css_file, ctx.outputs.css_map_file],
    )

sass_deps_attr = attr.label_list(
    providers = ["transitive_sass_files"],
    allow_files = False,
)

sass_library = rule(
    attrs = {
        "srcs": attr.label_list(
            allow_files = SASS_FILETYPES,
            non_empty = True,
            mandatory = True,
        ),
        "deps": sass_deps_attr,
    },
    implementation = _sass_library_impl,
)

sass_binary = rule(
    attrs = {
        "src": attr.label(
            allow_files = SASS_FILETYPES,
            mandatory = True,
            single_file = True,
        ),
        "output_style": attr.string(default = "compressed"),
        "deps": sass_deps_attr,
        "_sassc": attr.label(
            default = Label("@bazel_tools//tools/build_defs/sass:sassc"),
            executable = True,
            single_file = True,
        ),
    },
    outputs = {
        "css_file": "%{name}.css",
        "css_map_file": "%{name}.css.map",
    },
    implementation = _sass_binary_impl,
)

LIBSASS_BUILD_FILE = """
package(default_visibility = ["@sassc//:__pkg__"])

filegroup(
    name = "srcs",
    srcs = glob([
         "src/**/*.h*",
         "src/**/*.c*",
    ]),
)

# Includes directive may seem unnecessary here, but its needed for the weird
# interplay between libsass/sassc projects. This is intentional.
cc_library(
    name = "headers",
    includes = ["include"],
    hdrs = glob(["include/**/*.h"]),
)
"""

SASSC_BUILD_FILE = """
package(default_visibility = ["//visibility:public"])

cc_binary(
    name = "sassc",
    srcs = [
        "@libsass//:srcs",
        "sassc.c",
        "sassc_version.h",
],
    linkopts = ["-ldl", "-lm"],
    deps = ["@libsass//:headers"],
)
"""

def sass_repositories():
  warning("sass_repositories")
  native.new_http_archive(
      name = "libsass",
      url = "https://github.com/sass/libsass/archive/3.3.0-beta1.tar.gz",
      sha256 = "6a4da39cc0b585f7a6ee660dc522916f0f417c890c3c0ac7ebbf6a85a16d220f",
      build_file_content = LIBSASS_BUILD_FILE,
      strip_prefix = "libsass-3.3.0-beta1",
  )

  native.new_http_archive(
      name = "sassc",
      url = "https://github.com/sass/sassc/archive/3.3.0-beta1.tar.gz",
      sha256 = "87494218eea2441a7a24b40f227330877dbba75c5fa9014ac6188711baed53f6",
      build_file_content = SASSC_BUILD_FILE,
      strip_prefix = "sassc-3.3.0-beta1",
  )