File: install-pdm.sh

package info (click to toggle)
pdm 2.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,664 kB
  • sloc: python: 26,438; sh: 314; javascript: 34; makefile: 26
file content (423 lines) | stat: -rwxr-xr-x 11,047 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env bash
set -euo pipefail

# PDM Installer Script
# Downloads and installs PDM from GitHub released binaries

REPO="pdm-project/pdm"
INSTALL_DIR="${PDM_HOME:-}"
VERSION="${PDM_VERSION:-latest}"
SKIP_ADD_TO_PATH="${PDM_SKIP_ADD_TO_PATH:-false}"
SKIP_CHECKSUM="${PDM_SKIP_CHECKSUM:-false}"

# Color output
if [ -t 1 ]; then
    RED='\033[0;31m'
    GREEN='\033[0;32m'
    YELLOW='\033[0;33m'
    CYAN='\033[0;36m'
    BOLD='\033[1m'
    NC='\033[0m' # No Color
else
    RED=''
    GREEN=''
    YELLOW=''
    CYAN=''
    BOLD=''
    NC=''
fi

log() {
    echo -e "${GREEN}${BOLD}PDM${NC}: ${CYAN}$1${NC}"
}

warn() {
    echo -e "${YELLOW}Warning:${NC} $1" >&2
}

error() {
    echo -e "${RED}Error:${NC} $1" >&2
    exit 1
}

# Detect OS and architecture
detect_platform() {
    local os arch target

    # Detect OS
    case "$(uname -s)" in
        Linux)
            os="unknown-linux-gnu"
            ;;
        Darwin)
            os="apple-darwin"
            ;;
        MINGW* | MSYS* | CYGWIN* | Windows_NT)
            os="pc-windows-msvc"
            ;;
        *)
            error "Unsupported OS: $(uname -s)"
            ;;
    esac

    # Detect architecture
    case "$(uname -m)" in
        x86_64 | amd64)
            arch="x86_64"
            ;;
        aarch64 | arm64)
            arch="aarch64"
            ;;
        armv7l)
            arch="armv7"
            ;;
        i686 | i386)
            arch="i686"
            ;;
        *)
            error "Unsupported architecture: $(uname -m)"
            ;;
    esac

    # Construct target triple
    target="${arch}-${os}"

    # Handle special cases
    if [ "$os" = "apple-darwin" ] && [ "$arch" = "aarch64" ]; then
        # aarch64-apple-darwin is the correct name for ARM64 Macs
        target="aarch64-apple-darwin"
    elif [ "$os" = "pc-windows-msvc" ] && [ "$arch" = "i686" ]; then
        # Windows 32-bit (limited support)
        target="i686-pc-windows-msvc"
    fi

    echo "$target"
}

# Get the download URL for a specific version
get_download_url() {
    local version="$1"
    local platform="$2"
    local release_url

    if [ "$version" = "latest" ]; then
        release_url="https://api.github.com/repos/${REPO}/releases/latest"
    else
        release_url="https://api.github.com/repos/${REPO}/releases/tags/${version}"
    fi

    # Use curl or wget
    local json
    if command -v curl >/dev/null 2>&1; then
        json=$(curl -s "$release_url")
    elif command -v wget >/dev/null 2>&1; then
        json=$(wget -qO- "$release_url")
    else
        error "Neither curl nor wget found. Please install one of them."
    fi

    # Extract download URL for the platform
    # PDM binaries are named like: pdm-2.26.1-x86_64-unknown-linux-gnu.tar.gz
    local pattern="pdm-[^-]*-${platform}\\.tar\\.gz"
    local url
    url=$(echo "$json" | grep -o "https://github.com[^\"]*${pattern}" | head -1)

    if [ -z "$url" ]; then
        error "No binary found for platform ${platform}"
    fi

    echo "$url"
}

# Determine install directory
determine_install_dir() {
    if [ -n "$INSTALL_DIR" ]; then
        echo "$INSTALL_DIR"
    else
        # Default to ~/.local/bin on Unix-like systems
        echo "$HOME/.local/bin"
    fi
}

# Download and extract PDM
download_and_install() {
    local url="$1"
    local install_dir="$2"
    local temp_dir temp_file checksum_file

    temp_dir=$(mktemp -d)
    temp_file="${temp_dir}/pdm.tar.gz"
    checksum_file="${temp_dir}/pdm.tar.gz.sha256"

    log "Downloading PDM from $url"

    # First try to download checksum file
    local checksum_url="${url}.sha256"
    local checksum_downloaded=false

    if command -v curl >/dev/null 2>&1; then
        if curl -sL -o "$checksum_file" "$checksum_url" 2>/dev/null; then
            if [ -s "$checksum_file" ]; then
                checksum_downloaded=true
            fi
        fi
    elif command -v wget >/dev/null 2>&1; then
        if wget -O "$checksum_file" "$checksum_url" 2>/dev/null; then
            if [ -s "$checksum_file" ]; then
                checksum_downloaded=true
            fi
        fi
    fi

    # Download the binary
    if command -v curl >/dev/null 2>&1; then
        curl -sL -o "$temp_file" "$url"
    elif command -v wget >/dev/null 2>&1; then
        wget -O "$temp_file" "$url"
    fi

    # Verify checksum if we downloaded it
    if [ "$checksum_downloaded" = true ]; then
        verify_checksum_from_file "$checksum_file" "$temp_file"
    else
        log "No checksum file found. Skipping verification."
    fi

    log "Extracting to $install_dir"

    # Create install directory
    mkdir -p "$install_dir"

    # Extract tar.gz file
    if command -v tar >/dev/null 2>&1; then
        tar -xzf "$temp_file" -C "$temp_dir"
    else
        error "tar not found. Please install tar to extract the archive."
    fi

    # Find and copy the binary
    local binary
    if [ "$(uname -s)" = "Linux" ] || [ "$(uname -s)" = "Darwin" ]; then
        binary=$(find "$temp_dir" -name "pdm" -type f | head -1)
        if [ -z "$binary" ]; then
            error "PDM binary not found in archive"
        fi
        cp "$binary" "$install_dir/pdm"
        chmod +x "$install_dir/pdm"
    else
        # Windows
        binary=$(find "$temp_dir" -name "pdm.exe" -type f | head -1)
        if [ -z "$binary" ]; then
            error "PDM binary not found in archive"
        fi
        cp "$binary" "$install_dir/pdm.exe"
    fi

    # Cleanup
    rm -rf "$temp_dir"
}

# Verify checksum from a checksum file
verify_checksum_from_file() {
    local checksum_file="$1"
    local file_path="$2"
    local expected_checksum actual_checksum

    # Skip verification if explicitly disabled
    if [ "$SKIP_CHECKSUM" = "true" ]; then
        log "Checksum verification skipped (--skip-checksum)."
        return 0
    fi

    # Read expected checksum from file
    expected_checksum=$(awk '{print $1}' "$checksum_file")

    # If no checksum found, skip verification
    if [ -z "$expected_checksum" ]; then
        log "No checksum found in file. Skipping verification."
        return 0
    fi

    # Check if sha256sum is available
    if ! command -v sha256sum >/dev/null 2>&1; then
        log "sha256sum not found. Skipping verification."
        return 0
    fi

    log "Verifying checksum..."

    # Calculate actual checksum
    actual_checksum=$(sha256sum "$file_path" | awk '{print $1}')

    if [ "$expected_checksum" = "$actual_checksum" ]; then
        log "Checksum verification passed."
        return 0
    else
        error "Checksum verification failed!"
        echo "Expected: $expected_checksum"
        echo "Actual:   $actual_checksum"
        return 1
    fi
}

# Add to PATH
add_to_path() {
    local bin_dir="$1"

    if [ "$SKIP_ADD_TO_PATH" = "true" ]; then
        return 0
    fi

    case "$(uname -s)" in
        Linux* | Darwin*)
            # Detect shell
            local shell_name="$SHELL"
            local rcfile=""

            if [[ "$shell_name" == *bash* ]]; then
                rcfile="$HOME/.bashrc"
            elif [[ "$shell_name" == *zsh* ]]; then
                rcfile="$HOME/.zshrc"
            elif [[ "$shell_name" == *fish* ]]; then
                rcfile="$HOME/.config/fish/config.fish"
            else
                warn "Cannot detect shell. Please manually add $bin_dir to your PATH."
                return 0
            fi

            # Check if already in PATH
            if echo ":$PATH:" | grep -q ":${bin_dir}:"; then
                return 0
            fi

            # Add to rcfile
            log "Adding $bin_dir to PATH in $rcfile"
            if [[ "$shell_name" == *fish* ]]; then
                echo "set -gx PATH $bin_dir \$PATH" >> "$rcfile"
            else
                echo "export PATH=\"$bin_dir:\$PATH\"" >> "$rcfile"
            fi

            echo
            echo -e "${YELLOW}Please restart your terminal or run:${NC}"
            if [[ "$shell_name" == *fish* ]]; then
                echo -e "${CYAN}    source $rcfile${NC}"
            else
                echo -e "${CYAN}    source $rcfile${NC}"
            fi
            ;;
        MINGW* | MSYS* | CYGWIN* | Windows_NT)
            warn "Please manually add $bin_dir to your PATH environment variable."
            ;;
    esac
}

# Verify installation
verify_installation() {
    local bin_path="$1"
    local pdm_cmd

    if [ "$(uname -s)" = "Linux" ] || [ "$(uname -s)" = "Darwin" ]; then
        pdm_cmd="$bin_path/pdm"
    else
        pdm_cmd="$bin_path/pdm.exe"
    fi

    if [ ! -x "$pdm_cmd" ]; then
        error "PDM binary not found or not executable at $pdm_cmd"
    fi

    log "Verifying installation..."
    "$pdm_cmd" --version
}

# Print help
usage() {
    cat << EOF
PDM Installer - Install PDM from GitHub released binaries

Usage: $0 [OPTIONS]

OPTIONS:
    -v, --version VERSION    Install specific version (default: latest)
    -p, --path PATH          Installation directory (default: ~/.local/bin)
    -h, --help               Show this help message
        --skip-add-to-path   Skip adding to PATH
        --skip-checksum      Skip checksum verification

ENVIRONMENT VARIABLES:
    PDM_VERSION              Version to install (overridden by -v)
    PDM_HOME                 Installation directory (overridden by -p)
    PDM_SKIP_ADD_TO_PATH     Whether to skip adding to PATH (overridden by --skip-add-to-path)
    PDM_SKIP_CHECKSUM        Whether to skip checksum verification (overridden by --skip-checksum)

Examples:
    $0                       # Install latest version
    $0 -v 2.17.0             # Install version 2.17.0
    $0 -p /usr/local/bin     # Install to /usr/local/bin
    $0 --skip-checksum       # Skip checksum verification

EOF
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -v|--version)
            VERSION="$2"
            shift 2
            ;;
        -p|--path)
            INSTALL_DIR="$2"
            shift 2
            ;;
        --skip-add-to-path)
            SKIP_ADD_TO_PATH="true"
            shift
            ;;
        --skip-checksum)
            SKIP_CHECKSUM="true"
            shift
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            error "Unknown option: $1"
            ;;
    esac
done

# Main installation process
main() {
    log "Detecting platform..."
    local platform
    platform=$(detect_platform)
    echo "  Platform: $platform"

    log "Getting download URL for $VERSION..."
    local download_url
    download_url=$(get_download_url "$VERSION" "$platform")
    echo "  URL: $download_url"

    local install_dir
    install_dir=$(determine_install_dir)
    echo "  Install directory: $install_dir"

    # Install
    download_and_install "$download_url" "$install_dir"
    echo

    # Verify
    verify_installation "$install_dir"
    echo

    # Add to PATH
    add_to_path "$install_dir"

    log "Installation completed successfully!"
}

# Run main function
main "$@"