Android builds proprietary hooks to kill non-standard codes in the bud

As the saying goes, there is no rule without rules, and it is also very appropriate to put it in the code. The so-called rules in the code refer to the norms. Projects under the constraints of certain norms are very important whether they participate in development or later maintenance. Intuitive and convenient, it can’t be said to be pleasing to the eye, but can also be expressed in terms of robustness and maintainability; after all, for collaborative development projects, everyone has their own set of development standards. You don’t have a set of specifications, or the specifications have not been implemented. Think about it , if things go on like this, what will happen? Code piled up? Maintenance costs doubled? Difficulty for newcomers to take over? Wait, the so-called problems will come to you.

As the so-called specification is the cornerstone of a project, it is also a standard for measuring whether a project is robust, stable, and maintainable. It can be said to be very important. I believe that most companies have their own set of norms and standards. I also believe that many of them may be just decorations. After all, there are so many people that it is impossible to restrain them one by one. A lot of time and labor costs, based on this, the so-called norms are also difficult to implement.

Due to the investment of labor and time, I have developed a visual code inspection tool in my previous research and exploration. I have shared it before, "A convenient and easy-to-operate Android visual specification inspection", and the veterans who want to know can see it At first glance, this article does not introduce too much. At that time, it only introduced the use of tools, and did not introduce the development process of related functions. Later, when I have time, I will sort out the open source and have been busy with development. Old irons, please understand . Although this visual inspection tool greatly improves the inspection efficiency and saves manpower and time, it has a potential drawback, that is, it can only check whether the code after submission meets the specification. , before submitting, whether it is standardized or not, it can be submitted. After checking with tools, make modifications, and then submit after changing the irregularities. This can only be checked in such a mode.

Such a model is more consistent. The final code inspection is suitable for the project leader, the development leader, and the standard review of the code submitted by the team members. In fact, it does not apply to developers. Not applicable does not mean that it is not available. It’s just a few steps more complicated than the process; to deal with such a factor, how to apply it to developers, so as to facilitate standard inspection before submitting code, put it on the research and development agenda as a whole. After a few days of research and writing, a simple The convenient Android-side Git submission proprietary hook came into being.

It is simple because there is no need to write any code logic, and it only needs a few steps of commands to complete the installation. Through the configuration file, you can flexibly customize your own inspection scope.

In order to better explain the functions and describe the implementation process, it is convenient for everyone to customize their own development specifications, coupled with space constraints, I have summarized four articles to sort out the system, and please stay tuned. Today’s article, mainly Tell the final development results, that is, how to standardize the use of tools, standardize this thing, in fact, the difference is not bad, you can use this set that I have developed myself.

The development of this tool uses git hooks (hooks), and of course uses the related functions implemented by Node.js. The next article will introduce in detail. Let’s install the program first to witness the actual effect. Install the program , you only need to execute a few steps of commands, no code intervention is required, and developers are required to install them separately in actual development.

installation process


1. Install Node.js, if it is already installed, you can go directly to step 2:

Node.js allows the use of JavaScript to develop server and command line programs. We can go to the official website https://nodejs.org
to download the latest version of the installation program, and then install it step by step. There is nothing to say about this, it is all development personnel.

2. Install android_standard

android_standard is the ultimate tool, which contains various logics for intercepting code judgments. Execute the following command in the project root directory:

npm install android_standard --save-dev

After executing the command, you will find that there is an additional directory under your project, and there are two json files, as shown in the following figure:

node_modules, which is used to store the downloaded and installed package folder, contains the functions we want to use. In fact, it is very similar to the lib directory in Android, and they are all libraries that provide functions.

The package.json file is a configuration file, such as the name, author, introduction, and related dependencies of the application. It is similar to the build.gradle file in Android.


3. Create a git configuration file and execute the following command

node node_modules/android_standard/gitCommitConfig

If the command is successfully executed, the following information will be returned:

After this command is executed, the gitCommitConfig file will be created in the root directory of the project. This file is very important. It is the configuration file for us to execute related commands. The content is as follows. You can modify it according to your actual project needs.

The gitCommitConfig.android file is generated under the project, and .android is defined by myself. As for the format, it can be completely customized when you develop it yourself, just a file.

After opening, the content of the file is as follows. This file is more important. All subsequent standard checks must be performed according to the parameters in this file. When you use it, you can use this file to operate specific standard checks.

4. Change the execution file and execute the following command

The execution file is the package.json file that needs to be generated above, and the running program is added to make it hook intercept when git is submitted.

node node_modules/android_standard/package

5. Add git filter

Because after executing the above command, several files will be generated, and these files do not need to be uploaded to the remote warehouse, so we need to add ignore to the .gitignore file and copy it directly.

/node_modules
package.json
package-lock.json
gitCommitConfig.android

6. If there is an update in the future, you can operate the command to operate:

Note: This command is executed when updating

npm update android_standard --save-dev

7. Delete operation

Note: If you don’t want to use it later, you can execute the following command:

npm uninstall android_standard --save-dev

Specific use

Through the above installation process, our specification check is installed in just a few commands, and then we only need to dynamically change the parameters through the gitCommitConfig.android file. Isn’t it very convenient? Next, let’s do the actual Do something.

There are also comments on the relevant parameters of the configuration file, which can be seen at a glance. Here is a simple explanation for the last parameter, that is, the parameter of gitCommand, true is the tool, false is the command method; true or false , there is no difference in the main function verification, the only difference is that the command line submission will be color-coded, and there will be effects later.

Let's first look at the execution effect under the command line. When the configuration file switch gitCommitSwitch is enabled and gitCommand is false, you can modify other configuration parameters according to your needs. When the code is submitted, the effect is as follows:

 Submit the code execution effect in Android studio

 TortoiseGit submitted code execution effect:

At present, for the standard inspection on the Android side, whether it is java, Kotlin, or resource files, certain adaptations have been made. After many tests, everything is normal. If your company also needs such a hook tool, welcome to use it. Welcome to continue to pay attention to the next related implementation logic articles.

Well, everyone, this article ends here first, and the next article will describe the specific implementation process, so stay tuned!

Guess you like

Origin blog.csdn.net/ming_147/article/details/126813366