Skip to content
This repository was archived by the owner on Apr 15, 2019. It is now read-only.

Commit b0aee75

Browse files
author
hazae41
committed
.
0 parents  commit b0aee75

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Kotlin-Compiler-GUI
2+
Easily compile .kt and .kts files to class files

exe/kotlin-compiler-gui-1.0.exe

31.4 MB
Binary file not shown.

jar/kotlin-compiler-gui-1.0.jar

31.3 MB
Binary file not shown.

resources/icon.png

188 KB
Loading

src/Compiler.kt

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
package fr.rhaz.kotlin
2+
3+
import javafx.application.Application
4+
import javafx.application.Platform
5+
import javafx.geometry.Insets
6+
import javafx.geometry.Pos
7+
import javafx.scene.Scene
8+
import javafx.scene.control.Button
9+
import javafx.scene.control.Label
10+
import javafx.scene.image.Image
11+
import javafx.scene.input.ClipboardContent
12+
import javafx.scene.input.TransferMode
13+
import javafx.scene.layout.*
14+
import javafx.scene.paint.Color
15+
import javafx.scene.text.Font
16+
import javafx.stage.FileChooser
17+
import javafx.stage.FileChooser.ExtensionFilter
18+
import javafx.stage.Modality
19+
import javafx.stage.Stage
20+
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
21+
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
22+
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
23+
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
24+
import org.jetbrains.kotlin.config.Services
25+
import java.io.File
26+
import java.io.PrintStream
27+
import java.nio.file.Files
28+
import kotlin.streams.toList
29+
30+
31+
fun main(args: Array<String>) {
32+
Application.launch(KotlinCompiler::class.java, *args)
33+
}
34+
35+
class KotlinCompiler : Application() {
36+
37+
override fun start(stage: Stage) {
38+
window = stage
39+
window.apply(Window).apply { show() }
40+
}
41+
42+
lateinit var window: Stage;
43+
44+
val icon
45+
get() = KotlinCompiler::class.java.classLoader.getResourceAsStream("icon.png")
46+
47+
val stylesheet
48+
get() = KotlinCompiler::class.java.classLoader.getResource("style.css").toExternalForm()
49+
50+
val Window: Stage.() -> Unit = {
51+
icons.add(Image(icon))
52+
title = "Kotlin Compiler"
53+
minWidth = 400.0
54+
minHeight = 200.0
55+
isResizable = false
56+
scene()
57+
}
58+
59+
val scene: Stage.() -> Unit = {
60+
scene = Scene(StackPane().apply(Content), minWidth, minHeight)
61+
}
62+
63+
val Top: HBox by lazy {
64+
HBox().apply {
65+
66+
background = Background(BackgroundFill(Color.ORANGE, CornerRadii.EMPTY, Insets.EMPTY))
67+
68+
Label("Kotlin Compiler").apply {
69+
padding = Insets(16.0, 16.0, 16.0, 16.0)
70+
}.also { children.add(it); }
71+
72+
var x = 0 ; var y = 0
73+
setOnMousePressed {
74+
x = it.sceneX.toInt();
75+
y = it.sceneY.toInt();
76+
}
77+
setOnMouseDragged {
78+
window.x = it.screenX - x
79+
window.y = it.screenY - y
80+
}
81+
82+
Button("").apply {
83+
style = "-fx-background-color: transparent";
84+
translateX = 400.0
85+
padding = Insets(16.0, 0.0, 16.0, 0.0)
86+
setOnAction { window.isIconified = true }
87+
}.also { children.add(it); }
88+
89+
Button("X").apply {
90+
style = "-fx-background-color: transparent";
91+
translateX = 420.0
92+
padding = Insets(16.0, 0.0, 16.0, 0.0)
93+
setOnAction { System.exit(0) }
94+
}.also { children.add(it); }
95+
}
96+
}
97+
98+
lateinit var status: Label;
99+
lateinit var hint: Label;
100+
val Content: StackPane.() -> Unit = {
101+
102+
val chooser = FileChooser().apply {
103+
ExtensionFilter("All Kotlin Files", "*.kt", "*.kts").also{extensionFilters.add(it)}
104+
}
105+
106+
status = Label("Choose a file").apply {
107+
font = Font.font(30.0);
108+
translateY = -20.0
109+
}.also { children.add(it); }
110+
111+
hint = Label("or drag and drop")
112+
.apply { translateY = 10.0 }
113+
.also { children.add(it); }
114+
115+
setOnMouseClicked{ chooser.showOpenDialog(window)?.let{open(it)} }
116+
setOnDragOver { it.acceptTransferModes(*TransferMode.ANY); }
117+
setOnDragDropped here@{
118+
val drag = it.dragboard
119+
if(!drag.hasFiles()) return@here
120+
open(drag.files.singleOrNull() ?: return@here)
121+
}
122+
}
123+
124+
val open: StackPane.(File) -> Unit = content@{ file ->
125+
val types = listOf("kt", "kts")
126+
if(types.none{file.name.endsWith(it)}) return@content
127+
status.text = file.name
128+
children.remove(hint)
129+
130+
val toclass = Button("Compile to class").apply {
131+
translateY = 20.0
132+
style = "-fx-background-color: white";
133+
setOnAction {
134+
135+
children.remove(this)
136+
status.text = "Processing..."
137+
hint.text = "please wait"
138+
children.add(hint)
139+
140+
this@content.apply {
141+
setOnMouseClicked {}
142+
setOnDragOver {}
143+
}
144+
145+
Button("X").apply {
146+
style = "-fx-background-color: transparent";
147+
minWidth = 40.0
148+
minHeight = 40.0
149+
translateX = 180.0
150+
translateY = -80.0
151+
setOnAction { window.scene() }
152+
}.also { children.add(it) }
153+
154+
wait(500) {
155+
156+
val out = File(file.parentFile, file.nameWithoutExtension).apply {if(!exists()) mkdir()}
157+
val log = File(out, "log.txt").apply {if(!exists()) createNewFile()}
158+
val stream = PrintStream(log)
159+
val msgs = PrintingMessageCollector(stream, MessageRenderer.WITHOUT_PATHS, true)
160+
161+
val result = K2JVMCompiler().run {
162+
val args = K2JVMCompilerArguments().apply {
163+
freeArgs = listOf(file.absolutePath)
164+
destination = out.absolutePath
165+
classpath = System.getProperty("java.class.path")
166+
.split(System.getProperty("path.separator"))
167+
.filter{File(it).exists() && File(it).canRead()}
168+
.joinToString(":")
169+
noStdlib = true
170+
noReflect = true
171+
skipRuntimeVersionCheck = true
172+
reportPerf = true
173+
}
174+
execImpl(msgs, Services.EMPTY, args)
175+
}.code
176+
177+
stream.close();
178+
179+
status.text = when(result){
180+
0 -> "Done!"
181+
else -> "Error!".also {
182+
children.remove(hint)
183+
Button("Show logs").apply {
184+
style = "-fx-background-color: white";
185+
translateY = 20.0
186+
setOnAction {
187+
188+
val dialog = Stage()
189+
190+
val vbox = VBox().apply {
191+
alignment = Pos.CENTER
192+
padding = Insets(15.0)
193+
Files.lines(log.toPath()).apply {
194+
toList().joinToString("\n").also{children.add(Label(it)) }
195+
close()
196+
}
197+
Button("Close").apply {
198+
style = "-fx-background-color: white";
199+
setOnAction { dialog.close() }
200+
}.also { children.add(it) }
201+
}
202+
203+
dialog.apply {
204+
icons.add(Image(icon))
205+
title = "Error while compiling "+file.name
206+
initModality(Modality.WINDOW_MODAL)
207+
scene = Scene(vbox)
208+
show()
209+
}
210+
211+
}
212+
}.also {children.add(it)}
213+
}
214+
}
215+
216+
hint.text = "drag me out!"
217+
218+
this@content.setOnDragDetected {
219+
val d = this@content.startDragAndDrop(*TransferMode.ANY)
220+
val content = ClipboardContent().apply {
221+
putFiles(out.listFiles { _, name -> name.endsWith(".class") }.toMutableList())
222+
}.also { d.setContent(it) }
223+
}
224+
225+
}
226+
}
227+
}.also { children.add(it); }
228+
}
229+
230+
231+
}
232+
233+
fun async(timeout: Int, runnable: () -> Unit, callback: () -> Unit) = Thread(runnable).apply { tasker(timeout, callback) }
234+
235+
fun Thread.tasker(timeout: Int, callback: () -> Unit) = {
236+
start()
237+
val start = System.currentTimeMillis()
238+
while (this.isAlive) {
239+
Thread.sleep(1000)
240+
if (System.currentTimeMillis() - start > (timeout * 1000))
241+
interrupt()
242+
}
243+
Platform.runLater(callback)
244+
}.let { Thread(it).start() }
245+
246+
fun wait(timeout: Long, callback: () -> Unit) = {
247+
Thread.sleep(timeout)
248+
Platform.runLater { callback() }
249+
}.let { Thread(it).start() }

0 commit comments

Comments
 (0)