さて、Lightning Talkで掲題の話をしたのですが、実用するには速すぎてわからなかったと思うので読み物としてわかるようにまとめたいと思います。
概要
MSBuild(.csprojなんかで使われている)はビルドには必要なんだけど、ちょっとコマンドライン叩くの面倒だよね、適当なGUIないよね。→MSBuild Launcherの誕生。
あれ、このGUIなら逆にビルドじゃない用途でも(スクリプト実行の用途でも)MSBuildにExec書いて使ったら便利なんじゃね?
→MSBuild LauncherありきのMSBuildの邪道な使い方の誕生。
というお話です。
前提として、MSBuild Launcherをインストーラでインストール(関連付けオプション有効)を想定しています。
---
1. シンプルな使い方
単純に.ps1ファイルを置いて、それを実行するだけ。これでも十分に便利。一応、コマンドが長くなりすぎちゃうので、プロパティ(※1)としてpowershellコマンドを別出しにしておくのがおすすめ。※1: パラメータのようなもの。PropertyGroup要素に好きな名前の要素を置くと、それがプロパティになる。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project DefaultTargets="Build" | |
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<PSCmd>powershell -ExecutionPolicy RemoteSigned -NonInteractive -Command </PSCmd> | |
</PropertyGroup> | |
<Target Name="Build"> | |
<Exec Command='$(PSCmd) .\simple.ps1' /> | |
</Target> | |
</Project> |
なお、-NonInteractiveは常に入れておくのがおすすめ。発表時も話したけども、例えば必須引数が足りなかったりすると通常のコンソールでは入力待ちで止まるけども、こいつはそんなことできないので、無言で待ちに入る。そして「進まない!」と悩む。なので-NonInteractiveで即座にエラーにする。
対応するスクリプトは好きな処理を書けばいいのだけど、一応例として。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Write-Output "Hello, World." |
動かすとこんな感じ。
2. プロパティを持たせる使い方
パラメータが欲しいときは、MSBuildにプロパティを持たせるとよい。そしてコマンドの中で$(Name)で使う。ただし、Exec Command=""の中身の文字列はcmd解釈?のようなので注意。Command=をくくるのは""にして、プロパティを'$(Name)'という感じにくくるのがおすすめ。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project DefaultTargets="Build" | |
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<Msg>こんにちは</Msg> | |
<!-- 選択肢にしたいとき --> | |
<Select>おはよう</Select> | |
<Select Condition=" '$(Select)' == 'おやすみ' "></Select> | |
<PSCmd>powershell -ExecutionPolicy RemoteSigned -NonInteractive -Command </PSCmd> | |
</PropertyGroup> | |
<Target Name="Build"> | |
<Exec Command="$(PSCmd) .\property.ps1 -Msg '$(Msg)'" /> | |
</Target> | |
</Project> |
プロパティはLTでは話さなかったけども、候補としてConditionを追加してあげると、コンボボックスの選択になるので、選択のときはそうしてあげるとよいかも。
スクリプトはこんな感じ。paramで受けてあげればいい。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param($Msg) | |
Write-Output "$Msg, World." |
動かすとこんな感じ。右側で設定できる。
3. 複数のターゲットを用意する便利な使い方
複数のやりたいことを一つの画面で行うには、もちろんスクリプトを複数用意してもよいのだけど、処理自体は少ない場合は、モジュール(.psm1)に関数を定義して、それをMSBuildから使う方式がおすすめ。具体的には、PSCmdの中にImport-Moduleまで入れてしまって、Execの中では関数名を書く形にする。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project DefaultTargets="Build" | |
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<PSCmd>powershell -ExecutionPolicy RemoteSigned -NonInteractive -Command Import-Module .\module.psm1; </PSCmd> | |
</PropertyGroup> | |
<Target Name="GetService"> | |
<Exec Command='$(PSCmd) Get-MyService' /> | |
</Target> | |
<Target Name="GetProcess"> | |
<Exec Command='$(PSCmd) Get-MyProcess' /> | |
</Target> | |
</Project> |
スクリプトはこんな感じ。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-MyService { | |
Get-Service | Select-Object -First 5 | Format-List | |
} | |
function Get-MyProcess { | |
Get-Process | Select-Object -First 5 | Format-List | |
} |
動かすとこんな感じ。
4. PowerShellの便利機能を活用する
PowerShellを簡易GUIっぽくしちゃうことで有名なOut-GridViewやShow-Commandはここでも便利。ただ1点注意があって、Execで実行するとコンソールと違ってホストが残らないので、そのままだとすぐ画面がいなくなってしまう。
なので、以下のように終了を待ってあげるのがいい。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get-Service | Out-GridView -Passthru | Out-Null |
---
今度こそぜひ活用してあげてください。