Punkt BAT
Dealing with Batch-Files in non-Windows environments
by Lea Rosema
I work in several client projects as a web developer. Usually, projects are mostly cross-platform. Most web development tools are available on any platform. However, sometimes, projects require to run Windows. I had this situation recently.
So, I was wondering, why do I have to run Windows for development tasks in the project? I might get the point. Maybe the team doesn't want to care too much about cross-platform compatibility. But most of the tools were cross-platform anyway. So I was asking myself again, why?
The machine I work with is a Mac with a Silicon Chip. Usually, this will do fine for most for web development tasks.
I learned more about why the project requires Windows for development. Most of the tools used (eg. Visual Studio Code, Azure Data Studio, Docker, Node, etc) were in fact perfectly available for Mac. But the database server running in a Docker image doesn't support the Apple Silicon chip.
So, an option proposed to me was to hand me a second machine that runs Windows. Another option would be to use a VM. I wanted to avoid both options if possible. Working inside a VM is pretty cumbersome, and having to carry two laptops around every day in the Office would be quite exhausting. it would require me to always switch between the two computers all the time (operative tasks on Windows, administrative tasks on the Mac).
The database server inside Docker was the first hurdle. But all I had to do was to install the x86 CPU emulation layer on my mac, via:
softwareupdate --install-rosetta
Far from optimal, but this made it run on my Silicon Mac. I'm not sure (and not a backend dev), but the backend was using an Object-relational Mapper (ORM). It might have been possible to configure the project with a something more cross-platform-compatible database engine like PostgreSQL. But we went with the emulation and it worked just fine.
The second thing was a question of comfort. The whole project had a lot of good old DOS-style Batch files for running repetitive tasks during development.
For example, there was a RUN.BAT
for running frontends, backends, etc.
Simplified and anonymized, it looked something like this:
@echo off
if "%1" === "frontend" goto frontend
if "%1" === "backend" goto backend
if "%1" === "migrate" goto migrations
echo Usage: RUN [frontend|backend|migrations]
goto :end
:frontend
cd frontend
npm start
goto :end
:backend
dotnet run backend\backend.vspjoj
goto :end
:migrations
CALL db migrate %2
goto :end
:end
Most of the commands inside that batch file also run just fine on the Mac. So the quick solution was to always do a cat RUN.BAT
and copy the code from the appropriate jump marks.
I did this to get the thing running on day 1. On day 2, I got tired of this.
First thought was to introduce a top-level package.json
and use the scripts section for running tasks:
npm init -y
As a result, you get a json file. Then, you can edit the scripts section accordingly, so you can invoke the tasks via npm run frontend
and so on:
{
"name": "foldername",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"frontend": "cd frontend && npm start",
"backend": "cd backend && command_to_run_backend"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
That was declined. Second thought was to install WINE which is a Windows environment wrapper for UNIX-style OSses, including Mac. Actually, it does come with winecmd
which enables me to run batch files on my Mac. But I didn't manage to make it work with tools like node
installed on my machine.
My solution in the end was to port the Batch files to UNIX-style shell scripts. I'm not a Shell Guru. But that turned out to be straightforward:
#!/bin/sh
frontend() {
echo running frontend
# frontend tasks here
}
backend() {
echo running backend
# backend tasks here
}
if [ "$1" == "frontend" ]; then frontend; fi
if [ "$1" == "backend" ]; then backend; fi
Whatever you use, I would recommend avoiding Batch files in web development codebases. The same for proprietary database engines with limited CPU support. This would make the project unnecessarily dependant on propietary platforms like Windows.
Yeah, to be fair, unix-style shell scripts aren't available in Windows by default, but you can install MSYS (aka Minimalist GNU for Windows) when you are on Windows. Often, you don't even have to install it separately, as git
, the version system which is most commonly used nowadays comes with a rudimentary bash shell which you can use.
I wanted to share this. Maybe it helps you in case you encounter a project like this.