Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Install-Module UnitySetup -Scope CurrentUser

## Using

### Cmdlets
Find all of your Unity installs:
```powershell
Get-UnitySetupInstance
Expand Down Expand Up @@ -113,6 +114,29 @@ Find-UnitySetupInstaller -Version '2017.3.0f3' | Install-UnitySetupInstance
Install-UnitySetupInstance -Installers (Find-UnitySetupInstaller -Version '2017.3.0f3')
```

### DSC
UnitySetup includes the xUnitySetup DSC Resource. An example configuration might look like:

```powershell
<#
Install multiple versions of Unity and several components
#>
Configuration Sample_xUnitySetup_Install {

Import-DscResource -ModuleName UnitySetup

Node 'localhost' {

xUnitySetup Unity {
Versions = '2017.3.1f1,2018.1.0b9'
Components = 'Setup', 'Mac', 'Linux', 'Metro', 'iOS'
Ensure = 'Present'
}
}
}
```

See more by perusing the `UnitySetup\Examples` folder.

# Feedback
To file issues or suggestions, please use the [Issues](https://github.com/Microsoft/unitysetup.powershell/issues) page for this project on GitHub.
Expand Down
210 changes: 210 additions & 0 deletions UnitySetup/DSCResources/xUnitySetup/xUnitySetup.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@

<#
.Synopsis
Returns the status of Unity installs and the set of their overlapping components.
.Parameter Versions
The versions of Unity to test for.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Versions
)
Write-Verbose "Begin executing Get on $Versions"

[string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() }

$setupInstances = Get-UnitySetupInstance | Where-Object { $splitVersions -contains $_.Version }
$result = @{
"Versions" = $setupInstances | Select-Object -ExpandProperty Version | Sort-Object -Unique
"Ensure" = if($setupInstances.Count -gt 0) { 'Present'} else { 'Absent' }
}

Write-Verbose "Found versions: $($result['Versions'])"

if( $setupInstances.Count -gt 0 )
{
$components = $setupInstances[0].Components;
for( $i = 1; $i -lt $setupInstances.Count; $i++)
{
$components = $components -band $setupInstances[$i].Components;
}

$result["Components"] = $components
}

$result

Write-Verbose "Found overlapping components: $($result['Components'])"
Write-Verbose "End executing Get on $Versions"
}

<#
.Synopsis
Installs or uninstalls the specified Versions of Unity and corresponding Components.
.Parameter Versions
What versions are we concered with?
.Parameter Ensure
Should we ensure they're there or ensure they're not?
.Parameter Components
What components are we concerned with?
.Notes
Only uninstalls whole versions of Unity. Ensuring components doesn't
mean other components weren't previously installed and aren't still available.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Versions,

[ValidateSet("Present","Absent")]
[System.String]
$Ensure = 'Present',

[System.String[]]
$Components = @('All')
)

Write-Verbose "Begin executing Set to Ensure $Versions with $Components are $Ensure"

[string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() }

switch($Ensure) {
'Present' {
foreach($version in $splitVersions)
{
$findArgs = @{
'Version' = $version
'Components' = New-UnitySetupComponent -Components $Components
}

$installArgs = @{ 'Cache' = "$env:TEMP\.unitysetup" }

$setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version
if($setupInstances.Count -gt 0) {
$findArgs["Components"] = ($findArgs.Components -band (-bnot ($setupInstances[0].Components -band $findArgs.Components)))
$installArgs["Destination"] = $setupInstances[0].Path
}

# No missing components for this version
if( $findArgs.Components -eq 0 ) {
Write-Verbose "All components of $version were installed"
continue;
}

Write-Verbose "Finding $($findArgs["Components"]) installers for $version"
$installArgs["Installers"] = Find-UnitySetupInstaller @findArgs -WarningAction Stop
if( $installArgs.Installers.Count -gt 0 ) {
Write-Verbose "Starting install of $($installArgs.Installers.Count) components for $version"
Install-UnitySetupInstance @installArgs
Write-Verbose "Finished install of $($installArgs.Installers.Count) components for $version"
}
}
}
'Absent' {
$setupInstances = Get-UnitySetupInstance | Where-Object { $splitVersions -contains $_.Version }
Write-Verbose "Found $($setupInstances.Count) instance(s) of $splitVersions"

if( $setupInstances.Count -gt 0 ) {
Write-Verbose "Starting uninstall of $($setupInstances.Count) versions of Unity"
Uninstall-UnitySetupInstance -Instances $setupInstances
Write-Verbose "Finished uninstall of $($setupInstances.Count) versions of Unity"
}
}
}

Write-Verbose "End executing Set to Ensure $Versions with $Components are $Ensure"
}

<#
.Synopsis
Test if the Unity installs are in the desired state.
.Parameter Versions
What versions are we concered with?
.Parameter Ensure
Should we ensure they're there or ensure they're not?
.Parameter Components
What components are we concerned with?
.Notes
This test is not strict. Versions and Components not described are not considered.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Versions,

[ValidateSet("Present","Absent")]
[System.String]
$Ensure = 'Present',

[System.String[]]
$Components = @('All')
)

Write-Verbose "Begin executing Test to verify $Versions with $Components are $Ensure"

[string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() }

$result = $true
switch( $Ensure )
{
'Present' {
$setupComponents = New-UnitySetupComponent -Components $Components
foreach($version in $splitVersions)
{
Write-Verbose "Starting test for $version"
$setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version
Write-Verbose "Found $($setupInstances.Count) instance(s) of $version"

if($setupInstances.Count -eq 0) {
Write-Verbose "Found $version missing."
$result = $false
break
}

$availableComponents = ($setupInstances[0].Components -band $setupComponents)
if($availableComponents -ne $setupComponents){
$missingComponents = New-UnitySetupComponent ($setupComponents -bxor $availableComponents)
Write-Verbose "Found $version missing $($missingComponents)"
$result = $false
break
}
}
}
'Absent' {
foreach($version in $splitVersions)
{
$setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version
Write-Verbose "Found $($setupInstances.Count) instance(s) of $version"

if($setupInstances.Count -gt 0) {
Write-Verbose "Found $version installed."
$result = $false
break
}
}
}
}

$result

Write-Verbose "End executing Test to verify $Versions with $Components are $Ensure"
}


Export-ModuleMember -Function *-TargetResource

Binary file not shown.
28 changes: 28 additions & 0 deletions UnitySetup/Examples/Sample_xUnitySetup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<#
Create a custom configuration by passing in necessary values
#>
Configuration Sample_xUnitySetup {
param
(
[System.String]
$Versions = '2017.3.0f3,2018.1.0b9',

[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',

[System.String[]]
$Components = @('Setup','Linux','Metro','Mac','iOS')
)

Import-DscResource -ModuleName UnitySetup

Node 'localhost' {

xUnitySetup Unity {
Versions = $Versions
Components = $Components
Ensure = $Ensure
}
}
}
16 changes: 16 additions & 0 deletions UnitySetup/Examples/Sample_xUnitySetup_Install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<#
Install multiple versions of Unity and several components
#>
Configuration Sample_xUnitySetup_Install {

Import-DscResource -ModuleName UnitySetup

Node 'localhost' {

xUnitySetup Unity {
Versions = '2017.3.1f1,2018.1.0b9'
Components = 'Setup', 'Mac', 'Linux', 'Metro', 'iOS'
Ensure = 'Present'
}
}
}
15 changes: 15 additions & 0 deletions UnitySetup/Examples/Sample_xUnitySetup_Uninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<#
Uninstall multiple versions of Unity
#>
Configuration Sample_xUnitySetup_Install {

Import-DscResource -ModuleName UnitySetup

Node 'localhost' {

xUnitySetup Unity {
Versions = '2017.3.1f1,2018.1.0b9'
Ensure = 'Absent'
}
}
}
3 changes: 2 additions & 1 deletion UnitySetup/UnitySetup.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ FunctionsToExport = @(
'Install-UnitySetupInstance',
'Select-UnitySetupInstance',
'Uninstall-UnitySetupInstance',
'Start-UnityEditor'
'Start-UnityEditor',
'New-UnitySetupComponent'
)

# 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.
Expand Down
Loading