@Nullable public static ExternalPackage getExternalPackage(Environment env) throws RepositoryFunctionException { SkyKey packageKey = PackageValue.key(ExternalPackage.PACKAGE_IDENTIFIER); PackageValue packageValue; try { packageValue = (PackageValue) env.getValueOrThrow(packageKey, NoSuchPackageException.class); } catch (NoSuchPackageException e) { throw new RepositoryFunctionException( new BuildFileNotFoundException( ExternalPackage.PACKAGE_IDENTIFIER, "Could not load //external package"), Transience.PERSISTENT); } if (packageValue == null) { return null; } ExternalPackage externalPackage = (ExternalPackage) packageValue.getPackage(); if (externalPackage.containsErrors()) { throw new RepositoryFunctionException( new BuildFileContainsErrorsException( ExternalPackage.PACKAGE_IDENTIFIER, "Could not load //external package"), Transience.PERSISTENT); } return externalPackage; }
/** * Uses a remote repository name to fetch the corresponding Rule describing how to get it. This * should be called from {@link SkyFunction#compute} functions, which should return null if this * returns null. If {@code ruleClassName} is set, the rule found must have a matching rule class * name. */ @Nullable public static Rule getRule( RepositoryName repositoryName, @Nullable String ruleClassName, Environment env) throws RepositoryFunctionException { ExternalPackage externalPackage = getExternalPackage(env); if (externalPackage == null) { return null; } Rule rule = externalPackage.getRepositoryInfo(repositoryName); if (rule == null) { throw new RepositoryFunctionException( new BuildFileContainsErrorsException( ExternalPackage.PACKAGE_IDENTIFIER, "The repository named '" + repositoryName + "' could not be resolved"), Transience.PERSISTENT); } Preconditions.checkState( ruleClassName == null || rule.getRuleClass().equals(ruleClassName), "Got %s, was expecting a %s", rule, ruleClassName); return rule; }