Skip to content
Merged
10 changes: 10 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ project.addDevDeps('[email protected]');
project.addDevDeps('@types/node@^18');
project.addDevDeps('jest@^29');
project.addDevDeps('ts-jest@^29');
project.addDevDeps('@types/is-url@^1.2.32');
project.addBundledDeps('js-toml@^0.1.1');
project.addBundledDeps('is-url@^1.2.4');

project.addGitIgnore('target');
project.gitignore.removePatterns('*.tgz');
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ lambda-project
└── main.rs
```

Note that `manifestPath` can be a remote git repository which will be cloned for you, i.e:

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

new RustFunction(stack, 'Rust function', {
manifestPath: 'https://github.com/your_user/your_repo'
});
```

### Runtime

The `RustFunction` uses the `provided.al2023` runtime. If you want to change it, you can use the property `runtime`. The only other valid option is `provided.al2`:
Expand Down
3 changes: 3 additions & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 33 additions & 5 deletions src/cargo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { existsSync, readFileSync } from 'node:fs';
import { mkdirSync, existsSync, readFileSync } from 'node:fs';
import { join, parse } from 'node:path';
import { tmpdir } from 'os';
import { load } from 'js-toml';
import { exec } from './util';

export interface Workspace {
members: string[];
Expand All @@ -17,9 +19,21 @@ export interface Manifest {
}

export function getManifestPath(manifestPath: string): string {
// const manifestPathProp = props.manifestPath ?? 'Cargo.toml';
const parsedManifestPath = parse(manifestPath);
let manifestPathResult: string;
// Determine what type of URL this is and download (git repo) locally
if (isValidUrl(manifestPath)) {
// i.e: 3ed81b4751e8f09bfa39fe743ee143df60304db5 HEAD
const latestCommit = exec('git', ['ls-remote', manifestPath, 'HEAD']).stdout.toString().split(/(\s+)/)[0];
const localPath = join(tmpdir(), latestCommit);
mkdirSync(localPath, { recursive: true });

exec('git', ['clone', '--depth', '1', manifestPath, localPath]);

// Append Cargo.toml to the path
manifestPath = join(localPath, 'Cargo.toml');
}

let manifestPathResult;
let parsedManifestPath = parse(manifestPath);

if (parsedManifestPath.base && parsedManifestPath.ext && parsedManifestPath.base === 'Cargo.toml') {
manifestPathResult = manifestPath;
Expand All @@ -36,7 +50,21 @@ export function getManifestPath(manifestPath: string): string {
return manifestPathResult;
}

function isValidUrl(s: string): boolean {
try {
new URL(s);
return true;
} catch (err) {
return false;
}
}

/**
* Takes in a manifest path and returns the original file if this is not a url.
* Otherwise, it attempts to clone a repository at the URL location, and returns
* a path to the cloned respository.
*/
export function getManifest(manifestPath: string): Manifest {
const data = readFileSync(manifestPath);
return load(data.toString('utf-8')) as Manifest;
}
}
Loading