-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptingWithJava.ps1
More file actions
69 lines (57 loc) · 2.3 KB
/
ScriptingWithJava.ps1
File metadata and controls
69 lines (57 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
[String]$PROG="MyApp"
[String]$MODE="DIR"
[bool]$COMPILE=$false
[String]$PACKAGE=""
[String]$PACKAGEPATH=""
function Init-App {
$Script:PACKAGE=(Select-String -Pattern 'package ' -SimpleMatch -Path "./$PROG.java").Line.replace("package ","").Replace(";","").Trim()
$Script:PACKAGEPATH=$PACKAGE.Replace(".","/")
}
function Compile-App {
if($MODE -eq "DIR") {
$COMPILE=(-Not [IO.File]::Exists("$PROG/$PACKAGEPATH/$PROG.class"))
if(-Not $COMPILE) {
$COMPILE=((Get-Item -Path "$PROG.java").LastWriteTime -gt (Get-Item -Path "$PROG/$PACKAGEPATH/$PROG.class").LastWriteTime)
}
}
if ($MODE -eq "JAR") {
$COMPILE=(-Not [IO.File]::Exists("$PROG.jar"))
if(-Not $COMPILE) {
$COMPILE=((Get-Item -Path "$PROG.java").LastWriteTime -gt (Get-Item -Path "$PROG.jar").LastWriteTime)
}
}
if($COMPILE) {
Write-Output "compiling..."
New-Item -Path "./$PROG" -ItemType Directory -Force | Out-Null
Invoke-Expression "javac -d `"./$PROG`" *.java"
if($MODE -eq "JAR") {
Write-Output "Creating JAR-File"
Set-Location "./$PROG"
[String]$manifestContent="Manifest-Version: 1.0"
$manifestContent += [Environment]::NewLine
$manifestContent += "Main-Class: $PACKAGE.$PROG"
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[IO.File]::WriteAllLines("$PROG/$PACKAGEPATH/$PROG.mf", $manifestContent, $Utf8NoBomEncoding)
Invoke-Expression "jar cmf `"$PACKAGEPATH/$PROG.mf`" `"../$PROG.jar`" $PACKAGEPATH/*.class ../$PROG.java"
Set-Location ..
Remove-Item -Force -Recurse "./$PROG/"
}
}
}
function Execute-App {
Param ([System.Array]$commandlineArgs)
if($MODE -eq "JAR") {
Write-Output "executing jar..."
Invoke-Expression "java -jar `"$PROG.jar`" `"$commandlineArgs`""
}
if($MODE -eq "DIR") {
Write-Output "executing class..."
Invoke-Expression "java -cp `"./$PROG`" `"$PACKAGE.$PROG`" `"$commandlineArgs`""
}
}
function Start-App() {
Param ([System.Array]$commandlineArgs)
Init-App
Compile-App
Execute-App -commandlineArgs $commandlineArgs
}