|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Colors for output |
| 6 | +GREEN='\033[0;32m' |
| 7 | +YELLOW='\033[1;33m' |
| 8 | +BLUE='\033[0;34m' |
| 9 | +NC='\033[0m' # No Color |
| 10 | + |
| 11 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 | + |
| 13 | +# Load .env.local if available |
| 14 | +if [ -f "${SCRIPT_DIR}/../.env.local" ]; then |
| 15 | + # shellcheck disable=SC1091 |
| 16 | + source "${SCRIPT_DIR}/../.env.local" |
| 17 | +fi |
| 18 | + |
| 19 | +# Determine path to packages.conf file |
| 20 | +if [ -n "${PACKAGES_CONF_DIR:-}" ] && [ -f "${PACKAGES_CONF_DIR}/packages.conf" ]; then |
| 21 | + PACKAGES_CONF="${PACKAGES_CONF_DIR}/packages.conf" |
| 22 | +elif [ -f "${SCRIPT_DIR}/packages.conf" ]; then |
| 23 | + PACKAGES_CONF="${SCRIPT_DIR}/packages.conf" |
| 24 | +else |
| 25 | + PACKAGES_CONF="${SCRIPT_DIR}/packages.conf.example" |
| 26 | +fi |
| 27 | + |
| 28 | +echo -e "${BLUE}Using config file:${NC} ${PACKAGES_CONF}" |
| 29 | + |
| 30 | +# Check availability functions (simplified versions of app.sh) |
| 31 | +check_homebrew_api() { |
| 32 | + local app=$1 |
| 33 | + local type=$2 |
| 34 | + |
| 35 | + if [ "$type" = "cask" ]; then |
| 36 | + curl -s -f -o /dev/null "https://formulae.brew.sh/api/cask/${app}.json" |
| 37 | + else |
| 38 | + curl -s -f -o /dev/null "https://formulae.brew.sh/api/formula/${app}.json" |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +check_chocolatey_api() { |
| 43 | + local app=$1 |
| 44 | + curl -s -f "https://community.chocolatey.org/api/v2/Packages()?%24filter=tolower(Id)%20eq%20tolower('${app}')" | grep -q "${app}" |
| 45 | +} |
| 46 | + |
| 47 | +# Detect operating system |
| 48 | +OS="$(uname -s)" |
| 49 | +case "${OS}" in |
| 50 | + Darwin*) MACHINE=Mac;; |
| 51 | + CYGWIN*|MINGW*|MSYS*) MACHINE=Windows;; |
| 52 | + *) MACHINE=Linux;; |
| 53 | +esac |
| 54 | + |
| 55 | +echo -e "${BLUE}Detected System:${NC} ${MACHINE}" |
| 56 | + |
| 57 | +# Function to check if package exists in config |
| 58 | +package_exists_in_config() { |
| 59 | + local name=$1 |
| 60 | + if [ ! -f "$PACKAGES_CONF" ]; then |
| 61 | + return 1 |
| 62 | + fi |
| 63 | + # Check if name appears in column 2 (Mac) or 3 (Win) |
| 64 | + # Format: TYPE|MAC|WIN|DESC |
| 65 | + if grep -qE "^[^|]+\|${name}\|" "$PACKAGES_CONF" || grep -qE "^[^|]+\|[^|]+\|${name}\|" "$PACKAGES_CONF"; then |
| 66 | + return 0 |
| 67 | + fi |
| 68 | + return 1 |
| 69 | +} |
| 70 | + |
| 71 | +count=0 |
| 72 | + |
| 73 | +process_package() { |
| 74 | + local name="$1" |
| 75 | + local type="$2" |
| 76 | + local origin="$3" |
| 77 | + |
| 78 | + if package_exists_in_config "$name"; then |
| 79 | + # echo "Skipping $name (already configured)" |
| 80 | + return |
| 81 | + fi |
| 82 | + |
| 83 | + echo -e "${GREEN}Found new package:${NC} $name ($type)" |
| 84 | + |
| 85 | + local mac_name="-" |
| 86 | + local win_name="-" |
| 87 | + local desc="Imported from $origin" |
| 88 | + |
| 89 | + if [ "$MACHINE" = "Mac" ]; then |
| 90 | + mac_name="$name" |
| 91 | + # Try to find match on Windows |
| 92 | + echo -n " Checking duplicate on Windows... " |
| 93 | + if check_chocolatey_api "$name"; then |
| 94 | + win_name="$name" |
| 95 | + echo "found ($name)" |
| 96 | + else |
| 97 | + echo "not found" |
| 98 | + fi |
| 99 | + elif [ "$MACHINE" = "Windows" ]; then |
| 100 | + win_name="$name" |
| 101 | + # Try to find match on Mac |
| 102 | + echo -n " Checking duplicate on macOS... " |
| 103 | + if check_homebrew_api "$name" "cask"; then |
| 104 | + mac_name="$name" |
| 105 | + type="cask" |
| 106 | + echo "found (cask)" |
| 107 | + elif check_homebrew_api "$name" "brew"; then |
| 108 | + mac_name="$name" |
| 109 | + if [ "$type" != "cask" ]; then type="brew"; fi |
| 110 | + echo "found (brew)" |
| 111 | + else |
| 112 | + echo "not found" |
| 113 | + fi |
| 114 | + fi |
| 115 | + |
| 116 | + echo "${type}|${mac_name}|${win_name}|${desc}" >> "$PACKAGES_CONF" |
| 117 | + ((count++)) |
| 118 | +} |
| 119 | + |
| 120 | +if [ "$MACHINE" = "Mac" ]; then |
| 121 | + if command -v brew &>/dev/null; then |
| 122 | + echo -e "\n${BLUE}Searching Homebrew Formulas...${NC}" |
| 123 | + # using leaves to avoid dependencies |
| 124 | + for pkg in $(brew leaves); do |
| 125 | + process_package "$pkg" "brew" "macOS" |
| 126 | + done |
| 127 | + |
| 128 | + echo -e "\n${BLUE}Searching Homebrew Casks...${NC}" |
| 129 | + for pkg in $(brew list --cask); do |
| 130 | + process_package "$pkg" "cask" "macOS" |
| 131 | + done |
| 132 | + fi |
| 133 | + |
| 134 | + if command -v mas &>/dev/null; then |
| 135 | + echo -e "\n${BLUE}Searching Mac App Store...${NC}" |
| 136 | + # mas list output: "123456 App Name" |
| 137 | + # But config format is usually brew|mas|-|Desc ?? |
| 138 | + # Actually existing config has 'brew|mas|-|Mac App Store CLI' which refers to the mas tool itself. |
| 139 | + # ok_computer doesn't seem to have explicit 'mas' generic support in install logic yet? |
| 140 | + # src/init_conf_macOs.sh usually handles 'mas' lines. |
| 141 | + # Let's verify src/init_conf_macOs.sh logic. |
| 142 | + # Assuming format 'mas|app_id|name|desc' or 'type|mac_name|...' where mac_name is id? |
| 143 | + # Let's stick to brew/cask for now to be safe unless we verify mas support. |
| 144 | + # Skipping 'mas' for now to avoid breaking config with IDs. |
| 145 | + echo "Skipping MAS apps (auto-import not fully supported yet)" |
| 146 | + fi |
| 147 | + |
| 148 | +elif [ "$MACHINE" = "Windows" ]; then |
| 149 | + if command -v choco &>/dev/null; then |
| 150 | + echo -e "\n${BLUE}Searching Chocolatey packages...${NC}" |
| 151 | + # choco list -lo -r returns "name|version" |
| 152 | + # use -r for pipe delimited |
| 153 | + choco list --local-only --limit-output | while IFS='|' read -r name ver; do |
| 154 | + # Filter out chocolatey itself or common lib packages? |
| 155 | + if [ "$name" = "chocolatey" ]; then continue; fi |
| 156 | + |
| 157 | + # Heuristic for type: default to brew (CLI) or cask (GUI)? |
| 158 | + # Hard to know. Let's assume 'cask' for everything on Windows default as usually people install tools. |
| 159 | + # OR check if it exists as a cask on mac? |
| 160 | + process_package "$name" "brew" "Windows" |
| 161 | + done |
| 162 | + else |
| 163 | + echo "Chocolatey not found." |
| 164 | + fi |
| 165 | +else |
| 166 | + echo "Linux import not implemented yet." |
| 167 | +fi |
| 168 | + |
| 169 | +echo -e "\n${GREEN}Done!${NC} Added $count new packages to $PACKAGES_CONF" |
0 commit comments