934d8306d9
Signed-off-by: zhaoxudong <bayanxing@kaihongdigi.com> |
||
---|---|---|
docs | ||
examples | ||
figures | ||
prebuilt | ||
release-notes | ||
src | ||
test | ||
.gitattributes | ||
.README_ZH.md.swp | ||
DEVELOP_ZH.md | ||
FAQ.md | ||
LICENSE | ||
OAT.xml | ||
package-lock.json | ||
package.json | ||
README_ZH.md | ||
README.md |
napi_generator
Introduction
This paper mainly introduces the NAPi framework code generation tool, which can generate NAPi framework code, business code framework and designed.the GN file according to the TS interface file in the path specified by the user. So developers need to focus on Nodejs related grammar framework layer, interface between c + + with JS type conversion, data type conversion, etc.. This tool is mainly used in the scenario of JS application calling NaPi interface, can focus on business logic implementation, Thus, the development efficiency can be greatly improved. Currently, the tool supports command line and VScode plug-in.
Software architecture
Directory Structure
├── napi_generator # NAPI framework code generation tool
│ ├── docs # Plan the current tool capabilities and versions
│ ├── figures # Image resource file
│ ├── code # Tool source
| | ├── tool_code # Tool source
│ | ├── gen
│ | |── analyze # Parser
│ │ |── extend # Extension module, including GN file generation, Linux environment adaptation code, etc
│ │ |── generate # Generator
│ │ └── tools # Public module code, including message body check, file read and write, regular expression
│ | └── test # Plug-in test cases
| | └──ts_test # Example ts files required by the tool
│ └── README
└──README
code directory source code introduction
This directory is the source code corresponding to the NAPI framework code generation tool, developers can be based on this code for secondary development.
Software environment preparation
System: Ubuntu 20.04 or windows 10 is recommended
Dependent version: vscode 1.62.0
Development description
Developers can use the source code to compile and package the corresponding executable files and plug-ins for type conversion between JS and C + +. The development instructions of executable files and plug-ins are as follows:
Source code
Through the command: git clone git@gitee.com:openharmony-sig/napi_generator.git to download the corresponding source code.
pack
Executable file development instructions
1.Install typescript: execute the command npm i typescript under napi_generator directory
2.Install stdio: execute the command npm i stdio under napi_generator direction
3.Install pkg :execute the command sudo npm i -g pkg
4.Package three versions : execute the command pkg .
After performing the preceding steps,ou can generate Windows, Linux, and MAC executable programs in the Gen directory:napi_generator-win.exe、napi_generator-linux、napi_generator-macos.
Plug-in development Instructions
-
Install yo : execute the command npm install -g yo generator-code.
-
Use yo : execute the command yo code,gnapi is a plug-in code framework generated by Yo Code, in which gnAPI/Gen directory is the source code of the automatic generation of NAPI program we developed.
xudong@xudong:~/gen_plug_napi/napi/gen$ ls analyze analyze.js cmd_gen.js extend generate generate.js main.js test.js tools
-
Run npm i vsce in the napi_generator/vscode_plugin/gnapi directory.
-
Execute the command./node_modules/vsce/vsce package command,The final package generates a plugin, gnapI-0.0.1.vsix.
xudong@xudong:~/gen_plug_napi/napi$ npx vsce package WARNING A 'repository' field is missing from the 'package.json' manifest file. Do you want to continue? [y/N] y WARNING Using '*' activation is usually a bad idea as it impacts performance. More info: https://code.visualstudio.com/api/references/activation-events#Start-up Do you want to continue? [y/N] y WARNING LICENSE.md, LICENSE.txt or LICENSE not found Do you want to continue? [y/N] y DONE Packaged: /home/xudong/gen_plug_napi/napi/napi3-0.0.1.vsix (30 files, 37.7KB) xudong@xudong:~/gen_plug_napi/napi$ ls CHANGELOG.md extension.js gen jsconfig.json napi3-0.0.1.vsix node_modules package.json package-lock.json README.md test vsc-extension-quickstart.md xudong@xudong:~/gen_plug_napi/napi$
instructions
- Copy the Windows executable program napi_generator-win.exe from the gen directory to the corresponding directory. You do not need to install the program.
- Copy the Linux executable program napi_generator-linux from the gen directory to the corresponding directory, and run it directly on the terminal.
Usage
Windows
- The.d.ts files to be converted should be placed in any directory. It is recommended to put them in the same directory as the executable program napi_generator-win.exe. And check whether the imported d.ts file is declared in the d.ts file to be converted. If so, it is necessary to put the imported d.ts file into the same directory as the d.ts file to be converted, for example:
- Right-click Windows Start menu, click Run, enter CMD, and click OK.
-
In the command line interface (CLI), go to the directory where the previous executable program napi_generator-win.exe resides and run napi_generator-win.exe. After napi_generator-win.exe, the corresponding.
E:\napi_tool>napi_generator-win.exe -f @ohos.power.d.ts
The command line parameters are as follows, - f is the parameter that must be added and - o is the optional parameter. If it is not added, the current directory will be added by default.
-f, --filename <ARG1> .d.ts file -o, --out <ARG1> output directory ("." by default)
- After successful operation, the corresponding file will be generated in the directory where the. D. ts file says, for example:
Linux
-
Put the.d.ts files to be converted in any directory. It is recommended to put them in the same directory as the executable program napi_generator-linux. And check whether the imported d.ts file is declared in the d.ts file to be converted. If so, it is necessary to put the imported d.ts file into the same directory as the d.ts file to be converted, for example:
xudong@xudong:~/napi_tool$ ls napi_generator-linux @ohos.power.d.ts basic.d.ts xudong@xudong:~/napi_tool$
-
In the terminal, go to the directory where the previous executable programnapi_generator-linux resides and run napi_generator-linux. After napi_generator-linux, the corresponding. D. s file name, for example:
xudong@xudong:~/napi_tool$ ls napi_generator-linux @ohos.power.d.ts basic.d.ts xudong@xudong:~/napi_tool$ ./napi_generator-linux -f @ohos.power.d.ts
-
After successful operation, the corresponding file will be generated in the directory where the. D. ts file says, for example:
xudong@xudong:~/napi_tool$ ls basic.d.ts binding.gyp BUILD.gn napi_generator-linux @ohos.power.d.ts power.cpp power.h power_middle.cpp test.sh x_napi_tool.cpp x_napi_tool.h xudong@xudong:~/napi_tool$
Method Step For details about how to use Windows and Linux
Plug-in usage
Instruction
Visual Studio Code version 1.62.0 or later
Steps
-
Open vscode and select plug-in installation in the left sidebar.
-
Click the three buttons above, select the install from VSIX option, then select the gnapI-0.0.1. VSIX plug-in file you just generated, and click Install.
-
After installation, the gnapi plugin should be visible in vscode's plugin manager.
-
Find the.d.ts file to convert in vscode, for example:
-
Right click on.d.ts and select.d.ts to generate c++ options.
-
The corresponding files will be generated in this directory, for example:
Matters needing attention
The examples above are for Windows. The methods for Linux and MAC are similar.
Participate in the contribution
Not yet.
Related to the storehouse
Not yet.