Published on

Automating starting tasks for programming

Authors
  • avatar
    Name
    Alex Urbina
    Twitter

Why

When a web developer works on more than one project, it is necessary to switch applications, start running new local servers, open the browser to see the user interface, or open the project management tool to track progress. Repeating those tasks several times and every day can be time consuming.

Batch files in Windows can be a good option to start automating these tasks, this article shows a process to automate starting tasks for programming.

What

Define what tasks are going to be defined is the first step. Some common tasks can include:

  • Start IDE in the project path
  • Start comand line or termianl in the correct path and start the application
  • Start the browser and open the application

After thouse steps the developer will be ready to satrt coding

How

In order to achieve automation on Windows, a batch file will be used. Creating a batch file only requires the use of any text editor, even NotePad can be used.

What is @echo off?

Echo is a function that allows displaying inputs in the terminal. In order to avoid that the terminal displays all the next inputs, echo will be set to off.

The first line of the batch script will be:

@echo off

What is SETLOCAL ENABLEEXTENSIONS?

Using SETLOCAL will allow setting of enviroment variables, using the parameter ENABLEEXTENSIONS will allow to use of other applications through the comand line.

The batch file would look like this:

@echo off
SETLOCAL ENABLEEXTENSIONS

How to set variable in a batch file?

SET <nameOfTheVariable>=<valueOfTheVariable> is the sintax for declaring a variable. Since this article is oriented to automate software developers starting tasks, the parameters used will be realted to this subject.

The batch file would look like this:

@echo off
SETLOCAL ENABLEEXTENSIONS
:: Setting variables
SET projectName=NFT-asset-generator
SET projectRoot=C:\_MyFiles\code\
SET projectRelativePath=business\nft-asset-generator
SET projectPort=3001
SET projectAditionalUrl1=https://some.url1.com
SET projectAditionalUrl2=https://some.url2.com

How to start applications in a batch file?

It needs to user the start command. The sintax is start "Name of the process" /option1 ["parameters"] "applicationRoute" /option2 [operationsForOpions] For /option1 can be /D that is used for especify working directory. For /option2 can be /k that is used for run Command and then return to the CMD prompt. For %parameterName% it will read the value of the parameter used.

The final batch file would look like this:

@echo off
SETLOCAL ENABLEEXTENSIONS
:: Setting variables
SET projectName=NFT-asset-generator
SET projectRoot=C:\_MyFiles\code\
SET projectRelativePath=business\nft-asset-generator
SET projectPort=3001
SET projectAditionalUrl1=https://some.url1.com
SET projectAditionalUrl2=https://some.url2.com
:: Starting execution
start "%projectName% | server" /D "%projectRoot%%projectRelativePath%" "%windir%\system32\cmd.exe" /k "npm install && npm run dev -- -p %projectPort%"
start "VSCode" /D "%projectRoot%%projectRelativePath%" "%windir%\system32\cmd.exe" /k "code ."
start "Chrome" "C:\Program Files\Google\Chrome\Application\chrome.exe" "http://localhost:%projectPort%" "%projectManagementRoot%" "%projectManagementRoot1%"

Developers can modify or add values and add more lines. In this example the start section is running a project developed in Node.js in the specified port. The next line is opening Visual Studio Code in the right directory. The last line is opening the Chrome browser for "http://localhost:%projectPort%" and other two URLs.

Final thoughts

Working on multiple projects requieres organization and focus in relevant tasks, it means, that automating tasks that are repetitive, but necesary for start developing is a big advantage. Developers should not spend time searching manually by the project folder, opening VSCode in the right directory, opening command line, starting local servers or opening the browser; when these tasks can be fully automated as shown in this article.

Hope you could find this usefull.