forked from yohasebe/code-packager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-unpackager
More file actions
executable file
·169 lines (146 loc) · 5.35 KB
/
code-unpackager
File metadata and controls
executable file
·169 lines (146 loc) · 5.35 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# Version information
VERSION="0.3.1"
# Signal handling and cleanup
cleanup() {
echo -e "\n\nOperation cancelled by user (CTRL+C)"
# Remove incomplete destination directory if it was created during this session
if [ -n "$DESTINATION_DIRECTORY" ] && [ -d "$DESTINATION_DIRECTORY" ]; then
# Only remove if it looks like it was created by this script
# (check if it's empty or contains only partially restored files)
if [ -z "$(ls -A "$DESTINATION_DIRECTORY" 2>/dev/null)" ]; then
rmdir "$DESTINATION_DIRECTORY" 2>/dev/null
echo "Removed empty destination directory: $DESTINATION_DIRECTORY"
else
echo "Warning: Destination directory contains files. Manual cleanup may be needed: $DESTINATION_DIRECTORY"
fi
fi
echo "Exiting gracefully..."
exit 130
}
# Trap SIGINT (CTRL+C) to call cleanup function
trap cleanup SIGINT
# Function to display help
show_help() {
echo "Usage: $0 -j <json_file> -d <destination_directory> [options]"
echo ""
echo "Options:"
echo " -j <json_file> Path to the JSON file generated by code-packager."
echo " -d <destination_dir> Path to the directory where the folder structure should be restored."
echo " -s, --silent Skip confirmation before restoration."
echo " -v, --version Display version information and exit."
echo " -h, --help Display this help and exit."
echo ""
}
# Function to display version
show_version() {
echo "Code Unpackager for Language Models - Version $VERSION"
}
# Function to check for required dependencies
check_dependencies() {
local dependencies=("jq")
local missing_deps=0
for dep in "${dependencies[@]}"; do
if ! command -v "$dep" &> /dev/null; then
echo "Error: Required dependency '$dep' is not installed."
missing_deps=1
fi
done
if [ "$missing_deps" -ne 0 ]; then
echo "Please install the missing dependencies and try again."
exit 1
fi
}
# Check dependencies before proceeding
check_dependencies
# Parse command line arguments
SILENT=false
while getopts "j:d:svh-" opt; do
case $opt in
j) JSON_FILE="${OPTARG}" ;;
d) DESTINATION_DIRECTORY="${OPTARG}" ;;
s) SILENT=true ;;
v) show_version
exit 0 ;;
h) show_help
exit 0 ;;
-) case "${OPTARG}" in
version) show_version
exit 0 ;;
help) show_help
exit 0 ;;
silent) SILENT=true ;;
*) echo "Error: Invalid option -${OPTARG}. Use -h or --help for usage information." >&2
exit 1 ;;
esac ;;
esac
done
# Check if required parameters are provided
if [ -z "$JSON_FILE" ] || [ -z "$DESTINATION_DIRECTORY" ]; then
echo "Error: Both JSON file path and destination directory are required."
show_help
exit 1
fi
# Check if JSON file exists
if [ ! -f "$JSON_FILE" ]; then
echo "Error: JSON file '$JSON_FILE' does not exist."
exit 1
fi
# Validate JSON structure
if ! jq -e '.files | length > 0' "$JSON_FILE" > /dev/null; then
echo "Error: JSON structure is invalid or does not contain any files."
exit 1
fi
# Confirm restoration unless silent mode is enabled
if [ "$SILENT" = false ]; then
read -p "Are you sure you want to restore the folder structure to '$DESTINATION_DIRECTORY'? (y/n): " confirm
if [ "$confirm" != "y" ]; then
echo "Operation cancelled."
exit 0
fi
fi
# Create destination directory if it doesn't exist
mkdir -p "$DESTINATION_DIRECTORY"
# Iterate through the files in the JSON file
jq -c '.files[]' "$JSON_FILE" | while read -r file; do
# Check if this is the new vector store format (has metadata field)
has_metadata=$(echo "$file" | jq -r 'has("metadata")')
if [ "$has_metadata" = "true" ]; then
# New vector store format
filename=$(echo "$file" | jq -r '.metadata.filename')
path=$(echo "$file" | jq -r '.metadata.path')
content=$(echo "$file" | jq -r '.content')
# Extract original content by removing the "File: path" prefix
if [ "$content" != "null" ]; then
# Remove the first two lines (File: path and empty line)
original_content=$(echo "$content" | tail -n +3)
# Check if it's a binary file marker
if [[ "$content" == *"[Binary file - content not included]"* ]]; then
content="null"
else
content="$original_content"
fi
fi
else
# Legacy format
filename=$(echo "$file" | jq -r '.filename')
content=$(echo "$file" | jq -r '.content')
path=$(echo "$file" | jq -r '.path')
fi
# Validate each file entry
if [ -z "$filename" ] || [ -z "$path" ]; then
echo "Error: Invalid file entry in JSON. Missing 'filename' or 'path'."
exit 1
fi
# Create the directory structure
full_path="$DESTINATION_DIRECTORY$path"
mkdir -p "$full_path"
# Write the file content
if [ "$content" != "null" ] && [ -n "$content" ]; then
echo "$content" > "$full_path/$filename"
else
# Create an empty file for binary files (content is null)
touch "$full_path/$filename"
fi
done
echo "Folder structure restored to: $DESTINATION_DIRECTORY"