0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コマンド一つでテンプレートから環境を構築する為のシェルスクリプトを書く

Posted at

例えばTypeScriptなどで書き殴りのコードを書きたい時、
自分の場合、yarn init -yしてyarn add -D typescript @types/nodeして、index.tsを作ってtsconfig.jsonを適当に作って書き換えて、package.jsonにnpm-scriptsを書き足して・・・みたいな事をしています。
前に書いたコードを引っ張ってきたら良いじゃんって言うのはごもっともですが、無駄なパッケージが削ぎ落とされたコードを探したり編集したりするのはめんどくさいです。

環境構築の為のツールは公式、サードパーティ問わずたくさん存在しているのでそちらを使っても良いのですが、ゴテゴテしていることが多いので、今回はオレオレテンプレートを自分で管理・編集したいと思ったのでシェルスクリプトで作ってみました。

よかったら使ってみて下さい。
Github: https://github.com/ivgtr/tepy

やりたいこと

  • templateディレクトリを作り、その配下のディレクトリをテンプレートとして選択できるようにする
  • 選択したテンプレートの中身を作業中のディレクトリにコピーする

実装

準備

$HOMEディレクトリにシェルスクリプト用のディレクトリを作り、その中にtemplateディレクトリを作成します。

shell
$ mkdir "${HOME}/.sh" && mkdir "${HOME}/.sh/template"

このtemplateディレクトリ内に、使い回したいテンプレートをディレクトリごと入れていきます。

シェルスクリプト用のディレクトリ配下にシェルスクリプトを作り、書いていきます。

shell
$ touch "${HOME}/.sh/main.sh"

スクリプトを書いていく

main.sh
function tepy () {
  # メイン処理
}

読み込み時に実行されてしまうのでスコープで囲う。
この関数名がコマンド名になるので入力しやすい物にしとく。
今回はTemplate-Copyなのでtepyと名付けました。

main.sh
  template_path="${HOME}/.sh/template"
  if [ ! -d $template_path ]; then
    echo "Not found template directory."
    mkdir $template_path
    return
  fi

if [ ! $hoge ]でNOT条件、if [ -d ${DIR} ]で、ディレクトリの存在確認ができ、
エラーハンドリングとして、templateディレクトリが存在するかチェックし、存在しなかったら生成します。

main.sh
  template_list=()
  for template in `find $template_path -maxdepth 1 -mindepth 1 -type d`; do
    template_list+=${template##*/}
  done

hoge=()でbash 配列を作ります。

テンプレートを列挙する為に、find"${HOME}/.sh/template"内のディレクトリを探します。
オプションとして-type dを付けるとディレクトリのみが検索でき、-maxdepth 1 -mindepth 1template本体を含まない一層目までの子ディレクトリを検索します。

取得したデータはpathになっているので、template名としてディレクトリ名が欲しいので、##*/で左から最長の/を探し、削除。配列に追加します。

main.sh
  if [ ${#template_list[@]} -gt 0 ]; then
    # メイン処理
  else
    echo "Not found tempate. pls add a directory."
    open $template_path
  fi

テンプレートが存在しない時はメッセージを出し、追加する様にopenコマンドでディレクトリを表示します。
${#array[@]}$arraylengthが取得でき、if [ ${#array[@]} -gt 0 ]${#array[@]}が0より大きいか条件分岐しています。

main.sh
  PS3="Please select a number: "
  select i in $template_list; do
    if [ -n "${i}" ]; then
      cp -i -r -v `find "${template_path}/${i}" -maxdepth 1 -mindepth 1` "$pwd"
      echo "Copying of ${i} completed."
    else
      echo "Cannot select ${REPLY}. Please select a number."
    fi
    break
  done

PS3変数を変更するとselectなどのプロンプトの表示が変更できるので、番号以外を入力されない様に変更。
select i in $arrayで簡易的な対話メニューが作れる。
入力を受け付けると$iにはテンプレート名が、$REPLYに入力した値が代入される。
エラーハンドリングとしてif [ -n $hoge ]$hogeの存在チェックをし、存在しない物を入力した際にはメッセージを出す。
存在した場合、findで指定したテンプレートのディレクトリ配下のファイルなどを、作業中のディレクトリにコピーする。

コード全体

main.sh
# !/bin/bash

function tepy () {
  template_path="${HOME}/.sh/template"
  template_list=()
  if [ ! -d $template_path ]; then
    echo "Not found template directory."
    mkdir $template_path
    return
  fi

  for template in `find $template_path -maxdepth 1 -mindepth 1 -type d`; do
    template_list+=${template##*/}
  done

  if [ ${#template_list[@]} -gt 0 ]; then
    PS3="Please select a number: "
    select i in $template_list; do
      if [ -n "${i}" ]; then
        cp -i -r -v `find "${template_path}/${i}" -maxdepth 1 -mindepth 1` "$pwd"
        echo "Copying of ${i} completed."
      else
        echo "Cannot select ${REPLY}. Please select a number."
      fi
      break
    done
  else
    echo "Not found template. pls add a directory."
    open $template_path
  fi

  return
}

PATH を通す

PATHを通す為に、.zshrcもしくは.bashrcsource "${HOME}/.sh/main.sh"を記述し、保存したら、source "${HOME}/.zshrc"もしくはsource "${HOME}/.bashrc"で再読み込みします。

実行

上記で関数名を命名した様に、tepyコマンドで呼び出せます。
templateディレクトリに適当にテンプレートにしたいディレクトリを作成し、tepyコマンドを打ちます。

例:

shell
$ tepy
1) typescript  2) react
Please select a number: 1

やった!ぱぱぱっと環境構築ができました!
これでTypeScriptをもっと書き殴っていこうと思います。

終わりに

何か間違いがあればコメント等で指摘頂ければ幸いです。

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?