Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions script/setup-claude.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ KNOWN_MARKETPLACES="${PLUGINS_DIR}/known_marketplaces.json"
REPO_PLUGINS_DIR="${REPO_ROOT}/.claude/plugins"
SETTINGS_LOCAL_FILE="${CLAUDE_DIR}/settings.local.json"
DEFAULT_SETTINGS_LOCAL="${REPO_ROOT}/.devcontainer/claude-settings.local.json"
SETTINGS_FILE="${CLAUDE_DIR}/settings.json"

# 複数の CLAUDE_CONFIG_DIR に共有設定を配るためのキー。
# ここに無いキー(model / theme / tui 等)は各 dir 固有として保持される。
# shellcheck disable=SC2016 # jq に渡すJSONリテラル。$schema はシェル変数ではない
CLAUDE_SHARED_SETTINGS_KEYS='["$schema","hooks","permissions","enabledPlugins","extraKnownMarketplaces","attribution"]'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Install plugins for each synced config dir

When setup is run from the normal shell, this copies enabledPlugins/extraKnownMarketplaces into ~/.claude-*, but the later claude plugin install flow still runs only for the active Claude config and never installs plugin artifacts under those target dirs. I checked the Claude Code docs: CLAUDE_CONFIG_DIR stores plugins under that path, and enabledPlugins does not by itself install external plugins. As a result, Agent Deck sessions using CLAUDE_CONFIG_DIR=~/.claude-private can get settings that claim plugins are enabled while the plugin commands/hooks/LSPs remain absent until a separate per-dir install is run.

Useful? React with 👍 / 👎.


# settings.local.json のセットアップ
# ホストに settings.local.json がない場合、デフォルトをコピー
Expand All @@ -52,6 +58,78 @@ setup_settings_local() {
fi
}

# 追加の CLAUDE_CONFIG_DIR を列挙する
# Agent Deck の config.toml が group ごとに config_dir を切り替えるため、
# ~/.claude 以外の dir が存在しうる(例: ~/.claude-private, ~/.claude-elu)。
# CLAUDE_EXTRA_CONFIG_DIRS で明示指定も可能(スペース区切り)。
list_extra_config_dirs() {
if [[ -n "${CLAUDE_EXTRA_CONFIG_DIRS:-}" ]]; then
local -a explicit_dirs
read -r -a explicit_dirs <<< "$CLAUDE_EXTRA_CONFIG_DIRS"
printf '%s\n' "${explicit_dirs[@]}"
return
fi
local dir
for dir in "${HOME}"/.claude-*; do
[[ -d "$dir" ]] || continue
printf '%s\n' "$dir"
done
}

# 共有設定を追加の CLAUDE_CONFIG_DIR に同期する
#
# なぜ必要か: settings.json は CLAUDE_CONFIG_DIR ごとに独立しており、
# ~/.claude の hooks / permissions / plugins は他の dir では一切読まれない。
# 2026-07-15 時点で ~/.claude-private は hooks 1個・~/.claude-elu は 0個と、
# Quality Gates も permissions も attribution も効かない状態になっていた。
sync_settings_to_extra_config_dirs() {
if [[ ! -f "$SETTINGS_FILE" ]]; then
log_warn "正本の settings.json が見つかりません: ${SETTINGS_FILE}(同期をスキップ)"
return
fi
if ! command -v jq &> /dev/null; then
log_warn "jq が見つかりません。追加 config dir への設定同期をスキップします"
return
fi

local target_dir target_file tmp_file
while IFS= read -r target_dir; do
[[ -n "$target_dir" ]] || continue
target_file="${target_dir}/settings.json"
Comment on lines +96 to +98

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Expand tildes in explicit directory paths.

If a user specifies paths starting with ~/ in CLAUDE_EXTRA_CONFIG_DIRS (e.g., passing it in quotes), bash will not automatically perform tilde expansion when reading the variable. This will result in a literal directory named ~ being created in the current working directory. You can use parameter expansion to safely replace a leading tilde with the $HOME path.

💡 Proposed fix
     while IFS= read -r target_dir; do
         [[ -n "$target_dir" ]] || continue
+
+        # Handle tilde expansion for explicitly provided paths
+        if [[ "$target_dir" == "~" ]] || [[ "$target_dir" == "~/"* ]]; then
+            target_dir="${target_dir/#\~/$HOME}"
+        fi
+
         target_file="${target_dir}/settings.json"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
while IFS= read -r target_dir; do
[[ -n "$target_dir" ]] || continue
target_file="${target_dir}/settings.json"
while IFS= read -r target_dir; do
[[ -n "$target_dir" ]] || continue
# Handle tilde expansion for explicitly provided paths
if [[ "$target_dir" == "~" ]] || [[ "$target_dir" == "~/"* ]]; then
target_dir="${target_dir/#\~/$HOME}"
fi
target_file="${target_dir}/settings.json"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@script/setup-claude.sh` around lines 96 - 98, Update the directory-processing
loop around target_dir so each non-empty path with a leading "~/” is expanded to
use $HOME before constructing target_file, while preserving absolute and other
explicit paths unchanged.


if [[ -e "$target_file" ]] && ! jq -e . "$target_file" > /dev/null 2>&1; then
log_warn " ${target_file} が不正なJSONのためスキップします"
continue
fi

tmp_file="$(mktemp)"
if ! jq --argjson keys "$CLAUDE_SHARED_SETTINGS_KEYS" \
'with_entries(select(.key as $k | $keys | index($k)))' \
"$SETTINGS_FILE" > "$tmp_file" 2>/dev/null; then
log_warn " 共有設定の抽出に失敗しました: ${SETTINGS_FILE}"
rm -f "$tmp_file"
continue
fi

local merged_file
merged_file="$(mktemp)"
if jq -s '(.[0] // {}) + .[1]' \
<(if [[ -f "$target_file" ]]; then cat "$target_file"; else echo '{}'; fi) \
"$tmp_file" > "$merged_file" 2>/dev/null && jq -e . "$merged_file" > /dev/null 2>&1; then
if [[ -f "$target_file" ]] && cmp -s "$target_file" "$merged_file"; then
log_info " ${target_dir} は最新です"
else
mkdir -p "$target_dir"
cp "$merged_file" "$target_file"
log_success " ${target_dir} に共有設定を同期しました"
Comment on lines +123 to +124

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Verify the exit status of cp to ensure accurate logging.

If the cp command fails (e.g., due to file permission issues or lack of disk space), the script will incorrectly log a success message. Additionally, if the script is executed in an environment with set -e, an unhandled command failure will unexpectedly terminate the setup process.

🛡️ Proposed fix to handle copy failures
-                cp "$merged_file" "$target_file"
-                log_success "  ${target_dir} に共有設定を同期しました"
+                if cp "$merged_file" "$target_file"; then
+                    log_success "  ${target_dir} に共有設定を同期しました"
+                else
+                    log_warn "  ${target_dir} への共有設定の同期に失敗しました"
+                fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cp "$merged_file" "$target_file"
log_success " ${target_dir} に共有設定を同期しました"
if cp "$merged_file" "$target_file"; then
log_success " ${target_dir} に共有設定を同期しました"
else
log_warn " ${target_dir} への共有設定の同期に失敗しました"
fi
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@script/setup-claude.sh` around lines 123 - 124, Update the copy flow
surrounding cp and log_success so it checks the cp exit status before reporting
success. On failure, handle the copy error explicitly according to the script’s
existing error-handling conventions, and only call log_success after the copy
completes successfully.

fi
else
log_warn " ${target_file} のマージに失敗しました"
fi
rm -f "$tmp_file" "$merged_file"
done < <(list_extra_config_dirs)
}

main() {
log_info "Claude Code セットアップを開始します..."
log_info "環境: HOME=${HOME}"
Expand All @@ -70,6 +148,10 @@ main() {
# settings.local.json のセットアップ
setup_settings_local

# 追加の CLAUDE_CONFIG_DIR(~/.claude-private 等)へ共有設定を同期
log_info "追加の CLAUDE_CONFIG_DIR に共有設定を同期します..."
sync_settings_to_extra_config_dirs

Comment on lines +151 to +154

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Execute synchronization after the canonical settings.json is fully updated.

Running sync_settings_to_extra_config_dirs at this stage causes it to sync the stale version of settings.json. If subsequent setup steps—such as plugins::sync_repo_content (which copies the latest repo configuration to $CLAUDE_DIR) or plugins::apply_hookify_patch—update the canonical settings, those updates will not reach the extra directories until the user runs the setup script a second time.

Remove these lines from their current position and place them at the very end of the main() function, immediately before the final log_success message, to ensure the most up-to-date canonical settings are always distributed.

✂️ Remove from here
-    # 追加の CLAUDE_CONFIG_DIR(~/.claude-private 等)へ共有設定を同期
-    log_info "追加の CLAUDE_CONFIG_DIR に共有設定を同期します..."
-    sync_settings_to_extra_config_dirs
-
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@script/setup-claude.sh` around lines 151 - 154, Move the
sync_settings_to_extra_config_dirs invocation and its preceding log_info from
the current position to the end of main(), immediately before the final
log_success message. Keep synchronization after all canonical settings updates,
including plugins::sync_repo_content and plugins::apply_hookify_patch.

# Claude CLI の存在確認
if ! command -v claude &> /dev/null; then
log_warn "Claude CLI が見つかりません。プラグインのインストールはスキップされます。"
Expand Down
Loading