Skip to content

Commit 75ab045

Browse files
authored
Support for cargo.toml files (OctoLinker#858)
* Initial support for cargo.toml files * Adjust plugin name to match the others * Set needsContext to true * Simplify regex creation * Simplify toml parsing * Sort imports * Add some e2e tests * Remove unused parameter * Add githubClasses with an empty array
1 parent c027961 commit 75ab045

File tree

7 files changed

+90
-4
lines changed

7 files changed

+90
-4
lines changed

e2e/fixtures/rust/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "Octolinker"
3+
version = "1.0.0"
4+
5+
[dependencies]
6+
# @OctoLinkerResolve(https://github.com/actix/actix-net)
7+
actix-rt = "1.0.0"
8+
9+
# @OctoLinkerResolve(https://github.com/actix/actix-web)
10+
actix-web = "2.0.0"
11+
12+
# @OctoLinkerResolve(https://github.com/actix/actix-web)
13+
actix-files = "0.2.1"
14+
15+
# @OctoLinkerResolve(https://github.com/rust-lang/futures-rs)
16+
futures = "0.3.1"
17+
18+
# @OctoLinkerResolve(https://github.com/sebasmagri/env_logger)
19+
env_logger = "0.5"
20+
21+
# @OctoLinkerResolve(https://github.com/tokio-rs/bytes)
22+
bytes = "0.5"

package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/load-plugins.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export { default as Python } from '@octolinker/plugin-python';
2020
export { default as RequirementsTxt } from '@octolinker/plugin-requirements-txt';
2121
export { default as Ruby } from '@octolinker/plugin-ruby';
2222
export { default as Rust } from '@octolinker/plugin-rust';
23+
export { default as RustCargo } from '@octolinker/plugin-rust-cargo';
2324
export { default as Sass } from '@octolinker/plugin-sass';
2425
export { default as TypeScript } from '@octolinker/plugin-typescript';
2526
export { default as Vim } from '@octolinker/plugin-vim';

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@octolinker/plugin-requirements-txt": "1.0.0",
3838
"@octolinker/plugin-ruby": "1.0.0",
3939
"@octolinker/plugin-rust": "1.0.0",
40+
"@octolinker/plugin-rust-cargo": "1.0.0",
4041
"@octolinker/plugin-sass": "1.0.0",
4142
"@octolinker/plugin-typescript": "1.0.0",
4243
"@octolinker/plugin-vim": "1.0.0",

packages/helper-regex-builder/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ export function jsonRegExKeyValue(key, value, global = true) {
2222
export function jsonRegExValue(key, value, global = true) {
2323
return regexBuilder(key, value, false, global);
2424
}
25+
26+
export function tomlRegExKeyValue(key, value) {
27+
const regexKey = escapeRegexString(key);
28+
const regexValue = escapeRegexString(value);
29+
30+
return new RegExp(`(${regexKey})\\s*=\\s*"(${regexValue})"`, 'g');
31+
}

packages/plugin-rust-cargo/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as TOML from '@iarna/toml';
2+
import insertLink from '@octolinker/helper-insert-link';
3+
import { tomlRegExKeyValue } from '@octolinker/helper-regex-builder';
4+
import liveResolverQuery from '@octolinker/resolver-live-query';
5+
6+
function linkDependency(blob, key, value) {
7+
const regex = tomlRegExKeyValue(key, value);
8+
return insertLink(blob, regex, this);
9+
}
10+
11+
function processTOML(blob, plugin) {
12+
let results = [];
13+
const json = TOML.parse(blob.toString());
14+
15+
Object.entries(json.dependencies).forEach(([name, version]) => {
16+
const linked = linkDependency.call(plugin, blob, name, version);
17+
18+
results = results.concat(linked);
19+
});
20+
21+
return results;
22+
}
23+
24+
export default {
25+
name: 'RustCargo',
26+
needsContext: true,
27+
28+
resolve(path, values) {
29+
return liveResolverQuery({ type: 'crates', target: values[0] });
30+
},
31+
32+
getPattern() {
33+
return {
34+
pathRegexes: [/cargo\.toml/i],
35+
githubClasses: [],
36+
};
37+
},
38+
39+
parseBlob(blob) {
40+
return processTOML(blob, this);
41+
},
42+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@octolinker/plugin-rust-cargo",
3+
"version": "1.0.0",
4+
"description": "",
5+
"repository": "https://github.com/octolinker/octolinker/tree/master/packages/plugin-rust",
6+
"license": "MIT",
7+
"main": "./index.js",
8+
"dependencies": {
9+
"@iarna/toml": "^2.2.3",
10+
"@octolinker/helper-grammar-regex-collection": "1.0.0",
11+
"@octolinker/helper-insert-link": "1.0.0",
12+
"@octolinker/helper-regex-builder": "1.0.0",
13+
"@octolinker/resolver-live-query": "1.0.0"
14+
}
15+
}

0 commit comments

Comments
 (0)