diff --git a/lib/bundling.ts b/lib/bundling.ts index 50a421c..89d72c7 100644 --- a/lib/bundling.ts +++ b/lib/bundling.ts @@ -6,16 +6,24 @@ import { Code, Runtime, } from 'aws-cdk-lib/aws-lambda'; -import { dirname } from 'path'; +import * as fs from 'fs'; +import * as path from 'path'; +import { build } from './build'; /** * Options for bundling */ export interface BundlingProps extends DockerRunOptions { /** - * CDK output (staging) directory for the lambda function + * Path to the directory that contains the project to be built; i.e., the + * directory containing `Cargo.toml`. */ - readonly handlerDir: string; + readonly entry: string; + + /** + * Executable name. + */ + readonly bin?: string; /** * The runtime of the lambda function @@ -26,6 +34,11 @@ export interface BundlingProps extends DockerRunOptions { * The system architecture of the lambda function */ readonly architecture: Architecture; + + /** + * Target of `cargo build`. + */ + readonly target: string; } /** @@ -35,7 +48,7 @@ export class Bundling implements cdk.BundlingOptions { public static bundle(options: BundlingProps): AssetCode { const bundling = new Bundling(options); - return Code.fromAsset(dirname(options.handlerDir), { + return Code.fromAsset(options.entry, { assetHashType: cdk.AssetHashType.OUTPUT, bundling: { image: bundling.image, @@ -54,9 +67,22 @@ export class Bundling implements cdk.BundlingOptions { this.image = cdk.DockerImage.fromRegistry('dummy'); // Do not build if we don't need to this.local = { - tryBundle(outputDir: string) { - // TODO - console.log(`BUNDLING...: ${outputDir}`); + tryBundle(outDir: string) { + console.log(`BUNDLING...: ${outDir}`); + build({ + entry: props.entry, + bin: props.bin, + target: props.target, + outDir, + }); + // moves /bootstrap → ./bootstrap + // and cleans up the empty folder + const binDir = path.join(outDir, props.bin!); + fs.renameSync( + path.join(binDir, 'bootstrap'), + path.join(outDir, 'bootstrap') + ); + fs.rmdirSync(binDir); return true; }, diff --git a/lib/function.ts b/lib/function.ts index bb210b1..4432991 100644 --- a/lib/function.ts +++ b/lib/function.ts @@ -3,8 +3,8 @@ import { Construct } from 'constructs'; import { join } from 'path'; import { performance } from 'perf_hooks'; import { Settings } from '.'; -import { BaseBuildProps, build } from './build'; -// import { Bundling } from './bundling'; +import { BaseBuildProps } from './build'; +import { Bundling } from './bundling'; import { Code, Function, @@ -99,27 +99,6 @@ export class RustFunction extends Function { 'T' ); - // Build with `cargo-lambda` - if (shouldBuild && !Settings.SKIP_BUILD) { - let start = performance.now(); - - build({ - ...props, - entry, - bin: binName, - target: target, - outDir: buildDir, - }); - - logTime(start, `🎯 Cross-compile \`${executable}\``); - } - // Else, skip the build (or bundle) step. - // - // At a minimum, we need to ensure the output directory - // exists -- otherwise, CDK complains that it can't - // locate the asset. - else ensureDirExists(handlerDir); - let lambdaEnv = props.environment; // Sets up logging if needed. // Ref: https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html @@ -136,12 +115,13 @@ export class RustFunction extends Function { ...props, runtime: Settings.RUNTIME, architecture: arch, - code: Code.fromAsset(handlerDir), - // code: Bundling.bundle({ - // handlerDir, - // runtime: Settings.RUNTIME, - // architecture: arch, - // }), + code: Bundling.bundle({ + entry, + bin: binName, + runtime: Settings.RUNTIME, + architecture: arch, + target, + }), handler: handler, environment: lambdaEnv, });