ballerina 学习四 如何进行项目结构规划

备注:

   *   ballerina 程序员可以将代码放到一个文件或者一个项目目录

   *   一个ballerina program是一个已经编译以及链接的二进制文件

   *   package是一个包含ballerina 源码文件的目录

   *   repository是一个版本化的已经编译或者源码

   *    project自动管理包以及程序的集合

1.   program

程序是运行时可执行的,以balx 为扩展名,程序包必须包含一个main() 函数(一个进程的进入点),或者一个service (网络可访问的api)

参考(包含一个main 以及service  demo.bal):

import ballerina/http;
import ballerina/io;

function main (string... args) {
    io:println("Hello, World!");
}

service<http:Service> hello bind { port: 9090 } {
    sayHello (endpoint caller, http:Request req) {
        http:Response res = new;
        res.setPayload("Hello, World!");
        _ = caller->respond(res);
    }
}

运行:

ballerina  run demo.bal  如何只包含service 可以使用
ballerian run -s  demo.bal

构建:

ballerina build  demo.bal

2.  package

包可以实现共享,以及重用
包具有以下特点:
a. 可以有一个可选的版本
b. 但是没有版本的包是不能发布到一个仓库实现代码的共享
c. 包的使用是以 org-name/package-name 格式引用的,org-name在仓库中做为命名空间

包的导入格式:
import [<org-name>]/<package-name> her.package [ [version <string>] [as <identifier>] ];
最后一个包的名称,或者距离最近一个.的字符串为包的标示,可以用来进行包中服务或者共享函数的使用,同时可以
使用as 进行别名命名
比如:
import ballerina/http;  http: 做为标示符
import tyler/net.http.exception exception: 做为标示符

参考代码:

import ballerina/http;

# The 'Service' object comes from the imported package.
service<http:Service> hello bind { port:9090 } {

    # The 'Request' object comes from the imported package.
    sayHello (endpoint caller, http:Reqeust req) {
        ...
    }
}
    
import ballerina/http as network;

service<network:Service> hello bind { port:9090 } {

    # The 'Request' object comes from the imported package.
    sayHello (endpoint caller, network:Reqeust req) {
        ...
    }
}
    
包的版本依赖:
如下,如果没有使用的是latest
import tyler/http version 3.0.1;

导入不同版本的包
import tyler/http version 3.0.1 as identity3;
import tyler/http version 4.5.0 as identity4;

function main(string... args) {
  identity3:Person x = identity3:getPerson();
  identity4:Person y = identity4:getPerson();
}

3. project

project 具有如下特点:

*  一个project 是一个目录自动会进行包以及程序的管理
*  同时包含一个用户管理清单文件 Ballerina.toml
*  一个托管的.ballerina/文件实现元数据以及cache
*  project 的repository进行以来的存储

a. 创建project

ballerina init -i  按照提示操作即可

b. 创建一个包

每一个根目录子目录都是一个单独的包,子目录名称就是包的名称
.ballerina/, tests/, and resources/ 做为保留目录,不会做为包


一个参考的结构

/
  .gitignore
  Ballerina-lock.toml  # Generated during build, used to rebuild identical binary
  Ballerina.toml       # Configuration that defines project intent
  .ballerina/          # Internal cache management and contains project repository
                       # Project repository is built or downloaded package dependencies

  main.bal             # Part of the “unnamed” package, compiled into a main.balx
                       # You can have many files in the "unnamed" package, though unadvisable

  package1/            # The source in this directory will be named “<org-name>/package1” 
    Package.md         # Optional, contains descriptive metadata for display at Ballerina Central
    *.bal              # In this dir and recursively in subdirs except tests/ and resources/
    [tests/]           # Package-specific unit and integration tests
    [resources/]       # Package-specific resources
      *.jar            # Optional, if package includes native Java libraries to link + embed 

  packages.can.include.dots.in.dir.name/
    Package.md
    *.bal
    *.jar
    [tests/]         
    [resources/]     
      *.jar            # Optional, if package includes native Java libraries to link + embed 

  [tests/]             # Tests executed for every package in the project
  [resources/]         # Resources included with every package in the project

  target/              # Compiled binaries and other artifacts end up here
      main.balx
      package1.balo
      packages.can.include.dots.in.dir.name.bal

c. 构建一个project

ballerina build

d. 构建一个包
ballerina  build  package-name

4. repository

仓库是包的集合,仓库帮助团队或者组织进行管理他们代码的版本以及资源
存在四类仓库:
a. project 仓库,这个是本地的在 ./ballerina 文件夹
b. home 仓库这个目录在开发者的本地 
c. system 仓库 一个特殊的仓库,包含在ballerian 的分发包中,主要是ballerian 的一个核心包
d. ballerina 中心仓库,http://central.ballerina.io

仓库的操作:

ballerina install
ballerina push --repository=home
ballerina pull <org-name>/<package-name>[:<version>] [--repository=<url>] (remote)
ballerina push <org-name>/<package-name>:<version> [--repository=home|url](remote)
ballerina remove <org-name>/<package>:<version> [--repository=home|<url>]

5. 参考资料

https://ballerina.io/learn/how-to-structure-ballerina-code/

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/9036574.html
今日推荐