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

feat(sdk): addfile() is a preflight method which adds file from absolute path #3801

Merged
merged 29 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
78e4d96
Implemented preflight method addfile() functionality
g3Bg2 Aug 13, 2023
d507b4b
Added instructions for addfile method in docs
g3Bg2 Aug 13, 2023
4f30ccc
Correcting for PR comments
g3Bg2 Aug 15, 2023
6f9bff0
made changes to docs and added test files
g3Bg2 Aug 15, 2023
5ce490c
Update bucket.ts
g3Bg2 Aug 17, 2023
a1c867f
chore: self mutation
monadabot Aug 17, 2023
1ca5e58
Merge branch 'winglang:main' into akhil-addfile
g3Bg2 Aug 17, 2023
322bcc5
Merge branch 'main' into akhil-addfile
g3Bg2 Aug 18, 2023
8faf5ba
Merge branch 'winglang:main' into akhil-addfile
g3Bg2 Aug 18, 2023
0c42bd7
Merge branch 'winglang:main' into akhil-addfile
g3Bg2 Aug 18, 2023
6c6a0cf
Merge branch 'winglang:main' into akhil-addfile
g3Bg2 Aug 19, 2023
0a1580f
fixing paths and entrypointDir
tsuf239 Aug 20, 2023
9d085ec
chore: self mutation
monadabot Aug 20, 2023
15b79c6
Merge remote-tracking branch 'upstream/main' into akhil-addfile
tsuf239 Aug 20, 2023
c10756e
Merge branch 'akhil-addfile' of https://github.com/0018akhil/wing int…
tsuf239 Aug 20, 2023
e6e9ef0
Merge branch 'winglang:main' into akhil-addfile
g3Bg2 Aug 20, 2023
d4471f8
Fixed indentation and added @param to addfile method
g3Bg2 Aug 20, 2023
12e5d2b
Removed License file
g3Bg2 Aug 20, 2023
41e7338
Removed License file
g3Bg2 Aug 20, 2023
6fc4e01
Indentation fix
g3Bg2 Aug 20, 2023
fb62218
Merge branch 'winglang:main' into akhil-addfile
g3Bg2 Aug 20, 2023
8d0951e
Indentation fix
g3Bg2 Aug 20, 2023
3541a8e
chore: self mutation
monadabot Aug 21, 2023
d857771
Delete LICENSE
g3Bg2 Aug 21, 2023
34869df
Delete LICENSE
g3Bg2 Aug 21, 2023
f195527
Update package.json
g3Bg2 Aug 21, 2023
3a490df
Update add_file.w
g3Bg2 Aug 21, 2023
2c16f1d
chore: self mutation
monadabot Aug 21, 2023
74f295e
Merge branch 'winglang:main' into akhil-addfile
g3Bg2 Aug 21, 2023
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
202 changes: 202 additions & 0 deletions apps/jsii-docgen/LICENSE

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

19 changes: 19 additions & 0 deletions apps/wing-api-checker/LICENSE

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

2 changes: 1 addition & 1 deletion apps/wing-api-checker/package.json

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

27 changes: 27 additions & 0 deletions docs/docs/04-standard-library/01-cloud/bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ new cloud.Bucket(props?: BucketProps);

| **Name** | **Description** |
| --- | --- |
| <code><a href="#@winglang/sdk.cloud.Bucket.addFile">addFile</a></code> | Add a file to the bucket from system folder. |
| <code><a href="#@winglang/sdk.cloud.Bucket.addObject">addObject</a></code> | Add a file to the bucket that is uploaded when the app is deployed. |
| <code><a href="#@winglang/sdk.cloud.Bucket.onCreate">onCreate</a></code> | Run an inflight whenever a file is uploaded to the bucket. |
| <code><a href="#@winglang/sdk.cloud.Bucket.onDelete">onDelete</a></code> | Run an inflight whenever a file is deleted from the bucket. |
Expand All @@ -174,6 +175,32 @@ new cloud.Bucket(props?: BucketProps);

---

##### `addFile` <a name="addFile" id="@winglang/sdk.cloud.Bucket.addFile"></a>

```wing
addFile(key: str, path: str, encoding?: str): void
```

Add a file to the bucket from system folder.

###### `key`<sup>Required</sup> <a name="key" id="@winglang/sdk.cloud.Bucket.addFile.parameter.key"></a>

- *Type:* str

---

###### `path`<sup>Required</sup> <a name="path" id="@winglang/sdk.cloud.Bucket.addFile.parameter.path"></a>

- *Type:* str

---

###### `encoding`<sup>Optional</sup> <a name="encoding" id="@winglang/sdk.cloud.Bucket.addFile.parameter.encoding"></a>

- *Type:* str

---

##### `addObject` <a name="addObject" id="@winglang/sdk.cloud.Bucket.addObject"></a>

```wing
Expand Down
12 changes: 12 additions & 0 deletions examples/tests/sdk_tests/bucket/add_file.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bring cloud;

let b = new cloud.Bucket();

b.addFile("file1.txt", "testFiles/test1.txt");
b.addFile("file2.txt", "testFiles/test2.txt");

test "addObject" {
assert(b.list().length == 2);
assert(b.get("file1.txt") == "test1");
assert(b.get("file2.txt") == "test2");
}
1 change: 1 addition & 0 deletions examples/tests/sdk_tests/bucket/testFiles/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions examples/tests/sdk_tests/bucket/testFiles/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2
26 changes: 26 additions & 0 deletions libs/wingsdk/src/cloud/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as fs from "fs";
import { isAbsolute, resolve } from "path";
import { Construct } from "constructs";
import { Topic } from "./topic";
import { fqnForType } from "../constants";
Expand Down Expand Up @@ -41,12 +43,14 @@ export abstract class Bucket extends Resource {

/** @internal */
protected readonly _topics = new Map<BucketEventType, Topic>();
private scope: Construct;

constructor(scope: Construct, id: string, props: BucketProps = {}) {
super(scope, id);

this.display.title = "Bucket";
this.display.description = "A cloud object store";
this.scope = scope;

this._addInflightOps(
BucketInflightMethods.DELETE,
Expand All @@ -73,6 +77,28 @@ export abstract class Bucket extends Resource {
*/
public abstract addObject(key: string, body: string): void;

/**
* Add a file to the bucket from system folder
*/

g3Bg2 marked this conversation as resolved.
Show resolved Hide resolved
public addFile(
key: string,
path: string,
encoding: BufferEncoding = "utf-8"
): void {
if (isAbsolute(path)) {
path = path;
} else {
if (!App.of(this.scope).entrypointDir) {
throw new Error("Missing environment variable: WING_SOURCE_DIR");
}
path = resolve(App.of(this.scope).entrypointDir, path);
}
const data = fs.readFileSync(path, { encoding: encoding });

this.addObject(key, data);
}
g3Bg2 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates a topic for subscribing to notification events
* @param actionType
Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/src/target-sim/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { BaseResourceSchema } from "../testing/simulator";
export class Bucket extends cloud.Bucket implements ISimulatorResource {
private readonly public: boolean;
private readonly initialObjects: Record<string, string> = {};

g3Bg2 marked this conversation as resolved.
Show resolved Hide resolved
constructor(scope: Construct, id: string, props: cloud.BucketProps = {}) {
super(scope, id, props);

this.public = props.public ?? false;
}

Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/test/sim-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class SimApp extends sim.App {
private functionIndex: number = 0;

constructor() {
super({ outdir: mkdtemp() });
super({ outdir: mkdtemp(), entrypointDir: __dirname });

// symlink the node_modules so we can test imports and stuffs
fs.symlinkSync(
Expand Down
Loading