From fcc0a214e8f130f983929aa02fb6273e0a773fa3 Mon Sep 17 00:00:00 2001 From: Seray Uzgur Date: Sat, 13 Feb 2021 23:52:10 +0300 Subject: [PATCH] Support alternative local index #109 --- CHANGELOG.md | 1 + README.md | 8 +++++--- package.json | 6 ++++++ src/api/local_registry.ts | 9 ++++++--- src/core/fetcher.ts | 10 ++++++---- src/core/listener.ts | 7 ++++--- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bb96a6..afd1beb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to the "crates" extension will be documented in this file. - Comma handled for ranged versions [#117](https://github.com/serayuzgur/crates/issues/117) - Fix for comments with brackets inside arrays [#115](https://github.com/serayuzgur/crates/pull/115) - ${version} is not being replaced for compatible decorator [#118](https://github.com/serayuzgur/crates/issues/118) +- Support alternative local index [#109](https://github.com/serayuzgur/crates/issues/109) ## 0.5.5 diff --git a/README.md b/README.md index 0f4fd51..d5aba9a 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,15 @@ It is so **simple** that you do not need any configuration, but if you insist... `crates.useLocalCargoIndex`: If true, crates will use local cargo repository. +`crates.localCargoIndexHash`: he hash path for crates.io index. Default value goes to official index. Alternative values would support registry source mirror with source replacement setup. + `crates.githubAuthBasic`: The `:` or `:` for accessing Github API with increased access rates 5000 req/h. -`crates.errorDecorator`: The text to show when a dependency has errors. Default is ❗️. +`crates.errorDecorator`: The text to show when a dependency has errors. Default is ❗️❗️❗️. -`crates.compatibleDecorator`: The text template to show when a dependency is semver compatible. ${version} will be replaced by the latest version info. Default is '🟢 ${version}' +`crates.compatibleDecorator`: The text template to show when a dependency is semver compatible. ${version} will be replaced by the latest version info. Default is '✅' -`crates.incompatibleDecorator`: The text template to show when a dependency is not semver compatible. ${version} will be replaced by the latest version info. Default is '🔴 ${version}' +`crates.incompatibleDecorator`: The text template to show when a dependency is not semver compatible. ${version} will be replaced by the latest version info. Default is '❌ ${version}' `crates.listPreReleases`: If true, pre-release versions will be listed in hover and at decoration. The default is false. diff --git a/package.json b/package.json index 85d166d..2c41110 100644 --- a/package.json +++ b/package.json @@ -97,6 +97,12 @@ "scope": "resource", "default": true, "description": "If true, crates will use local cargo index. (Requires git, cargo and updated crates.io index)" + }, + "crates.localCargoIndexHash": { + "type": "string", + "scope": "resource", + "default": "github.com-1ecc6299db9ec823", + "description": "The hash path for crates.io index. Default value goes to official index. Alternative values would support registry source mirror with source replacement setup" } } } diff --git a/src/api/local_registry.ts b/src/api/local_registry.ts index 3663b4f..f43af17 100644 --- a/src/api/local_registry.ts +++ b/src/api/local_registry.ts @@ -14,7 +14,7 @@ import { decidePath, parseVersions } from "./index-utils"; const exec = util.promisify(require("child_process").exec); // check for the crates index. If none found switch to github and show error -const cargoHome = getCargoPath() +const cargoHome = getCargoPath(); function getCargoPath() { // Trailing slash on macos (does not want / at the end) and windows (needs / at end) @@ -25,11 +25,14 @@ function getCargoPath() { -const gitDir = path.resolve(cargoHome, "registry/index/github.com-1ecc6299db9ec823/.git/"); +let gitDir = path.resolve(cargoHome, "registry/index/github.com-1ecc6299db9ec823/.git/"); -export function checkCargoRegistry() { +export function checkCargoRegistry(localIndexHash?: string) { + if (localIndexHash) { + gitDir = path.resolve(cargoHome, `registry/index/${localIndexHash}/.git/`); + } return fs.existsSync(gitDir); } diff --git a/src/core/fetcher.ts b/src/core/fetcher.ts index 740c733..6d923dc 100644 --- a/src/core/fetcher.ts +++ b/src/core/fetcher.ts @@ -9,14 +9,16 @@ export function fetchCrateVersions( dependencies: Item[], shouldListPreRels: boolean, githubToken?: string, - isLocalRegistry?: boolean): Promise { + useLocalIndex?: boolean, + localIndexHash?: string +): Promise { statusBarItem.setText("👀 Fetching crates.io"); + const isLocalIndexAvailable = useLocalIndex && checkCargoRegistry(localIndexHash); + const versions = isLocalIndexAvailable ? loVersions : ghVersions; + const responses = dependencies.map( (item: Item): Promise => { // Check settings and if local registry enabled control cargo home. Fallback is the github index. - const isLocalRegistryAvailable = isLocalRegistry && checkCargoRegistry(); - const versions = isLocalRegistryAvailable ? loVersions : ghVersions; - return versions(item.key, githubToken) .then((json: any) => { return { diff --git a/src/core/listener.ts b/src/core/listener.ts index 9f95781..611bd9b 100644 --- a/src/core/listener.ts +++ b/src/core/listener.ts @@ -26,14 +26,15 @@ function parseAndDecorate(editor: TextEditor) { const config = workspace.getConfiguration("", editor.document.uri); const shouldListPreRels = config.get("crates.listPreReleases"); const basicAuth = config.get("crates.githubAuthBasic"); - const isLocalRegistery = config.get("crates.useLocalCargoIndex"); + const useLocalIndex = config.get("crates.useLocalCargoIndex"); + const localIndexHash = config.get("crates.localCargoIndexHash"); const githubToken = basicAuth ? `Basic ${Buffer.from(basicAuth).toString("base64")}` : undefined; // Handle Promise's catch and normal try/catch the same way with an async closure. (async () => { try { // Parse const dependencies = parseToml(text); - const fetchedDeps = await fetchCrateVersions(dependencies, !!shouldListPreRels, githubToken, isLocalRegistery); + const fetchedDeps = await fetchCrateVersions(dependencies, !!shouldListPreRels, githubToken, useLocalIndex, localIndexHash); decorate(editor, fetchedDeps); } catch (e) { @@ -46,7 +47,7 @@ function parseAndDecorate(editor: TextEditor) { })(); } -export default function listener (editor: TextEditor | undefined): void { +export default function listener(editor: TextEditor | undefined): void { if (editor) { const { fileName } = editor.document; if (fileName.toLocaleLowerCase().endsWith("cargo.toml")) {