Understanding Large Framework Source Code Better With GREP

grep is a tool to find text in files. For example, if we have 1000 files, if we want to know what files that contain the word of “indonesia”, using grep we can find it easily.

grep is a tool that runs via the command line or terminal. Usually grep is among the default application in linux, so we don’t need to install it manually. There is a windows version, but because I rarely use windows for application development, I never tried it.

For a programmer, grep is very useful if we are working on a large project, or a project that we do not create from scratch, for example because we use a certain framework. So it is impossible for us to memorize in what file a certain feature is written.

As an example case, in odoo there is a save button as shown below.

On my computer, the odoo 13 community edition consists of 28,909 files and directories, with a size of 694 MB. The question is, in what file, the save button above was written ? Should we read one by one those 28 thousand files and directories to know the file name ?

There are several options or parameters to run the grep command. But I prefer this.

grep -Rl 'PATTERN'

The -R option is a recursive option, its means that the grep doesn’t only read the current active directory. If the current active directory has a child, grep will read the child and the grandchildren of that directory until the last directory.

The -l option or it can also be written as –files-with-matches is used to display the file names only. By default, if this option is not used grep will display the file name along with the text snippet according to the word or keyword we are looking for. If the file that we are looking for is contains matches word more than one line, grep will display all of them. Meanwhile, if we use this option grep will display the file name only without text snippet, so it looks tidier and easier to read.

‘PATTERN’ is the word or text or keyword that we are looking for. Pattern can be written as is or using regular expression. Pattern must be wrapped in single or double quote.

In the case of the save button above, we need to know the pattern or word that we can use as a keyword. Because odoo is a web-based application, we can inspect the element with the google chrome developer tool.

You can see that the save button in the image above has a class o_form_button_save so we can use this class as a pattern or keyword. Before typing the grep command, first, we must enter to the directory where our project is located in terminal. Open a terminal then go to the directory where our odoo project is located. Because my odoo project is located in /odoo13/odoo13-server directory, I can use the command like below.

cd /odoo13/odoo13-server

Then type the grep command with the text of o_form_button_save as a keyword as shown below.

You can see that the text of o_form_button_save is exist in 18 files. Now we can check those 18 files to see whether the save button is written in those file or not, not bad, rather than checking 28 thousand files.

But from the picture above it can be seen that the text of o_form_button_save is also exist in the test directory which is not executed by odoo. Therefore, we can use the –exclude-dir=*test* option, so the grep command does not display the files in the directory containing the word of test as shown below.

Now the text of o_form_button_save is only exist in 5 files. Because I didn’t install the hr_expense and project modules i can ignore it. Now I only need to check the files in the sale and web modules. Because I know that the web module is a basic module which is definitely installed when we create the database before installing another module, I will check the addons/web/static/src/js/views/form/form_controller.js file first, using the Sublime Text Editor’s find feature.

We can see that when the o_form_button_save class is clicked, it will call the _onSave method, so I will test whether this _onSave method is called when the save button is clicked or not by adding console.log code as shown below.

Then I will restart the odoo service, refresh the browser then make a transaction, for example a sales order and click the save button while monitoring the google chrome console.

You can see that the code that I just added is displayed on the google chrome console, which means that the addons/web/static/src/js/views/form/form_controller.js file is the right file where the save button functionality is written in odoo. It has proved that grep is very useful, especially if we want to understand a large project or framework better by reading their source code.

Related Article

Leave a Reply

Your email address will not be published. Required fields are marked *