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

Commit

Permalink
Support alternative local index #109
Browse files Browse the repository at this point in the history
  • Loading branch information
serayuzgur committed Feb 13, 2021
1 parent f7d92d2 commit fcc0a21
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<username>:<personal-access-token>` or `<username>:<password>` 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.

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/api/local_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down
10 changes: 6 additions & 4 deletions src/core/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ export function fetchCrateVersions(
dependencies: Item[],
shouldListPreRels: boolean,
githubToken?: string,
isLocalRegistry?: boolean): Promise<Dependency[]> {
useLocalIndex?: boolean,
localIndexHash?: string
): Promise<Dependency[]> {
statusBarItem.setText("👀 Fetching crates.io");
const isLocalIndexAvailable = useLocalIndex && checkCargoRegistry(localIndexHash);
const versions = isLocalIndexAvailable ? loVersions : ghVersions;

const responses = dependencies.map(
(item: Item): Promise<Dependency> => {
// 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 {
Expand Down
7 changes: 4 additions & 3 deletions src/core/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>("crates.githubAuthBasic");
const isLocalRegistery = config.get<boolean>("crates.useLocalCargoIndex");
const useLocalIndex = config.get<boolean>("crates.useLocalCargoIndex");
const localIndexHash = config.get<string>("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) {
Expand All @@ -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")) {
Expand Down

0 comments on commit fcc0a21

Please sign in to comment.