Dear all , 

Hope you are all well today..I have been working on a script that reads parameters described in a Json file , builds war files from one GIT repository and exports them to a destination GIT repository. So far my script works processes single War files at a time but I would like it to process multiple war files.  Am fairly new to Json but will be heavily using it now instead of XML. 

Here is the script 

#.PARAMETER configfile
    The full path of a json config file. All command line parameters except configfile and commit default to values from this
   URL of the git repo with the sources
   Tag or branch to check out from the source repo. Optional
   URL of the git repo to commit the WAR file to. Optional
.PARAMETER trgbranch
   Tag or branch in the target repo to use. Optional
.PARAMETER warname
   File name of the WAR file to build
   Working directory. Optional. Will be deleted!
   Full path of the Java 'jar' tool
   Full path of the git binary
   Full path and filename to export the WAR file to. Optional
   Flag to enable committing and pushing to the target repo
param (
    [string]$configfile = $null,
    [string]$srcurl = $null,
    [string]$srctag = $null,
    [string]$trgurl = $null,
    [string]$trgbranch = $null,
    [string]$warname = $null,
    [string]$wd = $null,
    [string]$jarexe = $null,
    [string]$gitexe = $null,
    [string]$export = $null,
    [switch]$commit = $false

if (! $configfile) {
$configfile = $PSCommandPath.Replace(".ps1", ".json")

try {
$json = ConvertFrom-Json -InputObject (Gc $configfile -Raw -ErrorAction Stop)
} catch {
exit 1

# defaults from config file
if (! $srcurl) {
$srcurl = $json.srcurl
if (! $srctag) {
$srctag = $json.srctag
if (! $trgurl) {
$trgurl = $json.trgurl
if (! $trgbranch) {
$trgbranch = $json.trgbranch
if (! $warname) {
$warname = $json.warname
if (! $wd) {
$wd = $json.wd
if (! $jarexe) {
$jarexe = $json.jarexe
if (! $gitexe) {
$gitexe = $json.gitexe
if (! $export) {
$export = $json.export

# internal defaults and compueted parameters

if (! $jarexe) {
$jarexe = "jar.exe"
if (! $gitexe) {
$gitexe = "git.exe"
if (! $srcurl) {
throw "-srcurl is required"
if (! $warname) {
if (!$commit) {
$warname = "temp.war"
} else {
throw "-warname is required"
if (!$commit -and !$export) {
$export = $PSCommandPath.Replace(".ps1", ".war")

function New-TemporaryDirectory {
    $parent = [System.IO.Path]::GetTempPath()
    [string] $name = [System.Guid]::NewGuid()
    New-Item -ItemType Directory -Path (Join-Path $parent $name)

if (! $wd) {
$wd = New-TemporaryDirectory

$srcdir = (Join-Path $wd "srcdir")
$trgdir = (Join-Path $wd "trgdir")
$wardir = (Join-Path $wd "wardir")
$warfile = (Join-Path $trgdir $warname)
$infofile = (Join-Path $wardir "")
$buildts = Get-Date
$homedir = Get-Location

# processing

cd $wd

$wardir = New-Item -path $wardir -itemtype directory
Invoke-Expression -Command "$gitexe clone -q $srcurl $srcdir"
if ($trgurl) {
Invoke-Expression -Command "$gitexe clone -q $trgurl $trgdir"
if ($trgbranch) {
cd $trgdir
Invoke-Expression -Command "$gitexe checkout -q $trgbranch"
cd $wd
} else {
$trgdir = New-Item -path $trgdir -itemtype directory
if (Test-Path $warfile) {
Remove-Item $warfile

cd $srcdir
if ($srctag) {
Invoke-Expression -Command "$gitexe checkout -q $srctag"
#git checkout tags/
Invoke-Expression -Command "$gitexe checkout-index -q -a -f --prefix=$wardir\"
$id = Invoke-Expression -Command "$gitexe rev-parse HEAD"

cd $wardir
Set-Content -Path $infofile -Value "Git Tag: $srctag`r`nGit ID: $id`r`nBuilt date: $buildts"
if ($json.exclude) {
foreach ($exclude in $json.exclude) {
Remove-Item $exclude
Invoke-Expression -Command "$jarexe cf $warfile ."

if ($trgurl) {
cd $trgdir
Invoke-Expression -Command "$gitexe add $warfile"
if ($commit) {
Invoke-Expression -Command "$gitexe commit -q -m 'auto build'"
Invoke-Expression -Command "$gitexe push -q"

echo "export is $export"
if ($export) {
Copy-Item $warfile -Destination $export

cd $homedir
Remove-Item $wd -Recurse -Force

This is a sample Json file am using..


Any advice/suggestions will be highly appreciated.
0 Comments   [ + ] Show Comments


Please log in to comment

There are no answers at this time


Answer this question or Comment on this question for clarity