Skip to content
This repository was archived by the owner on Nov 8, 2019. It is now read-only.

Commit d18fac8

Browse files
committed
Add launch + cargo task refactoring
1 parent 61b3637 commit d18fac8

File tree

9 files changed

+135
-122
lines changed

9 files changed

+135
-122
lines changed

package-lock.json

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

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
"scripts": {
77
"lint": "tslint -c tslint.json 'src/**/*.ts'",
88
"compile": "tsc",
9-
"build": "gulp",
9+
"build": "npm run format && gulp",
1010
"package": "gulp pre-package && tfx extension create --root ./tmp --manifest-globs ./vss-extension.json --output-path ./dist",
11-
"deploy": "tfx extension publish"
11+
"deploy": "tfx extension publish",
12+
"format": "prettier --write \"{src, test}/**/*.ts\""
1213
},
1314
"repository": {
1415
"type": "git",
@@ -42,9 +43,11 @@
4243
"gulp-run": "^1.7.1",
4344
"gulp-typescript": "^5.0.0",
4445
"moment": "^2.24.0",
46+
"prettier": "^1.16.4",
4547
"run-sequence": "^2.2.1",
4648
"tfx-cli": "^0.6.4",
4749
"tslint": "^5.12.1",
50+
"tslint-config-prettier": "^1.18.0",
4851
"typescript": "^3.3.1",
4952
"yargs": "^12.0.5"
5053
}

src/cargo.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
import { getInput, setResult, TaskResult } from "azure-pipelines-task-lib";
2+
13
import {
2-
getInput,
3-
setResult,
4-
TaskResult,
5-
} from "azure-pipelines-task-lib";
4+
Command,
5+
executeCommand
6+
} from "./common/command";
7+
import { launch } from "./common/launch";
8+
9+
const name = getInput("cargoCommand");
10+
const options = getInput("cargoCommandOptions");
611

7-
import executeCommand from "./common/command";
12+
const command: Command = {
13+
args: options.split(" "),
14+
name: name,
15+
tool: "cargo"
16+
}
817

9-
(async (command, options) => {
10-
try {
11-
await executeCommand("cargo", command, options);
12-
setResult(TaskResult.Succeeded, "Task done!");
13-
} catch (e) {
14-
setResult(TaskResult.Failed, e.message);
15-
}
16-
})(
17-
getInput("cargoCommand"),
18-
getInput("cargoCommandOptions"),
19-
);
18+
launch(async () => await executeCommand(command));

src/common/command.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
import {
2-
exec,
3-
which,
4-
} from "azure-pipelines-task-lib";
1+
import { exec, which } from "azure-pipelines-task-lib";
52

6-
import {
7-
addRustToolToPath
8-
} from "./path";
3+
import { addRustToolToPath } from "./path";
94

10-
export default async (tool: string, command: string, args: string, isInput: boolean = false) => {
11-
addRustToolToPath();
12-
13-
const toolArgs = args
14-
? isInput
15-
? [...args.split(" "), command]
16-
: [command, ...args.split(" ")]
17-
: command;
5+
type Command = {
6+
tool: string,
7+
name: string,
8+
args: string[]
9+
};
1810

19-
return which("cargo")
20-
? await exec(tool, toolArgs) > 0
21-
? Promise.reject(new Error("An error has occured."))
22-
: Promise.resolve()
23-
: Promise.reject(new Error("Rust toolchains are not available."));
11+
const executeCommand = async (command: Command) => {
12+
addRustToolToPath();
13+
const toolArgs = [command.name, ...command.args];
14+
return which("cargo")
15+
? (await exec(command.tool, toolArgs)) > 0
16+
? Promise.reject(new Error("An error has occured."))
17+
: Promise.resolve()
18+
: Promise.reject(new Error("Rust toolchains are not available."));
2419
};
20+
21+
export {
22+
Command,
23+
executeCommand
24+
}
25+
26+
/*const toolArgs = args
27+
? isInput
28+
? [...args.split(" "), command]
29+
: [command, ...args.split(" ")]
30+
: command;*/

src/common/launch.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { setResult, TaskResult } from "azure-pipelines-task-lib";
2+
3+
const launch = async (task: (...parameters: any[]) => Promise<void>) => {
4+
try {
5+
await task();
6+
setResult(TaskResult.Succeeded, "Task done!");
7+
} catch (e) {
8+
setResult(TaskResult.Failed, e.message);
9+
}
10+
};
11+
12+
export {
13+
launch
14+
}

src/common/path.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@ import process from "process";
33
import { homedir } from "os";
44

55
const addRustToolToPath = () => {
6-
const toolPath = getToolPath();
7-
const path = process.env.PATH;
8-
process.env.PATH = !path
9-
? toolPath
10-
: combineToPath(path, toolPath);
6+
const toolPath = getToolPath();
7+
const path = process.env.PATH;
8+
process.env.PATH = !path ? toolPath : combineToPath(path, toolPath);
119
};
1210

1311
const combineToPath = (path: string, toolPath: string) => {
14-
return isInPath(path, toolPath)
15-
? path
16-
: `${ toolPath }${ delimiter }${ path }`;
12+
return isInPath(path, toolPath) ? path : `${toolPath}${delimiter}${path}`;
1713
};
1814

1915
const isInPath = (path: string, toolPath: string) => {
20-
return !!path && path.indexOf(toolPath) !== -1
16+
return !!path && path.indexOf(toolPath) !== -1;
2117
};
2218

2319
const getToolPath = () => {
24-
return join(homedir(), ".cargo", "bin");
20+
return join(homedir(), ".cargo", "bin");
2521
};
2622

27-
export {
28-
addRustToolToPath
29-
}
23+
export { addRustToolToPath };

src/install.ts

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
11
import {
2-
debug,
3-
exec,
4-
getBoolInput,
5-
setResult,
6-
TaskResult,
7-
tool,
8-
which,
2+
debug,
3+
exec,
4+
getBoolInput,
5+
setResult,
6+
TaskResult,
7+
tool,
8+
which
99
} from "azure-pipelines-task-lib";
1010
import { addRustToolToPath } from "./common/path";
1111
import executeCommand from "./common/command";
1212

1313
(async (installNightly: boolean) => {
14-
try {
15-
addRustToolToPath();
14+
try {
15+
addRustToolToPath();
1616

17-
const returnCode = which("rustup")
18-
? await update()
19-
: await downloadAndInstall();
17+
const returnCode = which("rustup")
18+
? await update()
19+
: await downloadAndInstall();
2020

21-
if (installNightly) {
22-
await executeCommand("rustup", "install", "nightly");
23-
await executeCommand("rustup", "default", "nightly");
24-
setResult(TaskResult.Succeeded, "Rust nightly installed");
25-
} else {
26-
setUpdateResult(returnCode);
27-
}
28-
} catch (e) {
29-
setResult(TaskResult.Failed, e.message);
21+
if (installNightly) {
22+
await executeCommand("rustup", "install", "nightly");
23+
await executeCommand("rustup", "default", "nightly");
24+
setResult(TaskResult.Succeeded, "Rust nightly installed");
25+
} else {
26+
setUpdateResult(returnCode);
3027
}
28+
} catch (e) {
29+
setResult(TaskResult.Failed, e.message);
30+
}
3131
})(getBoolInput("installNightly"));
3232

3333
async function downloadAndInstall() {
34-
debug("Rustup not available.");
35-
return await tool(which("curl"))
36-
.arg("https://sh.rustup.rs")
37-
.arg("-sSf")
38-
.pipeExecOutputToTool(tool(which("sh"))
39-
.arg("-s")
40-
.arg("--")
41-
.arg("-y"))
42-
.exec();
34+
debug("Rustup not available.");
35+
return await tool(which("curl"))
36+
.arg("https://sh.rustup.rs")
37+
.arg("-sSf")
38+
.pipeExecOutputToTool(
39+
tool(which("sh"))
40+
.arg("-s")
41+
.arg("--")
42+
.arg("-y")
43+
)
44+
.exec();
4345
}
4446

4547
async function update() {
46-
debug("Rustup available.");
47-
return await exec("rustup", "update");
48+
debug("Rustup available.");
49+
return await exec("rustup", "update");
4850
}
4951

5052
function setUpdateResult(returnCode: Readonly<number>) {
51-
debug(`Return code: ${returnCode}`);
52-
const updated = returnCode === 0;
53-
setResult(
54-
updated
55-
? TaskResult.Succeeded
56-
: TaskResult.Failed,
57-
updated
58-
? "Rust updated."
59-
: "Rustup update failed.");
53+
debug(`Return code: ${returnCode}`);
54+
const updated = returnCode === 0;
55+
setResult(
56+
updated ? TaskResult.Succeeded : TaskResult.Failed,
57+
updated ? "Rust updated." : "Rustup update failed."
58+
);
6059
}

src/rustc.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
import {
2-
getInput,
3-
setResult,
4-
TaskResult,
5-
} from "azure-pipelines-task-lib";
1+
import { getInput, setResult, TaskResult } from "azure-pipelines-task-lib";
62

73
import executeCommand from "./common/command";
84

95
(async (options, input) => {
10-
try {
11-
await executeCommand("rustc", input, options, true);
12-
setResult(TaskResult.Succeeded, "Task done!");
13-
} catch (e) {
14-
setResult(TaskResult.Failed, e.message);
15-
}
16-
})(
17-
getInput("rustcOptions"),
18-
getInput("rustcInput"),
19-
);
6+
try {
7+
await executeCommand("rustc", input, options, true);
8+
setResult(TaskResult.Succeeded, "Task done!");
9+
} catch (e) {
10+
setResult(TaskResult.Failed, e.message);
11+
}
12+
})(getInput("rustcOptions"), getInput("rustcInput"));

src/rustup.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
import {
2-
getInput,
3-
setResult,
4-
TaskResult,
5-
} from "azure-pipelines-task-lib";
1+
import { getInput, setResult, TaskResult } from "azure-pipelines-task-lib";
62

73
import executeCommand from "./common/command";
84

95
(async (command, args) => {
10-
try {
11-
await executeCommand("rustup", command, args);
12-
setResult(TaskResult.Succeeded, "Task done!");
13-
} catch (e) {
14-
setResult(TaskResult.Failed, e.message);
15-
}
16-
})(
17-
getInput("rustupCommand"),
18-
getInput("rustupCommandArguments"),
19-
);
6+
try {
7+
await executeCommand("rustup", command, args);
8+
setResult(TaskResult.Succeeded, "Task done!");
9+
} catch (e) {
10+
setResult(TaskResult.Failed, e.message);
11+
}
12+
})(getInput("rustupCommand"), getInput("rustupCommandArguments"));

0 commit comments

Comments
 (0)