style: modify napi docs
Signed-off-by: zhaojunxia <zhaojunxia@kaihong.com>
26
README_zh.md
@ -46,7 +46,7 @@
|
||||
|
||||
### 工具使用
|
||||
|
||||
工具有三种类型,分别是可执行文件、VS Code插件、IntelliJ插件。其中的可执行文件可根据工具使用者的开发环境选择,支持Windows,Linux和Mac。可执行文件、IntelliJ插件、VS Code插件下载路径如下。
|
||||
工具有三种类型,分别是可执行文件、VS Code插件、IntelliJ插件。其中的可执行文件可根据工具使用者的开发环境选择,支持Windows,Linux和Mac。可执行文件、VS Code插件下载路径如下。
|
||||
|
||||
[下载链接1]( http://ftpkaihongdigi.i234.me:5000/sharing/PC6uOorrM)
|
||||
|
||||
@ -54,6 +54,10 @@
|
||||
|
||||
[下载链接3]( http://ftp.kaihongdigi.com:5000/sharing/PC6uOorrM)
|
||||
|
||||
IntelliJ插件下载路径如下:
|
||||
|
||||
[IntelliJ插件下载链接](https://plugins.jetbrains.com/plugin/19593-napi-generator/versions)
|
||||
|
||||
具体的工具使用步骤,可以左键单击以下链接了解:
|
||||
|
||||
[工具使用说明](https://gitee.com/openharmony/napi_generator/blob/master/docs/INSTRUCTION_ZH.md)
|
||||
@ -76,19 +80,15 @@ napi_generator/examples/ts
|
||||
|
||||
|
||||
```c++
|
||||
#include "napitest.h"
|
||||
|
||||
#include "napitest.h"
|
||||
namespace napitest {
|
||||
bool TestClass1::if_direct(std::string &v1, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
bool TestClass1::if_callback(std::string &v1, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
......
|
||||
}
|
||||
namespace napitest {
|
||||
bool func1(std::string& v1, std::string& out)
|
||||
{
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 代码集成
|
||||
|
@ -5,8 +5,10 @@
|
||||
为了实现工具生成的接口被其他子系统或者应用调用,需将生成的代码编译集成到OpenHarmony系统中,使其生成动态库,供OpenHarmony应用层调用。
|
||||
本文介绍如何将工具生成的源码利用OpenHarmony编译系统生成动态库供应用层调用,主要是有以下两种方式,分别为增加ohos.build文件方式和增加bundle.json文件方式。
|
||||
|
||||
## ohos.build方式集成
|
||||
### 建立模块位置
|
||||
## 3.1 版本
|
||||
|
||||
### ohos.build方式集成
|
||||
#### 建立模块位置
|
||||
|
||||
模块目录理论上可以建立在OpenHarmony代码库的任何地方,假设OpenHarmony代码库的目录为`OHOS_SRC`,在`OHOS_SRC/foundation`目录下,例如建立此次测试模块目录:napitest。此时,`OHOS_SRC/foundation`目录下应该有aafwk,ace,ai, …, napitest等目录,其中napitest就是刚刚建立的,在napitest目录下,把之前用可执行文件或者插件转换出来的文件全部拷贝到该目录下,并且在该目录下新建一个文件ohos.build。例如napitest目录下有以下文件:
|
||||
|
||||
@ -21,54 +23,21 @@
|
||||
|-- tool_utility.cpp
|
||||
|-- tool_utility.h
|
||||
|
||||
### 编译修改点
|
||||
#### 编译修改点
|
||||
|
||||
#### 修改build.gn文件
|
||||
##### 修改BUILD.gn文件(可选)
|
||||
|
||||
```
|
||||
import("//build/ohos.gni")
|
||||
正常生成代码之后不需要修改,若用户需要修改子系统和部件名称,则根据自身需求修改BUILD.gn文件和ohos.build文件中子系统与部件名称即可。
|
||||
|
||||
ohos_shared_library("napitest")
|
||||
{
|
||||
# 指定编译源文件
|
||||
sources = [
|
||||
"napitest_middle.cpp",
|
||||
"napitest.cpp",
|
||||
"tool_utility.cpp",
|
||||
]
|
||||
include_dirs = [
|
||||
".",
|
||||
"//third_party/node/src",
|
||||
]
|
||||
# 指定编译依赖,如果依赖第三方库,需要在此添加
|
||||
deps=[
|
||||
"//foundation/ace/napi:ace_napi",
|
||||
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
|
||||
]
|
||||
remove_configs = [ "//build/config/compiler:no_rtti" ]
|
||||
cflags=[
|
||||
]
|
||||
cflags_cc=[
|
||||
"-frtti",
|
||||
]
|
||||
ldflags = [
|
||||
]
|
||||
# 指定库生成的路径
|
||||
relative_install_dir = "module"
|
||||
# 主要是修改下面两条内容,子系统及其组件,后面会引用
|
||||
part_name = "napitest_interface"
|
||||
subsystem_name = "napitest"
|
||||
}
|
||||
```
|
||||
##### 修改ohos.build文件
|
||||
|
||||
#### 修改ohos.build文件
|
||||
其中module_list选项中的"//foundation/napitest"指的是napitest目录,":napitest"指的是上面BUILD.gn中的目标ohos_shared_library("napitest")。
|
||||
|
||||
```
|
||||
{
|
||||
"subsystem": "napitest",
|
||||
"parts": {
|
||||
"napitest_interface": {
|
||||
"napitest": {
|
||||
"module_list": [
|
||||
"//foundation/napitest:napitest"
|
||||
],
|
||||
@ -78,7 +47,7 @@
|
||||
}
|
||||
```
|
||||
|
||||
#### 修改napitest.cpp文件
|
||||
##### 修改napitest.cpp文件
|
||||
|
||||
为方便调试,在napitest.cpp文件或napitest_middle.cpp文件中加入打印日志语句。以修改napitest.cpp文件为例,增加以下代码:
|
||||
|
||||
@ -93,14 +62,25 @@
|
||||
"%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__)
|
||||
}
|
||||
namespace napitest {
|
||||
bool fun1(std::string &v, std::string &out)
|
||||
bool func1(std::string &v, std::string &out)
|
||||
{
|
||||
NAPITEST_LOG("napitest fun1 begin!!!!!!!!!!!!!!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
#### 增加子系统
|
||||
并在BUILD.gn文件deps依赖中增加依赖libhilog,如下所示:
|
||||
|
||||
```
|
||||
...
|
||||
deps=[
|
||||
"//foundation/ace/napi:ace_napi",
|
||||
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
|
||||
]
|
||||
...
|
||||
```
|
||||
|
||||
##### 增加子系统
|
||||
|
||||
在源码/build/subsystem_config.json中增加子系统选项。如下所示:
|
||||
|
||||
@ -113,19 +93,19 @@
|
||||
}
|
||||
```
|
||||
|
||||
### 添加功能模块
|
||||
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码/productdefine/common/products/rk3566.json中增加part选项,其中napitest就是上面填的subsystem_name,napitest_interface就是上面填的part_name。
|
||||
#### 添加功能模块
|
||||
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码/productdefine/common/products/rk3566.json中增加part选项,其中第一个napitest就是BUILD.gn文件中的subsystem_name,第二个napitest就是BUILD.gn文件中的part_name。
|
||||
|
||||
"napitest:napitest_interface":{}
|
||||
"napitest:napitest":{}
|
||||
|
||||
### 编译验证
|
||||
#### 编译验证
|
||||
|
||||
编译成功后,就会生成libnapitest.z.so,目录如下所示:
|
||||
编译成功后,就会在 /out/产品名/packages/phone/system/lib/module/ 生成libnapitest.z.so,如下所示:
|
||||
|
||||
/out/ohos-arm-release/packages/phone/system/lib/module
|
||||
|
||||
## bundle.json方式集成
|
||||
### 建立模块位置
|
||||
### bundle.json方式集成
|
||||
#### 建立模块位置
|
||||
|
||||
模块目录理论上可以建立在OpenHarmony代码库的任何地方,假设OpenHarmony代码库的目录为`OHOS_SRC`,在`OHOS_SRC/foundation`目录下,例如建立此次测试模块目录:napitest。此时,`OHOS_SRC/foundation`目录下应该有aafwk,ace,ai, …, napitest等目录,其中napitest就是刚刚建立的,在napitest目录下,把之前用可执行文件或者插件转换出来的文件全部拷贝到该目录下,并且在该目录下新建一个文件bundle.json。例如napitest目录下有以下文件:
|
||||
|
||||
@ -140,49 +120,13 @@
|
||||
|-- tool_utility.cpp
|
||||
|-- tool_utility.h
|
||||
|
||||
### 编译修改点
|
||||
#### 编译修改点(可选)
|
||||
|
||||
#### 修改build.gn文件
|
||||
##### 修改BUILD.gn文件(可选)
|
||||
|
||||
```
|
||||
正常生成代码之后不需要修改,若用户需要修改子系统和部件名称,则根据自身需求修改BUILD.gn文件和bundle.json文件中子系统与部件名称即可。
|
||||
|
||||
import("//build/ohos.gni")
|
||||
|
||||
ohos_shared_library("napitest")
|
||||
{
|
||||
# 指定编译源文件
|
||||
sources = [
|
||||
"napitest_middle.cpp",
|
||||
"napitest.cpp",
|
||||
"tool_utility.cpp",
|
||||
]
|
||||
include_dirs = [
|
||||
".",
|
||||
"//third_party/node/src",
|
||||
]
|
||||
# 指定编译依赖,如果依赖第三方库,需要在此添加
|
||||
deps=[
|
||||
# 3.1及之前的版本中,napi在foundation/ace/目录下,3.2 release版本中,napi在foundation/arkui/目录下
|
||||
"//foundation/arkui/napi:ace_napi",
|
||||
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
|
||||
]
|
||||
remove_configs = [ "//build/config/compiler:no_rtti" ]
|
||||
cflags=[
|
||||
]
|
||||
cflags_cc=[
|
||||
"-frtti",
|
||||
]
|
||||
ldflags = [
|
||||
]
|
||||
# 指定库生成的路径
|
||||
relative_install_dir = "module"
|
||||
# 主要是修改下面两条内容,子系统及其组件,后面会引用
|
||||
part_name = "napitest_interface"
|
||||
subsystem_name = "napitest"
|
||||
}
|
||||
```
|
||||
|
||||
#### 修改bundle.json文件
|
||||
##### 修改bundle.json文件
|
||||
其中destPath选项中的"//foundation/napitest"指的是napitest目录,":napitest"指的是上面BUILD.gn中的目标ohos_shared_library("napitest")。
|
||||
|
||||
```
|
||||
@ -198,7 +142,175 @@
|
||||
"dirs": {},
|
||||
"scripts": {},
|
||||
"component": {
|
||||
"name": "napitest_interface",
|
||||
"name": "napitest",
|
||||
"subsystem": "napitest",
|
||||
"features": [],
|
||||
"adapted_system_type": [
|
||||
"standard"
|
||||
],
|
||||
"rom": "10000KB",
|
||||
"ram": "10000KB",
|
||||
"deps": {
|
||||
"components": [
|
||||
"ace_napi",
|
||||
"ipc_core",
|
||||
"libhilog"
|
||||
],
|
||||
"third_party": [
|
||||
"node"
|
||||
]
|
||||
},
|
||||
"build": {
|
||||
"sub_component": [
|
||||
"//foundation/napitest:napitest"
|
||||
],
|
||||
"inner_kits": [
|
||||
{
|
||||
"header": {
|
||||
"header_base": "//foundation/napitest",
|
||||
"header_files": [
|
||||
"tool_utility.h",
|
||||
"napitest.h"
|
||||
]
|
||||
},
|
||||
"name": "//foundation/napitest:napitest"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
##### 修改napitest.cpp文件
|
||||
为方便调试,在napitest.cpp文件或napitest_middle.cpp文件中加入打印日志语句。以修改napitest.cpp文件为例,增加以下代码:
|
||||
|
||||
|
||||
```
|
||||
#include "napitest.h"
|
||||
#include <hilog/log.h>
|
||||
using namespace OHOS;
|
||||
namespace {
|
||||
constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0, "NAPITESTNAPILayer" };
|
||||
#define NAPITEST_LOG(fmt, ...) HiviewDFX::HiLog::Info(LABEL, \
|
||||
"%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__)
|
||||
}
|
||||
namespace napitest {
|
||||
bool func1(std::string &v, std::string &out)
|
||||
{
|
||||
NAPITEST_LOG("napitest fun1 begin!!!!!!!!!!!!!!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
并在BUILD.gn文件deps依赖中增加依赖libhilog,如下所示:
|
||||
|
||||
```
|
||||
...
|
||||
deps=[
|
||||
"//foundation/ace/napi:ace_napi",
|
||||
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
|
||||
]
|
||||
...
|
||||
```
|
||||
|
||||
##### 增加子系统
|
||||
|
||||
在源码/build/subsystem_config.json中增加子系统选项。如下所示:
|
||||
|
||||
```
|
||||
"napitest": {
|
||||
"project": "hmf/napitest",
|
||||
"path": "foundation/napitest",
|
||||
"name": "napitest",
|
||||
"dir": "foundation"
|
||||
}
|
||||
```
|
||||
|
||||
#### 添加功能模块
|
||||
|
||||
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码/productdefine/common/products/rk3566.json中增加part选项,其中第一个napitest就是BUILD.gn文件中的subsystem_name,第二个napitest就是BUILD.gn文件中的part_name。
|
||||
|
||||
"napitest:napitest":{}
|
||||
|
||||
#### 编译验证
|
||||
|
||||
编译成功后,就会在 /out/产品名/packages/phone/system/lib/module/ 生成libnapitest.z.so,如下所示:
|
||||
|
||||
/out/ohos-arm-release/packages/phone/system/lib/module
|
||||
|
||||
## 3.2 版本
|
||||
|
||||
### 建立模块位置
|
||||
|
||||
模块目录理论上可以建立在OpenHarmony代码库的任何地方,假设OpenHarmony代码库的目录为`OHOS_SRC`,在`OHOS_SRC/foundation`目录下,例如建立此次测试模块目录:napitest。此时,`OHOS_SRC/foundation`目录下应该有aafwk,arkui,ai, …, napitest等目录,其中napitest就是刚刚建立的,在napitest目录下,把之前用可执行文件或者插件转换出来的文件全部拷贝到该目录下,并且在该目录下新建一个文件bundle.json。例如napitest目录下有以下文件:
|
||||
|
||||
foundation/napitest
|
||||
|-- binding.gyp
|
||||
|-- BUILD.gn
|
||||
|-- bundle.json
|
||||
|-- napitest.cpp
|
||||
|-- napitest.h
|
||||
|-- napitest_middle.cpp
|
||||
|-- test.sh
|
||||
|-- tool_utility.cpp
|
||||
|-- tool_utility.h
|
||||
|
||||
### 编译修改点
|
||||
|
||||
#### 修改BUILD.gn文件
|
||||
|
||||
将deps中的"//foundation/ace/napi:ace_napi"修改为"//foundation/arkui/napi:ace_napi",修改后的BUILD.gn文件内容如下所示:
|
||||
|
||||
```
|
||||
import("//build/ohos.gni")
|
||||
|
||||
ohos_shared_library("napitest")
|
||||
{
|
||||
sources = [
|
||||
"napitest_middle.cpp",
|
||||
"napitest.cpp",
|
||||
"tool_utility.cpp",
|
||||
]
|
||||
include_dirs = [
|
||||
".",
|
||||
"//third_party/node/src",
|
||||
]
|
||||
deps=[
|
||||
"//foundation/arkui/napi:ace_napi",
|
||||
]
|
||||
remove_configs = [ "//build/config/compiler:no_rtti" ]
|
||||
cflags=[
|
||||
]
|
||||
cflags_cc=[
|
||||
"-frtti",
|
||||
]
|
||||
ldflags = [
|
||||
]
|
||||
|
||||
relative_install_dir = "module"
|
||||
part_name = "napitest"
|
||||
subsystem_name = "napitest"
|
||||
}
|
||||
```
|
||||
|
||||
#### 修改bundle.json文件
|
||||
|
||||
其中destPath选项中的"//foundation/napitest"指的是napitest目录,":napitest"指的是上面BUILD.gn中的目标ohos_shared_library("napitest")。
|
||||
|
||||
```
|
||||
{
|
||||
"name": "@ohos/napitest",
|
||||
"description": "napitest provides atomic capabilities",
|
||||
"version": "3.2",
|
||||
"license": "Apache License 2.0",
|
||||
"publishAs": "code-segment",
|
||||
"segment": {
|
||||
"destPath": "foundation/napitest"
|
||||
},
|
||||
"dirs": {},
|
||||
"scripts": {},
|
||||
"component": {
|
||||
"name": "napitest",
|
||||
"subsystem": "napitest",
|
||||
"features": [],
|
||||
"adapted_system_type": [
|
||||
@ -238,6 +350,7 @@
|
||||
```
|
||||
|
||||
#### 修改napitest.cpp文件
|
||||
|
||||
为方便调试,在napitest.cpp文件或napitest_middle.cpp文件中加入打印日志语句。以修改napitest.cpp文件为例,增加以下代码:
|
||||
|
||||
|
||||
@ -251,44 +364,46 @@
|
||||
"%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__)
|
||||
}
|
||||
namespace napitest {
|
||||
bool fun1(std::string &v, std::string &out)
|
||||
bool func1(std::string &v, std::string &out)
|
||||
{
|
||||
NAPITEST_LOG("napitest fun1 begin!!!!!!!!!!!!!!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
并在BUILD.gn文件deps依赖中增加依赖libhilog,如下所示:
|
||||
|
||||
```
|
||||
...
|
||||
deps=[
|
||||
"//foundation/arkui/napi:ace_napi",
|
||||
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
|
||||
]
|
||||
...
|
||||
```
|
||||
|
||||
#### 增加子系统
|
||||
|
||||
在源码/build/subsystem_config.json中增加子系统选项。如下所示:
|
||||
|
||||
```
|
||||
"napitest": {
|
||||
"project": "hmf/napitest",
|
||||
"path": "foundation/napitest",
|
||||
"name": "napitest",
|
||||
"dir": "foundation"
|
||||
"name": "napitest"
|
||||
}
|
||||
```
|
||||
|
||||
### 添加功能模块
|
||||
|
||||
#### 3.1 版本
|
||||
|
||||
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码/productdefine/common/products/rk3566.json中增加part选项,其中napitest就是上面填的subsystem_name,napitest_interface就是上面填的part_name。
|
||||
|
||||
"napitest:napitest_interface":{}
|
||||
|
||||
#### 3.2 版本
|
||||
|
||||
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码vendor/hihope/rk3568/config.json中增加part选项,其中napitest就是上面填的subsystem_name,napitest_interface就是上面填的part_name。
|
||||
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码vendor/hihope/rk3568/config.json中增加part选项,其中第一个napitest就是BUILD.gn文件中的subsystem_name,第二个napitest就是BUILD.gn文件中的part_name。
|
||||
|
||||
```
|
||||
{
|
||||
"subsystem": "napitest",
|
||||
"components": [
|
||||
{
|
||||
"component": "napitest_interface",
|
||||
"component": "napitest",
|
||||
"features": []
|
||||
}
|
||||
]
|
||||
@ -299,13 +414,15 @@
|
||||
|
||||
编译成功后,就会在 /out/产品名/packages/phone/system/lib/module/ 生成libnapitest.z.so,如下所示:
|
||||
|
||||
/out/ohos-arm-release/packages/phone/system/lib/module
|
||||
/out/rk3568/packages/phone/system/lib/module
|
||||
|
||||
## 总结
|
||||
|
||||
两种集成方式使用场景说明:
|
||||
3.1版本两种集成方式使用场景说明:
|
||||
|
||||
ohos.build方式集成:适合3.0前版本使用。
|
||||
|
||||
bundle.json方式集成:兼容ohos.build方式,但3.1及以后版本建议使用此种方式集成。
|
||||
|
||||
3.2版本适合使用bundle.json方式集成。
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
# NAPI框架生成工具使用说明
|
||||
## 简介
|
||||
|
||||
NAPI框架生成工具支持三种入口,分别是可执行程序、VS Code插件、IntelliJ插件,使用者可以根据自己的需要选择合适的工具。可执行文件、IntelliJ插件、VS Code插件下载路径如下:
|
||||
NAPI框架生成工具支持三种入口,分别是可执行程序、VS Code插件、IntelliJ插件,使用者可以根据自己的需要选择合适的工具。可执行文件、VS Code插件下载路径如下:
|
||||
|
||||
[下载链接1]( http://ftpkaihongdigi.i234.me:5000/sharing/PC6uOorrM)
|
||||
|
||||
@ -17,6 +17,10 @@ NAPI框架生成工具支持三种入口,分别是可执行程序、VS Code插
|
||||
│ │ |── napi_generator-win.exe # Windows可执行程序
|
||||
│ │ └── napi_generator-macos # Mac可执行程序
|
||||
|
||||
IntelliJ插件下载路径如下:
|
||||
|
||||
[IntelliJ插件下载链接](https://plugins.jetbrains.com/plugin/19593-napi-generator/versions)
|
||||
|
||||
## 工具介绍
|
||||
|
||||
通过NAPI框架生成工具,使用者可输入一个接口定义的ts文件,一键生成NAPI框架代码、业务代码框架、GN脚本等文件,并使用生成的NAPI接口及功能。使用者也可以输入一个定义方法的.h头文件,反向生成ts文件。
|
||||
@ -50,10 +54,10 @@ napi_generator的可执行程序方式和插件方式都具有预检查的功能
|
||||
|
||||
#### Linux
|
||||
|
||||
1.将待转换的.d.ts文件、napi_generator-linux放在同级目录下。若.d.ts文件中声明了basic.d.ts文件,将basic.d.ts文件放置在待转换.d.ts文件上一级目录;若除此之外还声明其它.d.ts文件,将此类文件放置在待转换.d.ts文件同级目录。此处新建out文件夹,用于存放生成框架代码。整体目录文件如下:
|
||||
1.将待转换的.d.ts文件、napi_generator-linux放在同级目录下。若.d.ts文件中声明了basic.d.ts文件,将basic.d.ts文件放置在待转换.d.ts文件同一级目录;若除此之外还声明其它.d.ts文件,将此类文件放置在待转换.d.ts文件同级目录。此处新建out文件夹,用于存放生成框架代码。整体目录文件如下:
|
||||
|
||||
harmony@Ubuntu-64:~/service$ ls
|
||||
napi_generator-linux @ohos.napitest.d.ts out
|
||||
napi_generator-linux @ohos.napitest.d.ts out basic.d.ts
|
||||
|
||||
2.在终端中进入到之前可执行程序napi_generator-linux所在的目录,并运行napi_generator-linux,命令如下:
|
||||
|
||||
@ -75,10 +79,11 @@ napi_generator的可执行程序方式和插件方式都具有预检查的功能
|
||||
|
||||
#### Windows
|
||||
|
||||
1.将待转换的.d.ts文件、napi_generator-win.exe放在同级目录下。若.d.ts文件中声明了basic.d.ts文件,将basic.d.ts文件放置在待转换.d.ts文件上一级目录;若除此之外还声明其它.d.ts文件,将此类文件放置在待转换.d.ts文件同级目录。此处新建out文件夹,用于存放生成框架代码。整体目录文件如下:
|
||||
1.将待转换的.d.ts文件、napi_generator-win.exe放在同级目录下。若.d.ts文件中声明了basic.d.ts文件,将basic.d.ts文件放置在待转换.d.ts文件同一级目录;若除此之外还声明其它.d.ts文件,将此类文件放置在待转换.d.ts文件同级目录。此处新建out文件夹,用于存放生成框架代码。整体目录文件如下:
|
||||
|
||||
E:\demo\napi>dir /B
|
||||
@ohos.napitest.d.ts
|
||||
basic.d.ts
|
||||
napi_generator-win.exe
|
||||
out
|
||||
|
||||
|
31
examples/ts/@ohos.napitest.d.ts
vendored
@ -21,36 +21,7 @@ import {AsyncCallback} from './basic';
|
||||
* @since 7
|
||||
*/
|
||||
declare namespace napitest {
|
||||
/**
|
||||
* Shuts down the system.
|
||||
*
|
||||
* <p>This method requires the ohos.permission.SHUTDOWN permission.
|
||||
*
|
||||
* @param reason Indicates the shutdown reason.
|
||||
* @systemapi
|
||||
* @since 7
|
||||
*/
|
||||
function shutdownDevice(reason: string): void;
|
||||
|
||||
/**
|
||||
* Restarts the system.
|
||||
*
|
||||
* <p>This method requires the ohos.permission.REBOOT permission.
|
||||
*
|
||||
* @param reason Indicates the restart reason. For example, "updater" indicates entering the updater mode
|
||||
* after the restart. If the parameter is not specified, the system enters the normal mode after the restart.
|
||||
* @since 7
|
||||
*/
|
||||
function rebootDevice(reason: string): void;
|
||||
|
||||
/**
|
||||
* Checks whether the screen of a device is on or off.
|
||||
*
|
||||
* @return Returns true if the screen is on; returns false otherwise.
|
||||
* @since 7
|
||||
*/
|
||||
function isScreenOn(callback: AsyncCallback<boolean>): void;
|
||||
function isScreenOn(): Promise<boolean>;
|
||||
function func1(v1: string): string;
|
||||
}
|
||||
export default napitest;
|
||||
|
||||
|
BIN
figures/DevEco_run_result.png
Normal file
After Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 60 KiB After Width: | Height: | Size: 80 KiB |
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 9.7 KiB |
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 13 KiB |
BIN
figures/pic-napi_jar_download.png
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
figures/picGoogle-napi_jar_download.png
Normal file
After Width: | Height: | Size: 30 KiB |
@ -35,13 +35,15 @@ NAPI框架代码生成工具,它可以根据用户指定路径下的ts(typescr
|
||||
|
||||
### 工具使用
|
||||
|
||||
插件下载路径如下,选择napi_generator.jar下载。
|
||||
插件下载路径如下,选择新版本下载。
|
||||
|
||||
[下载链接1]( http://ftpkaihongdigi.i234.me:5000/sharing/PC6uOorrM)
|
||||
[IntelliJ插件下载链接](https://plugins.jetbrains.com/plugin/19593-napi-generator/versions)
|
||||
|
||||
[下载链接2]( http://ftp.kaihong.com:5000/sharing/PC6uOorrM)
|
||||
![](../figures/pic-napi_jar_download.png)
|
||||
|
||||
[下载链接3]( http://ftp.kaihongdigi.com:5000/sharing/PC6uOorrM)
|
||||
使用谷歌浏览器时:
|
||||
|
||||
![](../figures/picGoogle-napi_jar_download.png)
|
||||
|
||||
具体的工具使用步骤,可以左键单击以下链接了解:
|
||||
|
||||
@ -67,20 +69,12 @@ napi_IntelliJ_plugin
|
||||
```c++
|
||||
#include "napitest.h"
|
||||
|
||||
|
||||
namespace napitest {
|
||||
|
||||
bool TestClass1::if_direct(std::string &v1, std::string &out) {
|
||||
bool func1(std::string& v1, std::string& out)
|
||||
{
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TestClass1::if_callback(std::string &v1, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
......
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -5,11 +5,13 @@
|
||||
|
||||
## 准备
|
||||
|
||||
1. 待转换的@napitest.test.d.ts文件如下:
|
||||
1. 待转换的@ohos.napitest.d.ts文件如下:
|
||||
|
||||
```
|
||||
import {AsyncCallback} from './basic';
|
||||
|
||||
declare namespace napitest {
|
||||
function fun1(v1: number): number;
|
||||
function fun1(v1: string): string;
|
||||
}
|
||||
|
||||
export default napitest;
|
||||
@ -18,40 +20,15 @@ export default napitest;
|
||||
2. 生成的napitest.cpp给out赋初值:
|
||||
|
||||
```
|
||||
out = 23;
|
||||
out = "NapiOut";
|
||||
```
|
||||
|
||||
3. 生成的CmakeLists.txt中引入hilog库libhilog_ndk.z.so:
|
||||
|
||||
```
|
||||
target_link_libraries(test PUBLIC libace_napi.z.so libuv.so libhilog_ndk.z.so)
|
||||
```
|
||||
|
||||
4. 在生成的napitest_middle.cpp中打印hilog日志:
|
||||
|
||||
```
|
||||
#include "hilog/log.h"
|
||||
// LOG
|
||||
static unsigned int logDomain = 0xD001234;
|
||||
static const char* APP_TAG = "napitest";
|
||||
#define MYAPP_LOGERR(type, ...) ((void)OH_LOG_Print((type), LOG_ERROR, logDomain, APP_TAG, __VA_ARGS__));
|
||||
|
||||
...
|
||||
|
||||
//在需要的地方打印日志
|
||||
MYAPP_LOGERR(LOG_APP, "==========test fun1_middle begin==========");
|
||||
...
|
||||
MYAPP_LOGERR(LOG_APP, "----------test fun1_middle result = %{public}d ----------", vio->out);
|
||||
...
|
||||
MYAPP_LOGERR(LOG_APP, "==========test fun1_middle end==========");
|
||||
```
|
||||
|
||||
5. 在DevEco Studio中增加调用napi方法的测试用例。其中修改index.js文件内容如下:
|
||||
3. 在DevEco Studio中增加调用napi方法的测试用例。其中修改index.js文件内容如下:
|
||||
|
||||
|
||||
```
|
||||
import router from '@ohos.router';
|
||||
import napitest from 'libtest.so';
|
||||
import napitest from 'libnapitest.so';
|
||||
export default {
|
||||
data: {
|
||||
title: ""
|
||||
@ -66,8 +43,8 @@ export default {
|
||||
},
|
||||
ontest: function () {
|
||||
console.log("napitest begin AAAAAAAAAAAAAAAAAA")
|
||||
let testNum = napitest.fun1(1);
|
||||
console.info("napitest testNum = " + testNum);
|
||||
let testOut = napitest.func1("NapiIn");
|
||||
console.info("napitest testOut = " + testOut);
|
||||
console.log("napitest end AAAAAAAAAAAAAAAAAA")
|
||||
}
|
||||
}
|
||||
@ -99,16 +76,12 @@ export default {
|
||||
4. 执行成功后,设备中会出现安装的APP并进入APP测试页面,点击测试按钮DevEco Studio控制台中Log->OpenLog中会出现以下结果:
|
||||
|
||||
```
|
||||
01-01 04:34:40.862 5993-6003/com.example.myapplication D 03b00/JSApp: app Log: napitest begin AAAAAAAAAAAAAAAAAA
|
||||
01-01 04:34:40.862 5993-6003/com.example.myapplication E 01234/napi: ==========test fun1_middle begin==========
|
||||
01-01 04:34:40.862 5993-6003/com.example.myapplication E 01234/napi: ----------test fun1_middle result = 23 ----------
|
||||
01-01 04:34:40.862 5993-6003/com.example.myapplication E 01234/napi: ==========test fun1_middle end==========
|
||||
01-01 04:34:40.862 5993-6003/com.example.myapplication I 03b00/JSApp: app Log: napitest testNum = 23
|
||||
01-01 04:34:40.862 5993-6003/com.example.myapplication D 03b00/JSApp: app Log: napitest end AAAAAAAAAAAAAAAAAA
|
||||
01-01 04:35:26.914 370-890/foundation D 01120/BundleMgrService: [inner_bundle_info.cpp(GetBundleWithAbilities):1733] bundleName:com.example.callbacktest userid:100
|
||||
01-01 09:05:46.719 3225-3237/com.example.myapplication I A0c0d0/JSApp: app Log: napitest begin AAAAAAAAAAAAAAAAAA
|
||||
01-01 09:05:46.719 3225-3237/com.example.myapplication I A0c0d0/JSApp: app Log: napitest testOut = NapiOut
|
||||
01-01 09:05:46.719 3225-3237/com.example.myapplication I A0c0d0/JSApp: app Log: napitest end AAAAAAAAAAAAAAAAAA
|
||||
```
|
||||
|
||||
|
||||
![](../../../figures/DevEco_run_result.png)
|
||||
|
||||
## 相关仓
|
||||
|
||||
|
@ -2,13 +2,15 @@
|
||||
|
||||
## 简介
|
||||
|
||||
NAPI框架生成工具目前支持三种入口,分别是可执行程序、IntelliJ插件、VS Code插件,本文主要介绍IntelliJ插件使用说明。IntelliJ插件下载路径如下,选择napi_generator.jar下载。
|
||||
NAPI框架生成工具目前支持三种入口,分别是可执行程序、IntelliJ插件、VS Code插件,本文主要介绍IntelliJ插件使用说明。IntelliJ插件下载路径如下,选择新版本下载。
|
||||
|
||||
[下载链接1]( http://ftpkaihongdigi.i234.me:5000/sharing/PC6uOorrM)
|
||||
[IntelliJ插件下载链接](https://plugins.jetbrains.com/plugin/19593-napi-generator/versions)
|
||||
|
||||
[下载链接2]( http://ftp.kaihong.com:5000/sharing/PC6uOorrM)
|
||||
![](../../../figures/pic-napi_jar_download.png)
|
||||
|
||||
[下载链接3]( http://ftp.kaihongdigi.com:5000/sharing/PC6uOorrM)
|
||||
使用谷歌浏览器时:
|
||||
|
||||
![](../../../figures/picGoogle-napi_jar_download.png)
|
||||
|
||||
## IntelliJ插件使用方法
|
||||
|
||||
@ -34,13 +36,13 @@ Ability Template选择Empty Ability,单击Next。
|
||||
|
||||
![](../../../figures/DevEco_step_finish.png)
|
||||
|
||||
2.安装插件,File->Settings->Plugins->Installed->Install Plugin from Disk...,选择下载的generator.jar,安装成功之后重启IDE。
|
||||
2.安装插件,File->Settings->Plugins->Installed->Install Plugin from Disk...,选择下载的napi_generator.jar,安装成功之后重启IDE。
|
||||
|
||||
![](../../../figures/DevEco_step_pluginsOk.png)
|
||||
|
||||
![](../../../figures/DevEco_step_applyPlugins.png)
|
||||
|
||||
3.把待转换的.d.ts文件放在DevEco stdio新建项目的src目录下。若.d.ts文件中声明了basic.d.ts文件,将basic.d.ts文件放置在待转换.d.ts文件上一级目录;若除此之外还声明其它.d.ts文件,将此类文件放置在待转换.d.ts文件同级目录。
|
||||
3.把待转换的.d.ts文件放在DevEco stdio新建项目的src目录下。若.d.ts文件中声明了basic.d.ts文件,将basic.d.ts文件放置在待转换.d.ts文件同一级目录;若除此之外还声明其它.d.ts文件,将此类文件放置在待转换.d.ts文件同级目录。
|
||||
|
||||
![](../../../figures/DevEco_step_napi.png)
|
||||
|
||||
|
@ -64,20 +64,12 @@ napi_vs_plugin
|
||||
```c++
|
||||
#include "napitest.h"
|
||||
|
||||
|
||||
namespace napitest {
|
||||
|
||||
bool TestClass1::if_direct(std::string &v1, std::string &out) {
|
||||
bool func1(std::string& v1, std::string& out)
|
||||
{
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TestClass1::if_callback(std::string &v1, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
......
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -29,7 +29,7 @@ visual studio code 版本需1.62.0及以上。
|
||||
|
||||
![](../../../figures/pic-plug-in-gnapi.png)
|
||||
|
||||
4.在VS Code中找到需要转换的.d.ts文件,若.d.ts文件中声明了basic.d.ts文件,将basic.d.ts文件放置在待转换.d.ts文件上一级目录;若除此之外还声明其它.d.ts文件,将此类文件放置在待转换.d.ts文件同级目录。例如:
|
||||
4.在VS Code中找到需要转换的.d.ts文件,若.d.ts文件中声明了basic.d.ts文件,将basic.d.ts文件放置在待转换.d.ts文件同一级目录;若除此之外还声明其它.d.ts文件,将此类文件放置在待转换.d.ts文件同级目录。例如:
|
||||
|
||||
![](../../../figures/pic-plug-in-select-d-ts.png)
|
||||
|
||||
|
@ -23,7 +23,7 @@ function doGenerate(ifname, destdir, imports,numberType) {
|
||||
let fn = re.getFileInPath(ifname);
|
||||
let tt = re.match('(@ohos\.)*([.a-z_A-Z0-9]+).d.ts', fn);
|
||||
if (tt) {
|
||||
let moduleName = re.getReg(fn, tt.regs[1]);
|
||||
let moduleName = re.getReg(fn, tt.regs[2]);
|
||||
let importsStr = '' + imports
|
||||
if (importsStr == 'true') {
|
||||
importsFun(structOfTs.imports, destdir, ifname);
|
||||
|