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

Commit

Permalink
* Retry command added for reloading versions
Browse files Browse the repository at this point in the history
* RegExps removed and migrated to a custom parser.  [#17](#17).
* Additional depencency case added for the test. [#22](#22).
* All failed fetchs will be reported via status mesage and notification with a *retry* command [#23](#23).
* A bug at caching fixed.
  • Loading branch information
serayuzgur committed Jul 28, 2018
1 parent 09f53c5 commit 5bca084
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 93 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
All notable changes to the "crates" extension will be documented in this file.

## 0.3.0
* RegExps removed and migrated to inhouse parser. [#17](https://github.com/serayuzgur/crates/issues/17).
* This is the most complete release so far.
* Retry command added for reloading versions
* RegExps removed and migrated to a custom parser. [#17](https://github.com/serayuzgur/crates/issues/17).
* Additional depencency case added for the test. [#22](https://github.com/serayuzgur/crates/issues/22).
* All failed fetchs will be reported via status mesage and notification with a *retry* command [#23](https://github.com/serayuzgur/crates/issues/23).
* A bug at caching fixed.
* Thanks to [@userzimmermann](https://github.com/userzimmermann) for this release.

## 0.2.4
* Regexp bug fixed by [@userzimmermann](https://github.com/userzimmermann)
Expand Down
29 changes: 8 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,11 @@ It is so **simple** that you do not need any configuration, but if you insist...

## Known Issues

- For **out of line dependencies**

**Supported:**

```toml
[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"
features = [
"winnt",
]
```

**Not Supported:**

```toml
[target.'cfg(target_os = "windows")'.dependencies.winapi]
features = [
"winnt",
]
version = "0.3"
```
- All glitches will be cleared on save.


## Thanks to

[@ademozay](https://github.com/ademozay)

[@userzimmermann](https://github.com/userzimmermann)
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
{
"command": "crates.replaceVersion",
"title": "crates: Replace Version"
},
{
"command": "crates.retry",
"title": "crates: Retry to fetch Cargo.toml"
}
],
"configuration": {
Expand Down
20 changes: 10 additions & 10 deletions sample/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ version = "0.14.0"
readme = "README.md"

[package.metadata.docs.rs]
features = ["icon_loading"]
features = ["icon_loading","osman"]

[features]
icon_loading = ["image"]

[dependencies]
lazy_static = "1.0.1"
libc = "0.2.42"
lazy_static = "0.2.10"
libc = "0.2.24"
image = {version = "0.19.0",optional = true}
futures = "0.1.21"
# futures-await = "0.1.0"
futures-await = "0.1.0"
futures-await = "0.1.1"

[dev-dependencies]
tempdir = "0.3.7"
tempdir = "0.2.0"

[build-dependencies]
gcc = "0.3"
Expand All @@ -31,17 +31,16 @@ objc = "0.2.1"

[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
cocoa = "0.16.0"
cocoa = "0.9.2"
core-foundation = "0.6.0"
core-graphics = "0.5.0"
core-graphics = "0.15.0"
futures = ""

[target.'cfg(unix)'.dev-dependencies]
mio = "0.6"


[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"
features = [
"winnt",
"winuser",
Expand All @@ -56,10 +55,11 @@ features = [
"objbase",
"unknwnbase",
]
version = "0.3.4"

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))'.dependencies]
wayland-client = {version = "0.10.3",features = ["dlopen","egl","cursor"]}
wayland-client = {version = "0.20.4",features = ["dlopen","egl","cursor"]}
smithay-client-toolkit = "0.2.4"
x11-dl = "2"
parking_lot = "0.6.2"
percent-encoding = "1.0.1"
percent-encoding = "1.0.0"
2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const API = "https://crates.io/api/v1";
const data: any = {};

function cache(key: string, func: any, url: string) {
if (!data[key]) {
if (!data[key] || data[key].isRejected()) {
console.log("Fetching dependency: ", key);
data[key] = func(url).then((response: string) => {
try {
Expand Down
22 changes: 0 additions & 22 deletions src/test/extension.test.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/test/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ wayland-client = {version = "0.10.3",features = ["dlopen","egl","cursor"]}
smithay-client-toolkit = "0.2.4"
x11-dl = "2"
parking_lot = "0.6.2"
percent-encoding = "1.0.1"
percent-encoding = "1.0.1"

[dependencies.libc]
version = "0.2.42"
6 changes: 4 additions & 2 deletions src/test/toml/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ suite("parser Tests", function() {
"target.'cfg(unix)'.dev-dependencies",
"target.'cfg(target_os = \"windows\")'.dependencies.winapi",
'target.\'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))\'.dependencies',
"dependencies.libc",
];

assert.equal(doc.values.length, expected.length);
Expand All @@ -49,14 +50,14 @@ suite("parser Tests", function() {
{
const item = doc.values[11];
const section = tomlFile.substring(item.start, item.end);
const desiredSection = tomlFile.substring(1037, 1331);
const desiredSection = tomlFile.substring(1037, 1333);
assert.equal(section, desiredSection);
}
});

test("Read Values", function() {
const doc = parse(tomlFile);
const expected = [3, 1, 1, 5, 1, 1, 1, 1, 5, 1, 2, 5];
const expected = [3, 1, 1, 5, 1, 1, 1, 1, 5, 1, 2, 5,1];

assert.equal(doc.values.length, expected.length);
for (let i = 0; i < expected.length; i++) {
Expand Down Expand Up @@ -102,6 +103,7 @@ suite("parser Tests", function() {
'x11-dl = "2"',
'parking_lot = "0.6.2"',
'percent-encoding = "1.0.1"',
'libc = "0.2.42"',
];

const actual = filterCrates(doc.values);
Expand Down
10 changes: 10 additions & 0 deletions src/toml/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Commands related to TOML files.
*/
import { commands, TextEditor, TextEditorEdit, Range } from "vscode";
import tomlListener from "./listener";

export const status = { inProgress: false };

Expand All @@ -25,4 +26,13 @@ export const replaceVersion = commands.registerTextEditorCommand(
},
);

export const reload = commands.registerTextEditorCommand(
"crates.retry",
(editor: TextEditor, edit: TextEditorEdit, info) => {
if (editor) {
tomlListener(editor);
}
},
);

export default { replaceVersion };
13 changes: 6 additions & 7 deletions src/toml/decorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
MarkdownString,
TextEditorDecorationType,
} from "vscode";

import { Item } from "./parser";
import { Dependency } from "./listener";

export const latestVersion = (text: string) =>
window.createTextEditorDecorationType({
Expand All @@ -36,12 +38,9 @@ function decoration(
// Also handle json valued dependencies

const start = item.start;
const end = item.end ;
const end = item.end;
const currentVersion = item.value;
console.log(editor.document.getText(new Range(
editor.document.positionAt(start),
editor.document.positionAt(end),
), ));

const hasLatest =
versions[0] === currentVersion ||
versions[0].indexOf(`${currentVersion}.`) === 0;
Expand Down Expand Up @@ -83,14 +82,14 @@ function decoration(
*/
export function decorate(
editor: TextEditor,
dependencies: Array<{ item: Item; versions: Array<string> }>,
dependencies: Array<Dependency>,
): TextEditorDecorationType {
const config = workspace.getConfiguration("", editor.document.uri);
const upToDateChar = config.get("crates.upToDateDecorator");
const upToDateDecorator = upToDateChar ? upToDateChar + "" : "";
const options: DecorationOptions[] = [];

dependencies.map((dependency: { item: Item; versions: Array<string> }) => {
dependencies.map((dependency: Dependency) => {
const decor = decoration(
editor,
dependency.item,
Expand Down
78 changes: 51 additions & 27 deletions src/toml/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import {
TextEditorDecorationType,
window,
workspace,
commands,
} from "vscode";
import { parse, filterCrates, Item } from "../toml/parser";
import { statusBarItem } from "../ui/indicators";
import { decorate } from "./decorations";
import { status } from "./commands";
import { versions } from "../api";

export interface Dependency {
item: Item;
versions: Array<string>;
error: string;
}

let decoration: TextEditorDecorationType;

function parseToml(text: string): Item[] {
Expand All @@ -27,39 +34,56 @@ function parseToml(text: string): Item[] {
function fetchCrateVersions(
dependencies: Item[],
shouldListPreRels: boolean,
): Promise<any> {
): Promise<Dependency[]> {
statusBarItem.setText("Fetching crates.io");
const responses = dependencies.map((item: Item) => {
return versions(item.key)
.then((json: any) => {
return {
item,
versions: json.versions.reduce((result: any[], item: any) => {
const isPreRelease =
!shouldListPreRels && item.num.indexOf("-") !== -1;
if (!item.yanked && !isPreRelease) {
result.push(item.num);
}
return result;
}, []),
};
})
.catch((err: Error) => {
console.error(err);
});
});
const responses = dependencies.map(
(item: Item): Dependency => {
return versions(item.key)
.then((json: any) => {
return {
item,
versions: json.versions.reduce((result: any[], item: any) => {
const isPreRelease =
!shouldListPreRels && item.num.indexOf("-") !== -1;
if (!item.yanked && !isPreRelease) {
result.push(item.num);
}
return result;
}, []),
};
})
.catch((error: Error) => {
console.error(error);
statusBarItem.setText(`⚠️ Fetch Error for ${item.key}`);
return { item, error };
});
},
);
return Promise.all(responses);
}

function decorateVersions(
editor: TextEditor,
items: Array<{ item: Item; versions: Array<string> }>,
) {
function decorateVersions(editor: TextEditor, dependencies: Array<Dependency>) {
if (decoration) {
decoration.dispose();
}
decoration = decorate(editor, items);
statusBarItem.setText("OK");
const errors: Array<string> = [];
const filtered = dependencies.filter((dep: Dependency) => {
if (dep && !dep.error) {
return dep;
}
errors.push(`${dep.item.key}`);
});
decoration = decorate(editor, filtered);
if (errors.length) {
window
.showErrorMessage(`Fetch Errors: ${errors.join(" , ")}`, "Retry")
.then(action => {
commands.executeCommand("crates.retry");
});
statusBarItem.setText("⚠️ Completed with errors");
} else {
statusBarItem.setText("OK");
}
}

function parseAndDecorate(editor: TextEditor) {
Expand All @@ -73,7 +97,7 @@ function parseAndDecorate(editor: TextEditor) {

// Fetch Versions
fetchCrateVersions(dependencies, !!shouldListPreRels).then(
decorateVersions.bind(undefined,editor),
decorateVersions.bind(undefined, editor),
);
} catch (e) {
console.error(e);
Expand Down
2 changes: 1 addition & 1 deletion src/toml/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function parseArray(data: string, parent: Item, index: number): number {
while (i++ < data.length) {
const ch = data.charAt(i);

if (buff.length == 0) { item.start = i; }
if (buff.length === 0) { item.start = i; }

if (ch === " " || ch === "\n" || ch === "\t" || ch === "," || isComment) {
if (ch === "\n") {
Expand Down

0 comments on commit 5bca084

Please sign in to comment.