Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
Last active November 11, 2016 15:25
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 mlhaufe/1531798 to your computer and use it in GitHub Desktop.
Save mlhaufe/1531798 to your computer and use it in GitHub Desktop.
PowerShell ANT dependency parsing attempt [Abandoned]
set-psdebug -strict
cd C:\Users\-TNO-\Desktop\batik
$xml = get-content build.xml
$targets = $xml.selectNodes('/project/target')
$buildTargets = [Xml.XmlElement[]]$xml.selectNodes("//target[preceding-sibling::comment()[contains(.,'Build ..')]" +
" and " +
"following-sibling::comment()[contains(.,'Generates maven')]]")
$props = @{'${basedir}' = Get-Item .}
function antcall([Xml.XmlElement]$this,[HashTable]$tg){
#relies on <condition>
#$t = $targets | ?{ $_.name = unvar($this.target)} | parse
}
function available([Xml.XmlElement]$this,[HashTable]$tg){
<#ignore#>
}
function chmod([Xml.XmlElement]$this,[HashTable]$tg){
$tg.unix += 'chmod ' + $this.perm + ' ' + (unpath (unvar $this.file))
}
function checksum([Xml.XmlElement]$this,[HashTable]$tg){
$alg = $this.algorithm.toLower()
$ext = $this.fileext
$files = (fileset $this.fileset)
if($files){
$tg.files += $files
$files | %{
$n = join-path $_.directory.fullName "$($_.baseName).$ext"
$tg.files += New-Item -type file $n
$tg.unix += "$($alg)sum $(unpath $_)> $(unpath $n)"
}
} else {
echo "checksum failure"
}
}
function condition([Xml.XmlElement]$this,[HashTable]$tg){
#relies on the ability to interrogate the existence of class files
#and svn related activities... skipping for now?
#also os specific conditions
}
function copy_([Xml.XmlElement]$this,[HashTable]$tg){
if($this.file -and $this.tofile){
$file = unvar $this.file
$tofile = unvar $this.tofile
#@filtering and <filterset/> ignored as they modify contents of file and not the name
$tg.files += Get-Item $file
$tg.unix += "cp $(unpath $file) (unpath $tofile)"
#copy $file $tofile
} elseif($this.todir){
$todir = (unvar $this.todir)
$fs = fileset $this.fileset
$tg.files += $fs
$fs | %{
$tg.unix += "cp $(unpath (unvar $_)) $(unpath $todir)"
#copy $_ $todir
}
}
}
function delete([Xml.XmlElement]$this,[HashTable]$tg){
$dir = unvar $this.dir
$file = unvar $this.file
#no file dependency?
if($dir){
$tg.unix += "rm -rf $dir"
del $dir -recurse
} elseif($file) {
$tg.unix += "rm $file"
del $file
}
}
function dirset($tg,$d){
#relies on svn <exec> task....skip for now
}
function echo_([Xml.XmlElement]$this,[HashTable]$tg) {
$msg = if($this.message){ unvar $this.message } else { unvar $this.innerText } #escape line terminators?
$cmd = 'echo $msg'
if($this.file -ne $null){
$file = (New-Item -type file (unvar $this.file))
$tg.files += $file
$cmd += ' > $(unpath $this.file)'
}
$tg.unix += $cmd
}
function exec($this,$tg) {
#$d['unix'] += $this.executable + ' ' + (unvar $this.arg.line)
#output? resultproperty?
#just add a unix command?...side-effects may not be modeled and will probably affect later dependencies
}
function expandPath([String]$p){ #test on "test-sources\**\*.java"
$p = get-item (unvar $p)
if($p){
if($p[-1] -eq '/' -or $p[-1] -eq '\'){ $p += '**' }
if($p.indexOf('**') -gt -1){
dir -r $p.substring(0,$p.indexOf('**')) |
?{ !$_.PSIsContainer -and $_.FullName -like $p}
} else {
dir $p | ?{ !$_.PSIsContainer }
}
}
}
function fileset([Xml.XmlElement[]]$fs){
$fs | %{
$this = $_
$dir = Get-Item (unvar $this.dir)
$f = $this.include | %{ expandPath (join-path $dir $_.name) }
if($this.includes){
$f += expandPath (join-path $dir $this.includes) #zipfileset
}
$excludes = $this.excludes
if($excludes){
$e = expandPath (join-path $dir $excludes)
$f = $f | ?{ !($e -contains $_) } # may not work for ANT custom pattern: (excludes="**/.svn/"). Use -like pattern instead?
}
$exclude = $this.exclude
if($exclude){
$e = $exclude | %{ expandPath (join-path $dir $_) }
$f = $f | ?{ !($e -contains $_) } #borked?
}
$f
}
}
function filter([Xml.XmlElement]$this,[HashTable]$tg){
#ignored as irrelevant as it modifies contents of file and not the name
}
function javac([Xml.XmlElement]$this,[HashTable]$tg){
$deprecation = unvar $this.deprecation
$debug = unvar $this.debug
$optimize = unvar $this.optimize
$encoding = $this.encoding
$src
if($this.srcdir){
$src = (expandPath $this.srcdir) -like '*.java'
} elseif ($this.src){
$src = $this.src.path | %{ expandPath $_ } -like '*.java'
} else {
echo "Unmanaged condition in javac."
return $null;
}
if($this.exclude){
$excludes = $this.exclude | ?{!($props['${'+$_.unless+'}'])} | %{ unvar $_.name }
$excludes | %{
$e = $_
$src = $src | %{$_ -notlike $e}
}
}
$tg['files'] += $files
#classpath?
if($this.destdir){
} else {
echo "Unmanaged condition in javac."
return $null;
}
$tg['unix'] += 'javac ' #??? [in|ex]clude
}
function javadoc([Xml.XmlElement]$this,[HashTable]$tg) {
#http://download.oracle.com/javase/1.5.0/docs/tooldocs/solaris/javadoc.html
#http://ant.apache.org/manual/Tasks/javadoc.html
#etc
}
function jar([Xml.XmlElement]$this,[HashTable]$tg){
}
function loadfile([Xml.XmlElement]$this,[HashTable]$tg){
#relies on SVN, so skip unless command line svn requests are wanted....
}
function mkdir([Xml.XmlElement]$this,[HashTable]$tg){
$p = unpath (unvar $this.dir)
$tg.unix += "mkdir $p"
mkdir $p
}
function move([Xml.XmlElement]$this,[HashTable]$tg){
$file = unpath (unvar $this.file)
$tofile = unpath (unvar $this.tofile)
$tg.files += Get-Item $file
$tg.unix += 'mv $file $tofile'
move $file $tofile
}
function parse([Xml.XmlElement]$this){
if(($this.if -and $props['${'+$this.if+'}']) -or
!($this.unless -and $props['${'+$this.unless+'}'])){
if($deliverables[$this.name]){
$deliverables[$this.name]
} else {
$d = $deliverables[$this.name] = @{
'files' = @(); #[IO.FileInfo[]]
'unix' = @(); #[String[]]
'sql' = @(); #[String[]] #maybe?
}
if($this.depends){
$this.depends.split(',') | ?{$_} | %{
$n = $_.trim();
parse ($target | ?{$_.name -eq $n})
}
}
$this.selectNodes('*') | ?{$_} | %{
if('echo', 'copy' -contains $_.localname){
&"$($_.localname)_" $_ $d #built in functions can't be shadowed
} else {
&"$($_.localname)" $_ $d #execute the function with the same name as the tag
}
}
$d['files'] = $d['files'] | select -unique
}
}
}
function path([Xml.XmlElement]$this,[HashTable]$tg){
#just caching in $props?
}
function pathconvert([Xml.XmlElement]$this,[HashTable]$tg){
}
function property([Xml.XmlElement]$this,[HashTable]$tg){
if($this.name){
$props['${'+"$($this.name)"+'}'] = unvar $this.value
} else {
#ignoring <property location=""/> for now
#does this location count as a dependency?
}
}
function replace([Xml.XmlElement]$this,[HashTable]$tg){
#relies on <property location=""/> which is ignored for now
}
function tar([Xml.XmlElement]$this,[HashTable]$tg){
$tarfile = unvar $this.tarfile
$compression = $this.compression
$basedir = unvar $this.basedir
$includes = unvar $this.includes
$files
if($includes){
$files = expandPath (join-path $basedir $includes)
} elseif ($this.tarfileset){
$files = fileset $this.tarfileset #$basedir?
}
$tg.files += $files
$tg.unix += "tar "
}
function tstamp([Xml.XmlElement]$this,[HashTable]$tg){
#hardcoding for now as Powershell does not support 'z' format character
$props['${ts}'] = (Get-Date -format yyyyMMdd-HHmmss-) + (Get-WmiObject -Class win32_TimeZone).Caption
}
function unjar([Xml.XmlElement]$this,[HashTable]$tg){
$src = unpath (unvar $this.src)
$dest = unpath (unvar $this.dest)
$tg.files += $src
$tg.unix += "unzip $src -d $dest"
#windows unzip
$shell_app = New-Object -com shell.application
($shell_app.namespace($dest)).Copyhere(($shell_app.namespace($src)).items())
}
function unpath([string]$p){
$p.replace((Get-Item .).FullName, '.')
}
function unvar([String]$str){
$re = '\$\{[a-zA-Z0-9\.\-]+\}'
$fail = $false
$unvar = [regex]::Replace($str,$re,{
param($m)
[string]$m0 = $m.Groups[0]
if($props.containsKey($m0)){ $props[$m0] }
else { $fail = $true; $null }
})
if($unvar -match $re -and -not $fail){
unvar $unvar
} elseif(!$fail){
$unvar
} else {
$null
}
}
function uptodate([Xml.XmlElement]$this,[HashTable]$tg){
#assuming not up to date
#or just simply add to dependencies?
}
function zip([Xml.XmlElement]$this,[HashTable]$tg){
$zipfile = unvar $this.zipfile
$basedir = unvar $this.basefir
$includes = unvar $this.includes
$zfs = fileset $this.zipfileset
$tg['files'] += $zfs
$tg['unix'] += ""
#make a dummy zip at $zipfile
#$zfs.prefix is a target dir in the zip.
}
$deliverables = @{}
#$buildTargets | %{ Parse($_) }
#$deliverables
parse $xml.selectSingleNode("//target[@name='init']")
$c = $xml.selectSingleNode("//target[@name='appbundle']/copy")
$h = @{'files'=@();'unix'=@()}
copy_ $c $h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment