1. Business scenarios

When I joined the new company, I took over a project, which was to use an SDK for application development. Because some customization functions in the application can only be realized by modifying SDK source code, but this SDK is developed by another department and does not have the release permission of this SDK, only the pull permission of SDK source git repository.

Then copy the SDK file generated by SDK source compilation package to the application project code folder, and then reference the index.js in the SDK file in the application project code to develop.

2. Application of FS-Extra

Fs-extra is a library of file manipulation related tools.

Every time you modify the SDK source code, you need to compile and package the SDK file first, and then manually copy the SDK file to the application project code folder, this process is also quite annoying. The Git repository with SDK source code does not have push permission, nor does the modified SDK source code have version management. Therefore, the SDK source code and application code are developed in the same folder, and the same Git repository is used for code version management. The schematic diagram of the folder directory structure is as follows:

The sdkcode/ SRC file represents the SDK source file, and the sdkcode/lib file represents the SDK file generated after the SDK source code is compiled and packaged. The appcode/ SRC file represents the application code file, and the appcode/lib file represents the SDK file copied from the application.

Then you can use the API in FS-Extra to develop a JS script, which automatically deletes the lib folder in the appCode folder after execution, and then copies the sdkcode/lib folder to the appcode/ SRC folder to update the SDK in the application project.

Put the JS script in the root directory of the AppCode folder named updatasdk. js, and run the node development/ updatasdk. js command in the AppCode directory.

const fs = require('fs-extra');
const path = require('path');
/* libPath is the path of the appcode/lib file */
const libPath = path.resolve(__dirname, './lib');
/* sdkLibPath is the sdkcode/lib file path */
const sdkLibPath = path.resolve(__dirname, './lib');
/* Delete the appcode/lib file if it exists */
if (fs.existsSync(libPath)) {
  fs.removeSync(libPath);
}
/* Copy the appcode/lib file to the appcode folder */
fs.copySync(libPath, sdkLibPath);
Copy the code

3. Pain points during development

When a new version of SDK is released, if you want to update the SOURCE code of SDK in the application project, the operation process is as follows:

  • After creating a branch with the tag corresponding to the new version in the GIT repository, copy the SDK source code.

  • Create a new branch sdkcore with blank content in the git repository of the application project, paste the SDK source code into it, and submit the branch;

  • Switch to devlop development branch in git repository of application project to merge SDkcore branch and update SDK source;

  • After the update is complete, delete the SDkcore branch.

The above mode of updating SDK source code is fine in one application project, but in multiple application projects, there will be the following pain points.

  • 1. The Git repository of each application project contains SDK source code, resulting in the huge volume of git repository;

  • 2. Update THE SDK source code in each application project. It will take a lot of time to copy and paste the SDK source code.

  • 3. Failure to quickly switch the SDK source code on which the application project depends.

Git submodules can be used to solve all of the above pain points.

4. Concept of Git sub-module

Make one Git repository the parent repository and create multiple git repositories within it as child repositories. These repositories are git child modules, and parent repositories are Git parent modules.

Git operation of the child repository does not affect the parent repository. For example, when submitting the code of the child repository, the code of the parent repository will not be submitted. However, if the parent repository does not ignore the folder where the child repository is located, the code in the child repository will be submitted to the parent repository.

5. Git sub-module application

Extract SDK source code from each application project and use a Git repository for unified management, which is called SDK repository. Create branches in the SDK repository with the name of each application project, and the SDK source code in each branch corresponds to the SDK source code that the respective application project relies on.

Create a Git repository for each application project. Then make the SDK repository as the parent repository, that is, the Git parent module, and then add the repository for each application project as a sub-module in the SDK repository, so that the SDK source code is associated with the application project.

The above operation is equivalent to separating the SDK source code from the Git repository of each application project, and the volume of the Git repository of each application project will be greatly reduced, which solves the first pain point in the development process.

Create a blank branch without any content in the SDK repository, name it updata branch and push it up, it will be used in the update SDK source code. When a new version of the SDK is released, create a new branch in the SDK repository with the Updata branch named SDK version number.

Create a branch with the new version tag in the SDK source Git repository, and drop down the code to copy the SDK source code. Paste the SDK source code into the SDK repository folder, submit the SDK repository after pasting, and push it up.

If you want to update the SDK source code in the subsequent application project, you can cut to the SDK repository branch corresponding to the application project, and then merge the branches corresponding to the SDK version to realize the update of THE SDK source code. This only copies the SDK source once, instead of copying the SDK source several times to update several application projects, which solves the second pain point of the development process.

When developing the application PROJECT A, switch the SDK repository to the A branch, and then run the updatasdk. js script to copy the SDK file generated after the SDK source code is compiled and packaged to the application project A. The updatasdk. js script is stored in the root directory of each application project.

In the subsequent development process, application A project needs to use SDK source code of version 7.3.5 to check a BUG, and then the SDK repository can be switched to branch 7.3.5 (the code of this branch is the code in TAG V7.3.5 in SDK source repository). Then execute the updatasdk. js script to copy the SDK file generated after the SDK source code is compiled and packaged to the application project A, which realizes the problem of checking the SDK source code that the application project relies on and solves the third pain point in the development process.

6. Git actual combat operation

  • Create a repository on GitLab named sdk_source_code to manage the SDK source code in each application project. This git repository is called the SDK repository.

  • Once you have git installed, right-click on your D drive and choose git Bash Here to open the git command line tool.

  • Execute mkdir project to create a folder named project;

  • Git Clone SDK repository address to clone the SDK repository.

  • After cloning, a sdk_source_code folder is generated in the project folder. Run CD sdk_source_code to enter the sdk_source_code folder.

  • Git checkout -b updata and git push –set-upstream origin updata It is used to update the SDK source code.

  • Git checkout -b: git checkout -b: git checkout -b: git checkout -b: git checkout Git remote set-url origin SDK repository address, git push, so that the SDK source in application A project is submitted to the SDK repository branch named application A name.

  • Go back to the sdk_source_code folder and run git submodule add the git repository address of the application A project to add a Git submodule to the SDK repository. Once added, you will find a new folder in the sdk_source_code folder with the name of the Application A project. The code in this folder is the code in the Application A project repository.

  • Git rm -r –cached. Git add. Git commit -m ‘update. gitignore’ git push. Omit the application a project code from the SDK repository, so that the application A project code is not submitted to the SDK repository.

  • Finally, note that the app ignores the code for the app A project in the SDK repository. Git subModule init cannot be used to initialize submodules (clone application A project code) after cloning the SDK repository elsewhere. You can initialize the submodules by directly executing the Git Clone application a project’s Git repository address at the root of the SDK repository folder.