Source code search during build phase

Sandeep Kumar
3 min readNov 16, 2022

--

This is a script in PowerShell which can scan through your source code to search for a specific text in each source file and shows you the results (in console and a text file report).

The biggest use case for me was to know if my project source code is IPR (Intellectual Property Rights) compliant. IPR header is a proprietary text put on top of each source file in a project (as a comment). If any of the source code file is not IPR compliant, the build pipeline should fail and tell the list of files which are not IPR compliant.

This PowerShell script can be used as a task in DevOps pipeline to scan through the source code and search for a specific text (IPR header in my case) in each file. You can use the script for other similar requirements with no or little modifications.

Input to the script

  1. Source Code Path: Your source code path to scan. Eg. “D:\SourceCode\ProjectABC”. Use “$(System.DefaultWorkingDirectory)” if running the script as a task in DevOps pipeline
  2. Text: Text (part or full) to search for in your source code. Text should be a single line. Eg. “COMPANY XYZ PROPRIETARY. This document and its accompanying elements, contain information which is proprietary and confidential”
  3. Files to Include: Extension of source code files to consider for scanning. Eg. “*.cs, *.ts, *.html, *.htm”
  4. Folders to Exclude: Folders to skip from scanning. Eg. “bin, obj, node_modules, dist”
  5. Output File: Path to a file to report the search results. Eg. “D:\SourceCode\ProjectABC\IPRReport.txt”. Make use of “$(Build.ArtifactStagingDirectory)” if running the script as a task in DevOps pipeline. Eg. “$(Build.ArtifactStagingDirectory)/IPRReport.txt”

Script

Path to GitHub repository: https://github.com/iamsandeepkmr/SearchSourceCode

Refer file SearchSourceCode.ps1 from the repository

Run the Script

I am going to use this script in my project to find the source code files with missing IPR header. I will show how to run this script in a DevOps pipeline as well.

Use below command to run the script

Image (A)

Output

Image (B)

This is how the IPR report will look like:

Image (C)

Use the Script as a DevOps pipeline task

  • Add a PowerShell task in your pipeline
  • Copy the PowerShell script (Inline). Specify/modify input values
Image (D)
  • Add below code in the end of script if you want to fail the pipeline in case there are files with missing IPR header
if($fileCountWithoutText -gt 0) {
exit 1
}
  • Don’t forget to publish the report as a pipeline artifact in a separate task (or as a part of an existing publish artifacts task in your pipeline)
  • Run the pipeline

Output

Pipeline task output:

Image (E)

There are no files with missing IPR header in my source code.

Report in pipeline artifacts:

Image (F)
Image (G)

I hope this article helps you in one way or the other. Please Clap if you liked the article. Leave a comment for any feedback or query.

Cheers!

--

--

Sandeep Kumar
Sandeep Kumar

Written by Sandeep Kumar

Works at Kongsberg Digital. Angular, JavaScript, .Net, Azure https://github.com/iamsandeepkmr

No responses yet