Skip to content

Commit c9376e3

Browse files
refactor: replace backup/restore with idempotent blockinfile approach for RTK init
Agent-Logs-Url: https://github.com/sparkfabrik/sparkdock/sessions/6c79097f-5af8-4092-967a-572306583224 Co-authored-by: francescoben <[email protected]>
1 parent 8add4a2 commit c9376e3

1 file changed

Lines changed: 64 additions & 30 deletions

File tree

ansible/macos/macos/base.yml

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -821,52 +821,86 @@
821821
- name: Configure RTK for LLM token optimization
822822
tags: rtk
823823
block:
824-
- name: Backup existing Claude Code CLAUDE.md
825-
copy:
826-
src: "{{ ansible_facts['env']['HOME'] }}/.claude/CLAUDE.md"
827-
dest: "{{ ansible_facts['env']['HOME'] }}/.claude/CLAUDE.md.bak"
828-
remote_src: true
824+
# --- Claude Code ---
825+
- name: Install RTK hook for Claude Code (global, hook only)
826+
command: rtk init -g --hook-only --auto-patch
827+
register: rtk_claude_hook_result
828+
changed_when: true
829+
failed_when: rtk_claude_hook_result.rc != 0
829830
become: false
830-
ignore_errors: yes
831831

832-
- name: Initialize RTK for Claude Code
833-
command: rtk init -g --auto-patch
834-
register: rtk_claude_result
835-
changed_when: true
836-
failed_when: rtk_claude_result.rc != 0
832+
- name: Initialize RTK for Claude Code in temp directory
833+
shell: |
834+
RTK_TMPDIR=$(mktemp -d)
835+
cd "${RTK_TMPDIR}"
836+
rtk init --auto-patch
837+
echo "${RTK_TMPDIR}"
838+
register: rtk_claude_tmpdir
839+
changed_when: false
840+
become: false
841+
842+
- name: Ensure Claude Code config directory exists
843+
file:
844+
path: "{{ ansible_facts['env']['HOME'] }}/.claude"
845+
state: directory
846+
mode: '0755'
847+
become: false
848+
849+
- name: Inject RTK instructions into Claude Code CLAUDE.md
850+
blockinfile:
851+
path: "{{ ansible_facts['env']['HOME'] }}/.claude/CLAUDE.md"
852+
marker: "<!-- {mark} RTK MANAGED BLOCK -->"
853+
block: "{{ lookup('file', rtk_claude_tmpdir.stdout + '/CLAUDE.md') }}"
854+
create: yes
837855
become: false
838856

839-
- name: Restore user content to Claude Code CLAUDE.md
857+
- name: Cleanup Claude Code temp directory
858+
file:
859+
path: "{{ rtk_claude_tmpdir.stdout }}"
860+
state: absent
861+
become: false
862+
863+
# --- GitHub Copilot ---
864+
- name: Initialize RTK for GitHub Copilot in temp directory
840865
shell: |
841-
if [ -f "{{ ansible_facts['env']['HOME'] }}/.claude/CLAUDE.md.bak" ]; then
842-
cat "{{ ansible_facts['env']['HOME'] }}/.claude/CLAUDE.md.bak" >> "{{ ansible_facts['env']['HOME'] }}/.claude/CLAUDE.md"
843-
rm "{{ ansible_facts['env']['HOME'] }}/.claude/CLAUDE.md.bak"
844-
fi
866+
RTK_TMPDIR=$(mktemp -d)
867+
cd "${RTK_TMPDIR}"
868+
rtk init --copilot --auto-patch
869+
echo "${RTK_TMPDIR}"
870+
register: rtk_copilot_tmpdir
871+
changed_when: false
845872
become: false
846873

847-
- name: Backup existing Copilot custom instructions
874+
- name: Ensure GitHub Copilot hooks directory exists
875+
file:
876+
path: "{{ ansible_facts['env']['HOME'] }}/.github-copilot/hooks"
877+
state: directory
878+
mode: '0755'
879+
become: false
880+
881+
- name: Copy RTK hooks to GitHub Copilot config
848882
copy:
849-
src: "{{ ansible_facts['env']['HOME'] }}/.github-copilot/custom-instructions.md"
850-
dest: "{{ ansible_facts['env']['HOME'] }}/.github-copilot/custom-instructions.md.bak"
883+
src: "{{ rtk_copilot_tmpdir.stdout }}/.github-copilot/hooks/"
884+
dest: "{{ ansible_facts['env']['HOME'] }}/.github-copilot/hooks/"
851885
remote_src: true
852886
become: false
853887
ignore_errors: yes
854888

855-
- name: Initialize RTK for GitHub Copilot
856-
command: rtk init -g --copilot --auto-patch
857-
register: rtk_copilot_result
858-
changed_when: true
859-
failed_when: rtk_copilot_result.rc != 0
889+
- name: Inject RTK instructions into Copilot custom-instructions.md
890+
blockinfile:
891+
path: "{{ ansible_facts['env']['HOME'] }}/.github-copilot/custom-instructions.md"
892+
marker: "<!-- {mark} RTK MANAGED BLOCK -->"
893+
block: "{{ lookup('file', rtk_copilot_tmpdir.stdout + '/.github-copilot/custom-instructions.md') }}"
894+
create: yes
860895
become: false
861896

862-
- name: Restore user content to Copilot custom instructions
863-
shell: |
864-
if [ -f "{{ ansible_facts['env']['HOME'] }}/.github-copilot/custom-instructions.md.bak" ]; then
865-
cat "{{ ansible_facts['env']['HOME'] }}/.github-copilot/custom-instructions.md.bak" >> "{{ ansible_facts['env']['HOME'] }}/.github-copilot/custom-instructions.md"
866-
rm "{{ ansible_facts['env']['HOME'] }}/.github-copilot/custom-instructions.md.bak"
867-
fi
897+
- name: Cleanup GitHub Copilot temp directory
898+
file:
899+
path: "{{ rtk_copilot_tmpdir.stdout }}"
900+
state: absent
868901
become: false
869902

903+
# --- OpenCode ---
870904
- name: Initialize RTK for OpenCode
871905
command: rtk init -g --opencode --auto-patch
872906
register: rtk_opencode_result

0 commit comments

Comments
 (0)