|
| 1 | +# Copyright 2017 The TensorFlow 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 | +"""Compatibility hacks.""" |
| 15 | + |
| 16 | +# TODO(@jart): Merge this file into defs.bzl once that file is sync unified. |
| 17 | + |
| 18 | +def tensorboard_typescript_bundle( |
| 19 | + name, |
| 20 | + out, |
| 21 | + namespace_srcs, |
| 22 | + namespace_symbol_aliases={}, |
| 23 | + namespace_symbol_aliases_public={}, |
| 24 | +): |
| 25 | + """Rolls TypeScript ES6 modules into one vanilla source file without imports. |
| 26 | +
|
| 27 | + This is a genrule wrapper that concatenates TypeScripts sources inside |
| 28 | + namespace blocks while removing ^import lines. Because the sources themselves |
| 29 | + are not parsed, the structure of the modules must be passed to this macro as |
| 30 | + a Skylark data structure. |
| 31 | +
|
| 32 | + Args: |
| 33 | + name: Name of this build rule target. |
| 34 | + out: Path of outputted TypeScript source file. |
| 35 | + namespace_srcs: Multimap of namespace strings to build file targets. The |
| 36 | + ordering of the dictionary and nested lists does not matter when |
| 37 | + generating a typings file, but *does* matter when generating a source |
| 38 | + file. |
| 39 | + namespace_symbol_aliases: Map of namespace strings where each value is a |
| 40 | + map of symbol names to fully qualified symbol names. |
| 41 | + namespace_symbol_aliases_public: Same as namespace_symbol_aliases but the |
| 42 | + symbol will be visible to other namespaces. |
| 43 | + """ |
| 44 | + cmd = ["(", "echo // GENERATED BY TENSORBOARD_TYPESCRIPT_BUNDLE"] |
| 45 | + inputs_depsets = [] |
| 46 | + for namespace, srcs in namespace_srcs.items(): |
| 47 | + cmd.append("echo") |
| 48 | + if out[-5:] == ".d.ts": |
| 49 | + cmd.append("echo 'declare namespace %s {'" % namespace) |
| 50 | + elif out[-3:] == ".ts": |
| 51 | + cmd.append("echo 'module %s {'" % namespace) |
| 52 | + else: |
| 53 | + fail("'out' must end with .ts or .d.ts: " + out) |
| 54 | + for symbol, canon in namespace_symbol_aliases.get(namespace, {}).items(): |
| 55 | + cmd.append("echo 'import %s = %s;'" % (symbol, canon)) |
| 56 | + for symbol, canon in namespace_symbol_aliases_public.get(namespace, |
| 57 | + {}).items(): |
| 58 | + cmd.append("echo 'export import %s = %s;'" % (symbol, canon)) |
| 59 | + inputs_depsets.append(depset(srcs)) |
| 60 | + for src in srcs: |
| 61 | + cmd.append("for f in $(locations %s); do" % src) |
| 62 | + cmd.append(" echo") |
| 63 | + cmd.append(" echo /////////////////////////////////////////////////////") |
| 64 | + cmd.append(" echo // " + namespace) |
| 65 | + cmd.append(" echo // $$f") |
| 66 | + cmd.append(" echo /////////////////////////////////////////////////////") |
| 67 | + cmd.append(" echo") |
| 68 | + cmd.append(" sed 's!^import !// import !' $$f \\") |
| 69 | + cmd.append(" | sed 's!^export declare !export !' \\") |
| 70 | + cmd.append(" | sed '/^export .* from /d' \\") |
| 71 | + cmd.append(" | sed '/^export {.*};$$/d'") |
| 72 | + cmd.append("done") |
| 73 | + cmd.append("echo '}'") |
| 74 | + cmd.append(") >$@") |
| 75 | + native.genrule( |
| 76 | + name = name, |
| 77 | + srcs = depset(transitive=inputs_depsets).to_list(), |
| 78 | + outs = [out], |
| 79 | + cmd = "\n".join(cmd), |
| 80 | + ) |
0 commit comments