2
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 5 years have passed since last update.

Laravelでどれだけ関数があるか調べたい時にBashでやったこと

Last updated at Posted at 2020-09-20

このプロジェクトに関数がいくつあるか知りたい ってこと無いですか?ないですか。僕はありました。

Laravelのプロジェクトなんですけど、パブリックな関数がいくつあるのか知りたかったんですね。PhpStormのプラグインだったり、色々と探したんですが、なんか欲しい情報を簡単に出せるのがありませんでした。

最近shell(bash)の書き方を覚えたばかりなので、使って書いたほうが早そうという結論に。

前提

  • Bashで書いてます。shell初心者です。
  • コメントアウトも拾っちゃいます。
  • 文字列とかも拾っちゃいます。
  • とにかく public function って文字列は無差別に拾います。
  • 行数カウントです。
  • プロジェクトルートから実行するのを想定。

ワンライナー

$ result_file="class_analysis"; [[ -f "$result_file" ]] && rm "$result_file"; touch "$result_file"; targets=("Services" "Repositories"); for target in "${targets[@]}"; do ls -1 app/"$target"/ | grep ".*Interface.php" | while IFS= read file; do echo "app/$target/$file,$(grep -c "public function" "app/$target/$file")" >> $result_file; done; done;

分解

まずは出力先ファイル名を設定。

$ result_file="class_analysis";

出力先ファイルが既に存在する場合は、削除しちゃいます。

[[ -f "$result_file" ]] && rm "$result_file";

新規で出力先ファイルを作ります。

touch "$result_file";

今回は「すべてのディレクトリ」ではなく、ある程度絞って数を見たかったため、ディレクトリをある程度絞りました。
プロジェクト的に重要なロジックが詰まっている以下の2つを対象としています。

targets=("Services" "Repositories");

対象ディレクトリで回して行きます。

for target in "${targets[@]}"; do

app 配下のファイルを1ファイル1行で表示し、パイプで出力を次の処理へ渡す。

    ls -1 app/"$target"/ |

今回対象のプロジェクトではInterfaceを具象クラスと同じディレクトリに格納していたため、Interfaceだけ見ればいいや、ということでインターフェースだけで絞り込みました。
Interfaceという文字列で grep した結果を、またまたパイプで次の処理へ渡します。

    grep ".*Interface.php" |

1ファイルずつ回して public function という文字列を含む行数を抜き出して、カンマ区切りでファイル名とともに echo して、その結果を出力先ファイルに追加。

    while IFS= read file; do
        echo "app/$target/$file,$(grep -c "public function" "app/$target/$file")" >> $result_file;
    done;

ループお終い。

done;

以上

あとがき

上記、ニーズに合わせてカスタマイズして使っていただければと。
ファイルの grep 周りをもうちょっときれいにできる気がしてる。

2
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
2
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?