vimのCloudFormation用snippetをgithubのawsdocsからaws-cfn-snippet経由で生成取得してみる

2018.10.25

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

CloudFormationのテンプレートスニペットをvimで補完させるため、aws-cfn-snippetを導入してみました。

一部処理にて筆者のzsh環境では正常に動作せず、幾つかの手直しを行いました。aws-cfn-snippetの紹介と手直しを行った箇所について、vimでのsnippet補完導入からaws-cfn-snippetのインストールも含め、順を追って説明します。

aws-cfn-snippetとは

awsdocs/aws-cloudformation-user-guideから取得したdocsを利用して、vim用のCloudFormation用snippetを生成更新するライブラリです。今後vim plugin化も予定されているとのことです。

lunarxlark/aws-cfn-snippet.vim Qiita: CloudFormationのvim snippetを作ってみた

snippet補完の導入

Shougo/neosnippet.vimのInstallationに従ってプラグインを導入し、Configurationの内容をvimrcへ追記します。

aws-cfn-snippetの導入

現時点ではsnippetを指定ディレクトリに個別コピーする形になります。

% git clone git@github.com:lunarxlark/aws-cfn-snippet.vim.git
% cp aws-cfn-snippet.vim/snippets/*.snip ~/.vim/bundle/vim-snippets/snippets/

make-cfn-snippet.shでの更新

筆者環境で動作するように変更した内容になります。また、awsdocs/aws-cloudformation-user-guideのsubmodule化も合わせて行っています。

サブモジュール化

ディレクトリ内に丸ごとgit cloneをする仕組みになっていましたが、ディレクトリ移動せずに

git submodule foreach git pull origin master

で済ませてしまいたい為の変更です。

% cd aws-cfn-snippet.vim
% rm -rf aws-cloudformation-user-guide
% git rm aws-cloudformation-user-guide
% git add submodule git@github.com:awsdocs/aws-cloudformation-user-guide.git aws-cloudformation-user-guide

make-cfn-snippet.shの変更

大筋として以下の通りになります。

  • submoduleの更新取得を追加
  • bashのuppercase/lowercaseが動作しなかったため、awkのtoupperに代替
  • aws-cloudformation-user-guide.gitのclone関連処理を削除
  • 処理が正常なのか確認したかったため、処理中のファイル名を出力させた
  • 生成後にsnippetsディレクトリへコピー

なお、sedによる整形処理には手を加えていません。

#!/bin/bash

home=$(cd $(dirname $0);pwd)
git submodule foreach git pull origin master

aws_cfn_doc_repo=${home}/aws-cloudformation-user-guide
aws_cfn_doc_dir=${aws_cfn_doc_repo}/doc_source

cd ${aws_cfn_doc_dir}

for file_type in yaml json
do 
  
  # initialize
  snip=${home}/snippets/${file_type}.snip
  if [ -e "${snip}" ];then rm ${snip}; fi
  large_file_type=$(echo $file_type | awk '{ str = toupper($1); print str }')
  # AWS Resource snippets
  echo "### AWS Resource snippets" >> ${snip}
  for FILE in `grep -r "^### ${large_file_type}" ${aws_cfn_doc_dir} | awk -F: '{ print $1 }' | sort -u`
  do
    echo $FILE
    echo "snippet " `sed -n 1P $FILE | sed -e "s/^# //g" -e "s/<a .*//g" -e "s/ /::/g"` >> ${snip} 
  
    start=$(expr $(sed -ne '/^### '${large_file_type}'/,$p' $FILE | grep -n "\`\`\`" | awk -F: 'NR==1 { print $1}') + 1)
    end=$(expr $(sed -ne '/^### '${large_file_type}'/,$p' $FILE | grep -n "\`\`\`" | awk -F: 'NR==2 { print $1}') - 1)
  
    sed -ne '/^### '${large_file_type}'/,$p' $FILE | \
      sed -ne "${start},${end}p" | \
      sed -e "s/^/  /g" | \
      sed -e "s/([^)]*)//g" | \
      sed -e "s/\[//g" -e "s/\]//g" >> ${snip}
    echo "" >> ${snip}
    echo "" >> ${snip}
  done
 

  # Resource Properties snippets
  echo "### Resource Properties snippets" >> ${snip}
  for FILE in `grep "^### ${large_file_type}" aws-properties-* | awk -F: '{ print $1 }' | sort -u`
  do
    echo -n "snippet " >> ${snip}
    echo -n `sed -n 1P $FILE | sed -e "s/^# //g" -e "s/<a .*//g" -e "s/.* //g"` >> ${snip}
    echo $FILE | sed -e "s/aws-properties//g" -e "s/.md//g" >> ${snip}
  
    start=$(expr $(sed -ne '/^### '${large_file_type}'/,$p' $FILE | grep -n "\`\`\`" | awk -F: 'NR==1 { print $1}') + 1)
    end=$(expr $(sed -ne '/^### '${large_file_type}'/,$p' $FILE | grep -n "\`\`\`" | awk -F: 'NR==2 { print $1}') - 1)
  
    sed -ne '/^### '${large_file_type}'/,$p' $FILE | \
      sed -ne "${start},${end}p" | \
      sed -e "s/^/  /g" | \
      sed -e "s/([^)]*)//g" | \
      sed -e "s/\[//g" -e "s/\]//g" >> ${snip}
    echo "" >> ${snip}
    echo "" >> ${snip}
  done
done

cat << EOS >> ${home}/snippets/yaml.snip
snippet AWSTemplateFormatVersion
  AWSTemplateFormatVersion: "2010-09-09"
  Description: A sample template
  Resources:
    MyEC2Instance: # inline comment
      Type: "AWS::EC2::Instance"
      ...
EOS

# refact
sed -i -e "s/ $//g" ${home}/snippets/*.snip
echo "Copy to vim-snippets"
cp ${home}/snippets/*.snip ~/.vim/bundle/vim-snippets/snippets/

make-cfn-snippet.shの実行

sedでのillegal byte sequence対策で、実行前にexportします。

% export LANG=C; sh make-cfn-snippet.sh

まとめ

実行環境に依存するとは思いますが、エラーが発生した場合の参考になれれば幸いです。

各種エディタにおける補完

CloudFormationのsnippet補完は各種エディタにおいても、以下のようにアドオン等導入することにより可能になります。CloudFormationでの作業時には大きなサポートになってくれると思います。