Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit fcc0a21

Browse files
committed
Support alternative local index #109
1 parent f7d92d2 commit fcc0a21

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to the "crates" extension will be documented in this file.
88
- Comma handled for ranged versions [#117](https://github.com/serayuzgur/crates/issues/117)
99
- Fix for comments with brackets inside arrays [#115](https://github.com/serayuzgur/crates/pull/115)
1010
- ${version} is not being replaced for compatible decorator [#118](https://github.com/serayuzgur/crates/issues/118)
11+
- Support alternative local index [#109](https://github.com/serayuzgur/crates/issues/109)
1112

1213
## 0.5.5
1314

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ It is so **simple** that you do not need any configuration, but if you insist...
3939

4040
`crates.useLocalCargoIndex`: If true, crates will use local cargo repository.
4141

42+
`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.
43+
4244
`crates.githubAuthBasic`: The `<username>:<personal-access-token>` or `<username>:<password>` for accessing Github API with increased access rates 5000 req/h.
4345

44-
`crates.errorDecorator`: The text to show when a dependency has errors. Default is ❗️.
46+
`crates.errorDecorator`: The text to show when a dependency has errors. Default is ❗️❗️❗️.
4547

46-
`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}'
48+
`crates.compatibleDecorator`: The text template to show when a dependency is semver compatible. ${version} will be replaced by the latest version info. Default is ''
4749

48-
`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}'
50+
`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}'
4951

5052
`crates.listPreReleases`: If true, pre-release versions will be listed in hover and at decoration. The default is false.
5153

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
"scope": "resource",
9898
"default": true,
9999
"description": "If true, crates will use local cargo index. (Requires git, cargo and updated crates.io index)"
100+
},
101+
"crates.localCargoIndexHash": {
102+
"type": "string",
103+
"scope": "resource",
104+
"default": "github.com-1ecc6299db9ec823",
105+
"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"
100106
}
101107
}
102108
}

src/api/local_registry.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { decidePath, parseVersions } from "./index-utils";
1414
const exec = util.promisify(require("child_process").exec);
1515

1616
// check for the crates index. If none found switch to github and show error
17-
const cargoHome = getCargoPath()
17+
const cargoHome = getCargoPath();
1818

1919
function getCargoPath() {
2020
// Trailing slash on macos (does not want / at the end) and windows (needs / at end)
@@ -25,11 +25,14 @@ function getCargoPath() {
2525

2626

2727

28-
const gitDir = path.resolve(cargoHome, "registry/index/github.com-1ecc6299db9ec823/.git/");
28+
let gitDir = path.resolve(cargoHome, "registry/index/github.com-1ecc6299db9ec823/.git/");
2929

3030

3131

32-
export function checkCargoRegistry() {
32+
export function checkCargoRegistry(localIndexHash?: string) {
33+
if (localIndexHash) {
34+
gitDir = path.resolve(cargoHome, `registry/index/${localIndexHash}/.git/`);
35+
}
3336
return fs.existsSync(gitDir);
3437
}
3538

src/core/fetcher.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ export function fetchCrateVersions(
99
dependencies: Item[],
1010
shouldListPreRels: boolean,
1111
githubToken?: string,
12-
isLocalRegistry?: boolean): Promise<Dependency[]> {
12+
useLocalIndex?: boolean,
13+
localIndexHash?: string
14+
): Promise<Dependency[]> {
1315
statusBarItem.setText("👀 Fetching crates.io");
16+
const isLocalIndexAvailable = useLocalIndex && checkCargoRegistry(localIndexHash);
17+
const versions = isLocalIndexAvailable ? loVersions : ghVersions;
18+
1419
const responses = dependencies.map(
1520
(item: Item): Promise<Dependency> => {
1621
// Check settings and if local registry enabled control cargo home. Fallback is the github index.
17-
const isLocalRegistryAvailable = isLocalRegistry && checkCargoRegistry();
18-
const versions = isLocalRegistryAvailable ? loVersions : ghVersions;
19-
2022
return versions(item.key, githubToken)
2123
.then((json: any) => {
2224
return {

src/core/listener.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ function parseAndDecorate(editor: TextEditor) {
2626
const config = workspace.getConfiguration("", editor.document.uri);
2727
const shouldListPreRels = config.get("crates.listPreReleases");
2828
const basicAuth = config.get<string>("crates.githubAuthBasic");
29-
const isLocalRegistery = config.get<boolean>("crates.useLocalCargoIndex");
29+
const useLocalIndex = config.get<boolean>("crates.useLocalCargoIndex");
30+
const localIndexHash = config.get<string>("crates.localCargoIndexHash");
3031
const githubToken = basicAuth ? `Basic ${Buffer.from(basicAuth).toString("base64")}` : undefined;
3132
// Handle Promise's catch and normal try/catch the same way with an async closure.
3233
(async () => {
3334
try {
3435
// Parse
3536
const dependencies = parseToml(text);
36-
const fetchedDeps = await fetchCrateVersions(dependencies, !!shouldListPreRels, githubToken, isLocalRegistery);
37+
const fetchedDeps = await fetchCrateVersions(dependencies, !!shouldListPreRels, githubToken, useLocalIndex, localIndexHash);
3738

3839
decorate(editor, fetchedDeps);
3940
} catch (e) {
@@ -46,7 +47,7 @@ function parseAndDecorate(editor: TextEditor) {
4647
})();
4748
}
4849

49-
export default function listener (editor: TextEditor | undefined): void {
50+
export default function listener(editor: TextEditor | undefined): void {
5051
if (editor) {
5152
const { fileName } = editor.document;
5253
if (fileName.toLocaleLowerCase().endsWith("cargo.toml")) {

0 commit comments

Comments
 (0)