Skip to content

Commit f82e085

Browse files
authored
Merge pull request #197 from jwittner/dev/testMetaFileIntegrity
Add new cmdlet to test/report on meta file integrity
2 parents 13245dc + 004eb07 commit f82e085

File tree

3 files changed

+227
-2
lines changed

3 files changed

+227
-2
lines changed

README.md

Lines changed: 23 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,29 @@ 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+
# C:\MyUnityProject\Assets\SomeFolder\SomeNewShader.shader.meta Meta file guid collision with C:\MyUnityProject\Assets\SomeFolder\SomeOtherShader.shader.meta
99+
```
100+
80101
Find the installers for a particular version:
81102
```powershell
82103
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: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,209 @@ 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
1340+
and every .meta file an associated item
1341+
and that none of the meta file guids collide.
1342+
.PARAMETER Project
1343+
Unity Project Instance(s) to test the meta file integrity of.
1344+
.PARAMETER PassThru
1345+
Output the meta file integrity issues rather than $true (no issues) or $false (at least one issue).
1346+
.EXAMPLE
1347+
Test-UnityProjectInstanceMetaFileIntegrity
1348+
.EXAMPLE
1349+
Test-UnityProjectInstanceMetaFileIntegrity -PassThru
1350+
.EXAMPLE
1351+
Test-UnityProjectInstanceMetaFileIntegrity .\MyUnityProject
1352+
.EXAMPLE
1353+
Test-UnityProjectInstanceMetaFileIntegrity -Project .\MyUnityProject
1354+
.EXAMPLE
1355+
Get-UnityProjectInstance -Recurse | Test-UnityProjectInstanceMetaFileIntegrity
1356+
.EXAMPLE
1357+
Get-UnityProjectInstance -Recurse | Test-UnityProjectInstanceMetaFileIntegrity -PassThru
1358+
#>
1359+
function Test-UnityProjectInstanceMetaFileIntegrity {
1360+
[CmdletBinding(DefaultParameterSetName = "Context")]
1361+
param(
1362+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = "Projects")]
1363+
[ValidateNotNullOrEmpty()]
1364+
[UnityProjectInstance[]] $Project,
1365+
[switch] $PassThru
1366+
)
1367+
1368+
process {
1369+
1370+
switch ( $PSCmdlet.ParameterSetName) {
1371+
'Context' {
1372+
$currentFolderProject = Get-UnityProjectInstance $PWD.Path
1373+
if ($null -ne $currentFolderProject) {
1374+
$Project = @($currentFolderProject)
1375+
}
1376+
}
1377+
}
1378+
1379+
foreach ( $p in $Project) {
1380+
1381+
$testResult = $true
1382+
1383+
Write-Verbose "Getting meta file integrity for project at $($p.path)"
1384+
$assetDir = Join-Path $p.Path "Assets"
1385+
1386+
# get all the directories under assets
1387+
[System.IO.DirectoryInfo[]]$dirs = Get-ChildItem -Path "$assetDir/*" -Recurse -Directory -Exclude '.*'
1388+
1389+
Write-Verbose "Testing asset directories for missing meta files..."
1390+
[float]$progressCounter = 0
1391+
foreach ($dir in $dirs) {
1392+
1393+
$progress = @{
1394+
'Activity' = "Testing directories for missing meta files"
1395+
'Status' = $dir
1396+
'PercentComplete' = (((++$progressCounter) / $dirs.Length) * 100)
1397+
}
1398+
Write-Progress @progress
1399+
1400+
$testPath = "$($dir.FullName).meta";
1401+
if (Test-Path -PathType Leaf -Path $testPath) { continue }
1402+
1403+
if ($PassThru) {
1404+
[PSCustomObject]@{
1405+
'Item' = $dir
1406+
'Issue' = "Directory is missing associated meta file."
1407+
}
1408+
}
1409+
else {
1410+
$testResult = $false;
1411+
break;
1412+
}
1413+
}
1414+
1415+
if (-not $testResult) { $false; continue; }
1416+
1417+
# get all the non-meta files under assets
1418+
[System.IO.FileInfo[]]$files = Get-ChildItem -Path "$assetDir/*" -Exclude '.*', '*.meta' -File
1419+
foreach ($dir in $dirs) {
1420+
$files += Get-ChildItem -Path "$($dir.FullName)/*" -Exclude '.*', '*.meta' -File
1421+
}
1422+
1423+
Write-Verbose "Testing asset files for missing meta files..."
1424+
$progressCounter = 0
1425+
foreach ( $file in $files ) {
1426+
1427+
$progress = @{
1428+
'Activity' = "Testing files for missing meta files"
1429+
'Status' = $file
1430+
'PercentComplete' = (((++$progressCounter) / $files.Length) * 100)
1431+
}
1432+
Write-Progress @progress
1433+
1434+
$testPath = "$($file.FullName).meta";
1435+
if (Test-Path -PathType Leaf -Path $testPath) { continue }
1436+
1437+
if ($PassThru) {
1438+
[PSCustomObject]@{
1439+
'Item' = $file
1440+
'Issue' = "File is missing associated meta file."
1441+
}
1442+
}
1443+
else {
1444+
$testResult = $false;
1445+
break;
1446+
}
1447+
}
1448+
1449+
if (-not $testResult) { $false; continue; }
1450+
1451+
$metaFileSearchArgs = @{
1452+
'Exclude' = '.*'
1453+
'Include' = '*.meta'
1454+
'File' = $true
1455+
'Force' = $true # Ensure we include hidden meta files
1456+
}
1457+
1458+
# get all the meta files under assets
1459+
[System.IO.FileInfo[]]$metaFiles = Get-ChildItem -Path "$assetDir/*" @metaFileSearchArgs
1460+
foreach ($dir in $dirs) {
1461+
$metaFiles += Get-ChildItem -Path "$($dir.FullName)/*" @metaFileSearchArgs
1462+
}
1463+
1464+
Write-Verbose "Testing meta files for missing assets..."
1465+
$progressCounter = 0
1466+
foreach ($metaFile in $metaFiles) {
1467+
1468+
$progress = @{
1469+
'Activity' = "Testing meta files for missing assets"
1470+
'Status' = $metaFile
1471+
'PercentComplete' = (((++$progressCounter) / $metaFiles.Length) * 100)
1472+
}
1473+
Write-Progress @progress
1474+
1475+
$testPath = $metaFile.FullName.SubString(0, $metaFile.FullName.Length - $metaFile.Extension.Length);
1476+
if (Test-Path -Path $testPath) { continue }
1477+
1478+
if ($PassThru) {
1479+
[PSCustomObject]@{
1480+
'Item' = $metaFile
1481+
'Issue' = "Meta file is missing associated item."
1482+
}
1483+
}
1484+
else {
1485+
$testResult = $false;
1486+
break;
1487+
}
1488+
}
1489+
1490+
if (-not $testResult) { $false; continue; }
1491+
1492+
Write-Verbose "Testing meta files for guid collisions..."
1493+
$metaGuids = @{ }
1494+
$progressCounter = 0
1495+
foreach ($metaFile in $metaFiles) {
1496+
1497+
$progress = @{
1498+
'Activity' = "Testing meta files for guid collisions"
1499+
'Status' = $metaFile
1500+
'PercentComplete' = (((++$progressCounter) / $metaFiles.Length) * 100)
1501+
}
1502+
Write-Progress @progress
1503+
1504+
try {
1505+
$guidResult = Get-Content $metaFile.FullName | Select-String -Pattern '^guid:\s*([a-z,A-Z,\d]+)\s*$'
1506+
if ($guidResult.Matches.Groups.Length -lt 2) {
1507+
Write-Warning "Could not find guid in meta file - $metaFile"
1508+
continue;
1509+
}
1510+
1511+
$guid = $guidResult.Matches.Groups[1].Value
1512+
if ($null -eq $metaGuids[$guid]) {
1513+
$metaGuids[$guid] = $metaFile;
1514+
continue
1515+
}
1516+
1517+
if ($PassThru) {
1518+
[PSCustomObject]@{
1519+
'Item' = $metaFile
1520+
'Issue' = "Meta file guid collision with $($metaGuids[$guid])"
1521+
}
1522+
}
1523+
else {
1524+
$testResult = $false;
1525+
break;
1526+
}
1527+
}
1528+
catch {
1529+
Write-Error "Exception testing guid of $metaFile - $_"
1530+
}
1531+
}
1532+
1533+
if (-not $PassThru) { $testResult; }
1534+
}
1535+
}
1536+
}
1537+
13351538
<#
13361539
.Synopsis
13371540
Starts the Unity Editor

0 commit comments

Comments
 (0)