-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine-files.sh
125 lines (110 loc) · 3.47 KB
/
combine-files.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# Function to print usage
usage() {
echo "Usage: $0 --combine-at [path_to_directory] [--include-pattern pattern1,pattern2,...] [--exclude-pattern pattern1,pattern2,...] [--include-at dir1,dir2,...] [--exclude-at dir1,dir2,...] [--output-to path_to_output_file]"
exit 1
}
# Default values for include and exclude patterns and directories
include_patterns=()
exclude_patterns=()
include_dirs=()
exclude_dirs=()
output_file=""
verbose=0
# Parse the command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--combine-at) directory="$2"; shift ;;
--include-pattern) IFS=',' read -r -a include_patterns <<< "$2"; shift ;;
--exclude-pattern) IFS=',' read -r -a exclude_patterns <<< "$2"; shift ;;
--include-at) IFS=',' read -r -a include_dirs <<< "$2"; shift ;;
--exclude-at) IFS=',' read -r -a exclude_dirs <<< "$2"; shift ;;
--output-to) output_file="$2"; shift ;;
--verbose) verbose=1 ;;
*) usage ;;
esac
shift
done
# Check if directory is provided
if [ -z "$directory" ]; then
usage
fi
# Check if the provided directory exists
if [ ! -d "$directory" ]; then
echo "Directory does not exist."
exit 1
fi
# Set default output file name if not specified
if [ -z "$output_file" ]; then
output_file="output_$(date +%Y%m%d%H%M%S).txt"
fi
# Clear the output file before starting to append data
> "$output_file"
# Function to check if the file matches the include pattern
is_included_pattern() {
local filename=$(basename "$1")
[[ ${#include_patterns[@]} -eq 0 ]] && return 0
for pattern in "${include_patterns[@]}"; do
if [[ "$filename" == $pattern ]]; then
return 0
fi
done
return 1
}
# Function to check if the file matches the exclude pattern
is_excluded_pattern() {
local filename=$(basename "$1")
for pattern in "${exclude_patterns[@]}"; do
if [[ "$filename" == $pattern ]]; then
return 0
fi
done
return 1
}
# Function to get the relative path of the file from the base directory
get_relative_path() {
local file_path=$1
local base_path=$2
echo "${file_path#$base_path/}"
}
# Function to check if the file's directory is in the include list
is_included_dir() {
local file_path=$(get_relative_path "$1" "$directory")
[[ ${#include_dirs[@]} -eq 0 ]] && return 0
for i in "${include_dirs[@]}"; do
if [[ "$file_path" == "$i"* ]]; then
return 0
fi
done
return 1
}
# Function to check if the file's directory is in the exclude list
is_excluded_dir() {
local file_path=$(get_relative_path "$1" "$directory")
for e in "${exclude_dirs[@]}"; do
if [[ "$file_path" == "$e"* ]]; then
return 0
fi
done
return 1
}
# Function to optionally print a message in verbose mode
verbose_print() {
if [ "$verbose" -eq 1 ]; then
echo "$1"
fi
}
# Iterate over all files in the given directory and its subdirectories
find "$directory" -type f | while read file; do
if is_included_pattern "$file" && ! is_excluded_pattern "$file" && is_included_dir "$file" && ! is_excluded_dir "$file"; then
verbose_print "Adding $file"
echo -e "// $file" >> "$output_file"
cat "$file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
else
verbose_print "Skipping $file"
fi
done
# Print the full path of the output file
absolute_output_path=$(realpath "$output_file")
echo "File saved at $absolute_output_path"