Skip to content

Commit 71dcc37

Browse files
authored
refactor icaro new here (#31)
1 parent a42e168 commit 71dcc37

File tree

7 files changed

+125
-51
lines changed

7 files changed

+125
-51
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: CI
2+
on:
3+
[ push ]
4+
jobs:
5+
container-test-job:
6+
runs-on: ubuntu-latest
7+
container: maven:3.8.6-amazoncorretto-11
8+
steps:
9+
- uses: actions/checkout@v3
10+
- name: tests
11+
run: mvn test

pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.icaro</groupId>
5-
<artifactId>cli</artifactId>
6-
<version>0.1</version>
5+
<artifactId>cli-core</artifactId>
6+
<version>0.1-alpha.1</version>
77
<packaging>jar</packaging>
88
<name>Icaro programming language CLI</name>
99
<url>http://maven.apache.org</url>
@@ -14,6 +14,11 @@
1414
<maven.compiler.target>11</maven.compiler.target>
1515
</properties>
1616
<dependencies>
17+
<dependency>
18+
<groupId>commons-io</groupId>
19+
<artifactId>commons-io</artifactId>
20+
<version>2.11.0</version>
21+
</dependency>
1722
<dependency>
1823
<groupId>com.google.code.gson</groupId>
1924
<artifactId>gson</artifactId>
@@ -71,7 +76,7 @@
7176
</goals>
7277
<configuration>
7378
<sourceDirs>
74-
<source>src/main/kotlin</source>
79+
<source>src</source>
7580
</sourceDirs>
7681
</configuration>
7782
</execution>
@@ -83,7 +88,7 @@
8388
</goals>
8489
<configuration>
8590
<sourceDirs>
86-
<source>src/test/kotlin</source>
91+
<source>test</source>
8792
</sourceDirs>
8893
</configuration>
8994
</execution>

src/commands/new-here.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package commands
2+
3+
import BINARY_DIR_NAME
4+
import CLASSES_DIR_NAME
5+
import CLI_VERSION_DEPS_ATTRIBUTE_NAME
6+
import DEPS_FILE
7+
import LANG_VERSION_DEPS_ATTRIBUTE_NAME
8+
import MAIN_SOURCE_FILE
9+
import SOURCE_DIR_NAME
10+
import com.google.gson.GsonBuilder
11+
import picocli.CommandLine
12+
import java.io.File
13+
14+
@CommandLine.Command(name = "new-here", description = ["the current directory will become an Icaro project"])
15+
class NewHere : Runnable {
16+
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(SOURCE_DIR_NAME, BINARY_DIR_NAME)
26+
} catch (e: Throwable) {
27+
e.printStackTrace()
28+
}
29+
}
30+
}
31+
32+
/*
33+
doesn't make sense to pass constants but
34+
1. constants are useful
35+
2. in this case, we make this function parametrized, or we couldn't test it
36+
because creating a src directory in the root path of this folder conflict with this
37+
project src file (DX with docker is bad because everytime we need to wait for maven to build)
38+
*/
39+
fun createSrcAndBinDirs(sourceDirName: String, binaryDirName: String) {
40+
File(sourceDirName).mkdir()
41+
File("$sourceDirName/$MAIN_SOURCE_FILE").writeText("")
42+
43+
File("$binaryDirName/$CLASSES_DIR_NAME").mkdirs()
44+
}
45+
46+
fun createDependenciesFile(cliVersion: String, langVersion: String) {
47+
val depsInJson = GsonBuilder().setPrettyPrinting().create().toJson(
48+
mapOf(
49+
CLI_VERSION_DEPS_ATTRIBUTE_NAME to cliVersion,
50+
LANG_VERSION_DEPS_ATTRIBUTE_NAME to langVersion
51+
)
52+
)
53+
54+
File(DEPS_FILE).writeText(depsInJson)
55+
}
File renamed without changes.

src/main/kotlin/commands/new-here.kt

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/util.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const val DEPS_FILE = "deps.json"
2+
const val CLI_VERSION_DEPS_ATTRIBUTE_NAME = "cliVersion"
3+
const val LANG_VERSION_DEPS_ATTRIBUTE_NAME = "langVersion"
4+
const val SOURCE_DIR_NAME = "src"
5+
const val MAIN_SOURCE_FILE = "main.ic"
6+
const val BINARY_DIR_NAME = ".bin"
7+
const val CLASSES_DIR_NAME = "classes"

test/commands/new-here.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package commands
2+
3+
import CLASSES_DIR_NAME
4+
import CLI_VERSION_DEPS_ATTRIBUTE_NAME
5+
import DEPS_FILE
6+
import LANG_VERSION_DEPS_ATTRIBUTE_NAME
7+
import MAIN_SOURCE_FILE
8+
import com.google.gson.Gson
9+
import org.junit.jupiter.api.Test
10+
import java.io.File
11+
import kotlin.test.assertEquals
12+
import kotlin.test.assertTrue
13+
14+
class NewHereTest {
15+
@Test
16+
fun shouldCreateDependenciesFile() {
17+
val cliVersion = "1.2"
18+
val langVersion = "3.1-rc.3"
19+
20+
createDependenciesFile(cliVersion, langVersion)
21+
22+
val dependencies = Gson().fromJson(File(DEPS_FILE).readText(), Map::class.java)
23+
24+
assertEquals(cliVersion, dependencies[CLI_VERSION_DEPS_ATTRIBUTE_NAME])
25+
assertEquals(langVersion, dependencies[LANG_VERSION_DEPS_ATTRIBUTE_NAME])
26+
27+
File(DEPS_FILE).delete()
28+
}
29+
30+
@Test
31+
fun shouldCreateSourceAndBinaryDirectories() {
32+
val sourceDir = "src43234"
33+
val binDir = ".bin43334"
34+
35+
createSrcAndBinDirs(sourceDir, binDir)
36+
37+
assertTrue(File("$sourceDir/$MAIN_SOURCE_FILE").isFile)
38+
assertTrue(File("$binDir/$CLASSES_DIR_NAME").isDirectory)
39+
40+
File(sourceDir).deleteRecursively()
41+
File(binDir).deleteRecursively()
42+
}
43+
}

0 commit comments

Comments
 (0)