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?

LLMからアドバイスを貰うためにディレクトリ構成とファイル内容を1つのテキストファイルに書き出すスクリプト

Last updated at Posted at 2024-08-03
  1. gen_codes.shを配置する。

    #!/bin/bash
    
    # スクリプトのファイル名を取得し、拡張子を変更して出力ファイル名を設定
    script_name=$(basename "$0")
    output_file="${script_name%.sh}.txt"
    
    # 除外するディレクトリ名とファイル名を配列で指定
    exclude_dirs=("__pycache__" "venv" ".dart_tool" "build")
    exclude_files=("$script_name" "$output_file" ".gitignore" "__init__.py")
    
    # 出力ファイルを初期化
    > "$output_file"
    
    # 除外パターンを作成
    exclude_args=()
    for dir in "${exclude_dirs[@]}"; do
      exclude_args+=( -path "*/$dir/*" -prune -o )
    done
    for file in "${exclude_files[@]}"; do
      exclude_args+=( -name "$file" -o )
    done
    
    # ディレクトリ構造をtree形式で出力
    echo "Directory Structure" >> "$output_file"
    echo "===================" >> "$output_file"
    find . \( "${exclude_args[@]}" -false \) -o -print | sed -e 's|[^/]*/|    |g' -e 's|    |├── |g' -e 's|──  |── |g' >> "$output_file"
    echo "" >> "$output_file"
    
    # 出力ファイルのヘッダーにファイル内容の概要を追加
    echo "File Contents" >> "$output_file"
    echo "=============" >> "$output_file"
    echo "" >> "$output_file"
    
    # findコマンドを組み立てて実行
    find . \( "${exclude_args[@]}" -type f -print \) | while read -r file; do
      # ファイルパスの先頭 "./" を "/" に置き換え
      relative_path="${file#./}"
      echo "=== /$relative_path ===" >> "$output_file"
      cat "$file" >> "$output_file"
      echo -e "\n\n" >> "$output_file"
    done
    
    echo "Directory structure and contents have been written to $output_file"    
    
  2. ./gen_codes.shを実行すると、gen_codes.txtが出力される。

  3. LLMに投入して会話を続ける。

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?