diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java | 84 |
1 files changed, 34 insertions, 50 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java b/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java index 23c92dcd5d..8caec89db0 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java +++ b/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java @@ -16,6 +16,7 @@ package com.google.devtools.build.lib.packages; import static com.google.devtools.build.lib.syntax.Runtime.NONE; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -54,8 +55,7 @@ public class WorkspaceFactory { public static final String BIND = "bind"; private final LegacyBuilder builder; - private final StoredEventHandler localReporter; - + private final Path installDir; private final Path workspaceDir; private final Mutability mutability; @@ -93,7 +93,6 @@ public class WorkspaceFactory { @Nullable Path installDir, @Nullable Path workspaceDir) { this.builder = builder; - this.localReporter = new StoredEventHandler(); this.mutability = mutability; this.installDir = installDir; this.workspaceDir = workspaceDir; @@ -101,14 +100,6 @@ public class WorkspaceFactory { this.workspaceFunctions = createWorkspaceFunctions(ruleClassProvider); } - // State while parsing the WORKSPACE file. - // We store them so we can pause the parsing and load skylark imports from the - // WorkspaceFileFunction. Loading skylark imports require access to the .skyframe package - // which this package cannot depend on. - private BuildFileAST buildFileAST = null; - private Environment.Builder environmentBuilder = null; - private ParserInputSource source = null; - /** * Parses the given WORKSPACE file without resolving skylark imports. * @@ -116,44 +107,52 @@ public class WorkspaceFactory { * //src/tools/generate_workspace.</p> */ public void parse(ParserInputSource source) throws InterruptedException, IOException { + parse(source, null); + } + + @VisibleForTesting + public void parse(ParserInputSource source, @Nullable StoredEventHandler localReporter) + throws InterruptedException, IOException { // This method is split in 2 so WorkspaceFileFunction can call the two parts separately and // do the Skylark load imports in between. We can't load skylark imports from // generate_workspace at the moment because it doesn't have access to skyframe, but that's okay // because most people are just using it to resolve Maven dependencies. - parseWorkspaceFile(source); - execute(); - } - - /** - * Parses the WORKSPACE file, generating the AST. - * @throws IOException if parsing fails. - */ - public void parseWorkspaceFile(ParserInputSource source) throws IOException { - this.source = source; - buildFileAST = BuildFileAST.parseBuildFile(source, localReporter, false); + if (localReporter == null) { + localReporter = new StoredEventHandler(); + } + BuildFileAST buildFileAST = BuildFileAST.parseBuildFile(source, localReporter, false); if (buildFileAST.containsErrors()) { - environmentBuilder = null; throw new IOException("Failed to parse " + source.getPath()); } - environmentBuilder = Environment.builder(mutability) - .setGlobals(Environment.BUILD) - .setEventHandler(localReporter); + execute(buildFileAST, null, localReporter); } + /** * Actually runs through the AST, calling the functions in the WORKSPACE file and adding rules * to the //external package. */ - public void execute() throws InterruptedException { - Preconditions.checkNotNull(environmentBuilder); + public void execute(BuildFileAST ast, Map<String, Extension> importedExtensions) + throws InterruptedException { + Preconditions.checkNotNull(ast); + Preconditions.checkNotNull(importedExtensions); + execute(ast, importedExtensions, new StoredEventHandler()); + } + + private void execute(BuildFileAST ast, @Nullable Map<String, Extension> importedExtensions, + StoredEventHandler localReporter) + throws InterruptedException { + Environment.Builder environmentBuilder = Environment.builder(mutability) + .setGlobals(Environment.BUILD) + .setEventHandler(localReporter); + if (importedExtensions != null) { + environmentBuilder.setImportedExtensions(importedExtensions); + } Environment workspaceEnv = environmentBuilder.setLoadingPhase().build(); - addWorkspaceFunctions(workspaceEnv); - if (!buildFileAST.exec(workspaceEnv, localReporter)) { - localReporter.handle(Event.error("Error evaluating WORKSPACE file " + source.getPath())); + addWorkspaceFunctions(workspaceEnv, localReporter); + if (!ast.exec(workspaceEnv, localReporter)) { + localReporter.handle(Event.error("Error evaluating WORKSPACE file")); } - environmentBuilder = null; - buildFileAST = null; - source = null; builder.addEvents(localReporter.getEvents()); if (localReporter.hasErrors()) { @@ -162,14 +161,6 @@ public class WorkspaceFactory { localReporter.clear(); } - public BuildFileAST getBuildFileAST() { - return buildFileAST; - } - - public void setImportedExtensions(Map<String, Extension> importedExtensions) { - environmentBuilder.setImportedExtensions(importedExtensions); - } - // TODO(bazel-team): use @SkylarkSignature annotations on a BuiltinFunction.Factory // for signature + documentation of this and other functions in this file. private static BuiltinFunction newWorkspaceNameFunction() { @@ -263,7 +254,7 @@ public class WorkspaceFactory { return mapBuilder.build(); } - private void addWorkspaceFunctions(Environment workspaceEnv) { + private void addWorkspaceFunctions(Environment workspaceEnv, StoredEventHandler localReporter) { try { workspaceEnv.update("workspace", newWorkspaceNameFunction()); for (Map.Entry<String, BaseFunction> function : workspaceFunctions.entrySet()) { @@ -305,11 +296,4 @@ public class WorkspaceFactory { public static ClassObject newNativeModule(RuleClassProvider ruleClassProvider) { return newNativeModule(createWorkspaceFunctions(ruleClassProvider)); } - - /** - * @return a list of events that have occurred while parsing the WORKSPACE file. - */ - public ImmutableList<Event> getEvents() { - return localReporter.getEvents(); - } } |