Skip to content

Commit d3ee545

Browse files
committed
refactor(fork): improve file copying and template handling
- Refactor copy_file function for better robustness - Add proper directory creation and handling - Switch from sed to perl for more reliable text replacement - Add template comment stripping functionality - Improve error handling and file organization - Add pre-commit hook installation - Simplify README copying process - Improve git initialization workflow
1 parent 399402c commit d3ee545

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

fork.sh

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,53 @@ mkdir -p "${TARGET_DIR}"/{journal,tasks/{all,active,done,new,paused,cancelled,te
4646
echo "Copying core files..."
4747

4848
function copy_file() {
49-
# copies file/directory
50-
cp -r "${SOURCE_DIR}/$1" "${TARGET_DIR}/"
51-
# replaces NAME_TEMPLATE with NEW_AGENT in file contents
52-
find "${TARGET_DIR}/$1" -type f -exec sed -i '' "s/${NAME_TEMPLATE}/${NEW_AGENT}/g" {} \;
53-
# runs chmod +x on scripts
54-
find "${TARGET_DIR}/$1" -type f -name "*.sh" -exec chmod +x {} \;
49+
local src="${SOURCE_DIR}/$1"
50+
local dst="${TARGET_DIR}/$1"
51+
52+
# Create target directory if copying a directory
53+
if [ -d "$src" ]; then
54+
mkdir -p "$dst"
55+
cp -r "$src/"* "$dst/"
56+
else
57+
# Ensure parent directory exists for files
58+
mkdir -p "$(dirname "$dst")"
59+
cp -r "$src" "$dst"
60+
fi
61+
62+
# Process all files, whether dst is a file or directory
63+
find "$dst" -type f -print0 | while IFS= read -r -d '' file; do
64+
# Replace template strings
65+
perl -i -pe "s/${NAME_TEMPLATE}/${NEW_AGENT}/g" "$file"
66+
perl -i -pe "s/${NAME_TEMPLATE}-template/${NEW_AGENT}/g" "$file"
67+
# Strip template comments
68+
perl -i -pe 'BEGIN{undef $/;} s/<!--template-->.*?<!--\/template-->//gs' "$file"
69+
done
70+
71+
# Make shell scripts executable
72+
find "$dst" -type f -name "*.sh" -exec chmod +x {} \;
5573
}
5674

5775
# Core documentation and configuration
58-
copy_file Makefile
76+
copy_file README.md
77+
cp "${SOURCE_DIR}/Makefile" "${TARGET_DIR}/Makefile" # copy without replacing NAME_TEMPLATE
5978
copy_file ARCHITECTURE.md
6079
copy_file .pre-commit-config.yaml
6180
copy_file scripts
6281
copy_file run.sh
63-
# replace gptme-agent with the new agent name
64-
cat "${SOURCE_DIR}/fork.sh" | sed "s/gptme-agent/${NEW_AGENT}/g" > "${TARGET_DIR}/fork.sh"
65-
chmod +x "${TARGET_DIR}/fork.sh"
82+
copy_file fork.sh
6683

6784
# Copy base knowledge
68-
cp "${SOURCE_DIR}/knowledge/agent-forking.md" "${TARGET_DIR}/knowledge/"
69-
cp "${SOURCE_DIR}/knowledge/forking-workspace.md" "${TARGET_DIR}/knowledge/"
85+
copy_file knowledge/agent-forking.md
86+
copy_file knowledge/forking-workspace.md
7087

71-
# Copy person template
72-
cp "${SOURCE_DIR}/people/templates/"* "${TARGET_DIR}/people/templates/" 2>/dev/null || true
73-
cat "${SOURCE_DIR}/people/templates/person.md" | sed "s/${NAME_TEMPLATE}/${NEW_AGENT}/g" > "${TARGET_DIR}/people/templates/person.md"
88+
# Copy template
89+
copy_file */templates/*.md
7490

7591
# Initialize tasks
7692
echo "# No Active Task" > "${TARGET_DIR}/tasks/all/no-active-task.md"
7793

7894
# Initial setup task from template
79-
cp "${SOURCE_DIR}/tasks/templates/initial-agent-setup.md" "${TARGET_DIR}/tasks/templates/"
95+
copy_file tasks/templates/initial-agent-setup.md
8096
cp "${SOURCE_DIR}/tasks/templates/initial-agent-setup.md" "${TARGET_DIR}/tasks/all/"
8197
ln -sf "../all/initial-agent-setup.md" "${TARGET_DIR}/tasks/active/"
8298
ln -sf "./tasks/all/initial-agent-setup.md" "${TARGET_DIR}/CURRENT_TASK.md"
@@ -108,14 +124,6 @@ cat > "${TARGET_DIR}/ABOUT.md" << EOL
108124
[Core values and principles]
109125
EOL
110126

111-
# Create README
112-
# Replace occurrences of NAME_TEMPLATE with NEW_AGENT
113-
# Strip any <!--template--><!--/template--> comments
114-
cat "${SOURCE_DIR}/README.md" |
115-
sed "s/${NAME_TEMPLATE}-template/${NEW_AGENT}/g" |
116-
sed "s/${NAME_TEMPLATE}/${NEW_AGENT}/g" |
117-
sed '/<!--template-->/, /<!--\/template-->/d' > "${TARGET_DIR}/README.md"
118-
119127
# Create initial TASKS.md with setup as first task
120128
cat > "${TARGET_DIR}/TASKS.md" << EOL
121129
# Tasks
@@ -162,14 +170,16 @@ cat > "${TARGET_DIR}/people/creator.md" << EOL
162170
EOL
163171

164172
# Initialize git
165-
(cd "${TARGET_DIR}" && git init && git add . && git commit -m "feat: initialize ${NEW_AGENT} agent workspace")
173+
(cd "${TARGET_DIR}" && git init)
174+
175+
# Install pre-commit hooks
176+
(cd "${TARGET_DIR}" && pre-commit install)
166177

167-
# Run checks
168-
echo
169-
echo "Running pre-commit checks..."
170-
(cd "${TARGET_DIR}" && pre-commit run --all-files)
178+
# Commit initial files
179+
(cd "${TARGET_DIR}" && git add . && git commit -m "feat: initialize ${NEW_AGENT} agent workspace")
171180

172-
(cd "${TARGET_DIR}" && ./run.sh --dry-run)
181+
# Dry run the agent to check for errors
182+
(cd "${TARGET_DIR}" && ./run.sh --dry-run > /dev/null)
173183

174184
TARGET_DIR_RELATIVE="./$(realpath --relative-to="$(pwd)" "${TARGET_DIR}")"
175185

0 commit comments

Comments
 (0)