Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set default handler and runtime #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions sls-rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class SlsRust {
throw new SlsRustPluginNoRustFnsError()
}

this.log(`Building Rust funcs...`)
await this.runBuildCommand()

const buildPromises = rustFns.map(fnName => {
const fn = this.serverless.service.getFunction(fnName)
return this.build(fn)
Expand All @@ -55,14 +58,14 @@ class SlsRust {

}

async runBuildCommand ({ path, projectName }) {
async runBuildCommand () {
try {
await this.runCommand({
command: `cargo build --release --target ${this.targetRuntime}`,
cwd: path,
cwd: '.',
})
} catch (error) {
throw new Error(`Error building project ${projectName}: ${error}`)
throw new Error(`Error building rust functions: ${error}`)
}
}

Expand All @@ -86,27 +89,17 @@ class SlsRust {
}

async build (fn) {
const { projectPath, projectName } = this.getProjectPathAndName(fn)
this.log(`Building Rust ${fn.handler} func...`)
const path = join('.', projectPath)
const targetPath = join(path, 'target', this.targetRuntime, 'release')
await this.runBuildCommand({ path, projectName })
const projectName = fn.handler
const targetPath = join('.', 'target', this.targetRuntime, 'release')
await this.runZipArtifact({ path: targetPath, projectName })

const artifactPath = join(targetPath, `${projectName}.zip`)
fn.package = fn.package || {}
fn.package.artifact = artifactPath
fn.handler = 'bootstrap'
fn.runtime = 'provided.al2'
this.log(`Finished building ${projectName}!`)
}

getProjectPathAndName (fn) {
const [projectPath, projectName] = fn.handler.split('.')
if (!projectPath || !projectName) {
throw new SlsRustPluginWrongHandlerError()
}

return { projectPath, projectName }
}
}

class SlsRustPluginMainError extends Error {
Expand All @@ -130,7 +123,6 @@ class SlsRustPluginNoRustFnsError extends SlsRustPluginMainError {
functions:
rust:
handler: your_rust_project_name
runtime: provided.al2
tags:
rust: true
`
Expand Down