-
Notifications
You must be signed in to change notification settings - Fork 7
/
UProjectFile.ps1
executable file
·138 lines (112 loc) · 4.03 KB
/
UProjectFile.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
132
133
134
135
136
137
138
#!/usr/bin/env pwsh
#
# UProjectFile.ps1
#
# See: https://github.com/XistGG/UnrealXistTools/
#
# This takes an optional $Path value and expands it to an actual
# MyGame.uproject file location.
#
# If $Path does not expand to a valid .uproject file,
# an exception is thrown.
#
# If no exception is thrown, these variables will be set:
#
# $UProjectDirectory = the absolute path to MyGame.uproject parent directory
# $UProjectFile = the absolute path to MyGame.uproject
# $UProjectFileItem = Get-Item -Path $UProjectFile
#
[CmdletBinding()]
param(
[Parameter()]$Path
)
# Make sure the powershell version is good, or throw an exception
& $PSScriptRoot/PSVersionCheck.ps1
function FindUProjectInDirectory()
{
param(
[string]$ProjectName,
[string]$Directory
)
$Result = $null
$TempProjects = Get-ChildItem -Path $Directory -File `
| Where-Object {$_.Extension -ieq '.uproject'}
if ($TempProjects.count -eq 1)
{
# Found exactly 1 .uproject file, use it
$Result = $TempProjects[0]
Write-Debug "Found 1 .uproject in $Directory, using it: $Result"
}
elseif ($TempProjects.count -gt 1)
{
# $UProjectFile is a directory with multiple .uproject files
# Search for a project file with the same name as the directory
foreach ($ProjectFile in $TempProjects)
{
if ($ProjectName -ieq $ProjectFile.BaseName)
{
# Found it (example "Foo/Foo.uproject")
$Result = $ProjectFile
Write-Debug "Compare '$ProjectName' -ieq '$($ProjectFile.BaseName)' == TRUE"
}
else
{
Write-Debug "Compare '$ProjectName' -ieq '$($ProjectFile.BaseName)' == false"
}
}
# If we still don't know which .uproject to start, error.
# User needs to tell us explicitly.
if (!$Result)
{
foreach ($ProjectFile in $TempProjects)
{
Write-Warning "Ambiguous .uproject: $ProjectFile"
}
Write-Error "Cannot auto-select a .uproject file in a directory with multiple .uproject; You must specify which .uproject to use for this directory"
throw "Explicit uproject required for directory: $Directory"
}
Write-Debug "Found 2+ .uproject in $Directory, using: $Result"
}
else # $TempProjects.count -lte 0
{
# $UProjectFile is a directory without any .uproject files
throw "Not an Unreal Engine project directory; no .uproject 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)
$UProjectFileItem = Get-Item -Path $Path 2> $null
if (!$UProjectFileItem -or !$UProjectFileItem.Exists)
{
throw "No such UProject file or directory: $Path"
}
# First check of $UProjectFile is a file
if (!$UProjectFileItem.PSIsContainer)
{
# $UProjectFileItem is a file; make sure it has a .uproject extension
if (!($UProjectFileItem.Extension -ieq '.uproject'))
{
throw "File is not a .uproject: $Path"
}
Write-Debug "UProjectFile is a .uproject file; using it: $($UProjectFileItem.FullName)"
}
else
{
# $UProjectFileItem is a directory, try to find the correct .uproject to use
$UProjectFileItem = &FindUProjectInDirectory -ProjectName $UProjectFileItem.Name -Directory $UProjectFileItem.FullName
# We expect an exception will already have been thrown when !$UProjectFileItem,
# but just to drive this point, here it is explicitly:
if (!$UProjectFileItem) { throw "UProjectItem is null" };
}
$UProjectFile = $UProjectFileItem
Write-Debug "UProjectFile=$UProjectFile"
return $UProjectFile