Skip to content

Commit 9885f35

Browse files
authored
Add profile property to bundle with different profiles. (#49)
Signed-off-by: David Calavera <[email protected]>
1 parent 63274fe commit 9885f35

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

API.md

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

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,26 @@ new RustFunction(this, 'Rust function', {
8585
});
8686
```
8787

88+
### Cargo Build profiles
89+
90+
Use the `profile` option if you want to build with a different Cargo profile that's not `release`:
91+
92+
```ts
93+
import { RustFunction } from 'cargo-lambda-cdk';
94+
95+
new RustFunction(this, 'Rust function', {
96+
manifestPath: 'path/to/package/directory/with/Cargo.toml',
97+
bundling: {
98+
profile: 'dev'
99+
},
100+
});
101+
```
102+
88103
### Cargo Lambda Build flags
89104

90105
Use the `cargoLambdaFlags` option to add additional flags to the `cargo lambda build` command that's executed to bundle your function. You don't need to use this flag to set options like the target architecture or the binary to compile, since the construct infers those from other props.
91106

92-
If these flags include a `--target` flag, it will override the `architecture` option. If these flags include a `--release` or `--debug` flag, it will override the CDK's debug option.
107+
If these flags include a `--target` flag, it will override the `architecture` option. If these flags include a `--release` or `--profile` flag, it will override the release or any other profile specified.
93108

94109
```ts
95110
import { RustFunction } from 'cargo-lambda-cdk';

src/bundling.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface CommandOptions {
4343
readonly architecture?: Architecture;
4444
readonly lambdaExtension?: boolean;
4545
readonly cargoLambdaFlags: string[];
46+
readonly profile: string;
4647
readonly manifest: Manifest;
4748
}
4849

@@ -99,12 +100,14 @@ export class Bundling implements cdk.BundlingOptions {
99100
const manifest = getManifest(props.manifestPath);
100101

101102
const cargoLambdaFlags = props.cargoLambdaFlags ?? [];
103+
const profile = props.profile ?? 'release';
102104

103105
const osPlatform = platform();
104106
const bundlingCommand = this.createBundlingCommand({
105107
osPlatform,
106108
manifest,
107109
cargoLambdaFlags,
110+
profile,
108111
outputDir: cdk.AssetStaging.BUNDLING_OUTPUT_DIR,
109112
inputDir: cdk.AssetStaging.BUNDLING_INPUT_DIR,
110113
binaryName: props.binaryName,
@@ -123,6 +126,7 @@ export class Bundling implements cdk.BundlingOptions {
123126
manifest,
124127
outputDir,
125128
cargoLambdaFlags,
129+
profile,
126130
inputDir: projectRoot,
127131
binaryName: props.binaryName,
128132
architecture: props.architecture,
@@ -170,9 +174,14 @@ export class Bundling implements cdk.BundlingOptions {
170174
props.outputDir,
171175
];
172176

173-
if (!props.cargoLambdaFlags.includes('--release') &&
174-
!props.cargoLambdaFlags.includes('--debug')) {
175-
buildBinary.push('--release');
177+
if (!props.cargoLambdaFlags.includes('--profile')
178+
&& !props.cargoLambdaFlags.includes('--release')) {
179+
if (props.profile === 'release') {
180+
buildBinary.push('--release');
181+
} else {
182+
buildBinary.push('--profile');
183+
buildBinary.push(props.profile);
184+
}
176185
}
177186

178187
if (props.lambdaExtension) {

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ export interface BundlingOptions {
180180
* Additional list of flags to pass to `cargo lambda build`.
181181
*/
182182
readonly cargoLambdaFlags?: string[];
183+
184+
/**
185+
* Specify the Cargo Build profile to use.
186+
*
187+
* @default - `release`
188+
*/
189+
readonly profile?: string;
183190
}
184191

185192
/**

test/bundlingOptions.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,63 @@ describe('bundlingOptionsOverrideDefaults', () => {
145145

146146
expect((bundlingOptions as any).options.bundling.command).toContain(command);
147147
});
148+
149+
describe('Add Cargo Lambda Build with release flag', () => {
150+
const bundlingOptions = Bundling.bundle({
151+
manifestPath: getTestManifestPath(),
152+
forcedDockerBundling: true,
153+
cargoLambdaFlags: ['--release'],
154+
});
155+
156+
const command = 'cargo lambda build --lambda-dir /asset-output --flatten simple-package --release';
157+
158+
expect((bundlingOptions as any).options.bundling.command).toContain(command);
159+
});
160+
161+
describe('Add Cargo Lambda Build with profile flag', () => {
162+
const bundlingOptions = Bundling.bundle({
163+
manifestPath: getTestManifestPath(),
164+
forcedDockerBundling: true,
165+
cargoLambdaFlags: ['--profile', 'dev'],
166+
});
167+
168+
const command = 'cargo lambda build --lambda-dir /asset-output --flatten simple-package --profile dev';
169+
170+
expect((bundlingOptions as any).options.bundling.command).toContain(command);
171+
});
172+
173+
describe('Add Cargo Lambda Build without profile info', () => {
174+
const bundlingOptions = Bundling.bundle({
175+
manifestPath: getTestManifestPath(),
176+
forcedDockerBundling: true,
177+
});
178+
179+
const command = 'cargo lambda build --lambda-dir /asset-output --release --flatten simple-package';
180+
181+
expect((bundlingOptions as any).options.bundling.command).toContain(command);
182+
});
183+
184+
describe('Add Cargo Lambda Build with profile info', () => {
185+
const bundlingOptions = Bundling.bundle({
186+
manifestPath: getTestManifestPath(),
187+
forcedDockerBundling: true,
188+
profile: 'test',
189+
});
190+
191+
const command = 'cargo lambda build --lambda-dir /asset-output --profile test --flatten simple-package';
192+
193+
expect((bundlingOptions as any).options.bundling.command).toContain(command);
194+
});
195+
196+
describe('Add Cargo Lambda Build with release profile info', () => {
197+
const bundlingOptions = Bundling.bundle({
198+
manifestPath: getTestManifestPath(),
199+
forcedDockerBundling: true,
200+
profile: 'release',
201+
});
202+
203+
const command = 'cargo lambda build --lambda-dir /asset-output --release --flatten simple-package';
204+
205+
expect((bundlingOptions as any).options.bundling.command).toContain(command);
206+
});
148207
});

0 commit comments

Comments
 (0)