Skip to content

Commit

Permalink
feat: @app and @filename intrinsics (#7078)
Browse files Browse the repository at this point in the history
Co-authored-by: wingbot <[email protected]>
Co-authored-by: monada-bot[bot] <[email protected]>
Co-authored-by: Elad Ben-Israel <[email protected]>
  • Loading branch information
4 people authored Sep 6, 2024
1 parent 9adaea0 commit 07e2faf
Show file tree
Hide file tree
Showing 48 changed files with 598 additions and 284 deletions.
8 changes: 4 additions & 4 deletions docs/api/04-standard-library/std/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,10 @@ prepended to the unique identifier.
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#@winglang/sdk.std.IApp.property.node">node</a></code> | <code>constructs.Node</code> | The tree node. |
| <code><a href="#@winglang/sdk.std.IApp.property.entrypointDir">entrypointDir</a></code> | <code>str</code> | The directory of the entrypoint of the current program. |
| <code><a href="#@winglang/sdk.std.IApp.property.entrypointDir">entrypointDir</a></code> | <code>str</code> | The directory of the entrypoint of the current Wing application. |
| <code><a href="#@winglang/sdk.std.IApp.property.isTestEnvironment">isTestEnvironment</a></code> | <code>bool</code> | `true` if this is a testing environment. |
| <code><a href="#@winglang/sdk.std.IApp.property.parameters">parameters</a></code> | <code><a href="#@winglang/sdk.platform.ParameterRegistrar">ParameterRegistrar</a></code> | The application's parameter registrar. |
| <code><a href="#@winglang/sdk.std.IApp.property.workdir">workdir</a></code> | <code>str</code> | The `.wing` directory into which you can emit artifacts during preflight. |
| <code><a href="#@winglang/sdk.std.IApp.property.workdir">workdir</a></code> | <code>str</code> | The `.wing` directory into which you can emit intermediate artifacts during preflight. |

---

Expand All @@ -728,7 +728,7 @@ entrypointDir: str;

- *Type:* str

The directory of the entrypoint of the current program.
The directory of the entrypoint of the current Wing application.

---

Expand Down Expand Up @@ -764,7 +764,7 @@ workdir: str;

- *Type:* str

The `.wing` directory into which you can emit artifacts during preflight.
The `.wing` directory into which you can emit intermediate artifacts during preflight.

---

8 changes: 5 additions & 3 deletions docs/api/05-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,12 @@ the following properties (given an example intrinsic `@x`):
| --------------- | ------------------------------------------------------------------------------------------------------------------------- |
| `@log()` | logs str |
| `@assert()` | checks a condition and _throws_ if evaluated to false |
| `@dirname` | current source directory |
| `@filename` | absolute path of the source file |
| `@dirname` | absolute path of the source file's directory |
| `@app` | the root of the construct tree |
| `@unsafeCast()` | cast a value into a different type |
| `@nodeof()` | obtain the [tree node](/docs/concepts/application-tree) of a preflight object |
| `@lift()` | explicitly qualify a [lift](/docs/concepts/inflights) of a preflight object |
| `@nodeof()` | obtain the [tree node](/docs/concepts/application-tree) of a preflight object |
| `@lift()` | explicitly qualify a [lift](/docs/concepts/inflights) of a preflight object |
> ```TS
> @log("Hello {name}");
Expand Down
7 changes: 2 additions & 5 deletions packages/@winglang/sdk/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,8 @@ export function createExternRequire(dirname: string) {
};
}

export function resolveDirname(
outdir: string,
relativeSourceDir: string
): string {
return normalPath(path.resolve(outdir, relativeSourceDir));
export function resolve(outdir: string, relativeSourcePath: string): string {
return normalPath(path.resolve(outdir, relativeSourcePath));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/@winglang/sdk/src/std/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ export interface IApp extends IConstruct {
readonly [APP_SYMBOL]: true;

/**
* The `.wing` directory into which you can emit artifacts during preflight.
* The `.wing` directory into which you can emit intermediate artifacts during preflight.
*/
readonly workdir: string;

Expand All @@ -482,7 +482,7 @@ export interface IApp extends IConstruct {
readonly isTestEnvironment: boolean;

/**
* The directory of the entrypoint of the current program.
* The directory of the entrypoint of the current Wing application.
*/
readonly entrypointDir: string;

Expand Down
14 changes: 14 additions & 0 deletions packages/@winglang/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,17 @@ pub enum IntrinsicKind {
/// Error state
Unknown,
Dirname,
Filename,
App,
}

impl Display for IntrinsicKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
IntrinsicKind::Unknown => write!(f, "@"),
IntrinsicKind::Dirname => write!(f, "@dirname"),
IntrinsicKind::Filename => write!(f, "@filename"),
IntrinsicKind::App => write!(f, "@app"),
}
}
}
Expand All @@ -603,6 +607,8 @@ impl IntrinsicKind {
pub fn from_str(s: &str) -> Self {
match s {
"@dirname" => IntrinsicKind::Dirname,
"@filename" => IntrinsicKind::Filename,
"@app" => IntrinsicKind::App,
_ => IntrinsicKind::Unknown,
}
}
Expand All @@ -614,6 +620,14 @@ impl IntrinsicKind {
Phase::Preflight => true,
_ => false,
},
IntrinsicKind::Filename => match phase {
Phase::Preflight => true,
_ => false,
},
IntrinsicKind::App => match phase {
Phase::Preflight => true,
_ => false,
},
}
}
}
Expand Down
43 changes: 34 additions & 9 deletions packages/@winglang/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,24 +683,49 @@ impl<'a> JSifier<'a> {
// Only happens inflight, so we can assume an error was caught earlier
return new_code!(expr_span, "");
};
let relative_source_path = make_relative_path(
self.out_dir.as_str(),
source_path
.path
.parent()
.expect("source path is file in a directory")
.as_str(),
);
let source_dir = source_path.path.parent().expect("source path is file in a directory");

// Calculate the path of "if I'm at <outdir>, how do I get to <source_dir>"
// This relative path is what we will write inside the js file, with the motivation that
// the output is more stable across different users' machines than an absolute path.
let relative_source_path = make_relative_path(self.out_dir.as_str(), source_dir.as_str());

// At runtime, $helpers.resolve will normalize the path for Windows or Unix, and then convert it to an absolute path
new_code!(
expr_span,
HELPERS_VAR,
".resolveDirname(",
".resolve(",
__DIRNAME,
", \"",
relative_source_path,
"\")"
)
}
IntrinsicKind::Filename => {
let Some(source_path) = ctx.source_file else {
// Only happens inflight, so we can assume an error was caught earlier
return new_code!(expr_span, "");
};

// Calculate the path of "if I'm at <outdir>, how do I get to <source_path>"
// This relative path is what we will write inside the js file, with the motivation that
// the output is more stable across different users' machines than an absolute path.
let relative_source_path = make_relative_path(self.out_dir.as_str(), source_path.path.as_str());

// At runtime, $helpers.resolve will normalize the path for Windows or Unix, and then convert it to an absolute path
new_code!(
expr_span,
HELPERS_VAR,
".resolve(",
__DIRNAME,
", \"",
relative_source_path,
"\")"
)
}
IntrinsicKind::App => {
new_code!(expr_span, HELPERS_VAR, ".nodeof(this).app")
}
},
ExprKind::Call { callee, arg_list } => {
let function_type = match callee {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/jsify/tests.rs
source: packages/@winglang/wingc/src/jsify/tests.rs
---
## Errors
Member "print" does not exist in "Construct" 3:13
2 changes: 1 addition & 1 deletion packages/@winglang/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const WINGSDK_AUTOID_RESOURCE: &'static str = "std.AutoIdResource";
const WINGSDK_STRUCT: &'static str = "std.Struct";
const WINGSDK_TEST_CLASS_NAME: &'static str = "Test";
const WINGSDK_NODE: &'static str = "std.Node";
const WINGSDK_APP: &'static str = "std.IApp";

const WINGSDK_SIM_IRESOURCE: &'static str = "sim.IResource";
const WINGSDK_SIM_IRESOURCE_FQN: &'static str = formatcp!(
Expand Down Expand Up @@ -289,7 +290,6 @@ pub fn type_check_file(
None,
);
tc.add_builtins(scope);
tc.patch_constructs();

// If the file is an entrypoint file, we add "this" to its symbol environment
if is_entrypoint_file(&file.path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: "a:"
kind: 5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: "two:"
kind: 5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: this
kind: 6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: "@app"
kind: 6
detail: IApp
documentation:
kind: markdown
value: "Get the root application node.\n\nThe application node is the root of the construct tree and all other resources are children (or grandchildren) of this node.\nSee https://www.winglang.io/docs/concepts/application-tree for more information."
sortText: bb|@app
- label: "@dirname"
kind: 6
detail: str
documentation:
kind: markdown
value: "Get the normalized absolute path of the current source file's directory.\n\nThe resolved path represents a path during preflight only and is not guaranteed to be valid while inflight.\nIt should primarily be used in preflight or in inflights that are guaranteed to be executed in the same filesystem where preflight executed."
value: "Get the normalized absolute path of the current Wing source file's directory.\n\nThe resolved path represents a path during preflight only and is not guaranteed to be valid while inflight."
sortText: bb|@dirname
- label: "@filename"
kind: 6
detail: str
documentation:
kind: markdown
value: "Get the normalized absolute path of the current Wing source file.\n\nThe resolved path represents a path during preflight only and is not guaranteed to be valid while inflight."
sortText: bb|@filename

Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: "@app"
kind: 6
detail: IApp
documentation:
kind: markdown
value: "Get the root application node.\n\nThe application node is the root of the construct tree and all other resources are children (or grandchildren) of this node.\nSee https://www.winglang.io/docs/concepts/application-tree for more information."
sortText: bb|@app
- label: "@dirname"
kind: 6
detail: str
documentation:
kind: markdown
value: "Get the normalized absolute path of the current source file's directory.\n\nThe resolved path represents a path during preflight only and is not guaranteed to be valid while inflight.\nIt should primarily be used in preflight or in inflights that are guaranteed to be executed in the same filesystem where preflight executed."
value: "Get the normalized absolute path of the current Wing source file's directory.\n\nThe resolved path represents a path during preflight only and is not guaranteed to be valid while inflight."
sortText: bb|@dirname
- label: "@filename"
kind: 6
detail: str
documentation:
kind: markdown
value: "Get the normalized absolute path of the current Wing source file.\n\nThe resolved path represents a path during preflight only and is not guaranteed to be valid while inflight."
sortText: bb|@filename

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: a
kind: 6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: "a1:"
kind: 5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: this
kind: 6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/completions.rs
source: packages/@winglang/wingc/src/lsp/completions.rs
---
- label: this
kind: 6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
source: libs/wingc/src/lsp/hover.rs
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nstatic preflight @dirname: str\n```\n---\nGet the normalized absolute path of the current source file's directory.\n\nThe resolved path represents a path during preflight only and is not guaranteed to be valid while inflight.\nIt should primarily be used in preflight or in inflights that are guaranteed to be executed in the same filesystem where preflight executed."
value: "```wing\nstatic preflight @dirname: str\n```\n---\nGet the normalized absolute path of the current Wing source file's directory.\n\nThe resolved path represents a path during preflight only and is not guaranteed to be valid while inflight."
range:
start:
line: 1
Expand Down
Loading

0 comments on commit 07e2faf

Please sign in to comment.