winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.07k stars 198 forks source link

wing compile api #882

Open ekeren opened 1 year ago

ekeren commented 1 year ago

Summary

a compile api as part of @winglang/wing

Feature Spec

Alongside with wing cli, you can compile a wing program using @winglang/wing/compile API

import { compile } from '@winglang/wing';

const wsim = await compile('./hello.w', { 
  target: 'sim' 
  // any other compiler flag
});
const simulator = new sdk.testing.Simulator({simfile : wsim}) 

Use Cases

It allows developer to compile wing programs programmatically (without having to run the CLI) Here is a concrete example discussed in #876 :

Imagine this e2e test from the simulator.

Given the following code:

code.w

bring cloud;

let bucket = new cloud.Bucket();
inflight my_inflight(a:str): str{
  bucket.put("file.txt", a);
};

new cloud.Function(my_inflight);

We can use the compile command in the following jest test

basic.test.ts

import { compile } from '@winglang/wing';
import * as sdk from '@winglang/wingsdk';

let simulator : sdk.testing.Simulator
describe('basic compiler tests', () => { 
  beforeAll(async () => {
  const wsim = await compile('./code.w', { target: 'sim' });
  });
  beforeEach(async ()=> {
    simulator = new sdk.testing.Simulator({simfile : wsim}) 
    await simulator.start()
  });
  afterEach(async () => {
    await simulator.stop()
  })
  test('test function writes to bucket', async () => {
    let bucket = simulator.getResourceByPath("root/cloud.Bucket") as sdk.sim.IBucketClient
    let fn = simulator.getResourceByPath("root/cloud.Function") as sdk.sim.IFunctionClient
    await fn.invoke("somevalue")
    let output = await bucket.get('file.txt')
    expect(output).toBe('somevalue')
  });
});

Implementation Notes

No response

Component

Compiler

github-actions[bot] commented 1 year ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] commented 1 year ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!