mirror of
https://gitee.com/openharmony/napi_generator
synced 2024-11-27 02:30:36 +00:00
add vs plugin dtscpp template
Signed-off-by: gou-jingjing <goujingjing@kaihong.com>
This commit is contained in:
parent
787b65abcc
commit
b9f3fe8450
@ -0,0 +1,26 @@
|
||||
import { FileTemp } from "../../datatype";
|
||||
|
||||
export let napiCommonCppTemplate: FileTemp = {
|
||||
name: '[fileName]common.cpp',
|
||||
content: `
|
||||
#include "[fileName]common.h"
|
||||
|
||||
/*[NAPI_GEN]:错误处理,获取错误详细信息*/
|
||||
void getErrMessage(napi_status &status, napi_env &env, const napi_extended_error_info *&extended_error_info,
|
||||
const char *info, const char *tag)
|
||||
{
|
||||
status = napi_get_last_error_info(env, &extended_error_info);
|
||||
if (status == napi_ok && extended_error_info != NULL) {
|
||||
const char *errorMessage =
|
||||
extended_error_info->error_message != NULL ? extended_error_info->error_message : "Unknown error";
|
||||
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "errmsg %{public}s!, engine_err_code %{public}d!.",
|
||||
std::to_string(extended_error_info->engine_error_code).c_str(), extended_error_info->error_code);
|
||||
std::string myInfo = info;
|
||||
std::string res = "Failed to " + myInfo + " em = " + errorMessage +
|
||||
", eec = " + std::to_string(extended_error_info->engine_error_code) +
|
||||
", ec = " + std::to_string(extended_error_info->error_code);
|
||||
napi_throw_error(env, NULL, res.c_str());
|
||||
}
|
||||
}
|
||||
`
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
import { FileTemp } from "../../datatype";
|
||||
|
||||
export let napiCommonHTemplate: FileTemp = {
|
||||
name: '[fileName]common.h',
|
||||
content: `
|
||||
#ifndef NAPITUTORIALS_[upper_filename]COMMON_H
|
||||
#define NAPITUTORIALS_[upper_filename]COMMON_H
|
||||
|
||||
#include <js_native_api.h>
|
||||
#include <js_native_api_types.h>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include "hilog/log.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "[fileName].h"
|
||||
|
||||
#define GLOBAL_RESMGR (0xFFEE)
|
||||
const unsigned int LOG_PRINT_DOMAIN = 0xFF00;
|
||||
|
||||
constexpr int32_t STR_MAX_SIZES = 200;
|
||||
constexpr int32_t LONG_STR_MAX_SIZES = 1024;
|
||||
constexpr uint8_t PARAMS0 = 0;
|
||||
constexpr uint8_t PARAMS1 = 1;
|
||||
constexpr uint8_t PARAMS2 = 2;
|
||||
constexpr uint8_t PARAMS3 = 3;
|
||||
constexpr uint8_t PARAMS4 = 4;
|
||||
constexpr uint8_t PARAMS5 = 5;
|
||||
constexpr uint8_t PARAMS6 = 6;
|
||||
constexpr uint8_t PARAMS7 = 7;
|
||||
constexpr uint8_t PARAMS8 = 8;
|
||||
constexpr uint8_t PARAMS9 = 9;
|
||||
constexpr uint8_t PARAMS10 = 10;
|
||||
constexpr uint8_t PARAMS11 = 11;
|
||||
constexpr uint8_t PARAMS12 = 12;
|
||||
constexpr uint8_t PARAMS100 = 100;
|
||||
|
||||
void getErrMessage(napi_status &status, napi_env &env, const napi_extended_error_info *&extended_error_info,
|
||||
const char *info, const char *tag);
|
||||
|
||||
#endif //NAPITUTORIALS_[upper_filename]COMMON_H
|
||||
`
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
import { FileTemp } from "../../datatype";
|
||||
|
||||
export let indexdtsTemplate:FileTemp = {
|
||||
name: 'index.d.ts',
|
||||
content: `[dts_content_template]
|
||||
`
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
import { FileTemp } from "../../datatype";
|
||||
|
||||
export let napiCppTemplate: FileTemp = {
|
||||
name: '[fileName]napi.cpp',
|
||||
content: `#include "[fileName]napi.h"
|
||||
|
||||
[func_content_replace]
|
||||
`
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
import { FileTemp } from "../../datatype";
|
||||
|
||||
export let napiHTemplate: FileTemp = {
|
||||
name: '[fileName]napi.h',
|
||||
content: `
|
||||
#ifndef NAPITUTORIALS_[upper_filename]NAPI_H
|
||||
#define NAPITUTORIALS_[upper_filename]NAPI_H
|
||||
|
||||
#include "[fileName]common.h"
|
||||
|
||||
[func_declare_replace]
|
||||
|
||||
#endif //NAPITUTORIALS_[upper_filename]NAPI_H
|
||||
`
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
import { FileTemp } from "../../datatype";
|
||||
|
||||
export let napiInitTemplate: FileTemp = {
|
||||
name: '[fileName]init.cpp',
|
||||
content: `#include "napi/native_api.h"
|
||||
#include <hilog/log.h>
|
||||
#include <string>
|
||||
#include "[fileName]napi.h"
|
||||
|
||||
EXTERN_C_START
|
||||
|
||||
/*[NAPI_GEN]:Init函数被定义用来初始化模块(比如设置导出的函数或对象)*/
|
||||
static napi_value Init(napi_env env, napi_value exports)
|
||||
{
|
||||
/* [NAPI_GEN]:napi_property_descriptor是Node.js中N-API的一个结构体,它用来描述一个对象的属性,这包括数据属性和访问器
|
||||
* 属性(通常是getter和setter函数), 这个结构体包含了以下字段
|
||||
* utf8name: 属性的名称,以 null 结尾的 UTF-8 字符串。
|
||||
* name: 可选,代替 utf8name 的 napi_value 类型的名称。这允许使用预先存在的 napi_value,可能是一个字符串或符号。
|
||||
* method: 如果这个属性是一个函数,这里会是一个指向原生函数的指针。
|
||||
* getter: 如果这个属性有一个 getter 函数,这里会是一个指向该 getter 原生函数的指针。
|
||||
* setter: 如果这个属性有一个 setter 函数,这里会是一个指向该 setter 原生函数的指针。
|
||||
* value: 如果这个属性是一个数据属性,这里会是一个 napi_value 表示属性的值。
|
||||
* attributes: 属性的描述符,指定属性是否可写(napi_writable)、可枚举(napi_enumerable)、可配置(napi_configurable)等。
|
||||
* data: 一个指针,指向与属性相关联的任何原生数据。这通常用于存储与 getter 和 setter 相关联的上下文信息。
|
||||
*/
|
||||
napi_property_descriptor desc[] = {
|
||||
[init_replace]
|
||||
};
|
||||
|
||||
/* [NAPI_GEN]:napi_define_properties 是一个N-API函数,它允许原生模块在 JavaScript 对象上定义新的属性。允许一次性定义多个属性。
|
||||
* 这个函数是在原生代码中定义对象形状的关键工具,可以用来向对象添加属性,包括数据属性和访问器属性(getter/setter)
|
||||
* env: N-API 环境句柄,代表当前的运行环境。
|
||||
* object: 一个 napi_value,指向你想要定义属性的对象。
|
||||
* property_count: 属性描述符数组中的元素数量,告知函数有多少个属性需要定义。
|
||||
* properties: 指向 napi_property_descriptor 结构体数组的指针,每个结构体描述了一个要定义的属性。
|
||||
*/
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
return exports;
|
||||
}
|
||||
EXTERN_C_END
|
||||
static napi_module demoModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = Init, /*[NAPI_GEN]:将初始化函数Init与模块关联起来*/
|
||||
.nm_modname = "entry",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
|
||||
{
|
||||
/*[NAPI_GEN]:napi_module_register 宏被用来静态注册模块初始化函数,使得Node.js在加载模块时调用它。*/
|
||||
napi_module_register(&demoModule);
|
||||
}
|
||||
`
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
import { FileTemp } from "../../datatype";
|
||||
|
||||
export let dtscppReadmeTemplate: FileTemp = {
|
||||
name: 'readme.md',
|
||||
content: `## 依赖
|
||||
|
||||
插件版本: 0.0.1
|
||||
|
||||
DevEco Studio: DevEco Studio 4.1 Release
|
||||
|
||||
## 使用方法
|
||||
|
||||
生成物具体如何使用请参考以下链接:
|
||||
|
||||
[usage](https://gitee.com/openharmony/napi_generator/blob/master/src/cli/h2dtscpp/docs/usage/INSTRUCTION_ZH.md)
|
||||
|
||||
`
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
import { FileTemp } from "../../datatype";
|
||||
|
||||
export let testFirstGenTemplate:FileTemp = {
|
||||
name: '[fileName]Ability.test.ets',
|
||||
content: `import hilog from '@ohos.hilog';
|
||||
import testNapi from 'libentry.so';
|
||||
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
||||
|
||||
export default function [fileName]abilityTest() {
|
||||
describe('[fileName]ActsAbilityTest', () => {
|
||||
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
||||
beforeAll(() => {
|
||||
// Presets an action, which is performed only once before all test cases of the test suite start.
|
||||
// This API supports only one parameter: preset action function.
|
||||
})
|
||||
beforeEach(() => {
|
||||
// Presets an action, which is performed before each unit test case starts.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: preset action function.
|
||||
})
|
||||
afterEach(() => {
|
||||
// Presets a clear action, which is performed after each unit test case ends.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: clear action function.
|
||||
})
|
||||
afterAll(() => {
|
||||
// Presets a clear action, which is performed after all test cases of the test suite end.
|
||||
// This API supports only one parameter: clear action function.
|
||||
})
|
||||
|
||||
[testAbilityFunctions]
|
||||
})
|
||||
}
|
||||
`
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
import { DirTemp } from "../../datatype";
|
||||
import { napiCommonCppTemplate } from "./dtscpp_commoncpp_template";
|
||||
import { napiCommonHTemplate } from "./dtscpp_commonh_template";
|
||||
import { indexdtsTemplate } from "./dtscpp_dts_template";
|
||||
import { napiCppTemplate } from "./dtscpp_napicpp_template";
|
||||
import { napiHTemplate } from "./dtscpp_napih_template";
|
||||
import { napiInitTemplate } from "./dtscpp_napiinit_template";
|
||||
import { dtscppReadmeTemplate } from "./dtscpp_readme_template";
|
||||
import { testFirstGenTemplate } from "./dtscpp_testfirstgen_template";
|
||||
|
||||
// out/tsout
|
||||
export let dtscpp_tsout: DirTemp = {
|
||||
name: 'tsout',
|
||||
files: [indexdtsTemplate],
|
||||
dirs: []
|
||||
}
|
||||
|
||||
// out/testout
|
||||
export let dtscpp_testout: DirTemp = {
|
||||
name: 'testout',
|
||||
files: [testFirstGenTemplate],
|
||||
dirs: []
|
||||
}
|
||||
|
||||
// out/cppout
|
||||
export let dtscpp_cppout: DirTemp = {
|
||||
name: 'cppout',
|
||||
files: [napiCommonHTemplate, napiCommonCppTemplate, napiHTemplate, napiInitTemplate, napiCppTemplate],
|
||||
dirs: []
|
||||
}
|
||||
|
||||
export let dtscppout: DirTemp = {
|
||||
name: '',
|
||||
files: [dtscppReadmeTemplate],
|
||||
dirs: [dtscpp_cppout, dtscpp_testout, dtscpp_tsout]
|
||||
}
|
Loading…
Reference in New Issue
Block a user