Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Add scripts to insert the MPL notification #237

Open
wants to merge 17 commits into
base: feature/license_tools
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Tools/AddLicense/AddLicense.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

function addLicense() {
local MPL=(
'This Source Code Form is subject to the terms of the Mozilla Public'
'License, v. 2.0. If a copy of the MPL was not distributed with this'
'file, You can obtain one at https://mozilla.org/MPL/2.0/.'
)
local comment=''
local fileType=${1##*.}
local lineNumber=1

# 各種ファイルに適したコメントを生成する
if [ "$fileType" == 'axml' ]; then
comment='\\n<!--\n'$(printf ' %s\\n' "${MPL[@]}")'-->\n'
lineNumber=2 # XML 宣言の後に挿入する

elif [ "$fileType" == 'bat' ]; then
comment=$(printf '@REM %s\\n' "${MPL[@]}")

elif [ "$fileType" == 'cs' ]; then
comment=$(printf '// %s\\n' "${MPL[@]}")
Meiryo7743 marked this conversation as resolved.
Show resolved Hide resolved

elif [ "$fileType" == 'feature' ]; then
comment=$(printf '# %s\\n' "${MPL[@]}")

elif [ "$fileType" == 'resx' ]; then
comment='\\n<!--\n'$(printf ' %s\\n' "${MPL[@]}")'-->\n'
lineNumber=2 # XML 宣言の後に挿入する

elif [ "$fileType" == 'sh' ]; then
comment='\\n'$(printf '# %s\\n' "${MPL[@]}")
lineNumber=2 # Shebang の後に挿入する

elif [ "$fileType" == 'storyboard' ]; then
comment='\\n<!--\n'$(printf ' %s\\n' "${MPL[@]}")'-->\n'
lineNumber=2 # XML 宣言の後に挿入する

elif [ "$fileType" == 'strings' ]; then
comment='\\n/*\n'$(printf ' %s\\n' "${MPL[@]}")'*/\n'

elif [ "$fileType" == 'tf' ]; then
comment=$(printf '# %s\\n' "${MPL[@]}")

elif [ "$fileType" == 'xaml' ]; then
comment='\\n<!--\n'$(printf ' %s\\n' "${MPL[@]}")'-->\n'
lineNumber=2 # XML 宣言の後に挿入する

elif [ "$fileType" == 'xml' ]; then
comment='\\n<!--\n'$(printf ' %s\\n' "${MPL[@]}")'-->\n'
lineNumber=2 # XML 宣言の後に挿入する

elif [ "$fileType" == 'yml' ]; then
comment=$(printf '# %s\\n' "${MPL[@]}")
fi

# ライセンスの文言が無いファイルを探し,該当するものに生成したコメントを挿入する
git grep -z -L "${MPL[0]}" -- "$1" | xargs -0 sed -i -e "${lineNumber}i$comment"
}
36 changes: 36 additions & 0 deletions Tools/AddLicense/CheckAllFiles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# リポジトリ内のファイルについて MPL の記載漏れをチェックします。
# 詳しい処理につきましては Tools/AddLicense/AddLicense.sh をご参照ください。

# リポジトリのルートへ移動
cd "$(git rev-parse --show-toplevel)" || exit

# `addLicense` の読み込み
source Tools/AddLicense/AddLicense.sh

# 対象ファイル(リポジトリのルートを基準とした相対パスで記述)
FILES=(
'*.axml'
'*.bat'
'*.cs'
'*.feature'
'*.resx'
'*.sh'
'*.storyboard'
'*.strings'
'*.tf'
'*.xaml'
'*.xml'
'*.yml'
)
Meiryo7743 marked this conversation as resolved.
Show resolved Hide resolved

# 対象ファイルの内,MPL が抜けているもの対して文言を挿入する
for file in "${FILES[@]}"; do
addLicense "$file"
git add "$file"
Meiryo7743 marked this conversation as resolved.
Show resolved Hide resolved
done
24 changes: 24 additions & 0 deletions Tools/GitHooks/PreCommit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# Commit 時に MPL の記載漏れをチェックします。
# 有効化するには,GitHooks.sh を実行する必要があります。
# 詳しい処理につきましては Tools/AddLicense/AddLicense.sh をご参照ください。

# リポジトリのルートへ移動
cd "$(git rev-parse --show-toplevel)" || exit

# `addLicense` の読み込み
source Tools/AddLicense/AddLicense.sh

# ステージングエリアのファイル一覧を取得(削除を除く)
stagedFiles=$(git diff --name-only --cached --diff-filter=d)

# 対象ファイルの内,MPL が抜けているもの対して文言を挿入する
for file in $stagedFiles; do
addLicense "$file"
git add "$file"
done
15 changes: 15 additions & 0 deletions Tools/GitHooks/PrepareHooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# リポジトリのルートへ移動
cd "$(git rev-parse --show-toplevel)" || exit

# pre-commit 用のスクリプトを追加
cp Tools/GitHooks/PreCommit.sh .git/hooks/pre-commit
chmod a+x .git/hooks/pre-commit

# 追加した Git Hooks を有効にする
git init