cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 | |
| 15 | package com.google.devtools.build.skydoc.fakebuildapi; |
| 16 | |
cparsons | 858057c | 2019-09-17 13:16:24 -0700 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableCollection; |
| 18 | import com.google.common.collect.ImmutableList; |
gregce | 59f5cba | 2020-07-22 12:18:43 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.starlarkbuildapi.StarlarkNativeModuleApi; |
cparsons | 858057c | 2019-09-17 13:16:24 -0700 | [diff] [blame] | 20 | import javax.annotation.Nullable; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 21 | import net.starlark.java.eval.Dict; |
| 22 | import net.starlark.java.eval.EvalException; |
| 23 | import net.starlark.java.eval.NoneType; |
| 24 | import net.starlark.java.eval.Printer; |
| 25 | import net.starlark.java.eval.Sequence; |
| 26 | import net.starlark.java.eval.Starlark; |
| 27 | import net.starlark.java.eval.StarlarkCallable; |
adonovan | e7df682 | 2020-10-08 08:12:51 -0700 | [diff] [blame] | 28 | import net.starlark.java.eval.StarlarkInt; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 29 | import net.starlark.java.eval.StarlarkList; |
| 30 | import net.starlark.java.eval.StarlarkThread; |
adonovan | 267bdaa | 2020-11-04 11:32:24 -0800 | [diff] [blame^] | 31 | import net.starlark.java.eval.Structure; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 32 | import net.starlark.java.syntax.Location; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 33 | |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 34 | /** Fake implementation of {@link StarlarkNativeModuleApi}. */ |
adonovan | 267bdaa | 2020-11-04 11:32:24 -0800 | [diff] [blame^] | 35 | public class FakeStarlarkNativeModuleApi implements StarlarkNativeModuleApi, Structure { |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 36 | |
| 37 | @Override |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 38 | public Sequence<?> glob( |
| 39 | Sequence<?> include, |
| 40 | Sequence<?> exclude, |
adonovan | e7df682 | 2020-10-08 08:12:51 -0700 | [diff] [blame] | 41 | StarlarkInt excludeDirectories, |
laurentlb | 9bc841e | 2019-05-28 05:59:35 -0700 | [diff] [blame] | 42 | Object allowEmpty, |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 43 | StarlarkThread thread) |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 44 | throws EvalException, InterruptedException { |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 45 | return StarlarkList.of(thread.mutability()); |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 49 | public Object existingRule(String name, StarlarkThread thread) { |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 50 | return null; |
| 51 | } |
| 52 | |
| 53 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 54 | public Dict<String, Dict<String, Object>> existingRules(StarlarkThread thread) { |
Googler | 6456fcb | 2019-11-22 13:05:42 -0800 | [diff] [blame] | 55 | return Dict.of(thread.mutability()); |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 59 | public NoneType packageGroup( |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 60 | String name, Sequence<?> packages, Sequence<?> includes, StarlarkThread thread) { |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 61 | return null; |
| 62 | } |
| 63 | |
| 64 | @Override |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 65 | public NoneType exportsFiles( |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 66 | Sequence<?> srcs, Object visibility, Object licenses, StarlarkThread thread) { |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 67 | return null; |
| 68 | } |
| 69 | |
| 70 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 71 | public String packageName(StarlarkThread thread) { |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 72 | return ""; |
| 73 | } |
| 74 | |
| 75 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 76 | public String repositoryName(StarlarkThread thread) { |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 77 | return ""; |
| 78 | } |
cparsons | 858057c | 2019-09-17 13:16:24 -0700 | [diff] [blame] | 79 | |
| 80 | @Nullable |
| 81 | @Override |
| 82 | public Object getValue(String name) throws EvalException { |
| 83 | // Bazel's notion of the global "native" isn't fully exposed via public interfaces, for example, |
| 84 | // as far as native rules are concerned. Returning None on all unsupported invocations of |
| 85 | // native.[func_name]() is the safest "best effort" approach to implementing a fake for |
| 86 | // "native". |
Googler | 9eca6d5 | 2019-11-13 06:26:47 -0800 | [diff] [blame] | 87 | return new StarlarkCallable() { |
| 88 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 89 | public Object fastcall(StarlarkThread thread, Object[] positional, Object[] named) { |
Googler | 9eca6d5 | 2019-11-13 06:26:47 -0800 | [diff] [blame] | 90 | return Starlark.NONE; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String getName() { |
| 95 | return name; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Location getLocation() { |
| 100 | return Location.BUILTIN; |
| 101 | } |
| 102 | |
| 103 | @Override |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 104 | public void repr(Printer printer) { |
Googler | 9eca6d5 | 2019-11-13 06:26:47 -0800 | [diff] [blame] | 105 | printer.append("<faked no-op function " + name + ">"); |
| 106 | } |
| 107 | }; |
cparsons | 858057c | 2019-09-17 13:16:24 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
adonovan | 11e5fab | 2019-12-06 12:03:49 -0800 | [diff] [blame] | 111 | public ImmutableCollection<String> getFieldNames() { |
cparsons | 858057c | 2019-09-17 13:16:24 -0700 | [diff] [blame] | 112 | return ImmutableList.of(); |
| 113 | } |
| 114 | |
| 115 | @Nullable |
| 116 | @Override |
| 117 | public String getErrorMessageForUnknownField(String field) { |
| 118 | return ""; |
| 119 | } |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 120 | } |