Add Copyright or License text to the Source Files Recursively
Tags: productivity, scripting, automation, tool
Published: 2020-04-21 04:27:46

# Introduction
Recently I was assigned to the task of adding a `Copyright text block` to all the JavaScript Source Code files. Initially the task was assumed to be an easy one as I thought of using any related VSCode Extensions to achieve it. However it was proved to be hectic when I found, I have to do it for 250 odd files 😲!

What next? Of course the natural instinct was to,  [search on web](https://www.google.com/search?q=add+copyright+notice+to+source+files&rlz=1C1GCEU_enIN820IN820&oq=adding+copyright+text+to+source+&aqs=chrome.1.69i57j0j69i60l3j69i64l2.19743j0j7&sourceid=chrome&ie=UTF-8) for a tool that does it. I just did that. I found many responses, specially few great directions from  [StackOverFlow](https://stackoverflow.com/).

# Requirements
After looking into the responses for a while, I felt the need of bringing things together to create a small tool that does following:

- Ability to add a block of text(Copyright, License, Any Generic Comment etc) to the top of the Source Code(or any target files).
- Ability to read the block of text from a file and add to the Source Code.
- Do not update the Source Code file with the Copyright/License content if it present already.
- Add the Copyright content to the files recursively.
- Ability to ignore certain folders while adding the content to the Source Files. This was important because, I didn't want to add my organization's Copyright note into the Source Code of any external libraries like, reactJs.

# Meet the Tool
The tool `add-copyright` is able to meet all the requirements mentioned above with few limitations that we will see at the end. 

You can clone the repo and use it from here:

[https://github.com/atapas/add-copyright](https://github.com/atapas/add-copyright)

# Usage
The main files of the tool are followings,

- `copyright.txt`: An input file where you keep the text block(Copyright/License text) to apply on your Source Code.
- `addcopyright.sh`: The Script file which is responsible for adding the text block to the target Source Code.

## Command to Run

```bash
find <SOURCE_CODE_DIRECTIRY> -type d -name "<EXCLUDE_DIRECTORY>" -prune -o -name "*.js" -print0 | xargs -0 ./addcopyright.sh
```

Where the <*SOURCE_CODE_DIRECTIRY*> is the path of your source code. Where the <*EXCLUDE_DIRECTORY*> is the directory to exclude if it exists under <SOURCE_CODE_DIRECTIRY> for updating the Copyright information.

For example, running the tool on the JsvaScript Source Code under the folder `/opt/atapas/code` by excluding the folder `node_modules`, use this command,

```bash
find /opt/atapas/code -type d -name "node_modules" -prune -o -name "*.js" -print0 | xargs -0 ./addcopyright.sh
```

You can ignore multiple folders in single command and can use on different types of Source Code files.  [Please go through the Readme documentation](https://github.com/atapas/add-copyright/blob/master/README.md) for more details.

## Output
Here is the output of running the command,
![output.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1587113197356/y23Knfi1u.png)

# Limitations
Few Limitations included:

- This tool can only be run from a Linux Bash Shell. For running it from windows use any bash shell like [GitBash](https://git-scm.com/download/win).
- This tool can be made use for different language files like javascript, java, c, c++, html, shell-script etc. However the content of the `copyright.txt` should be changed according to the mult-line comment format. For example,

 - For Javascript(.js) or Java(.java) files this is the format:
   ```js
	   /*
	    This is a comment
	   */
   ```
  - For HTML(.htm or .html) file the format should be,
    ```html
	  <!-- 
	     This is a HTML Comment
	  -->
    ```
	 

# Last few words..
I hope the tool will be useful to many of you as it was to me. Please like/share(👍) this post and give a star(⭐)  to [my project in GitHub](https://github.com/atapas/add-copyright). If you are willing to contribute to it in any form, you are most welcome!

There could be various other ways(may be better one too) to achieve the same goal. Please feel free to share in the comment section.