24
24
import com .google .devtools .build .lib .cmdline .RepositoryName ;
25
25
import com .google .devtools .build .lib .events .ExtendedEventHandler ;
26
26
import com .google .devtools .build .lib .packages .NoSuchPackageException ;
27
- import com .google .devtools .build .lib .packages .Package ;
28
27
import com .google .devtools .build .lib .packages .Rule ;
29
28
import com .google .devtools .build .lib .packages .RuleClass ;
30
29
import com .google .devtools .build .lib .packages .RuleFactory .InvalidRuleException ;
@@ -53,14 +52,14 @@ public static ModuleExtensionEvalStarlarkThreadContext from(StarlarkThread threa
53
52
}
54
53
55
54
@ AutoValue
56
- abstract static class PackageAndLocation {
57
- abstract Package getPackage ();
55
+ abstract static class RepoSpecAndLocation {
56
+ abstract RepoSpec getRepoSpec ();
58
57
59
58
abstract Location getLocation ();
60
59
61
- static PackageAndLocation create (Package pkg , Location location ) {
62
- return new AutoValue_ModuleExtensionEvalStarlarkThreadContext_PackageAndLocation (
63
- pkg , location );
60
+ static RepoSpecAndLocation create (RepoSpec repoSpec , Location location ) {
61
+ return new AutoValue_ModuleExtensionEvalStarlarkThreadContext_RepoSpecAndLocation (
62
+ repoSpec , location );
64
63
}
65
64
}
66
65
@@ -69,7 +68,7 @@ static PackageAndLocation create(Package pkg, Location location) {
69
68
private final RepositoryMapping repoMapping ;
70
69
private final BlazeDirectories directories ;
71
70
private final ExtendedEventHandler eventHandler ;
72
- private final Map <String , PackageAndLocation > generatedRepos = new HashMap <>();
71
+ private final Map <String , RepoSpecAndLocation > generatedRepos = new HashMap <>();
73
72
74
73
public ModuleExtensionEvalStarlarkThreadContext (
75
74
String repoPrefix ,
@@ -84,11 +83,6 @@ public ModuleExtensionEvalStarlarkThreadContext(
84
83
this .eventHandler = eventHandler ;
85
84
}
86
85
87
- /** The string that should be prefixed to the names of all repos generated by this extension. */
88
- public String getRepoPrefix () {
89
- return repoPrefix ;
90
- }
91
-
92
86
public void createRepo (StarlarkThread thread , Dict <String , Object > kwargs , RuleClass ruleClass )
93
87
throws InterruptedException , EvalException {
94
88
Object nameValue = kwargs .getOrDefault ("name" , Starlark .NONE );
@@ -98,7 +92,7 @@ public void createRepo(StarlarkThread thread, Dict<String, Object> kwargs, RuleC
98
92
}
99
93
String name = (String ) nameValue ;
100
94
RepositoryName .validateUserProvidedRepoName (name );
101
- PackageAndLocation conflict = generatedRepos .get (name );
95
+ RepoSpecAndLocation conflict = generatedRepos .get (name );
102
96
if (conflict != null ) {
103
97
throw Starlark .errorf (
104
98
"A repo named %s is already generated by this module extension at %s" ,
@@ -116,8 +110,17 @@ public void createRepo(StarlarkThread thread, Dict<String, Object> kwargs, RuleC
116
110
"RepositoryRuleFunction.createRule" ,
117
111
ruleClass ,
118
112
Maps .transformEntries (kwargs , (k , v ) -> k .equals ("name" ) ? prefixedName : v ));
119
- generatedRepos .put (
120
- name , PackageAndLocation .create (rule .getPackage (), thread .getCallerLocation ()));
113
+
114
+ Map <String , Object > attributes = Maps .transformEntries (kwargs , (k , v ) -> rule .getAttr (k ));
115
+ String bzlFile = ruleClass .getRuleDefinitionEnvironmentLabel ().getUnambiguousCanonicalForm ();
116
+ RepoSpec repoSpec =
117
+ RepoSpec .builder ()
118
+ .setBzlFile (bzlFile )
119
+ .setRuleClassName (ruleClass .getName ())
120
+ .setAttributes (AttributeValues .create (attributes ))
121
+ .build ();
122
+
123
+ generatedRepos .put (name , RepoSpecAndLocation .create (repoSpec , thread .getCallerLocation ()));
121
124
} catch (InvalidRuleException | NoSuchPackageException e ) {
122
125
throw Starlark .errorf ("%s" , e .getMessage ());
123
126
}
@@ -128,8 +131,8 @@ public void createRepo(StarlarkThread thread, Dict<String, Object> kwargs, RuleC
128
131
* specified by the extension) of the repo, and the value is the package containing (only) the
129
132
* repo rule.
130
133
*/
131
- public ImmutableMap <String , Package > getGeneratedRepos () {
134
+ public ImmutableMap <String , RepoSpec > getGeneratedRepoSpecs () {
132
135
return ImmutableMap .copyOf (
133
- Maps .transformValues (generatedRepos , PackageAndLocation :: getPackage ));
136
+ Maps .transformValues (generatedRepos , RepoSpecAndLocation :: getRepoSpec ));
134
137
}
135
138
}
0 commit comments