Create a Package Dependency
A package can depend on other packages, meaning, installing a package will also install packages it depends on, if not already installed.
In this section, we will extend the previous how-to by adding a new package, app-dependency which a new version of example-app 2.0 will depend on.
Now, in your Example App Package directory, create a new directory called app-dependency, this directory will contain files to be included in our new package:
Example App Package │ CreatePackages.ps1 │ └───example-app │ │ Package.psm1 │ │ Example App.exe │ └───app-dependency │ │ Package.psm1
In the new directory, create a file called Package.psm1 with the following content:
$ErrorActionPreference = 'stop' function Install-Package($Context) { AddContent $Context "Installing" } function Update-Package($Context) { AddContent $Context "Updating" } function AddContent($Context, $Keyword) { $Dir = 'c:\temp' $LogFile = Join-Path $Dir "$($Context.Package.Id).txt" [System.IO.Directory]::CreateDirectory($Dir) | Out-Null "$(Get-Date) $Keyword package $($Context.Package.id) $($Context.Package.Version)" | Add-Content -Path $LogFile }
Notice how this script does not install anything, but instead only creates a log file and writes to it during install or update phase. This could easily be installing DotNet framework or a SQL server, which our example-app might depend on.
Next we extend our script CreatePackages.ps1 from previous how-to by adding a definition for our new package:
$ErrorActionPreference = 'stop' Import-Module UpdateServiceServer $AppDependency = @{ Id = 'app-dependency' Name = 'App Dependency' Version = '1.0' InputPath = @( Join-Path $PSScriptRoot 'app-dependency\*' # Add all files to package from directory. ) OutputDir = (Join-Path $PSScriptRoot 'Output') Commands = @{ Install = 'Package.psm1:Install-Package' Update = 'Package.psm1:Update-Package' } } New-UssPackage @AppDependency -Force | Import-UssPackage $ExampleApp = @{ Id = 'example-app' Name = 'Example App' Version = '2.0' InputPath = @( Join-Path $PSScriptRoot 'example-app\*' # Add all files to package from directory. ) Dependencies = @( @{ Id = 'app-dependency'; Version = $AppDependency.Version } ) OutputDir = (Join-Path $PSScriptRoot 'Output') Commands = @{ Install = 'Package.psm1:Install-Package' Update = 'Package.psm1:Install-Package' } } New-UssPackage @ExampleApp -Force | Import-UssPackage $InstallerGuid = "462DB9A7-06F0-4034-8CEB-EBA6A1325948" if (!(Test-UssInstaller -Guid $InstallerGuid)) { New-UssInstaller -Name "My Installer" -Guid $InstallerGuid Set-UssInstallerPackage -Guid $InstallerGuid -PackageId "example-app" Get-UssInstaller -Guid $InstallerGuid -OutputDir $PSScriptRoot }
First, at top of the script, the
$AppDependency
variable defines the package app-package of version 1.0. This package will contain all files in the app-package directory - which should just be Package.psm1. The first time the package is installed, it will executeInstall-Package
in Package.psm1 and if updating it will executeUpdate-Package
. Then the package is created and imported to the GoC server as in prior how-to.Next, we have updated the example-app package definition in
$ExampleApp
, now it will create package of version 2.0 and additionally, this package now has a dependency to app-dependency, defined with:Dependencies = @( @{ Id = 'app-dependency'; Version = $AppDependency.Version } )
Notice how we depend on app-package version defined in
$AppDependency
variable - this can be handy if we always create both of the packages at the same time, but that might not always be the case.We could also depend on range of versions, for example depend on the latest 1.x version:
Dependencies = @( @{ Id = 'app-dependency'; Version = '^ <=1.0 <2.0' } )
'^' means we want Update Service to pick the latest version of app-dependency somewhere between 1.0 <= X < 2.0. This notation is called package query and you can read more about it here.
Before we run the script, replace the Example App.exe in the example-app directory with the new 2.0 version from here.
Run CreatePackages.ps1 script to create the two packages and import them into the server.
If you already have example-app v1.0 installed you can now open the Update Service client on that machine and check for updates and install example-app v2.0. You can find the Update Service client through the Windows Start Menu:
Now you can both check and see if the Example App has updated to 2.0 in add remove programs and see if our new package app-dependency created the file at c:\temp\app-dependency.txt.