From 5b3c564394fb5ae63bc934bd6878b9ddb7077121 Mon Sep 17 00:00:00 2001 From: matsuda Date: Thu, 12 Feb 2026 10:56:52 +0900 Subject: [PATCH] Add launchd scripts --- ...m.github.mtsd.xcode-archives-cleaner.plist | 41 +++++++++++++++ .../com.github.mtsd.xcode-archives-cleaner.sh | 51 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 dot.launchd/com.github.mtsd.xcode-archives-cleaner.plist create mode 100755 dot.launchd/com.github.mtsd.xcode-archives-cleaner.sh diff --git a/dot.launchd/com.github.mtsd.xcode-archives-cleaner.plist b/dot.launchd/com.github.mtsd.xcode-archives-cleaner.plist new file mode 100644 index 0000000..d9104c7 --- /dev/null +++ b/dot.launchd/com.github.mtsd.xcode-archives-cleaner.plist @@ -0,0 +1,41 @@ + + + + + Label + com.github.mtsd.xcode-archives-cleaner + + ProgramArguments + + /bin/bash + /Users/kosuke-matsuda/.launchd/com.github.mtsd.xcode-archives-cleaner.sh + + + StartCalendarInterval + + Day + 1 + Hour + 3 + Minute + 0 + + + + StandardOutPath + /tmp/xcodearchivecleaner.log + StandardErrorPath + /tmp/xcodearchivecleaner.err + + + EnvironmentVariables + + PATH + /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin + + + + RunAtLoad + + + diff --git a/dot.launchd/com.github.mtsd.xcode-archives-cleaner.sh b/dot.launchd/com.github.mtsd.xcode-archives-cleaner.sh new file mode 100755 index 0000000..1db5252 --- /dev/null +++ b/dot.launchd/com.github.mtsd.xcode-archives-cleaner.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# macOS専用スクリプトのため、macOS以外では終了 +if [[ "$(uname -s)" != "Darwin"* ]]; then + echo "This script is for macOS only" + exit 0 +fi + +# Xcodeのアーカイブディレクトリ +ARCHIVE_DIR="$HOME/Library/Developer/Xcode/Archives" + +# 削除したいアーカイブの経過日数(例:30日以上経過したもの) +DAYS_OLD=30 + +# OS検出して適切なdateコマンドを使用 +if [[ "$(uname -s)" == "Darwin"* ]]; then + # macOS (BSD date) + CUTOFF_DATE=$(date -v-${DAYS_OLD}d +%Y-%m-%d) +else + # Linux (GNU date) - このスクリプトはmacOS専用だが念のため + CUTOFF_DATE=$(date -d "${DAYS_OLD} days ago" +%Y-%m-%d) +fi + +echo "削除対象: $CUTOFF_DATE より前のアーカイブ" + +# 古いアーカイブを見つけて削除 +find "$ARCHIVE_DIR" -type d -name "*.xcarchive" -print0 | while IFS= read -r -d '' archive; do + # アーカイブの作成日を取得(macOS用) + if [[ "$(uname -s)" == "Darwin"* ]]; then + # macOS (BSD stat) + CREATED_DATE=$(stat -f "%Sm" -t "%Y-%m-%d" "$archive") + else + # Linux (GNU stat) - このスクリプトはmacOS専用だが念のため + CREATED_DATE=$(stat -c "%y" "$archive" | cut -d' ' -f1) + fi + + # カットオフ日より古いかチェック + if [[ "$CREATED_DATE" < "$CUTOFF_DATE" ]]; then + echo "削除: $archive (作成日: $CREATED_DATE)" + # 実際に削除する場合はコメントを外す + rm -rf "$archive" + fi +done + +# アーカイブ削除後、空になった日付ディレクトリ(YYYY-MM-DD)を削除 +find "$ARCHIVE_DIR" -mindepth 1 -maxdepth 1 -type d -name "????-??-??" -empty -print0 | while IFS= read -r -d '' empty_date_dir; do + echo "空ディレクトリ削除: $empty_date_dir" + rmdir "$empty_date_dir" +done + +echo "クリーニング完了"