LoginSignup
4
6

More than 5 years have passed since last update.

バッチファイルによるスタブのパターン

Posted at

パッケージ管理システムが実行可能なツールをインストールするとき、Windows版ではスタブとなるbat、cmdファイルを生成することがある。

各言語のパッケージ管理ファイルでどのようなスタブを生成しているのか調べてみる。

Ruby(gem)

bundle.bat
@ECHO OFF
IF NOT "%~f0" == "~f0" GOTO :WinNT
@"ruby.exe" "C:/HashiCorp/Vagrant/embedded/bin/bundle" %1 %2 %3 %4 %5 %6 %7 %8 %9
GOTO :EOF
:WinNT
@"ruby.exe" "%~dpn0" %*

https://github.com/rubygems/rubygems/blob/master/lib/rubygems/installer.rb
で生成されている模様。

"%~dpn0"は、拡張子を除くフルパス。bundle.batと同じディレクトリにあるbundleという名前のrubyプログラムを実行している。

GOTOしないルートは、おそらくWin95/98/Me向け(動くのかは知りませんが)。

Node(npm)

grunt.cmd
@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\grunt-cli\bin\grunt" %*
) ELSE (
  node  "%~dp0\node_modules\grunt-cli\bin\grunt" %*
)

https://github.com/ForbesLindesay/cmd-shim
がやっているっぽい。
スタブファイルと同じディレクトリにnode.exeがあればそれを使う。

"%~dp0"は、ドライブ+パス(ファイル名と拡張子は除く)

PHP(Composer)

phpunit.bat
@ECHO OFF
SET BIN_TARGET=%~dp0/../phpunit/phpunit/composer/bin/phpunit
php "%BIN_TARGET%" %*

https://github.com/composer/composer/blob/master/src/Composer/Installer/LibraryInstaller.php
でやっている模様

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6