Skip to content

Commit 754be07

Browse files
authored
Merge pull request #75 from jwittner/dev/dscResource
DSC Resource 'xUnitySetup' for configuring your Unity installations.
2 parents a276a98 + a4c66a8 commit 754be07

File tree

8 files changed

+347
-34
lines changed

8 files changed

+347
-34
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Install-Module UnitySetup -Scope CurrentUser
2222

2323
## Using
2424

25+
### Cmdlets
2526
Find all of your Unity installs:
2627
```powershell
2728
Get-UnitySetupInstance
@@ -113,6 +114,29 @@ Find-UnitySetupInstaller -Version '2017.3.0f3' | Install-UnitySetupInstance
113114
Install-UnitySetupInstance -Installers (Find-UnitySetupInstaller -Version '2017.3.0f3')
114115
```
115116

117+
### DSC
118+
UnitySetup includes the xUnitySetup DSC Resource. An example configuration might look like:
119+
120+
```powershell
121+
<#
122+
Install multiple versions of Unity and several components
123+
#>
124+
Configuration Sample_xUnitySetup_Install {
125+
126+
Import-DscResource -ModuleName UnitySetup
127+
128+
Node 'localhost' {
129+
130+
xUnitySetup Unity {
131+
Versions = '2017.3.1f1,2018.1.0b9'
132+
Components = 'Setup', 'Mac', 'Linux', 'Metro', 'iOS'
133+
Ensure = 'Present'
134+
}
135+
}
136+
}
137+
```
138+
139+
See more by perusing the `UnitySetup\Examples` folder.
116140

117141
# Feedback
118142
To file issues or suggestions, please use the [Issues](https://github.com/Microsoft/unitysetup.powershell/issues) page for this project on GitHub.
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
2+
<#
3+
.Synopsis
4+
Returns the status of Unity installs and the set of their overlapping components.
5+
.Parameter Versions
6+
The versions of Unity to test for.
7+
#>
8+
function Get-TargetResource
9+
{
10+
[CmdletBinding()]
11+
[OutputType([System.Collections.Hashtable])]
12+
param
13+
(
14+
[parameter(Mandatory = $true)]
15+
[System.String]
16+
$Versions
17+
)
18+
Write-Verbose "Begin executing Get on $Versions"
19+
20+
[string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() }
21+
22+
$setupInstances = Get-UnitySetupInstance | Where-Object { $splitVersions -contains $_.Version }
23+
$result = @{
24+
"Versions" = $setupInstances | Select-Object -ExpandProperty Version | Sort-Object -Unique
25+
"Ensure" = if($setupInstances.Count -gt 0) { 'Present'} else { 'Absent' }
26+
}
27+
28+
Write-Verbose "Found versions: $($result['Versions'])"
29+
30+
if( $setupInstances.Count -gt 0 )
31+
{
32+
$components = $setupInstances[0].Components;
33+
for( $i = 1; $i -lt $setupInstances.Count; $i++)
34+
{
35+
$components = $components -band $setupInstances[$i].Components;
36+
}
37+
38+
$result["Components"] = $components
39+
}
40+
41+
$result
42+
43+
Write-Verbose "Found overlapping components: $($result['Components'])"
44+
Write-Verbose "End executing Get on $Versions"
45+
}
46+
47+
<#
48+
.Synopsis
49+
Installs or uninstalls the specified Versions of Unity and corresponding Components.
50+
.Parameter Versions
51+
What versions are we concered with?
52+
.Parameter Ensure
53+
Should we ensure they're there or ensure they're not?
54+
.Parameter Components
55+
What components are we concerned with?
56+
.Notes
57+
Only uninstalls whole versions of Unity. Ensuring components doesn't
58+
mean other components weren't previously installed and aren't still available.
59+
#>
60+
function Set-TargetResource
61+
{
62+
[CmdletBinding()]
63+
param
64+
(
65+
[parameter(Mandatory = $true)]
66+
[System.String]
67+
$Versions,
68+
69+
[ValidateSet("Present","Absent")]
70+
[System.String]
71+
$Ensure = 'Present',
72+
73+
[System.String[]]
74+
$Components = @('All')
75+
)
76+
77+
Write-Verbose "Begin executing Set to Ensure $Versions with $Components are $Ensure"
78+
79+
[string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() }
80+
81+
switch($Ensure) {
82+
'Present' {
83+
foreach($version in $splitVersions)
84+
{
85+
$findArgs = @{
86+
'Version' = $version
87+
'Components' = New-UnitySetupComponent -Components $Components
88+
}
89+
90+
$installArgs = @{ 'Cache' = "$env:TEMP\.unitysetup" }
91+
92+
$setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version
93+
if($setupInstances.Count -gt 0) {
94+
$findArgs["Components"] = ($findArgs.Components -band (-bnot ($setupInstances[0].Components -band $findArgs.Components)))
95+
$installArgs["Destination"] = $setupInstances[0].Path
96+
}
97+
98+
# No missing components for this version
99+
if( $findArgs.Components -eq 0 ) {
100+
Write-Verbose "All components of $version were installed"
101+
continue;
102+
}
103+
104+
Write-Verbose "Finding $($findArgs["Components"]) installers for $version"
105+
$installArgs["Installers"] = Find-UnitySetupInstaller @findArgs -WarningAction Stop
106+
if( $installArgs.Installers.Count -gt 0 ) {
107+
Write-Verbose "Starting install of $($installArgs.Installers.Count) components for $version"
108+
Install-UnitySetupInstance @installArgs
109+
Write-Verbose "Finished install of $($installArgs.Installers.Count) components for $version"
110+
}
111+
}
112+
}
113+
'Absent' {
114+
$setupInstances = Get-UnitySetupInstance | Where-Object { $splitVersions -contains $_.Version }
115+
Write-Verbose "Found $($setupInstances.Count) instance(s) of $splitVersions"
116+
117+
if( $setupInstances.Count -gt 0 ) {
118+
Write-Verbose "Starting uninstall of $($setupInstances.Count) versions of Unity"
119+
Uninstall-UnitySetupInstance -Instances $setupInstances
120+
Write-Verbose "Finished uninstall of $($setupInstances.Count) versions of Unity"
121+
}
122+
}
123+
}
124+
125+
Write-Verbose "End executing Set to Ensure $Versions with $Components are $Ensure"
126+
}
127+
128+
<#
129+
.Synopsis
130+
Test if the Unity installs are in the desired state.
131+
.Parameter Versions
132+
What versions are we concered with?
133+
.Parameter Ensure
134+
Should we ensure they're there or ensure they're not?
135+
.Parameter Components
136+
What components are we concerned with?
137+
.Notes
138+
This test is not strict. Versions and Components not described are not considered.
139+
#>
140+
function Test-TargetResource
141+
{
142+
[CmdletBinding()]
143+
[OutputType([System.Boolean])]
144+
param
145+
(
146+
[parameter(Mandatory = $true)]
147+
[System.String]
148+
$Versions,
149+
150+
[ValidateSet("Present","Absent")]
151+
[System.String]
152+
$Ensure = 'Present',
153+
154+
[System.String[]]
155+
$Components = @('All')
156+
)
157+
158+
Write-Verbose "Begin executing Test to verify $Versions with $Components are $Ensure"
159+
160+
[string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() }
161+
162+
$result = $true
163+
switch( $Ensure )
164+
{
165+
'Present' {
166+
$setupComponents = New-UnitySetupComponent -Components $Components
167+
foreach($version in $splitVersions)
168+
{
169+
Write-Verbose "Starting test for $version"
170+
$setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version
171+
Write-Verbose "Found $($setupInstances.Count) instance(s) of $version"
172+
173+
if($setupInstances.Count -eq 0) {
174+
Write-Verbose "Found $version missing."
175+
$result = $false
176+
break
177+
}
178+
179+
$availableComponents = ($setupInstances[0].Components -band $setupComponents)
180+
if($availableComponents -ne $setupComponents){
181+
$missingComponents = New-UnitySetupComponent ($setupComponents -bxor $availableComponents)
182+
Write-Verbose "Found $version missing $($missingComponents)"
183+
$result = $false
184+
break
185+
}
186+
}
187+
}
188+
'Absent' {
189+
foreach($version in $splitVersions)
190+
{
191+
$setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version
192+
Write-Verbose "Found $($setupInstances.Count) instance(s) of $version"
193+
194+
if($setupInstances.Count -gt 0) {
195+
Write-Verbose "Found $version installed."
196+
$result = $false
197+
break
198+
}
199+
}
200+
}
201+
}
202+
203+
$result
204+
205+
Write-Verbose "End executing Test to verify $Versions with $Components are $Ensure"
206+
}
207+
208+
209+
Export-ModuleMember -Function *-TargetResource
210+
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<#
2+
Create a custom configuration by passing in necessary values
3+
#>
4+
Configuration Sample_xUnitySetup {
5+
param
6+
(
7+
[System.String]
8+
$Versions = '2017.3.0f3,2018.1.0b9',
9+
10+
[ValidateSet('Present', 'Absent')]
11+
[System.String]
12+
$Ensure = 'Present',
13+
14+
[System.String[]]
15+
$Components = @('Setup','Linux','Metro','Mac','iOS')
16+
)
17+
18+
Import-DscResource -ModuleName UnitySetup
19+
20+
Node 'localhost' {
21+
22+
xUnitySetup Unity {
23+
Versions = $Versions
24+
Components = $Components
25+
Ensure = $Ensure
26+
}
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<#
2+
Install multiple versions of Unity and several components
3+
#>
4+
Configuration Sample_xUnitySetup_Install {
5+
6+
Import-DscResource -ModuleName UnitySetup
7+
8+
Node 'localhost' {
9+
10+
xUnitySetup Unity {
11+
Versions = '2017.3.1f1,2018.1.0b9'
12+
Components = 'Setup', 'Mac', 'Linux', 'Metro', 'iOS'
13+
Ensure = 'Present'
14+
}
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<#
2+
Uninstall multiple versions of Unity
3+
#>
4+
Configuration Sample_xUnitySetup_Install {
5+
6+
Import-DscResource -ModuleName UnitySetup
7+
8+
Node 'localhost' {
9+
10+
xUnitySetup Unity {
11+
Versions = '2017.3.1f1,2018.1.0b9'
12+
Ensure = 'Absent'
13+
}
14+
}
15+
}

UnitySetup/UnitySetup.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ FunctionsToExport = @(
8181
'Install-UnitySetupInstance',
8282
'Select-UnitySetupInstance',
8383
'Uninstall-UnitySetupInstance',
84-
'Start-UnityEditor'
84+
'Start-UnityEditor',
85+
'New-UnitySetupComponent'
8586
)
8687

8788
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.

0 commit comments

Comments
 (0)