Friday, November 7, 2014

Ruby Language as alternative to Powershell for Windows Scripting


Powershell is an enhanced version of DOS batch scripting.

It is much much more "powerful" than DOS batch, but "not powerful enough" in compared to the Unix/Linux scripting world (e.g. Bash, Python, Ruby, Perl, etc)

But because Powershell is installed by default in every Windows platform, people normally will opt to it, compared to using other better ones.

For people who have already been used to Linux scripting, Powershell syntax may look more dirty and messy, at the expense of getting "small improvement" compared to those other scripting languages (I know people who stand at Microsoft side will shout me on my rant about "small improvement" :-P)

On the other hands, getting scripts like Perl, Bash, or Python to work on Windows, need certain efforts on the installation steps and make us "inconvenience" to deploy it.

To minimize this kind of "inconvenience", we can make use of batch script to allow "one-time-click" process on deploying the scripts.

I use Ruby as an example below because it has been my favorite scripting language over the past seven years.


install.bat

@echo off
cls
NET FILE 1>NUL 2>NUL
IF NOT '%errorlevel%' == '0' (
    echo.
    echo ERROR: This script needs be run as admin
    echo Please rerun by doing right click and choose "Run as administrator"
    echo.
    pause
    exit
)
pushd "%~dp0"
 
echo Ruby engine installer
set args="[%*]"
IF "%args:silent=%" == "%args%" (pause)
start /wait taskkill /F /im ruby.exe >nul 2>&1
 
IF EXIST "C:\ruby\bin\ruby.exe" (
    powershell.exe "gem list | %%{$_.split(' ')[0]} | ? {$_ -notmatch 'test-unit|rdoc|psych|minitest|io-console|rake|bigdecimal|json'} | %%{gem uninstall -Iax $_ }" 2>&1 1>nul
)
.\rubygem\rubyinstaller-2.1.4.exe /silent /dir="C:\ruby" /tasks="addtk,assocfiles,modpath"
call C:\ruby\bin\setrbvars.bat
 
echo Performing the rest of installation from Ruby script... (in case you do hate Powershell)
call ruby .\post_install.rb %*
 
call gem install --local .\rubygem\ffi-1.9.6-x86-mingw32.gem 2>&1 1>nul
call gem install --local .\rubygem\win32-service-0.8.6.gem
call gem install --local .\rubygem\tins-1.3.3.gem 2>&1 1>nul
call gem install --local .\rubygem\term-ansicolor-1.3.0.gem 2>&1 1>nul
call gem install --local .\rubygem\ocra-1.3.3.gem 2>&1 1>nul
call gem install --local .\rubygem\rubyzip-1.1.6.gem 2>&1 1>nul
 
call gem install --local .\rubygem\win32-api-1.5.2-universal-mingw32.gem 2>&1 1>nul
call gem install --local .\rubygem\windows-api-0.4.3.gem 2>&1 1>nul
call gem install --local .\rubygem\windows-pr-1.2.4.gem 2>&1 1>nul
call gem install --local .\rubygem\serialport-1.3.1-x86-mingw32.gem 2>&1 1>nul
 
popd
echo.
echo Done.
IF "%args:silent=%" == "%args%" (pause)

That's it.

In the above script, you will notice powershell command that is used to remove the existing gem. If you are a Linux fanatics and hate to include even a single piece of powershell syntax, you can move that line to the post_install.rb script and perform system("gem uninstall -Iax #{gemname}") from there.