Skip to content

Instantly share code, notes, and snippets.

@kimcuhoang
Last active November 22, 2018 19:48
Show Gist options
  • Save kimcuhoang/8b7d5a630a76f5b4548192cca154ee1f to your computer and use it in GitHub Desktop.
Save kimcuhoang/8b7d5a630a76f5b4548192cca154ee1f to your computer and use it in GitHub Desktop.
Use this script for starting a new Sitecore.Commerce.Project without Plugin Example
Param(
[string]$siteName = "DemoHome",
[string]$siteSuffix = $siteName.ToLower(),
[string]$siteHostName = "$($siteSuffix).dev.local",
[string]$engineHostName = "localhost",
[string]$identityServerHost = "$($engineHostName):5050",
[string]$DefaultEnvironment = "$($siteName)Authoring",
[string]$DefaultShopName = "Demo Home",
[string]$webRoot = "E:\Inetpub\wwwroot",
[string]$BizFxPathName = "SitecoreBizFx_$($siteSuffix)",
[string]$IdentityServerPathName = "SitecoreIdentityServer_$($siteSuffix)",
[string]$CommerceOpsPort = "5000",
[string]$adminUser = "admin",
[string]$adminPassword = "b",
[string]$certificateName = "$($siteHostName).xConnect.Client",
[string]$solrUrl = "https://localhost:8662/solr"
)
$ErrorActionPreference = 'Stop'
Function Modify-GlobalJson ( [string] $commerceEngineSrcPath )
{
$pathToGlobalJson = $(Join-Path -Path $commerceEngineSrcPath -ChildPath "wwwroot\bootstrap\Global.json")
$global = Get-Content $pathToGlobalJson -Raw | ConvertFrom-Json
for($i = 0; $i -lt $global.Policies.'$values'.Length; $i++ )
{
$policyType = $global.Policies.'$values'[$i].'$type';
switch ($policyType)
{
"Sitecore.Commerce.Plugin.BusinessUsers.EnvironmentBusinessToolsPolicy,Sitecore.Commerce.Plugin.BusinessUsers"
{
$global.Policies.'$values'[$i].EnvironmentList.'$values' = @("$($siteName)Authoring", "$($siteName)Shops");
}
"Sitecore.Commerce.Plugin.SQL.EntityStoreSqlPolicy, Sitecore.Commerce.Plugin.SQL"
{
$global.Policies.'$values'[$i].Database = "$($siteSuffix)_Global"
}
"Sitecore.Commerce.Plugin.Management.SitecoreConnectionPolicy, Sitecore.Commerce.Plugin.Management"
{
$global.Policies.'$values'[$i].Host = $siteHostName
$global.Policies.'$values'[$i].UserName = $adminUser
$global.Policies.'$values'[$i].Password = $adminPassword
}
Default {}
}
}
## Remove the policies from example Plugins
$ExamplePlugins = @("Plugin.Sample.Upgrade.MigrationPolicy, Plugin.Sample.Upgrade", "Plugin.Sample.Upgrade.MigrationSqlPolicy, Plugin.Sample.Upgrade")
[System.Collections.ArrayList] $policies = $global.Policies.'$values'
$policies = $policies | Where-Object { $ExamplePlugins -notcontains $_.'$type' }
$global.Policies.'$values' = $policies
$global | ConvertTo-Json -Depth 10 | set-Content $pathToGlobalJson
}
Function Modify-ConfigJson([string] $commerceEngineSrcPath)
{
$pathToConfigJson = $(Join-Path -Path $commerceEngineSrcPath -ChildPath "wwwroot\config.json")
$config = Get-Content $pathToConfigJson -Raw | ConvertFrom-Json
$appSettings = $config.AppSettings
$appSettings.allowedOrigins = $appSettings.allowedOrigins.replace('localhost', $engineHostName)
$appSettings.allowedOrigins = $appSettings.allowedOrigins.replace('sxa.storefront.com', $siteHostName)
$appSettings.SitecoreIdentityServerUrl = ("https://{0}" -f $identityServerHost)
$appSettings.AntiForgeryEnabled = $false
$certPrefix = "CN="
$fullCertificateName = $certPrefix + $certificateName
$thumbprint = Get-ChildItem -path cert:\LocalMachine\my | Where-Object {$_.Subject -like $fullCertificateName} | Select-Object Thumbprint
$certificateNode = $config.Certificates.Certificates[0]
$certificateNode.Thumbprint = $thumbprint.Thumbprint
$config | ConvertTo-Json -Depth 10 | set-content $pathToConfigJson
}
Function Modify-PluginContent-PolicySet([string] $commerceEngineSrcPath)
{
# Modify PlugIn.Content.PolicySet
Write-Host "Updating PlugIn.Content.PolicySet-1.0.0.json" -ForegroundColor Yellow
$pathToContentPolicySet = $(Join-Path -Path $commerceEngineSrcPath -ChildPath "wwwroot\data\environments\PlugIn.Content.PolicySet-1.0.0.json")
$contentPolicySet = Get-Content $pathToContentPolicySet -Raw | ConvertFrom-Json
for($i = 0; $i -lt $contentPolicySet.Policies.'$values'.Length; $i++ )
{
$policyType = $contentPolicySet.Policies.'$values'[$i].'$type';
switch ($policyType) {
"Sitecore.Commerce.Plugin.Management.SitecoreConnectionPolicy, Sitecore.Commerce.Plugin.Management"
{
$contentPolicySet.Policies.'$values'[$i].Host = $siteHostName
$contentPolicySet.Policies.'$values'[$i].username = $adminUser
$contentPolicySet.Policies.'$values'[$i].password = $adminPassword
}
Default {}
}
}
$contentPolicySet | ConvertTo-Json -Depth 10 | Set-Content $pathToContentPolicySet
}
Function Modify-Plugin-SearchSolr-Policy([string] $commerceEngineSrcPath)
{
# Modify PlugIn.Search.Solr.PolicySet-1.0.0.json
Write-Host "PlugIn.Search.Solr.PolicySet-1.0.0.json" -ForegroundColor Yellow
$pathToJson = $(Join-Path -Path $commerceEngineSrcPath -ChildPath "wwwroot\data\Environments\PlugIn.Search.Solr.PolicySet-1.0.0.json")
$searchSolrPolicySet = Get-Content $pathToJson -Raw | ConvertFrom-Json
$searchSolrPolicySet.Policies.'$values'[0].SolrUrl = $solrUrl
$searchSolrPolicySet | ConvertTo-Json -Depth 10 | Set-Content $pathToJson
}
Function Modify-Plugin-SQL-Policy([string] $commerceEngineSrcPath)
{
# Modify Plugin.SQL.PolicySet-1.0.0.json
Write-Host "Plugin.SQL.PolicySet-1.0.0.json" -ForegroundColor Yellow
$pathToJson = $(Join-Path -Path $commerceEngineSrcPath -ChildPath "wwwroot\data\Environments\Plugin.SQL.PolicySet-1.0.0.json")
$sqlPolicySet = Get-Content $pathToJson -Raw | ConvertFrom-Json
$sqlPolicySet.Policies.'$values'[0].Database = "$($siteSuffix)_SharedEnvironments"
$sqlPolicySet | ConvertTo-Json -Depth 10 | Set-Content $pathToJson
}
Function Initialize-Plugin-CommerceEnvironment-Configuration([string] $commerceEngineSrcPath)
{
$ArtifactStoreId = [guid]::NewGuid().ToString()
$Id = [guid]::NewGuid().ToString().Replace("-", "")
$EvironmentConfigurationPath = Join-Path -Path $commerceEngineSrcPath -ChildPath "wwwroot\data\Environments"
$engineEnvironments = @("Authoring", "Shops", "Minions")
$configurationFileTemplate = "Plugin.{0}.{1}-1.0.0.json"
foreach($environment in $engineEnvironments)
{
$fullEnvironment = $("Commerce{0}" -f $environment)
$environmentNameWithPrefix = "$($siteName)$($environment)"
$originConfigurationFileName = "Plugin.Habitat.$($fullEnvironment)-1.0.0.json"
$newConfigurationFileName = "Plugin.$($siteName).$($fullEnvironment)-1.0.0.json"
Write-Host "Generating $($newConfigurationFileName)" -ForegroundColor Yellow
$originConfigurationFile = Join-Path -Path $EvironmentConfigurationPath -ChildPath $originConfigurationFileName
$newConfigurationFile = Join-Path -Path $EvironmentConfigurationPath -ChildPath $newConfigurationFileName
Copy-Item -Path $(Resolve-Path -Path $originConfigurationFile) -Destination $newConfigurationFile
if (!(Test-Path -Path $newConfigurationFile))
{
throw "$($newConfigurationFile) could not be found"
}
$config = Get-Content $newConfigurationFile -Raw | ConvertFrom-Json
$config.ArtifactStoreId = $ArtifactStoreId
$config.Components.'$values'[0].Id = $Id
$config.Id = $("Entity-CommerceEnvironment-$($environmentNameWithPrefix)")
$config.Name = $environmentNameWithPrefix
for($i = 0; $i -lt $config.Policies.'$values'.Length; $i++)
{
$policyType = $config.Policies.'$values'[$i].'$type'
switch ($policyType) {
"Sitecore.Commerce.Core.EnvironmentInitializationPolicy, Sitecore.Commerce.Core"
{
$config.Policies.'$values'[$i].InitialArtifactSets.'$values' = @('Carts.CartPolicySets-1.0', 'Catalog.DefaultSellableItems-1.0', 'Catalog.DefaultRelationshipDefinitions-1.0', 'GiftCards.DefaultGiftCard-1.0', 'GiftCards.TestGiftCards-1.0', 'Pricing.DefaultPriceBook-1.0')
}
"Sitecore.Commerce.Plugin.BusinessUsers.EnvironmentBusinessToolsPolicy,Sitecore.Commerce.Plugin.BusinessUsers"
{
$config.Policies.'$values'[$i].ServiceUrl = $("https://{0}:$($CommerceOpsPort)" -f $engineHostName)
}
Default {}
}
}
## Remove the policies from example Plugins
$ExamplePlugins = @("Plugin.Sample.Upgrade.MigrationPolicy, Plugin.Sample.Upgrade"
,"Plugin.Sample.Upgrade.MigrationSqlPolicy, Plugin.Sample.Upgrade"
,"Plugin.Sample.Customers.CsMigration.ProfilesSqlPolicy, Plugin.Sample.Customers.CsMigration"
,"Plugin.Sample.Customers.CsMigration.ProfilePropertiesMappingPolicy, Plugin.Sample.Customers.CsMigration")
[System.Collections.ArrayList] $policies = $config.Policies.'$values'
$policies = $policies | Where-Object { $ExamplePlugins -notcontains $_.'$type' } | Where-Object { $_.PolicySetId -ne "Entity-PolicySet-BraintreePolicySet" }
$config.Policies.'$values' = $policies
$config | ConvertTo-Json -Depth 10 | set-content $newConfigurationFile
}
}
Function Modify-IdentityServer-AppSettings
{
Write-Host "Modifying Identity Server configuration" -ForegroundColor Green
# Modify IdentityServer AppSettings based on new engine hostname
$idServerJson = $([System.IO.Path]::Combine($webRoot, $IdentityServerPathName, "wwwroot\appSettings.json"))
$idServerSettings = Get-Content $idServerJson -Raw | ConvertFrom-Json
$client = $idServerSettings.AppSettings.Clients | Where-Object {$_.ClientId -eq "CommerceBusinessTools"}
$client.RedirectUris = @(("https://{0}:4200" -f $engineHostName), ("https://{0}:4200/?" -f $engineHostName))
$client.PostLogoutRedirectUris = @(("https://{0}:4200" -f $engineHostName), ("https://{0}:4200/?" -f $engineHostName))
$client.AllowedCorsOrigins = @(("https://{0}:4200/" -f $engineHostName), ("https://{0}:4200" -f $engineHostName))
$idServerSettings | ConvertTo-Json -Depth 10 | set-content $idServerJson
}
Function Modify-BizFx-Config
{
Write-Host "Modifying BizFx (Business Tools) configuration" -ForegroundColor Green
#Modify BizFx to match new hostname
$bizFxJson = $([System.IO.Path]::Combine($webRoot, $BizFxPathName, "assets\config.json"))
$bizFxSettings = Get-Content $bizFxJson -Raw | ConvertFrom-Json
$bizFxSettings.EnvironmentName = $DefaultEnvironment
$bizFxSettings.BizFxUri = ("https://{0}:4200" -f $engineHostName)
$bizFxSettings.IdentityServerUri = ("https://{0}" -f $identityServerHost)
$bizFxSettings.EngineUri = ("https://{0}:5000" -f $engineHostName)
$bizFxSettings.ShopName = $DefaultShopName
$bizFxSettings | ConvertTo-Json -Depth 10 | set-content $bizFxJson
}
Function Disabled-Example-Plugins ([string] $commerceEngineSrcPath)
{
$examplePluginJsonFiles = @("PlugIn.AdventureWorks.CommerceAuthoring-1.0.0.json"
, "PlugIn.AdventureWorks.CommerceMinions-1.0.0.json"
, "PlugIn.AdventureWorks.CommerceShops-1.0.0.json"
, "PlugIn.Habitat.CommerceAuthoring-1.0.0.json"
, "PlugIn.Habitat.CommerceMinions-1.0.0.json"
, "PlugIn.Habitat.CommerceShops-1.0.0.json"
, "PlugIn.Payments.Braintree.PolicySet-1.0.0.json")
foreach($configFile in $examplePluginJsonFiles)
{
$pathToConfigFile = $(Join-Path -Path $commerceEngineSrcPath -ChildPath "wwwroot\data\Environments\$($configFile)")
if (Test-Path -Path $pathToConfigFile)
{
Write-Host "Disbaled $($configFile)"
Rename-Item -Path $pathToConfigFile -NewName "$($pathToConfigFile).disabled"
}
}
}
Function Remove-Example-Catalogs ([string] $commerceEngineSrcPath)
{
$exampleCatalogsPath = $(Join-Path -Path $commerceEngineSrcPath -ChildPath "wwwroot\data\Catalogs")
Write-Host "Removing Example Catalogs" -ForegroundColor Green
if (Test-Path -Path $exampleCatalogsPath)
{
Remove-Item -Path $exampleCatalogsPath -Include *.zip -Force -Recurse
}
}
Function Remove-Reference-Example-Project ([string] $csprojFilePath)
{
$examplePlugins = @("..\Plugin.Sample.AdventureWorks\Plugin.Sample.AdventureWorks.csproj"
, "..\Plugin.Sample.Customers.Upgrade\Plugin.Sample.Customers.Upgrade.csproj"
, "..\Plugin.Sample.Habitat\Plugin.Sample.Habitat.csproj"
, "..\Plugin.Sample.Payments.Braintree\Plugin.Sample.Payments.Braintree.csproj"
, "..\Plugin.Sample.Upgrade\Plugin.Sample.Upgrade.csproj"
, "..\Plugin.Sample.Customers.CsMigration\Plugin.Sample.Customers.CsMigration.csproj")
foreach($plugin in $examplePlugins)
{
dotnet remove $csprojFilePath reference $plugin
}
}
Function Add-SitecoreCommerce-Nuget ([string] $csprojFilePath)
{
$packages = @("Sitecore.Commerce.Plugin.Coupons"
, "Sitecore.Commerce.Plugin.Journaling"
, "Sitecore.Commerce.Plugin.SQL"
, "Sitecore.Commerce.Plugin.Tax" )
foreach($package in $packages)
{
dotnet add $csprojFilePath package $package
}
}
$commerceEngineSrcPath = Resolve-Path -Path ".\src\Sitecore.Commerce.Engine"
Modify-GlobalJson -commerceEngineSrcPath $commerceEngineSrcPath
Modify-ConfigJson -commerceEngineSrcPath $commerceEngineSrcPath
Modify-PluginContent-PolicySet -commerceEngineSrcPath $commerceEngineSrcPath
Modify-Plugin-SearchSolr-Policy -commerceEngineSrcPath $commerceEngineSrcPath
Modify-Plugin-SQL-Policy -commerceEngineSrcPath $commerceEngineSrcPath
Initialize-Plugin-CommerceEnvironment-Configuration -commerceEngineSrcPath $commerceEngineSrcPath
Disabled-Example-Plugins -commerceEngineSrcPath $commerceEngineSrcPath
Remove-Example-Catalogs -commerceEngineSrcPath $commerceEngineSrcPath
Modify-IdentityServer-AppSettings
Modify-BizFx-Config
$commerceEngineCsProj = Resolve-Path "$($commerceEngineSrcPath)\Sitecore.Commerce.Engine.csproj"
Remove-Reference-Example-Project -csprojFilePath $commerceEngineCsProj
Add-SitecoreCommerce-Nuget -csprojFilePath $commerceEngineCsProj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment