Skip to content

Commit 38c64f4

Browse files
try system homebrew before installing local version
1 parent 0979d0d commit 38c64f4

File tree

1 file changed

+131
-92
lines changed

1 file changed

+131
-92
lines changed

src/coveralls_functions.jl

Lines changed: 131 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -120,117 +120,128 @@ function download_coveralls_reporter(; force=false, install_dir=nothing)
120120
end
121121

122122
"""
123-
install_via_homebrew(reporter_info; force=false)
123+
detect_homebrew_status()
124124
125-
Install Coveralls reporter via Homebrew (macOS).
126-
First installs a local Homebrew if needed, then installs coveralls locally.
125+
Detect available Homebrew installations and return status information.
126+
Returns (brew_cmd, use_local, local_homebrew_dir, local_brew_path).
127127
"""
128-
function install_via_homebrew(reporter_info; force=false)
129-
# Set up local Homebrew installation directory using scratch space
128+
function detect_homebrew_status()
130129
local_homebrew_dir = @get_scratch!("local_homebrew")
131130
local_brew_path = joinpath(local_homebrew_dir, "bin", "brew")
132-
133-
# Use the local Homebrew installation
134-
brew_cmd = local_brew_path
135-
136-
# Check if local Homebrew is available, install if not
137-
if !isfile(local_brew_path)
138-
@info "Installing local Homebrew to: $local_homebrew_dir"
131+
132+
# Try system Homebrew first
133+
system_brew_cmd = Sys.which("brew")
134+
if system_brew_cmd !== nothing
139135
try
140-
# Create the directory
141-
mkpath(local_homebrew_dir)
142-
143-
# Download and extract Homebrew tarball directly
144-
@info "Downloading latest Homebrew release..."
145-
146-
# Get the latest release info
147-
latest_release_url = "https://api.github.com/repos/Homebrew/brew/releases/latest"
148-
response = HTTP.get(latest_release_url)
149-
release_data = JSON.parse(String(response.body))
150-
latest_tag = release_data["tag_name"]
151-
tarball_url = release_data["tarball_url"]
152-
153-
@info "Found latest Homebrew release: $latest_tag"
154-
tarball_path = joinpath(local_homebrew_dir, "homebrew-$latest_tag.tar.gz")
136+
# Simple writability test: check if we can write to the brew prefix
137+
brew_prefix = chomp(read(`$system_brew_cmd --prefix`, String))
138+
if isdir(brew_prefix) && iswritable(brew_prefix)
139+
@info "System Homebrew is available and writable"
140+
return (system_brew_cmd, false, local_homebrew_dir, local_brew_path)
141+
end
142+
catch e
143+
@debug "System Homebrew check failed: $e"
144+
end
145+
end
146+
147+
@info "Using local Homebrew installation"
148+
return (local_brew_path, true, local_homebrew_dir, local_brew_path)
149+
end
155150

156-
# Download the tarball
157-
Downloads.download(tarball_url, tarball_path)
151+
"""
152+
install_via_homebrew(reporter_info; force=false)
158153
159-
# Extract the tarball to our directory
160-
@info "Extracting Homebrew..."
161-
run(`tar -xzf $tarball_path -C $local_homebrew_dir --strip-components=1`; wait=true)
154+
Install Coveralls reporter via Homebrew (macOS).
155+
First tries system Homebrew, then falls back to local Homebrew if system is locked down.
156+
"""
157+
function install_via_homebrew(reporter_info; force=false)
158+
brew_cmd, use_local_homebrew, local_homebrew_dir, local_brew_path = detect_homebrew_status()
162159

163-
# Remove the tarball
164-
rm(tarball_path)
160+
# Install local Homebrew if needed
161+
if use_local_homebrew && !isfile(local_brew_path)
162+
install_local_homebrew(local_homebrew_dir, local_brew_path)
163+
end
165164

166-
# Verify the brew executable exists
167-
if !isfile(local_brew_path)
168-
error("Homebrew extraction failed - brew executable not found at: $local_brew_path")
169-
end
165+
# Determine coveralls installation path
166+
coveralls_path = if use_local_homebrew
167+
joinpath(local_homebrew_dir, "bin", "coveralls")
168+
else
169+
brew_prefix = chomp(read(`$brew_cmd --prefix`, String))
170+
joinpath(brew_prefix, "bin", "coveralls")
171+
end
170172

171-
# Post-install setup
172-
@info "Running Homebrew post-install setup..."
173-
run(`$brew_cmd update --force --quiet`; wait=true)
173+
# Check if already installed
174+
if !force && isfile(coveralls_path)
175+
@info "Coveralls reporter already installed at: $coveralls_path"
176+
return coveralls_path
177+
end
174178

175-
# Fix zsh permissions
176-
brew_prefix = chomp(read(`$brew_cmd --prefix`, String))
177-
zsh_share_dir = joinpath(brew_prefix, "share", "zsh")
178-
if isdir(zsh_share_dir)
179-
run(`chmod -R go-w $zsh_share_dir`; wait=true)
180-
end
179+
# Install coveralls
180+
return install_coveralls_with_homebrew(brew_cmd, reporter_info, coveralls_path, use_local_homebrew, force)
181+
end
181182

182-
@info "Local Homebrew installed successfully"
183-
catch e
184-
error("Failed to install local Homebrew: $e")
185-
end
186-
else
187-
@info "Local Homebrew found at: $local_brew_path"
188-
end
183+
"""
184+
install_local_homebrew(local_homebrew_dir, local_brew_path)
189185
190-
# Check if coveralls is already installed locally
191-
if !force
192-
# Check for coveralls in the local Homebrew bin directory
193-
local_coveralls_path = joinpath(local_homebrew_dir, "bin", "coveralls")
194-
if isfile(local_coveralls_path)
195-
@info "Coveralls reporter already installed via local Homebrew at: $local_coveralls_path"
196-
return local_coveralls_path
197-
end
186+
Install a local Homebrew instance.
187+
"""
188+
function install_local_homebrew(local_homebrew_dir, local_brew_path)
189+
@info "Installing local Homebrew to: $local_homebrew_dir"
190+
191+
mkpath(local_homebrew_dir)
192+
193+
# Download and extract Homebrew
194+
latest_release_url = "https://api.github.com/repos/Homebrew/brew/releases/latest"
195+
response = HTTP.get(latest_release_url)
196+
release_data = JSON.parse(String(response.body))
197+
tarball_url = release_data["tarball_url"]
198+
199+
tarball_path = joinpath(local_homebrew_dir, "homebrew-latest.tar.gz")
200+
Downloads.download(tarball_url, tarball_path)
201+
202+
run(`tar -xzf $tarball_path -C $local_homebrew_dir --strip-components=1`)
203+
rm(tarball_path)
204+
205+
if !isfile(local_brew_path)
206+
error("Homebrew extraction failed - brew executable not found")
198207
end
199208

200-
@info "Installing Coveralls reporter via local Homebrew..."
209+
# Post-install setup
210+
run(`$local_brew_path update --force --quiet`)
211+
@info "Local Homebrew installed successfully"
212+
end
201213

202-
try
203-
# Add the tap if it doesn't exist (ignore failures)
204-
@info "Adding Homebrew tap: $(reporter_info.tap)"
205-
try
206-
run(`$brew_cmd tap $(reporter_info.tap)`; wait=true)
207-
catch e
208-
@debug "Tap command failed (possibly already exists): $e"
209-
end
214+
"""
215+
install_coveralls_with_homebrew(brew_cmd, reporter_info, coveralls_path, use_local_homebrew, force=false)
210216
211-
# Install coveralls (ignore exit status)
212-
@info "Installing Coveralls reporter..."
213-
try
214-
if force
215-
run(`$brew_cmd reinstall $(reporter_info.package)`; wait=true)
216-
else
217-
run(`$brew_cmd install $(reporter_info.package)`; wait=true)
218-
end
219-
catch e
220-
@debug "Install command failed (possibly already installed): $e"
221-
end
217+
Install coveralls using the specified brew command.
218+
"""
219+
function install_coveralls_with_homebrew(brew_cmd, reporter_info, coveralls_path, use_local_homebrew, force=false)
220+
homebrew_type = use_local_homebrew ? "local Homebrew" : "system Homebrew"
221+
@info "Installing Coveralls reporter via $homebrew_type..."
222222

223-
# Check if the binary exists regardless of install command status
224-
local_coveralls_path = joinpath(local_homebrew_dir, "bin", "coveralls")
225-
if !isfile(local_coveralls_path)
226-
error("Coveralls installation failed - not found at expected path: $local_coveralls_path")
227-
end
228-
@info "Coveralls reporter installed locally at: $local_coveralls_path"
229-
return local_coveralls_path
223+
# Add tap (ignore failures)
224+
try
225+
run(`$brew_cmd tap $(reporter_info.tap)`)
226+
catch e
227+
@debug "Tap command failed (possibly already exists): $e"
228+
end
230229

230+
# Install coveralls
231+
install_cmd = force ? "reinstall" : "install"
232+
try
233+
run(`$brew_cmd $install_cmd $(reporter_info.package)`)
231234
catch e
232-
error("Failed to install Coveralls reporter via local Homebrew: $e")
235+
@debug "Install command failed (possibly already installed): $e"
236+
end
237+
238+
# Verify installation
239+
if !isfile(coveralls_path)
240+
error("Coveralls installation failed - not found at: $coveralls_path")
233241
end
242+
243+
@info "Coveralls reporter installed at: $coveralls_path"
244+
return coveralls_path
234245
end
235246

236247
"""
@@ -274,8 +285,7 @@ function get_coveralls_executable(; auto_download=true, install_dir=nothing)
274285
platform = CoverageUtils.detect_platform()
275286
reporter_info = get_coveralls_info(platform)
276287

277-
# First, check if coveralls is available in PATH
278-
# Try common executable names
288+
# Check if coveralls is available in PATH
279289
for exec_name in ["coveralls", "coveralls-reporter", reporter_info.filename]
280290
coveralls_path = Sys.which(exec_name)
281291
if coveralls_path !== nothing && isfile(coveralls_path)
@@ -284,6 +294,35 @@ function get_coveralls_executable(; auto_download=true, install_dir=nothing)
284294
end
285295
end
286296

297+
# For macOS, check Homebrew installations
298+
if platform == :macos
299+
_, use_local_homebrew, local_homebrew_dir, _ = detect_homebrew_status()
300+
301+
# Check system Homebrew if available
302+
if !use_local_homebrew
303+
system_brew_cmd = Sys.which("brew")
304+
if system_brew_cmd !== nothing
305+
try
306+
brew_prefix = chomp(read(`$system_brew_cmd --prefix`, String))
307+
system_coveralls_path = joinpath(brew_prefix, "bin", "coveralls")
308+
if isfile(system_coveralls_path)
309+
@info "Found Coveralls reporter in system Homebrew: $system_coveralls_path"
310+
return system_coveralls_path
311+
end
312+
catch e
313+
@debug "Could not check system Homebrew installation: $e"
314+
end
315+
end
316+
end
317+
318+
# Check local Homebrew installation
319+
local_coveralls_path = joinpath(local_homebrew_dir, "bin", "coveralls")
320+
if isfile(local_coveralls_path)
321+
@info "Found Coveralls reporter in local Homebrew: $local_coveralls_path"
322+
return local_coveralls_path
323+
end
324+
end
325+
287326
# Check in specified install directory
288327
if install_dir !== nothing
289328
local_path = joinpath(install_dir, reporter_info.filename)

0 commit comments

Comments
 (0)