Skip to content

Commit ac24eb3

Browse files
committed
Add new cmdlet to test/report on meta file integrity
1 parent 13245dc commit ac24eb3

File tree

3 files changed

+154
-2
lines changed

3 files changed

+154
-2
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@ Get-UnitySetupInstance
4242
# 2017.2.0f3 Windows, UWP, UWP_IL2CPP, Vuforia C:\Program Files\Unity.2017.2.0f3\
4343
# 2017.3.0f3 Windows, UWP, UWP_IL2CPP, Mac, Vuforia C:\Program Files\Unity.2017.3.0f3\
4444
```
45-
4645
Select the Unity installs that you want:
4746
```powershell
4847
Get-UnitySetupInstance | Select-UnitySetupInstance -Latest
4948
Get-UnitySetupInstance | Select-UnitySetupInstance -Version '2017.1.1f1'
5049
Get-UnitySetupInstance | Select-UnitySetupInstance -Project '.\MyUnityProject'
5150
```
52-
5351
Find all the Unity projects recursively:
5452
```powershell
5553
Get-UnityProjectInstance -Recurse
@@ -77,6 +75,28 @@ Invoke methods with arbitrary arguments:
7775
```powershell
7876
Start-UnityEditor -ExecuteMethod Build.Invoke -BatchMode -Quit -LogFile .\build.log -Wait -AdditionalArguments "-BuildArg1 -BuildArg2"
7977
```
78+
Test the meta file integrity of Unity Projects:
79+
```powershell
80+
Test-UnityProjectInstanceMetaFileIntegrity # Test project in current folder
81+
Test-UnityProjectInstanceMetaFileIntegrity .\MyUnityProject
82+
Test-UnityProjectInstanceMetaFileIntegrity -Project .\MyUnityProject
83+
Get-UnityProjectInstance -Recurse | Test-UnityProjectInstanceMetaFileIntegrity
84+
85+
# Example output:
86+
# True
87+
```
88+
Get meta file integrity issues for Unity Projects:
89+
```powershell
90+
Test-UnityProjectInstanceMetaFileIntegrity .\MyUnityProject -PassThru
91+
92+
# Example output:
93+
# Item Issue
94+
# ---- -----
95+
# C:\MyUnityProject\Assets\SomeFolder Directory is missing associated meta file.
96+
# C:\MyUnityProject\Assets\SomeFolder\SomeShader.shader File is missing associated meta file.
97+
# C:\MyUnityProject\Assets\SomeFolder\SomeOtherShader.shader.meta Meta file is missing associated item.
98+
```
99+
80100
Find the installers for a particular version:
81101
```powershell
82102
Find-UnitySetupInstaller -Version '2017.3.0f3' | Format-Table

UnitySetup/UnitySetup.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
'Select-UnitySetupInstaller',
8080
'Test-UnitySetupInstance',
8181
'Get-UnityProjectInstance',
82+
'Test-UnityProjectInstanceMetaFileIntegrity',
8283
'Get-UnitySetupInstance',
8384
'Request-UnitySetupInstaller',
8485
'Install-UnitySetupInstance',

UnitySetup/UnitySetup.psm1

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,137 @@ function Get-UnityProjectInstance {
13321332
}
13331333
}
13341334

1335+
<#
1336+
.Synopsis
1337+
Tests the meta file integrity of the Unity Project Instance(s).
1338+
.DESCRIPTION
1339+
Tests if every item under assets has an associated .meta file and every .meta file an associated item.
1340+
.PARAMETER Project
1341+
Unity Project Instance(s) to test the meta file integrity of.
1342+
.PARAMETER PassThru
1343+
Output the meta file integrity issues rather than $true (no issues) or $false (at least one issue).
1344+
.EXAMPLE
1345+
Test-UnityProjectInstanceMetaFileIntegrity
1346+
.EXAMPLE
1347+
Test-UnityProjectInstanceMetaFileIntegrity -PassThru
1348+
.EXAMPLE
1349+
Test-UnityProjectInstanceMetaFileIntegrity .\MyUnityProject
1350+
.EXAMPLE
1351+
Test-UnityProjectInstanceMetaFileIntegrity -Project .\MyUnityProject
1352+
.EXAMPLE
1353+
Get-UnityProjectInstance -Recurse | Test-UnityProjectInstanceMetaFileIntegrity
1354+
.EXAMPLE
1355+
Get-UnityProjectInstance -Recurse | Test-UnityProjectInstanceMetaFileIntegrity -PassThru
1356+
#>
1357+
function Test-UnityProjectInstanceMetaFileIntegrity {
1358+
[CmdletBinding(DefaultParameterSetName = "Context")]
1359+
param(
1360+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = "Projects")]
1361+
[ValidateNotNullOrEmpty()]
1362+
[UnityProjectInstance[]] $Project,
1363+
[switch] $PassThru
1364+
)
1365+
1366+
process {
1367+
1368+
switch ( $PSCmdlet.ParameterSetName) {
1369+
'Context' {
1370+
$currentFolderProject = Get-UnityProjectInstance $PWD.Path
1371+
if ($null -ne $currentFolderProject) {
1372+
$Project = @($currentFolderProject)
1373+
}
1374+
}
1375+
}
1376+
1377+
foreach ( $p in $Project) {
1378+
1379+
$testResult = $true
1380+
1381+
Write-Verbose "Getting meta file integrity for project at $($p.path)"
1382+
$assetDir = Join-Path $p.Path "Assets"
1383+
1384+
# get all the directories under assets
1385+
[System.IO.DirectoryInfo[]]$dirs = Get-ChildItem -Path "$assetDir/*" -Recurse -Directory -Exclude '.*'
1386+
1387+
Write-Verbose "Testing asset directories for missing meta files..."
1388+
foreach ($dir in $dirs) {
1389+
$testPath = "$($dir.FullName).meta";
1390+
if (Test-Path -PathType Leaf -Path $testPath) { continue }
1391+
1392+
if ($PassThru) {
1393+
[PSCustomObject]@{
1394+
'Item' = $dir
1395+
'Issue' = "Directory is missing associated meta file."
1396+
}
1397+
}
1398+
else {
1399+
$testResult = $false;
1400+
break;
1401+
}
1402+
}
1403+
1404+
if (-not $testResult) { $false; continue; }
1405+
1406+
# get all the non-meta files under assets
1407+
[System.IO.FileInfo[]]$files = Get-ChildItem -Path "$assetDir/*" -Exclude '.*', '*.meta' -File
1408+
foreach ($dir in $dirs) {
1409+
$files += Get-ChildItem -Path "$($dir.FullName)/*" -Exclude '.*', '*.meta' -File
1410+
}
1411+
1412+
Write-Verbose "Testing asset files for missing meta files..."
1413+
foreach ( $file in $files ) {
1414+
$testPath = "$($file.FullName).meta";
1415+
if (Test-Path -PathType Leaf -Path $testPath) { continue }
1416+
1417+
if ($PassThru) {
1418+
[PSCustomObject]@{
1419+
'Item' = $file
1420+
'Issue' = "File is missing associated meta file."
1421+
}
1422+
}
1423+
else {
1424+
$testResult = $false;
1425+
break;
1426+
}
1427+
}
1428+
1429+
if (-not $testResult) { $false; continue; }
1430+
1431+
$metaFileSearchArgs = @{
1432+
'Exclude' = '.*'
1433+
'Include' = '*.meta'
1434+
'File' = $true
1435+
'Force' = $true # Ensure we include hidden meta files
1436+
}
1437+
1438+
# get all the meta files under assets
1439+
[System.IO.FileInfo[]]$metaFiles = Get-ChildItem -Path "$assetDir/*" @metaFileSearchArgs
1440+
foreach ($dir in $dirs) {
1441+
$metaFiles += Get-ChildItem -Path "$($dir.FullName)/*" @metaFileSearchArgs
1442+
}
1443+
1444+
Write-Verbose "Testing meta files for missing assets..."
1445+
foreach ($metaFile in $metaFiles) {
1446+
$testPath = $metaFile.FullName.SubString(0, $metaFile.FullName.Length - $metaFile.Extension.Length);
1447+
if (Test-Path -Path $testPath) { continue }
1448+
1449+
if ($PassThru) {
1450+
[PSCustomObject]@{
1451+
'Item' = $metaFile
1452+
'Issue' = "Meta file is missing associated item."
1453+
}
1454+
}
1455+
else {
1456+
$testResult = $false;
1457+
break;
1458+
}
1459+
}
1460+
1461+
if (-not $PassThru) { $testResult; }
1462+
}
1463+
}
1464+
}
1465+
13351466
<#
13361467
.Synopsis
13371468
Starts the Unity Editor

0 commit comments

Comments
 (0)