Skip to content

Commit 95f86b6

Browse files
committed
refactor(psutils): 重构macOS应用检测逻辑到独立函数
将Test-ApplicationInstalled中macOS平台的检测逻辑提取到新的Test-MacOSApplicationInstalled函数 简化主函数结构并提高代码可维护性
1 parent 6f623b2 commit 95f86b6

1 file changed

Lines changed: 51 additions & 20 deletions

File tree

psutils/modules/test.psm1

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,7 @@ function Test-ApplicationInstalled {
243243

244244
switch ($os) {
245245
"macOS" {
246-
if ($FilterCli) {
247-
# 仅检测命令行程序
248-
return Test-EXEProgram -Name $AppName
249-
}
250-
else {
251-
# 检测所有类型:先检测命令行程序,再检测cask应用和formula
252-
$cliInstalled = Test-EXEProgram -Name $AppName
253-
if ($cliInstalled) {
254-
return $true
255-
}
256-
257-
# 如果命令行程序未安装,检测cask应用
258-
$caskInstalled = Test-MacOSCaskApp -AppName $AppName -UseBrew $true
259-
if ($caskInstalled) {
260-
return $true
261-
}
262-
263-
# 如果cask应用也未安装,检测Homebrew formula
264-
return Test-HomebrewFormula -AppName $AppName
265-
}
246+
return Test-MacOSApplicationInstalled -AppName $AppName -FilterCli $FilterCli
266247
}
267248
{ $_ -in @("Windows", "Linux") } {
268249
# Windows和Linux使用命令行程序检测
@@ -280,4 +261,54 @@ function Test-ApplicationInstalled {
280261
}
281262
}
282263

264+
function Test-MacOSApplicationInstalled {
265+
<#
266+
.SYNOPSIS
267+
检测macOS上应用程序是否已安装
268+
.DESCRIPTION
269+
在macOS上,根据FilterCli参数检测命令行程序、cask应用或Homebrew formula。
270+
.PARAMETER AppName
271+
要检测的应用程序名称
272+
.PARAMETER FilterCli
273+
是否仅检测命令行程序,默认为$false(检测所有类型)
274+
.EXAMPLE
275+
Test-MacOSApplicationInstalled -AppName "git"
276+
检测git是否已安装(包括命令行和应用程序)
277+
.EXAMPLE
278+
Test-MacOSApplicationInstalled -AppName "git" -FilterCli $true
279+
仅检测git命令行工具是否已安装
280+
.OUTPUTS
281+
[bool] 如果应用已安装返回$true,否则返回$false
282+
#>
283+
[CmdletBinding()]
284+
param(
285+
[Parameter(Mandatory = $true)]
286+
[string]$AppName,
287+
288+
[Parameter()]
289+
[bool]$FilterCli = $false
290+
)
291+
292+
if ($FilterCli) {
293+
# 仅检测命令行程序
294+
return Test-EXEProgram -Name $AppName
295+
}
296+
else {
297+
# 检测所有类型:先检测命令行程序,再检测cask应用和formula
298+
$cliInstalled = Test-EXEProgram -Name $AppName
299+
if ($cliInstalled) {
300+
return $true
301+
}
302+
303+
# 如果命令行程序未安装,检测cask应用
304+
$caskInstalled = Test-MacOSCaskApp -AppName $AppName -UseBrew $true
305+
if ($caskInstalled) {
306+
return $true
307+
}
308+
309+
# 如果cask应用也未安装,检测Homebrew formula
310+
return Test-HomebrewFormula -AppName $AppName
311+
}
312+
}
313+
283314
Export-ModuleMember -Function *

0 commit comments

Comments
 (0)