forked from XistGG/UnrealXistTools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UProjectSln.ps1
131 lines (106 loc) · 3.52 KB
/
UProjectSln.ps1
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
#
# UProjectSln.ps1
#
# See: https://github.com/XistGG/UnrealXistTools/
#
# This takes an optional $Path value and expands it to an actual
# MyGame.sln file location.
#
# If $Path does not expand to a valid .sln file,
# an exception is thrown.
#
# If no exception is thrown, the Get-Item result for the .sln is returned
#
# Usage: UProjectSln.ps1 -Debug
#
[CmdletBinding()]
param(
[Parameter()]$Path
)
function FindSlnInDirectory()
{
param(
[string]$ProjectName,
[string]$Directory
)
$Result = $null
$TempSlns = Get-ChildItem -Path $Directory -File `
| Where-Object {$_.Extension -ieq '.sln'}
if ($TempSlns.count -eq 1)
{
# Found exactly 1 .sln file, use it
$Result = $TempSlns[0]
Write-Debug "Found 1 .sln in $Directory, using it: $Result"
}
elseif ($TempSlns.count -gt 1)
{
# $UProjectSln is a directory with multiple .sln files
# Search for a project file with the same name as the directory
foreach ($ProjectSln in $TempSlns)
{
if ($ProjectName -ieq $ProjectSln.BaseName)
{
# Found it (example "Foo/Foo.sln")
$Result = $ProjectSln
Write-Debug "Compare '$ProjectName' -ieq '$($ProjectSln.BaseName)' == TRUE"
}
else
{
Write-Debug "Compare '$ProjectName' -ieq '$($ProjectSln.BaseName)' == false"
}
}
# If we still don't know which .sln to start, error.
# User needs to tell us explicitly.
if (!$Result)
{
foreach ($ProjectSln in $TempSlns)
{
Write-Warning "Ambiguous .sln: $ProjectSln"
}
Write-Error "Cannot auto-select a .sln file in a directory with multiple .sln; You must specify which .sln to use for this directory"
throw "Explicit .sln required for directory: $Directory"
}
Write-Debug "Found 2+ .sln in $Directory, using: $Result"
}
else # $TempSlns.count -lte 0
{
# $UProjectSln is a directory without any .sln files
throw "Not an Unreal Engine project directory; no .sln files in: $Directory"
}
return $Result
}
################################################################################
## Main
################################################################################
if (!$Path) # if $null, '' or any other empty value
{
# Default implicit $Path is current directory
$Path = Get-Location
Write-Debug "Auto-selecting current directory Path: $Path"
}
# Try to get information about the UProject (file or directory)
$UProjectSln = Get-Item -Path $Path 2> $null
if (!$UProjectSln -or !$UProjectSln.Exists)
{
throw "No such UProject file or directory: $Path"
}
# First check of $UProjectSln is a file
if (!$UProjectSln.PSIsContainer)
{
# $UProjectSln is a file; make sure it has a .sln extension
if (!($UProjectSln.Extension -ieq '.sln'))
{
throw "Sln is not a .sln: $Path"
}
Write-Debug "UProjectSln is a .sln file; using it: $($UProjectSln.FullName)"
}
else
{
# $UProjectSln is a directory, try to find the correct .sln to use
$UProjectSln =& FindSlnInDirectory -ProjectName $UProjectSln.Name -Directory $UProjectSln.FullName
# We expect an exception will already have been thrown when !$UProjectSln,
# but just to drive this point, here it is explicitly:
if (!$UProjectSln) { throw "UProjectSln is null" };
}
Write-Debug "UProjectSln=$UProjectSln"
return $UProjectSln