forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bazelbuild#4 from Wyverald/registry
Index registry impl
- Loading branch information
Showing
17 changed files
with
492 additions
and
31 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ArchiveFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.google.devtools.build.lib.bazel.bzlmod; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import java.net.URL; | ||
import java.util.Objects; | ||
|
||
public class ArchiveFetcher implements EarlyFetcher { | ||
|
||
private final ImmutableList<URL> urls; | ||
private final String integrity; | ||
private final String stripPrefix; | ||
|
||
ArchiveFetcher(ImmutableList<URL> urls, String integrity, String stripPrefix) { | ||
this.urls = urls; | ||
this.integrity = integrity; | ||
this.stripPrefix = stripPrefix; | ||
} | ||
|
||
@Override | ||
public Path earlyFetch() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Path fetch(String repoName, Path vendorDir) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ArchiveFetcher that = (ArchiveFetcher) o; | ||
return Objects.equals(urls, that.urls) && | ||
Objects.equals(integrity, that.integrity) && | ||
Objects.equals(stripPrefix, that.stripPrefix); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(urls, integrity, stripPrefix); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ArchiveFetcher{" + | ||
"urls=" + urls + | ||
", integrity='" + integrity + '\'' + | ||
", stripPrefix='" + stripPrefix + '\'' + | ||
'}'; | ||
} | ||
} |
13 changes: 9 additions & 4 deletions
13
src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ArchiveOverride.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
package com.google.devtools.build.lib.bazel.bzlmod; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import java.net.URL; | ||
|
||
@AutoValue | ||
public abstract class ArchiveOverride implements NonRegistryOverride { | ||
|
||
public static ArchiveOverride create(String url, String integrity) { | ||
return new AutoValue_ArchiveOverride(url, integrity); | ||
public static ArchiveOverride create(ImmutableList<URL> urls, String integrity, | ||
String stripPrefix) { | ||
return new AutoValue_ArchiveOverride(urls, integrity, stripPrefix); | ||
} | ||
|
||
public abstract String getUrl(); | ||
public abstract ImmutableList<URL> getUrls(); | ||
|
||
public abstract String getIntegrity(); | ||
|
||
public abstract String getStripPrefix(); | ||
|
||
@Override | ||
public EarlyFetcher toEarlyFetcher(FetcherFactory fetcherFactory) { | ||
return fetcherFactory.createArchiveFetcher(getUrl(), getIntegrity()); | ||
return fetcherFactory.createArchiveFetcher(getUrls(), getIntegrity(), getStripPrefix()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/com/google/devtools/build/lib/bazel/bzlmod/IndexRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.google.devtools.build.lib.bazel.bzlmod; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.devtools.build.lib.bazel.repository.downloader.HttpDownloader; | ||
import com.google.devtools.build.lib.events.ExtendedEventHandler; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import com.google.gson.FieldNamingPolicy; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class IndexRegistry implements Registry { | ||
|
||
private final URI uri; | ||
private final HttpDownloader httpDownloader; | ||
private final Map<String, String> clientEnv; | ||
private final FetcherFactory fetcherFactory; | ||
private final Gson gson; | ||
|
||
IndexRegistry(URI uri, HttpDownloader httpDownloader, Map<String, String> clientEnv, | ||
FetcherFactory fetcherFactory) { | ||
this.uri = uri; | ||
this.httpDownloader = httpDownloader; | ||
this.clientEnv = clientEnv; | ||
this.fetcherFactory = fetcherFactory; | ||
this.gson = new GsonBuilder() | ||
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | ||
.create(); | ||
} | ||
|
||
@Override | ||
public String getUrl() { | ||
return uri.toString(); | ||
} | ||
|
||
private Optional<byte[]> grabFile(String relativePath, ExtendedEventHandler eventHandler) | ||
throws IOException, InterruptedException { | ||
String plainBaseUrl = getUrl(); | ||
if (!plainBaseUrl.endsWith("/")) { | ||
plainBaseUrl += "/"; | ||
} | ||
try { | ||
return Optional.of(httpDownloader | ||
.downloadAndReadOneUrl(new URL(plainBaseUrl + relativePath), eventHandler, clientEnv)); | ||
} catch (FileNotFoundException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<byte[]> getModuleFile(ModuleKey key, ExtendedEventHandler eventHandler) | ||
throws IOException, InterruptedException { | ||
return grabFile(String.format("modules/%s/%s/MODULE.bazel", key.getName(), key.getVersion()), | ||
eventHandler); | ||
} | ||
|
||
private static class BazelRegistryJson { | ||
|
||
String[] mirrors; | ||
} | ||
|
||
private static class SourceJson { | ||
|
||
URL url; | ||
String integrity; | ||
String stripPrefix; | ||
String[] patchFiles; | ||
int patchStrip; | ||
} | ||
|
||
private <T> Optional<T> grabJson(String relativePath, Class<T> klass, | ||
ExtendedEventHandler eventHandler) | ||
throws IOException, InterruptedException { | ||
Optional<byte[]> bytes = grabFile(relativePath, eventHandler); | ||
if (!bytes.isPresent()) { | ||
return Optional.empty(); | ||
} | ||
String jsonString = new String(bytes.get(), StandardCharsets.UTF_8); | ||
return Optional.of(gson.fromJson(jsonString, klass)); | ||
} | ||
|
||
@Override | ||
public Fetcher getFetcher(ModuleKey key, ExtendedEventHandler eventHandler) | ||
throws IOException, InterruptedException { | ||
Optional<BazelRegistryJson> bazelRegistryJson = | ||
grabJson("bazel_registry.json", BazelRegistryJson.class, eventHandler); | ||
Optional<SourceJson> sourceJson = grabJson( | ||
String.format("modules/%s/%s/source.json", key.getName(), key.getVersion()), | ||
SourceJson.class, eventHandler); | ||
if (!sourceJson.isPresent()) { | ||
throw new FileNotFoundException( | ||
String.format("Module %s's source information not found in registry %s", key, getUrl())); | ||
} | ||
URL sourceUrl = sourceJson.get().url; | ||
ImmutableList.Builder<URL> urls = new ImmutableList.Builder<>(); | ||
if (bazelRegistryJson.isPresent()) { | ||
for (String mirror : bazelRegistryJson.get().mirrors) { | ||
StringBuilder url = new StringBuilder(mirror); | ||
if (url.charAt(url.length() - 1) != '/') { | ||
url.append('/'); | ||
} | ||
url.append(sourceUrl.getAuthority()); | ||
if (url.charAt(url.length() - 1) != '/' && sourceUrl.getFile().charAt(0) != '/') { | ||
url.append('/'); | ||
} | ||
url.append(sourceUrl.getFile()); | ||
urls.add(new URL(url.toString())); | ||
} | ||
} | ||
urls.add(sourceUrl); | ||
return fetcherFactory.createArchiveFetcher( | ||
urls.build(), | ||
sourceJson.get().integrity, | ||
sourceJson.get().stripPrefix); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.