Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Create soy_library #424

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions closure/private/closure_common.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2019 The Closure Rules 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("//closure/private/utils:soy.bzl", "soy")

closure_common = struct(
soy = soy,
)
30 changes: 30 additions & 0 deletions closure/private/providers.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2019 The Closure Rules 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.

SoyInfo = provider(
doc = "Encapsulates information provided by soy_library.",
fields = {
"direct_headers": "A set of soy header files of the direct sources. " +
"If the library is a proxy library that has no " +
"sources, it contains the direct_headers from this " +
"library's direct deps.",
"direct_sources": "A set of soy sources from the 'srcs' attribute.",
"proto_descriptor_sets": "A set of FileDescriptorSet files of all " +
"dependent proto_library rules.",
"indirect_headers": "A set of soy header files from all dependent " +
"soy_library rules, but excluding direct headers.",
"indirect_sources": "A set of soy sources of all dependent" +
"soy_library rules, but excluding direct sources.",
},
)
13 changes: 13 additions & 0 deletions closure/private/rules/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2019 The Closure Rules 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.
66 changes: 66 additions & 0 deletions closure/private/rules/soy_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2019 The Closure Rules 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("//closure/private:closure_common.bzl", "closure_common")
load("//closure/private:providers.bzl", "SoyInfo")

def _soy_library_impl(ctx):
soyh = ctx.actions.declare_file("{}.soyh.bin".format(ctx.label.name))
return [
DefaultInfo(
files = depset([soyh]),
),
closure_common.soy.compile(
actions = ctx.actions,
closure_toolchain = struct(
soy = struct(
header_compiler = ctx.attr._compiler.files_to_run,
),
),
srcs = ctx.files.srcs,
output = soyh,
deps = [dep[SoyInfo] for dep in ctx.attr.deps],
proto_descriptor_sets = depset(
direct = [],
transitive = [dep[ProtoInfo].transitive_descriptor_sets for dep in ctx.attr.proto_deps],
),
),
]

soy_library = rule(
implementation = _soy_library_impl,
attrs = {
"srcs": attr.label_list(
mandatory = True,
allow_files = [".soy"],
),
"deps": attr.label_list(
mandatory = False,
providers = [SoyInfo],
),
"proto_deps": attr.label_list(
mandatory = False,
providers = [ProtoInfo],
),
# TODO(yannic): Add this to closure_toolchain.
"_compiler": attr.label(
default = "@com_google_template_soy//:SoyHeaderCompiler",
executable = True,
cfg = "host",
),
},
provides = [
SoyInfo,
],
)
13 changes: 13 additions & 0 deletions closure/private/utils/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2019 The Closure Rules 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.
19 changes: 19 additions & 0 deletions closure/private/utils/soy.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2019 The Closure Rules 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("//closure/private/utils/soy:compile.bzl", "compile")

soy = struct(
compile = compile,
)
13 changes: 13 additions & 0 deletions closure/private/utils/soy/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2019 The Closure Rules 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.
95 changes: 95 additions & 0 deletions closure/private/utils/soy/compile.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright 2019 The Closure Rules 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("//closure/private:providers.bzl", "SoyInfo")

def compile(
actions,
closure_toolchain,
srcs,
output,
deps = [],
proto_descriptor_sets = depset()):
"""Compiles Soy source files into an intermediate build artifact and returns
a provider that represents the results of the compilation and can be added
to the set of providers emitted by this rule.

Compiling the source files that represent a single library into an
intermediate build artifact that later compilations can use in preference to
parsing dependencies improves overall compiler performance by making builds
more cacheable.

Args:
actions: Required. Instance of `ctx.actions`.
closure_toolchain: Required. The toolchain used to find the
SoyHeaderCompiler binary.
srcs: Required. A list of the Soy source files to be compiled.
output: Required. The output intermediate build artifact.
deps: Optional. A list of `ClosureTemplateInfo` of (direct) dependencies.
proto_descriptor_sets:
Returns:
An instance of `SoyInfo`.
"""

# TODO(yannic): Add support for dependency pruning.

# TODO(yannic): Add support for `--cssMetadataOutput`.

args = actions.args()

args.add("--output", output)
args.add_all(srcs, before_each = "--srcs")

direct_deps = depset(
direct = [],
transitive = [dep.direct_headers for dep in deps],
)
args.add_all(direct_deps, before_each = "--depHeaders")

indirect_deps = depset(
direct = [],
transitive = [dep.indirect_headers for dep in deps],
)
args.add_all(indirect_deps, before_each = "--indirectDepHeaders")

proto_descriptors = depset(
direct = [],
transitive = [proto_descriptor_sets] + [dep.proto_descriptor_sets for dep in deps],
)
args.add_all(proto_descriptors, before_each = "--protoFileDescriptors")

actions.run(
executable = closure_toolchain.soy.header_compiler,
inputs = depset(
direct = srcs,
transitive = [direct_deps, indirect_deps, proto_descriptors],
),
outputs = [output],
arguments = [args],
)

return SoyInfo(
direct_headers = depset([output]),
direct_sources = depset(srcs),
indirect_headers = depset(
direct = [],
transitive = [direct_deps, indirect_deps],
),
indirect_sources = depset(
direct = [],
transitive = [dep.indirect_sources for dep in deps] +
[dep.direct_sources for dep in deps],
),
proto_descriptor_sets = proto_descriptor_sets,
)
1 change: 1 addition & 0 deletions closure/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ def com_google_template_soy():
")\n") % (name, name)
for name in (
"SoyParseInfoGenerator",
"SoyHeaderCompiler",
"SoyToJbcSrcCompiler",
"SoyToJsSrcCompiler",
"SoyToPySrcCompiler",
Expand Down
29 changes: 29 additions & 0 deletions tests/soy/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2019 The Closure Rules 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("//closure/private/rules:soy_library.bzl", "soy_library")

soy_library(
name = "bold_soy",
srcs = [
"bold.soy",
],
)

soy_library(
name = "greet_soy",
srcs = [
"greet.soy",
],
)
26 changes: 26 additions & 0 deletions tests/soy/bold.soy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2019 The Closure Rules 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.

{namespace soy.io.bazel.rules.closure.tests}

/**
* Prints |text| in bold.
*/
{template .bold}
{@param text : html}

<strong>
{$text}
</strong>
{/template}
28 changes: 28 additions & 0 deletions tests/soy/greet.soy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2019 The Closure Rules 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.

{namespace soy.io.bazel.rules.closure.tests}

/**
* Prints |text| in bold.
*/
{template .greet}
{@param person : string}

{call .strong}
{param text kind="css"}
{$person}
{/param}
{/call}
{/template}