|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Print a formatted list of source files from specific directories. |
| 17 | +# Suggested usage: dumpsrc.sh <path1> [path2] [path3] [...] | pbcopy |
| 18 | + |
| 19 | +included_files=( |
| 20 | + '*.swig' |
| 21 | + '*.i' |
| 22 | + '*.c' |
| 23 | + '*.cc' |
| 24 | + '*.cpp' |
| 25 | + '*.cs' |
| 26 | + '*.cmake' |
| 27 | + '*.fbs' |
| 28 | + '*.gradle' |
| 29 | + '*.h' |
| 30 | + '*.hh' |
| 31 | + '*.java' |
| 32 | + '*.js' |
| 33 | + '*.json' |
| 34 | + '*.md' |
| 35 | + '*.m' |
| 36 | + '*.mm' |
| 37 | + 'CMakeLists.txt' |
| 38 | + 'Podfile' |
| 39 | +) |
| 40 | + |
| 41 | +get_markdown_language_for_file() { |
| 42 | + local filename="$1" |
| 43 | + local base="$(basename "$filename")" |
| 44 | + local ext="${base##*.}" |
| 45 | + if [[ "$base" == "$ext" && "$base" != .* ]]; then |
| 46 | + ext="" # No extension |
| 47 | + fi |
| 48 | + |
| 49 | + # Handle special filename case first |
| 50 | + if [[ "$base" == "CMakeLists.txt" ]]; then |
| 51 | + echo "cmake" |
| 52 | + return 0 |
| 53 | + fi |
| 54 | + |
| 55 | + # Main logic using case based on extension |
| 56 | + case "$ext" in |
| 57 | + c) echo "c" ;; |
| 58 | + cc) echo "cpp" ;; |
| 59 | + cs) echo "csharp" ;; |
| 60 | + hh) echo "cpp" ;; |
| 61 | + m) echo "objectivec" ;; |
| 62 | + mm) echo "objectivec" ;; |
| 63 | + sh) echo "bash" ;; |
| 64 | + py) echo "python" ;; |
| 65 | + md) echo "markdown" ;; |
| 66 | + json) echo "js" ;; |
| 67 | + h) |
| 68 | + if grep -qE "\@interface|\#import" "$filename"; then |
| 69 | + echo "objectivec"; |
| 70 | + else |
| 71 | + echo "cpp"; |
| 72 | + fi |
| 73 | + ;; |
| 74 | + "") # Explicitly handle no extension (after CMakeLists.txt check) |
| 75 | + : # Output nothing |
| 76 | + ;; |
| 77 | + *) # Default case for any other non-empty extension |
| 78 | + echo "$ext" |
| 79 | + ;; |
| 80 | + esac |
| 81 | + return 0 |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +if [[ -z "$1" ]]; then |
| 86 | + echo "Usage: $0 <path1> [path2] [path3] ..." |
| 87 | + exit 1 |
| 88 | +fi |
| 89 | + |
| 90 | +find_cmd_args=('-name' 'UNUSED') |
| 91 | + |
| 92 | +for pattern in "${included_files[@]}"; do |
| 93 | + if [[ -n "${pattern}" ]]; then |
| 94 | + find_cmd_args+=('-or' '-name' "$pattern") |
| 95 | + fi |
| 96 | +done |
| 97 | + |
| 98 | +for f in `find $* -type f -and "${find_cmd_args[@]}"`; do |
| 99 | + echo "*** BEGIN CONTENTS OF FILE '$f' ***"; |
| 100 | + echo '```'$(get_markdown_language_for_file "$f"); |
| 101 | + cat "$f"; |
| 102 | + echo '```'; |
| 103 | + echo "*** END CONTENTS OF FILE '$f' ***"; |
| 104 | + echo |
| 105 | +done |
0 commit comments