Skip to content

Latest commit

 

History

History
98 lines (75 loc) · 2.24 KB

getting_started.md

File metadata and controls

98 lines (75 loc) · 2.24 KB

5 分钟上手 Darabonba

让我们一起来创建你的第一个 Darabonba 模块。

Darabonba

Darabonba 流程图

安装 Darabonba CLI

Darabonba CLI 是由 Node.js 开发的,所以主要由 npm 来安装

$ npm install -g @darabonba/cli

其中包含了以下方法:

$ dara <command> [<args>]

The CLI for Darabonba 1.0.0

Usage:

    dara <command> [<args>]

Available commands:

start a Darabonba project

    init          initialization package information

working on the Darabonba project
    check         syntax check for tea file
    serve         start local server for browsing & debugging
    codegen       generate codes
    exec          execute the static main method of module
    info          get the info of a tea scope or pakcage
    clean         clean the libraries folder
    build         build ast file for tea file
    format        format the tea source file
    config        view or update configuration

help commands
    help          print the help information

构建你的第一个 Darabonba 模块

在编辑器,将下面的代码输入到 Darafile 文件里:

{
  "scope": "darabonba",
  "name": "sample",
  "version": "1.0.0",
  "main": "./main.dara"
}

在编辑器,将下面的代码输入到 main.dara 文件里:

type @organization = string

// 定义一个 model
model User {
  name: string,
  age: number
}

/**
 * 模块的初始化函数
 * @param organization 
 */
init(organization: string) {
  @organization = organization;
}

/**
 * 模块的静态方法
 * @param username 
 * @param age
 */
static function getUser(username: string, age: number): string {
  var user = new User{
    name = username,
    age = age
  };
  return `user's name is ${user.name} ,user's age is ${user.age}! `;
} 

生成代码

Darabonba 主要的作用是利用它为任意风格的接口生成多语言的 SDK 、代码示例、测试用例、接口编排等,比如我们生成 TypeScript 的代码就可以直接利用下面的命令编译 main.tea 并将生成的代码输出到同目录下的 tmp 文件夹中:

$ dara codegen ts ./tmp

其他语言输入对应的将 ts 替换为对应的 java、csharp、python、go、php 即可。