Skip to content

Instantly share code, notes, and snippets.

@aloisdeniel
Last active November 4, 2015 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloisdeniel/b097846d3772b63da198 to your computer and use it in GitHub Desktop.
Save aloisdeniel/b097846d3772b63da198 to your computer and use it in GitHub Desktop.
Powershell snippets - Building, packaging
## Example call : & .\FormatTemplate.ps1 "test.txt" "out.txt" "1=TEST" , "other=128" ,"things=ok" , "unfound=o"
param
(
[string]$gfileIn,
[string]$gfileOut,
[string[]]$gvalues
)
## Replaces all mustache like occurences ("{{ key }}") in $fileIn content, with $values corresponding values and saves
## it into $fileOut.
Function FormatTemplate()
{
param
(
[string]$fileIn,
[string]$fileOut,
[HashTable]$values
)
$content = Get-Content $fileIn
foreach ($pair in $values.GetEnumerator())
{
$templateKey = "{{(\ *)" + $pair.Key + "(\ *)}}"
$templateValue = $pair.Value
Write-Host "[Format] Replacing $templateKey with $templateValue ..."
$content = $content -replace "$templateKey", "$templateValue"
}
Set-Content -Encoding UTF8 -Path $fileOut -Value $content
}
# Starting format
$hashValues = ConvertFrom-StringData ($gvalues | out-string)
FormatTemplate $gfileIn $gfileOut $hashValues
# Make sure to add the same configuration for all your project references.
param (
[string]$name = "",
[string]$csproj = "",
[string]$version = "",
[string]$buildPlatform = "x86",
[string]$buildConfiguration = "Release",
[string]$appxBundlePlatforms = "x86",
[string]$dropfolder = (Split-Path (Get-Item $MyInvocation.MyCommand.Path).FullName) + "\..\Packages\\",
[string]$logsFolder = (Split-Path (Get-Item $MyInvocation.MyCommand.Path).FullName) + "\..\Logs",
[string]$msbuildExecutable = "C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe"
)
# Local variables
$projectfolder = Split-Path (Get-Item $csproj).FullName;
$projectname = [io.path]::GetFileNameWithoutExtension((Get-Item $csproj).FullName);
$logsFile = "$logsFolder\$name.win81.log.txt"
# Creating log file
If (-Not (Test-Path $logsFolder)){ New-Item $logsFolder -Type directory }
If (-Not (Test-Path $logsFile)){ New-Item $logsFile -Type file }
# Start build
& $msbuildExecutable $csproj /Verbosity:Diagnostic /t:Rebuild /p:Configuration="$buildConfiguration" /p:Platform="$buildPlatform" /p:AppxBundle="Always" /p:AppxBundlePlatforms="$appxBundlePlatforms" /p:AppxPackageDir="$dropfolder" | Out-File $logsFile
if($LASTEXITCODE -eq 0)
{
Write-Host "[Package][$name] Succeed ($version)" -ForegroundColor Green
}
else
{
Write-Host "[Package][$name] Failed ($version)" -ForegroundColor Red
exit 1
}
# Renaming folder
$appxfolder = "$dropfolder" + "$projectname" + "_" + "$version" + "_Test/";
Rename-Item $appxfolder $name
# Make sure to add the same configuration for all your project references.
param (
[string]$name = "",
[string]$csproj = "",
[string]$version = "",
[string]$buildPlatform = "AnyCPU",
[string]$buildConfiguration = "Release",
[string]$dropfolder = (Split-Path (Get-Item $MyInvocation.MyCommand.Path).FullName) + "\..\Packages\\",
[string]$logsFolder = (Split-Path (Get-Item $MyInvocation.MyCommand.Path).FullName) + "\..\Logs",
[string]$msbuildExecutable = "C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe"
)
# Local variables
$projectfolder = Split-Path (Get-Item $csproj).FullName;
$projectname = [io.path]::GetFileNameWithoutExtension((Get-Item $csproj).FullName);
$logsFile = "$logsFolder\$name.win81.log.txt"
# Creating log file
If (-Not (Test-Path $logsFolder)){ New-Item $logsFolder -Type directory }
If (-Not (Test-Path $logsFile)){ New-Item $logsFile -Type file }
# Start build
& $msbuildExecutable $csproj /Verbosity:Diagnostic /t:Publish /p:Configuration="$buildConfiguration" /p:Platform="$buildPlatform" | Out-File $logsFile
if($LASTEXITCODE -eq 0)
{
Write-Host "[Package][$name] Succeed ($version)" -ForegroundColor Green
}
else
{
Write-Host "[Package][$name] Failed ($version)" -ForegroundColor Red
exit 1
}
# Copying package content
$in = "$projectfolder\bin\$buildConfiguration\app.publish";
$out = "$dropfolder/$name"
If (Test-Path $out) { Remove-Item -Recurse -Force $out }
New-Item $out -Type directory
Copy-Item "$in\*" $out -Recurse
param (
[string]$csproj = "",
[string]$version = ""
)
# Local variables
$projectfolder = Split-Path (Get-Item $csproj).FullName;
# AssemblyInfo
$configFiles = Get-Childitem $projectfolder "AssemblyInfo.cs" -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "AssemblyVersion\(""[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+""\)", "AssemblyVersion(""$version"")"} |
Foreach-Object {$_ -replace "AssemblyFileVersion\(""[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+""\)", "AssemblyFileVersion(""$version"")"} |
Set-Content -Encoding UTF8 -Path $file.PSPath
}
# Appxmanifest
$configFiles = Get-Childitem $projectfolder "Package.appxmanifest" -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "Version=""[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+""", "Version=""$version"""} |
Set-Content -Encoding UTF8 -Path $file.PSPath
}
param (
[string]$csproj = "",
[string]$version = ""
)
# Local variables
$projectfolder = Split-Path (Get-Item $csproj).FullName;
# AssemblyInfo
$configFiles = Get-Childitem $projectfolder "AssemblyInfo.cs" -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "AssemblyVersion\(""[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+""\)", "AssemblyVersion(""$version"")"} |
Foreach-Object {$_ -replace "AssemblyFileVersion\(""[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+""\)", "AssemblyFileVersion(""$version"")"} |
Set-Content -Encoding UTF8 -Path $file.PSPath
}
# Csproj
$csprojContent = (Get-Content $csproj)
$csprojContent = $csprojContent -replace "<ApplicationVersion>[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+</ApplicationVersion>", "<ApplicationVersion>$version</ApplicationVersion>";
Set-Content -Encoding UTF8 -Path $csproj -Value $csprojContent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment