1717import static com .google .common .collect .ImmutableList .toImmutableList ;
1818
1919import com .google .auto .value .AutoValue ;
20- import com .google .common .collect .ImmutableBiMap ;
2120import com .google .common .collect .ImmutableList ;
22- import com .google .common .collect .ImmutableSet ;
21+ import com .google .common .collect .ImmutableMap ;
2322import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2423import com .ryanharter .auto .value .gson .GenerateTypeAdapter ;
2524import java .util .Optional ;
2625import net .starlark .java .syntax .Location ;
2726
2827/**
29- * Represents one usage of a module extension in one MODULE.bazel file. This class records all the
30- * information pertinent to the proxy object returned from the {@code use_extension} call.
28+ * Represents the usage of a module extension in one module. This class records all the information
29+ * pertinent to all the proxy objects returned from any {@code use_extension} calls in this module
30+ * that refer to the same extension (or isolate, when applicable).
3131 *
3232 * <p>When adding new fields, make sure to update {@link #trimForEvaluation()} as well.
3333 */
@@ -49,40 +49,80 @@ public abstract class ModuleExtensionUsage {
4949 /** The module that contains this particular extension usage. */
5050 public abstract ModuleKey getUsingModule ();
5151
52- /**
53- * The location where this proxy object was created (by the {@code use_extension} call). Note that
54- * if there were multiple {@code use_extension} calls on same extension, then this only stores the
55- * location of the first one.
56- */
57- public abstract Location getLocation ();
52+ /** Represents one "proxy object" returned from one {@code use_extension} call. */
53+ @ AutoValue
54+ @ GenerateTypeAdapter
55+ public abstract static class Proxy {
56+ /** The location of the {@code use_extension} call. */
57+ public abstract Location getLocation ();
58+
59+ /**
60+ * The name of the proxy object; as in, the name that the return value of {@code use_extension}
61+ * is bound to. Is the empty string if the return value is not bound to any name (e.g. {@code
62+ * use_repo(use_extension(...))}).
63+ */
64+ public abstract String getProxyName ();
65+
66+ /** Whether {@code dev_dependency} is set to true. */
67+ public abstract boolean isDevDependency ();
68+
69+ /**
70+ * All the repos imported, through this proxy, from this module extension into the scope of the
71+ * current module. The key is the local repo name (in the scope of the current module), and the
72+ * value is the name exported by the module extension.
73+ */
74+ public abstract ImmutableMap <String , String > getImports ();
75+
76+ public static Builder builder () {
77+ return new AutoValue_ModuleExtensionUsage_Proxy .Builder ().setProxyName ("" );
78+ }
5879
59- /**
60- * All the repos imported from this module extension into the scope of the current module. The key
61- * is the local repo name (in the scope of the current module), and the value is the name exported
62- * by the module extension.
63- */
64- public abstract ImmutableBiMap <String , String > getImports ();
80+ /** Builder for {@link ModuleExtensionUsage.Proxy}. */
81+ @ AutoValue .Builder
82+ public abstract static class Builder {
83+ public abstract Builder setLocation (Location value );
6584
66- /**
67- * The repo names as exported by the module extension that were imported using a proxy marked as a
68- * dev dependency.
69- */
70- public abstract ImmutableSet <String > getDevImports ();
85+ public abstract String getProxyName ();
86+
87+ public abstract Builder setProxyName (String value );
88+
89+ public abstract boolean isDevDependency ();
90+
91+ public abstract Builder setDevDependency (boolean value );
92+
93+ abstract ImmutableMap .Builder <String , String > importsBuilder ();
94+
95+ @ CanIgnoreReturnValue
96+ public final Builder addImport (String key , String value ) {
97+ importsBuilder ().put (key , value );
98+ return this ;
99+ }
100+
101+ public abstract Builder setImports (ImmutableMap <String , String > value );
102+
103+ public abstract Proxy build ();
104+ }
105+ }
106+
107+ /** The list of proxy objects that constitute */
108+ public abstract ImmutableList <Proxy > getProxies ();
71109
72110 /** All the tags specified by this module for this extension. */
73111 public abstract ImmutableList <Tag > getTags ();
74112
75113 /**
76- * Whether any <code>use_extension</code> calls for this usage had <code>dev_dependency = True
77- * </code> set.*
114+ * Whether any {@code use_extension} calls for this usage had {@code dev_dependency = True} set.
78115 */
79- public abstract boolean getHasDevUseExtension ();
116+ public final boolean getHasDevUseExtension () {
117+ return getProxies ().stream ().anyMatch (p -> p .isDevDependency ());
118+ }
80119
81120 /**
82- * Whether any <code>use_extension</code> calls for this usage had <code>dev_dependency = False
83- * </code> set.*
121+ * Whether any {@code use_extension} calls for this usage had {@code dev_dependency = False} set.
84122 */
85- public abstract boolean getHasNonDevUseExtension ();
123+ public final boolean getHasNonDevUseExtension () {
124+ return getProxies ().stream ().anyMatch (p -> !p .isDevDependency ());
125+ }
86126
87127 public abstract Builder toBuilder ();
88128
@@ -100,13 +140,12 @@ ModuleExtensionUsage trimForEvaluation() {
100140 // preserves correctness in case new fields are added without updating this code.
101141 return toBuilder ()
102142 .setTags (getTags ().stream ().map (Tag ::trimForEvaluation ).collect (toImmutableList ()))
143+ // Clear out all proxies as information contained therein isn't useful for evaluation.
103144 // Locations are only used for error reporting and thus don't influence whether the
104145 // evaluation of the extension is successful and what its result is in case of success.
105- .setLocation (Location .BUILTIN )
106146 // Extension implementation functions do not see the imports, they are only validated
107147 // against the set of generated repos in a validation step that comes afterward.
108- .setImports (ImmutableBiMap .of ())
109- .setDevImports (ImmutableSet .of ())
148+ .setProxies (ImmutableList .of ())
110149 .build ();
111150 }
112151
@@ -122,26 +161,26 @@ public abstract static class Builder {
122161
123162 public abstract Builder setUsingModule (ModuleKey value );
124163
125- public abstract Builder setLocation ( Location value );
164+ public abstract Builder setProxies ( ImmutableList < Proxy > value );
126165
127- public abstract Builder setImports ( ImmutableBiMap < String , String > value );
166+ abstract ImmutableList . Builder < Proxy > proxiesBuilder ( );
128167
129- public abstract Builder setDevImports (ImmutableSet <String > value );
168+ @ CanIgnoreReturnValue
169+ public Builder addProxy (Proxy value ) {
170+ proxiesBuilder ().add (value );
171+ return this ;
172+ }
130173
131174 public abstract Builder setTags (ImmutableList <Tag > value );
132175
133176 abstract ImmutableList .Builder <Tag > tagsBuilder ();
134177
135178 @ CanIgnoreReturnValue
136- public ModuleExtensionUsage . Builder addTag (Tag value ) {
179+ public Builder addTag (Tag value ) {
137180 tagsBuilder ().add (value );
138181 return this ;
139182 }
140183
141- public abstract Builder setHasDevUseExtension (boolean hasDevUseExtension );
142-
143- public abstract Builder setHasNonDevUseExtension (boolean hasNonDevUseExtension );
144-
145184 public abstract ModuleExtensionUsage build ();
146185 }
147186}
0 commit comments