Skip to content

Commit 7cd44a1

Browse files
committed
upgrade rust to 1.32.0
1 parent 38c0dc6 commit 7cd44a1

File tree

3 files changed

+888
-524
lines changed

3 files changed

+888
-524
lines changed

index.js

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
'use strict';
1+
"use strict";
22

33
// https://serverless.com/blog/writing-serverless-plugins/
44
// https://serverless.com/framework/docs/providers/aws/guide/plugins/
55
// https://github.com/softprops/lambda-rust/
66

7-
const { spawnSync } = require('child_process');
8-
const path = require('path');
7+
const { spawnSync } = require("child_process");
8+
const path = require("path");
99

10-
const DEFAULT_DOCKER_TAG = '0.2.0-rust-1.31.0';
11-
const RUST_RUNTIME = 'rust';
12-
const BASE_RUNTIME = 'provided';
13-
const NO_OUTPUT_CAPTURE = { stdio: ['ignore', process.stdout, process.stderr] };
10+
const DEFAULT_DOCKER_TAG = "0.2.0-rust-1.32.0";
11+
const RUST_RUNTIME = "rust";
12+
const BASE_RUNTIME = "provided";
13+
const NO_OUTPUT_CAPTURE = { stdio: ["ignore", process.stdout, process.stderr] };
1414

1515
/** assumes docker is on the host's execution path */
1616
class RustPlugin {
1717
constructor(serverless, options) {
18-
1918
this.serverless = serverless;
2019
this.options = options;
21-
this.servicePath = this.serverless.config.servicePath || '';
20+
this.servicePath = this.serverless.config.servicePath || "";
2221
this.hooks = {
23-
'before:package:createDeploymentArtifacts': this.build.bind(this),
24-
'before:deploy:function:packageFunction': this.build.bind(this),
22+
"before:package:createDeploymentArtifacts": this.build.bind(this),
23+
"before:deploy:function:packageFunction": this.build.bind(this)
2524
};
2625
this.custom = Object.assign(
2726
{
2827
cargoFlags: "",
2928
dockerTag: DEFAULT_DOCKER_TAG
3029
},
31-
this.serverless.service.custom && this.serverless.service.custom.rust || {}
30+
(this.serverless.service.custom && this.serverless.service.custom.rust) ||
31+
{}
3232
);
3333

3434
// By default, Serverless examines node_modules to figure out which
@@ -41,52 +41,51 @@ class RustPlugin {
4141

4242
runDocker(funcArgs, cargoPackage) {
4343
const defaultArgs = [
44-
'run',
45-
'--rm',
46-
'-t',
47-
`-v`, `${this.servicePath}:/code`,
48-
`-v`, `${process.env['HOME']}/.cargo/registry:/root/.cargo/registry`,
49-
`-v`, `${process.env['HOME']}/.cargo/git:/root/.cargo/git`,
44+
"run",
45+
"--rm",
46+
"-t",
47+
`-v`,
48+
`${this.servicePath}:/code`,
49+
`-v`,
50+
`${process.env["HOME"]}/.cargo/registry:/root/.cargo/registry`,
51+
`-v`,
52+
`${process.env["HOME"]}/.cargo/git:/root/.cargo/git`
5053
];
5154
const customArgs = [];
5255
let cargoFlags = (funcArgs || {}).cargoFlags || this.custom.cargoFlags;
5356
if (cargoPackage != undefined) {
5457
if (cargoFlags) {
55-
cargoFlags = `${cargoFlags} -p ${cargoPackage}`
58+
cargoFlags = `${cargoFlags} -p ${cargoPackage}`;
5659
} else {
5760
cargoFlags = ` -p ${cargoPackage}`;
5861
}
5962
}
6063
if (cargoFlags) {
6164
// --features awesome-feature, ect
62-
customArgs.push('-e', `CARGO_FLAGS=${cargoFlags}`);
63-
};
65+
customArgs.push("-e", `CARGO_FLAGS=${cargoFlags}`);
66+
}
6467
const dockerTag = (funcArgs || {}).dockerTag || this.custom.dockerTag;
6568
return spawnSync(
66-
'docker',
67-
[
68-
...defaultArgs,
69-
...customArgs,
70-
`softprops/lambda-rust:${dockerTag}`
71-
],
69+
"docker",
70+
[...defaultArgs, ...customArgs, `softprops/lambda-rust:${dockerTag}`],
7271
NO_OUTPUT_CAPTURE
7372
);
7473
}
7574

7675
functions() {
7776
if (this.options.function) {
78-
return [this.options.function];
77+
return [this.options.function];
7978
} else {
80-
return this.serverless.service.getAllFunctions();
79+
return this.serverless.service.getAllFunctions();
8180
}
8281
}
8382

8483
build() {
8584
const service = this.serverless.service;
86-
if (service.provider.name != 'aws') {
85+
if (service.provider.name != "aws") {
8786
return;
8887
}
89-
let rustFunctionsFound = false
88+
let rustFunctionsFound = false;
9089
this.functions().forEach(funcName => {
9190
const func = service.getFunction(funcName);
9291
const runtime = func.runtime || service.provider.runtime;
@@ -95,14 +94,18 @@ class RustPlugin {
9594
return;
9695
}
9796
rustFunctionsFound = true;
98-
let [cargoPackage, binary] = func.handler.split('.');
97+
let [cargoPackage, binary] = func.handler.split(".");
9998
if (binary == undefined) {
10099
binary = cargoPackage;
101100
}
102101
this.serverless.cli.log(`Building native Rust ${func.handler} func...`);
103102
const res = this.runDocker(func.rust, cargoPackage);
104103
if (res.error || res.status > 0) {
105-
this.serverless.cli.log(`Dockerized Rust build encountered an error: ${res.error} ${res.status}.`);
104+
this.serverless.cli.log(
105+
`Dockerized Rust build encountered an error: ${res.error} ${
106+
res.status
107+
}.`
108+
);
106109
throw new Error(res.error);
107110
}
108111
// If all went well, we should now have find a packaged compiled binary under `target/lambda/release`.
@@ -114,26 +117,26 @@ class RustPlugin {
114117
// we leverage the ability to declare a package artifact directly
115118
// see https://serverless.com/framework/docs/providers/aws/guide/packaging/
116119
// for more information
117-
const artifactPath = path.join('target/lambda/release', binary + ".zip")
120+
const artifactPath = path.join("target/lambda/release", binary + ".zip");
118121
func.package = func.package || {};
119122
func.package.artifact = artifactPath;
120123

121124
// Ensure the runtime is set to a sane value for other plugins
122125
if (func.runtime == RUST_RUNTIME) {
123-
func.runtime = BASE_RUNTIME
126+
func.runtime = BASE_RUNTIME;
124127
}
125-
})
128+
});
126129
if (service.provider.runtime === RUST_RUNTIME) {
127130
service.provider.runtime = BASE_RUNTIME;
128131
}
129132
if (!rustFunctionsFound) {
130133
throw new Error(
131134
`Error: no Rust functions found. ` +
132-
`Use 'runtime: ${RUST_RUNTIME}' in global or ` +
133-
`function configuration to use this plugin.`
135+
`Use 'runtime: ${RUST_RUNTIME}' in global or ` +
136+
`function configuration to use this plugin.`
134137
);
135138
}
136139
}
137140
}
138141

139-
module.exports = RustPlugin;
142+
module.exports = RustPlugin;

0 commit comments

Comments
 (0)