Skip to content

Commit 9ea35d6

Browse files
committed
Improve error handling and processing of variables
1 parent 7aeca8e commit 9ea35d6

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

src/Compile-SourceScript/Public/Compile-SourceScript.ps1

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function Compile-SourceScript {
2929

3030
[CmdletBinding()]
3131
param(
32-
[Parameter(Mandatory=$False)]
33-
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
32+
[Parameter(Mandatory=$True)]
33+
[ValidateNotNullOrEmpty()]
3434
$File
3535
,
3636
[Parameter(Mandatory=$False)]
@@ -41,8 +41,21 @@ function Compile-SourceScript {
4141
)
4242

4343
begin {
44-
"Starting Compile-SourceScript" | Write-Host -ForegroundColor Cyan
4544
$ErrorActionPreference = 'Stop'
45+
"Starting Compile-SourceScript" | Write-Host -ForegroundColor Cyan
46+
47+
# Verify the specified item's type and extension
48+
$sourceFile = Get-Item -Path $PSBoundParameters['File']
49+
if (!(Test-Path -Path $sourceFile.FullName -PathType Leaf)) {
50+
throw "The item is not a file."
51+
}
52+
$MOD_NAME = if ($sourceFile.Extension -eq '.sp') { 'sourcemod' }
53+
elseif ($sourceFile.Extension -eq '.sma') { 'amxmodx' }
54+
if (!$MOD_NAME) {
55+
throw "File is not a '.sp' or '.sma' source file."
56+
}
57+
58+
# Initialize variables
4659
$MOD = @{
4760
sourcemod = @{
4861
script_ext = '.sp'
@@ -77,32 +90,20 @@ function Compile-SourceScript {
7790
}
7891
}
7992
}
80-
$MOD_NAME = if ([System.IO.Path]::GetExtension($PSBoundParameters['File']) -eq '.sp') { 'sourcemod' }
81-
elseif ([System.IO.Path]::GetExtension($PSBoundParameters['File']) -eq '.sma') { 'amxmodx' }
82-
if (!$MOD_NAME) {
83-
throw "File is not a .sp or .sma source file."
84-
}
8593
$COMPILER_NAME = if ($env:OS) {
8694
if ($PSBoundParameters['SkipWrapper']) { $MOD[$MOD_NAME]['compiler']['windows']['bin'] }
8795
else { $MOD[$MOD_NAME]['compiler']['windows']['wrapper'] }
8896
}else {
8997
if ($PSBoundParameters['SkipWrapper']) { $MOD[$MOD_NAME]['compiler']['others']['bin'] }
9098
else { $MOD[$MOD_NAME]['compiler']['others']['wrapper'] }
9199
}
100+
$SCRIPTING_DIR = $sourceFile.DirectoryName
101+
$COMPILED_DIR = Join-Path $SCRIPTING_DIR $MOD[$MOD_NAME]['compiled_dir_name']
102+
$PLUGINS_DIR = Join-Path (Split-Path $SCRIPTING_DIR -Parent) $MOD[$MOD_NAME]['plugins_dir_name']
92103

93-
try {
94-
$sourceFile = Get-Item -Path $PSBoundParameters['File']
95-
96-
# Normalize paths
97-
$SCRIPTING_DIR = $sourceFile.DirectoryName
98-
$COMPILED_DIR = Join-Path $SCRIPTING_DIR $MOD[$MOD_NAME]['compiled_dir_name']
99-
$PLUGINS_DIR = Join-Path (Split-Path $SCRIPTING_DIR -Parent) $MOD[$MOD_NAME]['plugins_dir_name']
104+
# Verify the presence of the compiler item
105+
$compiler = Get-Item -Path (Join-Path $SCRIPTING_DIR $COMPILER_NAME)
100106

101-
# Validate compiler binary
102-
$compiler = Get-Item -Path (Join-Path $SCRIPTING_DIR $COMPILER_NAME)
103-
}catch {
104-
throw
105-
}
106107
}process {
107108
try {
108109
"Compiler: '$($compiler.FullName)'" | Write-Host

0 commit comments

Comments
 (0)