Skip to content

Commit 8554dee

Browse files
Refactor Linux installation logic for PowerShell to improve package detection and downloading process
1 parent 09d87d3 commit 8554dee

File tree

1 file changed

+55
-76
lines changed

1 file changed

+55
-76
lines changed

action.yml

Lines changed: 55 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,33 @@ runs:
5555
exit 0
5656
fi
5757
58-
if command -v apt-get >/dev/null; then
59-
echo "Using APT package manager (Debian/Ubuntu)..."
60-
61-
if ! grep -q "packages.microsoft.com" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
62-
wget -qO- https://packages.microsoft.com/keys/microsoft.asc \
63-
| sudo tee /etc/apt/trusted.gpg.d/microsoft.asc > /dev/null
64-
DIST_CODENAME=$(lsb_release -cs)
65-
sudo add-apt-repository \
66-
"deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/ubuntu/$DIST_CODENAME/prod $DIST_CODENAME main"
67-
fi
68-
69-
sudo apt-get update -y
70-
EXACT_PKG=$(apt-cache madison powershell | awk '{print $3}' \
71-
| grep -E "^${REQUESTED_VERSION}(-|$)" | head -n1 || true)
72-
73-
if [[ -n "$EXACT_PKG" ]]; then
74-
sudo apt-get install -y powershell=$EXACT_PKG
58+
# Determine Linux distribution type
59+
ARCH=$(dpkg --print-architecture 2>/dev/null || rpm --eval '%{_arch}' 2>/dev/null || echo "x86_64")
60+
61+
if command -v apt-get >/dev/null || command -v dpkg >/dev/null; then
62+
# Debian/Ubuntu based
63+
echo "Detected Debian/Ubuntu based system..."
64+
DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
65+
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
66+
echo "Downloading from: $URL"
67+
wget -q "$URL" -O "$DEB_NAME"
68+
sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
69+
elif command -v rpm >/dev/null; then
70+
# RHEL/Fedora/CentOS based
71+
echo "Detected RHEL/Fedora/CentOS based system..."
72+
73+
if [[ "$ARCH" == "aarch64" ]]; then
74+
RPM_NAME="powershell-${REQUESTED_VERSION}-1.rh.${ARCH}.rpm"
7575
else
76-
ARCH=$(dpkg --print-architecture)
77-
DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
78-
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
79-
wget -q "$URL" -O "$DEB_NAME"
80-
sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
76+
RPM_NAME="powershell-${REQUESTED_VERSION}-1.rh.x86_64.rpm"
8177
fi
78+
79+
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${RPM_NAME}"
80+
echo "Downloading from: $URL"
81+
wget -q "$URL" -O "$RPM_NAME"
82+
sudo rpm -i "$RPM_NAME" || sudo yum install -y "$RPM_NAME"
8283
else
83-
echo "Unsupported Linux distribution (no apt-get)."
84+
echo "Unsupported Linux distribution. Cannot determine package format."
8485
exit 1
8586
fi
8687
@@ -95,67 +96,45 @@ runs:
9596
9697
echo "Requested version: [$REQUESTED_VERSION]"
9798
98-
# Convert to lowercase for comparison (macOS-compatible method)
99-
REQ_VER_LOWER=$(echo "$REQUESTED_VERSION" | tr '[:upper:]' '[:lower:]')
100-
101-
# Only resolve to latest version if explicitly set to 'latest'
102-
if [[ "$REQ_VER_LOWER" == "latest" ]]; then
103-
REQUESTED_VERSION=$(
104-
curl -s -f \
105-
-H "Accept: application/vnd.github+json" \
106-
-H "Authorization: Bearer $GITHUB_TOKEN" \
107-
-H "X-GitHub-Api-Version: 2022-11-28" \
108-
https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
109-
jq -r '.tag_name' | sed 's/^v//'
110-
)
111-
echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
112-
fi
113-
114-
# Validate REQUESTED_VERSION is not empty
115-
if [[ -z "${REQUESTED_VERSION}" ]]; then
116-
echo "Error: Could not determine a valid PowerShell version."
117-
exit 1
118-
fi
99+
# Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
100+
case "${REQUESTED_VERSION:-}" in
101+
[Ll][Aa][Tt][Ee][Ss][Tt])
102+
REQUESTED_VERSION=$(
103+
curl -s -f \
104+
-H "Accept: application/vnd.github+json" \
105+
-H "Authorization: Bearer $GITHUB_TOKEN" \
106+
-H "X-GitHub-Api-Version: 2022-11-28" \
107+
https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
108+
jq -r '.tag_name' | sed 's/^v//'
109+
)
110+
echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
111+
;;
112+
"")
113+
echo "Error: Version input is required (or use 'latest')"
114+
exit 1
115+
;;
116+
esac
119117
120118
DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
121119
if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
122120
echo "PowerShell $DETECTED_VERSION already installed. Skipping."
123121
exit 0
124122
fi
125123
126-
# Try Homebrew first (simplified approach)
127-
if command -v brew >/dev/null; then
128-
echo "Using Homebrew package manager..."
129-
# Homebrew handles 'latest' automatically when no version is specified
130-
if [[ "$REQ_VER_LOWER" == "latest" ]]; then
131-
brew install --cask powershell
132-
else
133-
# Try specific version formula first
134-
if brew install --cask "powershell@$REQUESTED_VERSION" 2>/dev/null; then
135-
echo "Successfully installed via Homebrew"
136-
else
137-
# Fall back to generic formula if versioned one doesn't exist
138-
echo "Version-specific formula not found, installing latest via Homebrew"
139-
brew install --cask powershell
140-
fi
141-
fi
142-
else
143-
# Fall back to direct download
144-
echo "Homebrew not available, downloading directly..."
145-
ARCH=$(uname -m)
146-
case "$ARCH" in
147-
"arm64") PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg" ;;
148-
*) PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg" ;;
149-
esac
150-
151-
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
152-
echo "Downloading from: $URL"
153-
if ! curl -sSL "$URL" -o "$PKG_NAME"; then
154-
echo "Error: Failed to download PowerShell package"
155-
exit 1
156-
fi
157-
sudo installer -pkg "$PKG_NAME" -target /
124+
# Determine architecture and download appropriate package
125+
ARCH=$(uname -m)
126+
case "$ARCH" in
127+
"arm64") PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg" ;;
128+
*) PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg" ;;
129+
esac
130+
131+
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
132+
echo "Downloading from: $URL"
133+
if ! curl -sSL "$URL" -o "$PKG_NAME"; then
134+
echo "Error: Failed to download PowerShell package"
135+
exit 1
158136
fi
137+
sudo installer -pkg "$PKG_NAME" -target /
159138
160139
- name: Install PowerShell (Windows)
161140
if: runner.os == 'Windows'

0 commit comments

Comments
 (0)