(だいぶ前に書いたメモ)
調べたこと
-  コマンドライン引数の取得 
 →$Args[0]これで取れる
-  実行時にでたエラー 
 →Set-ExecutionPolicyで権限を与えましょう
-  変数の使い方 
 → 単純に$variable_name
-  sedの代わりに何を使う? 
 →% { $_ -replace "targetfile", $filename}なんかこれ
-  ファイルの読み込みと上書き保存 
 →Get-ContentとOut-Fileを使う
元になるシェルスクリプト
このシェルスクリプトをパワーシェルに置き換えようとしています.
内容は,MarkdownファイルをVivliostyleでPDF生成するスクリプトです.
#! /bin/sh
npm run scss
# vivliostyle.config.js の 変更
sed -n -e "s/targetfile/$1/g" vivliostyle.config.js
# build
pdffilename=`echo $1 | sed "s/.md//g"`
node ./node_modules/@vivliostyle/cli/dist/commands/build.js ./docs/$1 -o out/${pdffilename}.pdf
完成したパワーシェルスクリプト
npm run scss
$filename = $Args[0]
# vivliostyle config js を読み込み targetfile を コマンドライン引数1個目に置換し data に代入
$data = Get-Content .\vivliostyle.config.js | % { $_ -replace "targetfile", $filename}
$data | Out-File .\vivliostyle.config.js -Encoding UTF8
$pdffilename = $filename | % { $_ -replace ".md", ""}
node ./node_modules/@vivliostyle/cli/dist/commands/build.js ./docs/$filename -o out/${pdffilename}.pdf
Ref.
- https://qiita.com/Targityen/items/3d2e0b5b0b7b04963750
- https://qiita.com/ponsuke0531/items/4629626a3e84bcd9398f
- https://docs.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.1
- https://orebibou.com/ja/home/201501/20150119_001/
- https://techinfoofmicrosofttech.osscons.jp/index.php?PowerShellのPS1ファイルの作成と実行
