diff --git a/aider/io.py b/aider/io.py index c2eb06877ae..76e52994a03 100644 --- a/aider/io.py +++ b/aider/io.py @@ -30,7 +30,7 @@ from aider.mdstream import MarkdownStream from .dump import dump # noqa: F401 -from .utils import is_image_file +from .utils import is_image_file, get_canary_path @dataclass @@ -405,6 +405,14 @@ def get_input( ): self.rule() + canary_path=get_canary_path(root) + try: # remove canary file if it exists, so wait-for-aider.sh can continue + if os.path.exists(canary_path): + os.remove(canary_path) + except: + pass # yes we don't care if it fails + + rel_fnames = list(rel_fnames) show = "" if rel_fnames: diff --git a/aider/utils.py b/aider/utils.py index cda9777db63..a0463cfeb1c 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -81,6 +81,17 @@ def make_repo(path=None): return repo +def get_canary_path(from_path=None): + """used by scripts/wait-for-aider.sh""" + return Path(get_repo_root(from_path or os.getcwd())) / ".aider.working" + +def get_repo_root(path): + """Get the repository root directory for the given path.""" + try: + repo = git.Repo(path, search_parent_directories=True) + return repo.git.rev_parse("--show-toplevel") + except: + return path def is_image_file(file_name): """ diff --git a/aider/watch.py b/aider/watch.py index 45f4224fd01..b30b7f76569 100644 --- a/aider/watch.py +++ b/aider/watch.py @@ -9,6 +9,7 @@ from watchfiles import watch from aider.dump import dump # noqa +from aider.utils import get_canary_path from aider.watch_prompts import watch_ask_prompt, watch_code_prompt @@ -162,6 +163,9 @@ def process_changes(self): if not has_action: return "" + with open(get_canary_path(self.root), 'w') as file: + file.write("this is a canary file for wait-for-aider.sh which will be deleted after Aider finishes processing changes") + if self.analytics: self.analytics.event("ai-comments execute") self.io.tool_output("Processing your request...") diff --git a/scripts/wait-for-aider.sh b/scripts/wait-for-aider.sh new file mode 100755 index 00000000000..3e0704bdee1 --- /dev/null +++ b/scripts/wait-for-aider.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# usage: pycharm preferences > tools > file watchers > add +# +# - Files to watch > File type > Any; +# - Tool to run on changes: wait-for-aider.sh +# - Output paths to refresh: $Projectpath$ +# - [x] autosave, [-] trigger on external, [x] trigger regardless of syntax errors, [-] create output from stdout + +repo_root=$(git rev-parse --show-toplevel) +# created in FileWatcher.process_changes, removed in InputOutput.get_input +filename="$repo_root/.aider.working" + +has_one_second_passed() { + local current_time=$(date +%s) + local time_diff=$((current_time - start_time)) + [ $time_diff -ge 1 ] +} + +start_time=$(date +%s) + +# wait while the file exists and at least one second has passed so we don't exit immediately +echo "waiting for $filename to be deleted..." + +while [ -e "$filename" ] || ! has_one_second_passed; do + sleep 0.1 +done + +echo "exiting."