Anders G. Nordby

Senior Web Developer at CGI

PowerShell to the Rescue

Regarding today’s earlier post A Rant about AppData, Paths, and Modules, I’ve hacked together a solution that seems to work…

First of all, please note that in our solution, we’ve taken all the various config files and combined them into a single Web.config file, which in our view makes searching for config settings that much easier (you don’t need to remember what file to look in), and also makes XML Transforms work (this is one way to do it, but there are other ways to achieve that).

I’ve created a new folder inside my solution called _AppDataModuleRepository, and after installing a new add-on, I take all the contents from the AppData\ModulesRepository (which is located outside my solution) and copy the contents to the _AppDataModuleRepository folder. I make sure that everything here is included in my check-in, as well as the two other places that the new stuff appears, namely in the modulesbin folder and in the modules/_protected folder.

Now, for the PowerShell stuff… I’ve created a couple of scripts:

The moveAppData.ps1 script, which moves stuff to the outside AppData folder:

# Moves _AppDataModuleRepository to correct location [AppData]\ModuleRepository

[xml]$webconfig = Get-Content -Path ".\web.config"	# NOTE: Our solution has all info in a single .config file
													# In a default EPiServer solution, the appData basePath is 
													# found in the EPiServerFramework.config file.

$appdatapath = $webconfig.configuration.'episerver.framework'.appData.basePath

$current = Get-Location
Set-Location $appdatapath
New-Item -ItemType Directory -Force -Path "$pwd\ModulesRepository"
Set-Location .\ModulesRepository
$loc = Get-Location
Set-Location $current

copy-item -Path "$Pwd\_AppDataModuleRepository\*" -Destination "$loc\ModuleRepository"
remove-item "$Pwd\_AppDataModuleRepository\" -Recurse

The updateRepositoryConfigPath.ps1 script, which sets the backlink to the current site’s directory:

$currentpath = Get-Location
$newpath = "$currentpath\modules\_protected\packages.config"

[xml]$webconfig = Get-Content -Path ".\web.config"	# NOTE: Our solution has all info in a single .config file
													# In a default EPiServer solution, the appData basePath is 
													# found in the EPiServerFramework.config file.

$appdatapath = $webconfig.configuration.'episerver.framework'.appData.basePath
$repoconfigpath = "$appdatapath\ModulesRepository\repositories.config"
[xml]$repoconfig = Get-Content -Path $repoconfigpath
$repoconfig.repositories.repository.path = $newpath
$current = Get-Location
Set-Location $appdatapath
Set-Location .\ModulesRepository
$loc = Get-Location
Set-Location $current
$repo = "$loc\repositories.config"
$repoconfig.Save($repo)

…and finally I’ve called these from my PostDeploy.ps1 script:

.\moveAppData.ps1
.\updateRepositoryConfigPath.ps1

remove-item "$Pwd\*.ps1"

With all this in place, things sort of work – at least the site does not crash with the yellow screen of death complaining about a missing add-on dll (which it would otherwise do). However, for some strange reason, the *.nupkg and *.nuspec files are not included in the TeamCity artifacts, and are therefore never moved over to the deploy site. Perhaps this is why the Add-on store on the deploy site does not show anything under “Installed Add-ons”?

Follow

Get every new post delivered to your Inbox.