Skip to content

Commit 1ed8d9a

Browse files
authored
add icaro run (#35)
1 parent 9c68b03 commit 1ed8d9a

File tree

10 files changed

+292
-153
lines changed

10 files changed

+292
-153
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ on:
44
jobs:
55
container-test-job:
66
runs-on: ubuntu-latest
7-
container: maven:3.8.6-amazoncorretto-11
7+
container:
8+
image: maven:3.8.6-eclipse-temurin-11-alpine
9+
env:
10+
ICARO_HOME: /home/dilbert/icaro
811
steps:
912
- uses: actions/checkout@v3
1013
- name: tests

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM maven:3.8.6-amazoncorretto-11
2+
COPY pom.xml .
3+
RUN mvn package
4+
ENV ICARO_HOME=/home/icaro
5+
COPY ./src src
6+
COPY ./test test
7+
CMD mvn test

pom.xml

Lines changed: 136 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,139 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>org.icaro</groupId>
5-
<artifactId>cli-core</artifactId>
6-
<version>0.1-alpha.1</version>
7-
<packaging>jar</packaging>
8-
<name>Icaro programming language CLI</name>
9-
<url>http://maven.apache.org</url>
10-
<properties>
11-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12-
<kotlin.version>1.7.20</kotlin.version>
13-
<maven.compiler.source>11</maven.compiler.source>
14-
<maven.compiler.target>11</maven.compiler.target>
15-
</properties>
16-
<dependencies>
17-
<dependency>
18-
<groupId>commons-io</groupId>
19-
<artifactId>commons-io</artifactId>
20-
<version>2.11.0</version>
21-
</dependency>
22-
<dependency>
23-
<groupId>com.google.code.gson</groupId>
24-
<artifactId>gson</artifactId>
25-
<version>2.10</version>
26-
<scope>compile</scope>
27-
</dependency>
28-
<dependency>
29-
<groupId>info.picocli</groupId>
30-
<artifactId>picocli</artifactId>
31-
<version>4.6.3</version>
32-
</dependency>
33-
<dependency>
34-
<groupId>org.jetbrains.kotlin</groupId>
35-
<artifactId>kotlin-stdlib-jdk8</artifactId>
36-
<version>${kotlin.version}</version>
37-
</dependency>
38-
<dependency>
39-
<groupId>org.jetbrains.kotlin</groupId>
40-
<artifactId>kotlin-test</artifactId>
41-
<version>${kotlin.version}</version>
42-
<scope>test</scope>
43-
</dependency>
44-
<!-- Testing Dependencies -->
45-
<dependency>
46-
<groupId>org.assertj</groupId>
47-
<artifactId>assertj-core</artifactId>
48-
<version>3.23.1</version>
49-
<scope>test</scope>
50-
</dependency>
51-
<dependency>
52-
<groupId>org.junit.jupiter</groupId>
53-
<artifactId>junit-jupiter-api</artifactId>
54-
<version>5.9.0</version>
55-
<scope>test</scope>
56-
</dependency>
57-
<dependency>
58-
<groupId>org.junit.jupiter</groupId>
59-
<artifactId>junit-jupiter-engine</artifactId>
60-
<version>5.9.0</version>
61-
<scope>test</scope>
62-
</dependency>
63-
</dependencies>
64-
<build>
65-
<plugins>
66-
<plugin>
67-
<groupId>org.jetbrains.kotlin</groupId>
68-
<artifactId>kotlin-maven-plugin</artifactId>
69-
<version>${kotlin.version}</version>
70-
<executions>
71-
<execution>
72-
<id>compile</id>
73-
<phase>compile</phase>
74-
<goals>
75-
<goal>compile</goal>
76-
</goals>
77-
<configuration>
78-
<sourceDirs>
79-
<source>src</source>
80-
</sourceDirs>
81-
</configuration>
82-
</execution>
83-
<execution>
84-
<id>test-compile</id>
85-
<phase>test-compile</phase>
86-
<goals>
87-
<goal>test-compile</goal>
88-
</goals>
89-
<configuration>
90-
<sourceDirs>
91-
<source>test</source>
92-
</sourceDirs>
93-
</configuration>
94-
</execution>
95-
</executions>
96-
</plugin>
97-
<plugin>
98-
<groupId>org.apache.maven.plugins</groupId>
99-
<artifactId>maven-assembly-plugin</artifactId>
100-
<version>3.3.0</version>
101-
<configuration>
102-
<descriptorRefs>
103-
<descriptorRef>jar-with-dependencies</descriptorRef>
104-
</descriptorRefs>
105-
<archive>
106-
<manifest>
107-
<mainClass>MainKt</mainClass>
108-
</manifest>
109-
</archive>
110-
</configuration>
111-
<executions>
112-
<execution>
113-
<id>simple-command</id>
114-
<phase>package</phase>
115-
<goals>
116-
<goal>single</goal>
117-
</goals>
118-
</execution>
119-
</executions>
120-
</plugin>
121-
<plugin>
122-
<groupId>org.apache.maven.plugins</groupId>
123-
<artifactId>maven-surefire-plugin</artifactId>
124-
<version>2.22.1</version>
125-
<configuration>
126-
<workingDirectory>${project.build.directory}/test-generated</workingDirectory>
127-
</configuration>
128-
</plugin>
129-
</plugins>
130-
</build>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.icaro</groupId>
5+
<artifactId>cli-core</artifactId>
6+
<version>0.1-alpha.1</version>
7+
<packaging>jar</packaging>
8+
<name>Icaro programming language CLI</name>
9+
<url>http://maven.apache.org</url>
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<kotlin.version>1.7.20</kotlin.version>
13+
<maven.compiler.source>11</maven.compiler.source>
14+
<maven.compiler.target>11</maven.compiler.target>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>commons-io</groupId>
19+
<artifactId>commons-io</artifactId>
20+
<version>2.11.0</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.google.code.gson</groupId>
24+
<artifactId>gson</artifactId>
25+
<version>2.10</version>
26+
<scope>compile</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>info.picocli</groupId>
30+
<artifactId>picocli</artifactId>
31+
<version>4.6.3</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.jetbrains.kotlin</groupId>
35+
<artifactId>kotlin-stdlib-jdk8</artifactId>
36+
<version>${kotlin.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.jetbrains.kotlin</groupId>
40+
<artifactId>kotlin-test</artifactId>
41+
<version>${kotlin.version}</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<!-- Testing Dependencies -->
45+
<dependency>
46+
<groupId>org.assertj</groupId>
47+
<artifactId>assertj-core</artifactId>
48+
<version>3.23.1</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.junit.jupiter</groupId>
53+
<artifactId>junit-jupiter-api</artifactId>
54+
<version>5.9.0</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.junit.jupiter</groupId>
59+
<artifactId>junit-jupiter-engine</artifactId>
60+
<version>5.9.0</version>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.jetbrains.kotlin</groupId>
68+
<artifactId>kotlin-maven-plugin</artifactId>
69+
<version>${kotlin.version}</version>
70+
<executions>
71+
<execution>
72+
<id>compile</id>
73+
<phase>compile</phase>
74+
<goals>
75+
<goal>compile</goal>
76+
</goals>
77+
<configuration>
78+
<sourceDirs>
79+
<source>src</source>
80+
</sourceDirs>
81+
</configuration>
82+
</execution>
83+
<execution>
84+
<id>test-compile</id>
85+
<phase>test-compile</phase>
86+
<goals>
87+
<goal>test-compile</goal>
88+
</goals>
89+
<configuration>
90+
<sourceDirs>
91+
<source>test</source>
92+
</sourceDirs>
93+
</configuration>
94+
</execution>
95+
</executions>
96+
</plugin>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-assembly-plugin</artifactId>
100+
<version>3.3.0</version>
101+
<configuration>
102+
<descriptorRefs>
103+
<descriptorRef>jar-with-dependencies</descriptorRef>
104+
</descriptorRefs>
105+
<archive>
106+
<manifest>
107+
<mainClass>MainKt</mainClass>
108+
</manifest>
109+
</archive>
110+
</configuration>
111+
<executions>
112+
<execution>
113+
<id>simple-command</id>
114+
<phase>package</phase>
115+
<goals>
116+
<goal>single</goal>
117+
</goals>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
<plugin>
122+
<groupId>org.apache.maven.plugins</groupId>
123+
<artifactId>maven-surefire-plugin</artifactId>
124+
<version>2.22.1</version>
125+
<configuration>
126+
<workingDirectory>${project.build.directory}/test-generated</workingDirectory>
127+
</configuration>
128+
</plugin>
129+
<plugin>
130+
<groupId>org.apache.maven.plugins</groupId>
131+
<artifactId>maven-dependency-plugin</artifactId>
132+
<version>3.3.0</version>
133+
<configuration>
134+
<workingDirectory>${project.build.directory}/test-generated</workingDirectory>
135+
</configuration>
136+
</plugin>
137+
</plugins>
138+
</build>
131139
</project>

src/commands/new-here.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ import java.io.File
1414
@CommandLine.Command(name = "new-here", description = ["the current directory will become an Icaro project"])
1515
class NewHere : Runnable {
1616
override fun run() {
17-
try {
18-
print("insert cli version: ")
19-
val cliVersion = readLine()!!
20-
21-
print("insert lang version: ")
22-
val langVersion = readLine()!!
23-
24-
createDependenciesFile(cliVersion, langVersion)
25-
createSrcAndBinDirs()
26-
} catch (e: Throwable) {
27-
e.printStackTrace()
28-
}
17+
print("insert cli version: ")
18+
val cliVersion = readLine()!!
19+
20+
print("insert lang version: ")
21+
val langVersion = readLine()!!
22+
23+
createDependenciesFile(cliVersion, langVersion)
24+
createSrcAndBinDirs()
2925
}
3026
}
3127

src/commands/run.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package commands
2+
3+
import BINARY_DIR_NAME
4+
import CLASSES_DIR_NAME
5+
import DEPS_FILE
6+
import DepsFileNotFound
7+
import LANG_DIR_PATH
8+
import LANG_VERSION_DEPS_ATTRIBUTE_NAME
9+
import LangVersionNotFound
10+
import com.google.gson.Gson
11+
import com.google.gson.JsonSyntaxException
12+
import picocli.CommandLine
13+
import java.io.File
14+
import java.util.*
15+
16+
@CommandLine.Command(name = "run", description = ["build and execute the code inside src"])
17+
class Run : Runnable {
18+
override fun run() {
19+
ProcessBuilder(listOf("java", "-jar", "$LANG_DIR_PATH/${langVersionToUse()}.jar")).inheritIO().start().waitFor()
20+
21+
executeClassFile()
22+
}
23+
}
24+
25+
fun langVersionToUse(): String {
26+
if (!File(DEPS_FILE).isFile) throw DepsFileNotFound("In order to run this command you should be inside an icaro project")
27+
28+
var langVersion = ""
29+
30+
try {
31+
val dependencies = Gson().fromJson(File(DEPS_FILE).readText(), Map::class.java)
32+
33+
langVersion = dependencies[LANG_VERSION_DEPS_ATTRIBUTE_NAME].toString()
34+
} catch (e: Throwable) {
35+
throw JsonSyntaxException("deps.json file doesn't contain valid JSON!")
36+
}
37+
38+
if (!File("$LANG_DIR_PATH/$langVersion.jar").exists())
39+
throw LangVersionNotFound("I can't find installed the lang version you specified")
40+
41+
return langVersion
42+
}
43+
44+
fun executeClassFile() {
45+
val bytecode = File("$BINARY_DIR_NAME/$CLASSES_DIR_NAME/Main.class").readBytes()
46+
47+
IcaroClassLoader().defineClass("IcaroMain", bytecode).getMethod("main", Array<String>::class.java)
48+
.invoke(null, arrayOf<String>())
49+
}
50+
51+
class IcaroClassLoader : ClassLoader() {
52+
fun defineClass(name: String, bytecode: ByteArray): Class<*> = defineClass(name, bytecode, 0, bytecode.size)
53+
}

src/main.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import commands.NewHere
2+
import commands.Run
23
import picocli.CommandLine
3-
import picocli.CommandLine.*
4+
import picocli.CommandLine.Command
5+
import picocli.CommandLine.HelpCommand
46
import kotlin.system.exitProcess
57

68
@Command(
79
name = "icaro",
8-
subcommands = [NewHere::class, HelpCommand::class],
10+
subcommands = [NewHere::class, Run::class, HelpCommand::class],
911
description = ["The Icaro programming language CLI"]
1012
)
1113
class Icaro
1214

1315
fun main(args: Array<String>) {
14-
exitProcess(CommandLine(Icaro()).execute(*args))
16+
try {
17+
exitProcess(CommandLine(Icaro()).execute(*args))
18+
} catch (e: Throwable) {
19+
println(e.message)
20+
}
1521
}

0 commit comments

Comments
 (0)