add vs plugin dtscpp template

Signed-off-by: gou-jingjing <goujingjing@kaihong.com>
This commit is contained in:
gou-jingjing 2024-10-21 17:47:21 +08:00
parent 787b65abcc
commit b9f3fe8450
9 changed files with 246 additions and 0 deletions

View File

@ -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());
}
}
`
}

View File

@ -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
`
}

View File

@ -0,0 +1,7 @@
import { FileTemp } from "../../datatype";
export let indexdtsTemplate:FileTemp = {
name: 'index.d.ts',
content: `[dts_content_template]
`
}

View File

@ -0,0 +1,9 @@
import { FileTemp } from "../../datatype";
export let napiCppTemplate: FileTemp = {
name: '[fileName]napi.cpp',
content: `#include "[fileName]napi.h"
[func_content_replace]
`
}

View File

@ -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
`
}

View File

@ -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_descriptorNode.jsN-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_writablenapi_enumerablenapi_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);
}
`
}

View File

@ -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)
`
}

View File

@ -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]
})
}
`
}

View File

@ -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]
}