Go to file
bayanxing 4bf1f058f4 !7 modify readme file
Merge pull request !7 from Joey/feature/330
2022-03-01 01:27:05 +00:00
docs modify md 2022-02-24 15:18:42 +08:00
figures add md 2022-02-24 14:44:11 +08:00
prebuilt Fix the problem that the plug-in cannot be used 2022-02-22 17:06:25 +08:00
release-notes add generator 2022-02-17 17:45:37 +08:00
src modify license style 2022-02-25 16:24:43 +08:00
test modify unittest codecheck 2022-02-28 10:42:24 +08:00
ts_test/gen Add copyright license 2022-02-21 20:15:46 +08:00
DEVELOP.md modify readme file 2022-02-28 10:53:28 +08:00
FAQ.md modify readme 2022-02-15 10:50:35 +08:00
LICENSE Adjust directory structure 2022-02-10 10:28:10 +08:00
OAT.xml add generator 2022-02-18 10:50:23 +08:00
package-lock.json add autotest 2022-02-23 11:32:29 +08:00
package.json !34 【napi工具】增加自动化测试用例 2022-02-23 06:48:49 +00:00
README_ZH.md Fix the problem that the plug-in cannot be used 2022-02-22 17:06:25 +08:00
README.md Add pre check for d.ts files 2022-02-17 15:32:46 +08:00

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 stepsou 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

  1. Install yo : execute the command npm install -g yo generator-code.

  2. Use yo : execute the command yo codegnapi 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
    
    
  3. Run npm i vsce in the napi_generator/vscode_plugin/gnapi directory.

  4. Execute the command./node_modules/vsce/vsce package commandThe 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

  1. 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.
  2. 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

  1. 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:

  1. Right-click Windows Start menu, click Run, enter CMD, and click OK.

  1. 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)
    
  1. After successful operation, the corresponding file will be generated in the directory where the. D. ts file says, for example:

Linux

  1. 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$ 
    
  2. 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 
    
  1. 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

  1. Open vscode and select plug-in installation in the left sidebar.

  2. 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.

  3. After installation, the gnapi plugin should be visible in vscode's plugin manager.

  4. Find the.d.ts file to convert in vscode, for example:

  5. Right click on.d.ts and select.d.ts to generate c++ options.

  6. 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.

Not yet.