Skip to content

Instantly share code, notes, and snippets.

@koyablue
Last active July 9, 2021 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koyablue/cff01ee23a287f36c8a2eb6125c303d7 to your computer and use it in GitHub Desktop.
Save koyablue/cff01ee23a287f36c8a2eb6125c303d7 to your computer and use it in GitHub Desktop.
shell script
#!/bin/bash
# xxのビルド済みCSS, JSファイルと画像ファイルをバックエンドのLaravelのpublic以下にコピーする用
# FRONT_SRC_BASE_DIRは自分の環境に合わせて適宜書き換え
FRONT_SRC_BASE_DIR="/path/to/front/src"
# BACKEND_PUBLIC_DIRは自分の環境に合わせて適宜書き換え
BACKEND_SER_BASE_DIR="/path/to/backend/src"
# フロントエンドのリポジトリでcheckoutするブランチ名
BRANCH_TO_CHECKOUT="stag"
FRONT_SRC_DIR="$FRONT_SRC_BASE_DIR/build"
FRONT_CSS_DIR="${FRONT_SRC_DIR}/css"
FRONT_JS_DIR="${FRONT_SRC_DIR}/js"
FRONT_IMG_DIR="${FRONT_SRC_DIR}/img"
BACKEND_PUBLIC_DIR="$BACKEND_SER_BASE_DIR/public"
# ディレクトリあるかチェック
# Arguments:
# 存在確認したいディレクトリのパス 配列
check_dirs() {
local dirNames
dirNames=("${@}")
for dirPath in "${dirNames[@]}"; do
if [[ ! -d $dirPath ]]; then
echo "no such directory: $dirPath"
exit 1
fi
done
}
# ファイルをバックエンドのフォルダにコピー
# Arguments:
# コピーしたいファイルのパス 配列
copy_files() {
local files
files=("${@}")
for file in "${files[@]}"; do
fileName=${file#$FRONT_SRC_DIR}
backendFileName=$BACKEND_PUBLIC_DIR$fileName
backendDirName=`dirname $backendFileName`
if [ ! -d $backendDirName ]; then
mkdir -p $backendDirName || exit 1
mkdir -p $backendDirName
fi
cp -v $file $backendFileName
done
}
# ブランチ移動してgit pull
git_pull() {
cd $FRONT_SRC_BASE_DIR
git checkout $BRANCH_TO_CHECKOUT
git pull origin $BRANCH_TO_CHECKOUT
}
main() {
declare -a dirsToCheck
dirsToCheck=($FRONT_CSS_DIR $FRONT_JS_DIR $FRONT_IMG_DIR $BACKEND_PUBLIC_DIR)
check_dirs "${dirsToCheck[@]}"
git_pull
frontBuildFiles=$(find $FRONT_CSS_DIR -type f; find $FRONT_JS_DIR -type f; find $FRONT_IMG_DIR -type f)
copy_files $frontBuildFiles
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment