Skip to content

Commit

Permalink
Fixed a bug with paths that have spaces and updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
blackstardlb committed Dec 9, 2020
1 parent 27e165a commit c62f478
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 91 deletions.
16 changes: 0 additions & 16 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions .idea/flinteditor-mps.iml

This file was deleted.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ To build the FlintIDE run `gradlew buildFlintPlugin`. This will generate a zip i
### Install
1. Download and install mps 2019.3.4 from [here](https://www.jetbrains.com/mps/download/previous.html)
2. Download the [mbeddr platform distribution plugins zip](https://projects.itemis.de/nexus/content/repositories/mbeddr/com/mbeddr/platform/2019.3.22227.6f9f955/platform-2019.3.22227.6f9f955.zip)
3. Extract the files and folders from the com.mbeddr.platform directory in mbeddr platform-distribution.zip file to the plugins folder (`{mps-installation-dir}/plugins`) in your mps installation.
3. Extract the files and folders from the com.mbeddr.platform directory in mbeddr platform-distribution.zip file to the plugins folder in your mps installation.
- On linux (`~/.MPS2019.3/config/plugins`)
- On macos (`~/Library/Application Support/IntelliJIDEA60/`)
- On windows (`C:\Users\%USER%\.MPS2019.3\config\plugins`)
4. Extract the Flint plugin from Flint.zip to the plugins folder in your mps installation.

## Using Flint-mps
Expand Down
25 changes: 24 additions & 1 deletion code/java/FlintParser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,36 @@ compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

ext {
path_with_space = new File(buildDir, "path with space")
}

test {
environment "FLINT_FILLER_DIR", flint_filler_executables_dir.path
environment "FLINT_FILLER_WITH_SPACE", path_with_space.path
environment "FLINT_FILLER_OUTPUT_DIR", new File(buildDir, "tmp").path
environment "FLINT_FILLER_TEST_FILE", new File(projectDir, "src/test/resources/flint-filler-test-file.xml").path
useJUnitPlatform()
}

if (!new File(flint_filler_executables_dir, "flintfiller-linux").exists()) {
tasks.getByName("test").dependsOn ":fetchFlintFillerExecutables"
tasks.getByName("test").dependsOn ":fetchFlintFillerExecutables"
}

task copyToPathWithSpace(type: Copy) {
tasks.getByName("test").dependsOn "copyToPathWithSpace"
mustRunAfter(":fetchFlintFillerExecutables")
flint_filler_executables_dir.mkdirs()
path_with_space.mkdirs()
flint_filler_executables_dir.eachFile {
if (it.isFile()) {
from it
}
}
into path_with_space
}

task deletePathWithSpace(type: Delete) {
tasks.getByName("test").finalizedBy "deletePathWithSpace"
delete(path_with_space)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,48 @@ import org.apache.commons.exec.PumpStreamHandler
import org.apache.commons.lang3.SystemUtils
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths


class FlintFiller(private val pathToFillerDir: String, private val outputDir: String) {
fun run(file: String, onCommandOutput: (String) -> Unit = {}): String {
makeExecutable("${pathToFillerDir}/${osSpecificFlintFiller()}")
Files.createDirectories(Path.of(outputDir))
val command = "${pathToFillerDir}/${osSpecificFlintFiller()} ${fillerArgs(file, outputDir)}"
val cmdLine: CommandLine = CommandLine.parse(command)
class FlintFiller(private val pathToFillerDirString: String, private val outputDirString: String) {
fun run(fileString: String, onCommandOutput: (String) -> Unit = {}): String {
val flintFillerExecutable = Paths.get("${pathToFillerDirString}/${osSpecificFlintFiller()}")
val outputDir = Paths.get(outputDirString)
val file = Paths.get(fileString)
try {
makeExecutable(flintFillerExecutable)
Files.createDirectories(outputDir)
val cmdLine = fillerCommand(flintFillerExecutable, file, outputDir)

val outputStream = StringOutputStream()
val streamHandler = PumpStreamHandler(outputStream)
val outputStream = StringOutputStream()
val streamHandler = PumpStreamHandler(outputStream)

val executor = DefaultExecutor()
executor.streamHandler = streamHandler
println("Executing command: $cmdLine")
val executor = DefaultExecutor()
executor.streamHandler = streamHandler

try {
val exitCode = executor.execute(cmdLine)
if (exitCode != 0) throw Exception("Bad exit code")
onCommandOutput("command output:\n${outputStream.value()}")
return Files.readString(Path.of(outputDir).resolve("flintFrame.json"))
try {
val exitCode = executor.execute(cmdLine)
if (exitCode != 0) throw Exception("Bad exit code")
onCommandOutput("command output:\n${outputStream.value()}")
return Files.readString(outputDir.resolve("flintFrame.json"))
} catch (e: Exception) {
throw Exception(
"Something went wrong while running flint filler\ncommand output:\n${outputStream.value()}",
e
)
}
} catch (e: Exception) {
throw Exception(
"Something went wrong while running flint filler\ncommand output:\n${outputStream.value()}",
e
)
throw java.lang.Exception("failed to run command on $flintFillerExecutable", e)
}
}

private fun makeExecutable(file: String) {
private fun makeExecutable(file: Path) {
if (!SystemUtils.IS_OS_WINDOWS) {
val command = "chmod +x $file"
val cmdLine: CommandLine = CommandLine.parse(command)
val cmdLine: CommandLine = CommandLine("chmod")
cmdLine.addArgument("+x")
cmdLine.addArgument(file.toString(), false)
println("Executing command: $cmdLine")
val executor = DefaultExecutor()
executor.execute(cmdLine)
}
Expand All @@ -52,12 +62,29 @@ class FlintFiller(private val pathToFillerDir: String, private val outputDir: St
}
}

private fun fillerArgs(inputFile: String, outputDir: String): String {
val inputFileLine = "-x ${inputFile}"
val dictFile = "-d ${outputDir}/dict.json"
val dataFrameFile = "-df ${outputDir}/dataFrame.csv"
val postTaggedDataFrameFile = "-pt ${outputDir}/postTaggedDataFrame_.csv"
val flintFrame = "-fo ${outputDir}/flintFrame.json"
return "$inputFileLine $dictFile $dataFrameFile $postTaggedDataFrameFile $flintFrame"
private fun fillerCommand(flintFillerExecutable: Path, inputFile: Path, outputDir: Path): CommandLine {
val cmdLine: CommandLine = CommandLine(flintFillerExecutable.toFile())
with(cmdLine) {
addArgument("-x")
addArgument(inputFile.toString(), false)
addArgument("-d")
addArgument(outputDir.resolve("dict.json").toString(), false)
addArgument("-df")
addArgument(outputDir.resolve("dataFrame.cvs").toString(), false)
addArgument("-pt")
addArgument(outputDir.resolve("postTaggedDataFrame_.csv").toString(), false)
addArgument("-fo")
addArgument(outputDir.resolve("flintFrame.json").toString(), false)
}
return cmdLine
}

private fun Path.escaped(): String {
return when {
SystemUtils.IS_OS_WINDOWS -> this.toString().replace(" ", "\\\\ ");
SystemUtils.IS_OS_LINUX -> this.toString().replace(" ", "\\ ");
SystemUtils.IS_OS_MAC_OSX -> "flintfiller-macos"
else -> throw NotImplementedError("Os ${SystemUtils.OS_NAME} is not supported")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,52 @@ package org.discipl.flint.flintfiller
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.notNullValue
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.io.File

import java.util.Comparator


internal class FlintFillerTest {
private val filler: FlintFiller = FlintFiller(
System.getenv("FLINT_FILLER_DIR"),
System.getenv("FLINT_FILLER_OUTPUT_DIR")
)

@AfterEach
fun cleanUp() {
deleteDirectory(Paths.get(System.getenv("FLINT_FILLER_WITH_SPACE")).resolve("tmp"))
deleteDirectory(Paths.get(System.getenv("FLINT_FILLER_OUTPUT_DIR")))
}

private fun deleteDirectory(path: Path) {
if (Files.exists(path)) {
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map { obj: Path -> obj.toFile() }
.forEach { obj: File -> obj.delete() }
}
}

@Test
fun run() {
val flintModel = filler.run(System.getenv("FLINT_FILLER_TEST_FILE"))
val flintModel = filler.run(System.getenv("FLINT_FILLER_TEST_FILE")) { println(it) }
println(flintModel.substring(0..400))
assertThat(flintModel, `is`(notNullValue()))
}

@Test
fun supportsDirectoriesWithSpaces() {
val filler: FlintFiller = FlintFiller(
System.getenv("FLINT_FILLER_WITH_SPACE"),
Paths.get(System.getenv("FLINT_FILLER_WITH_SPACE")).resolve("tmp").toString()
)
val flintModel = filler.run(System.getenv("FLINT_FILLER_TEST_FILE")) { println(it) }
println(flintModel.substring(0..400))
assertThat(flintModel, `is`(notNullValue()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ internal class FlintParserTest {
flintParser.getFacts().flatMap { (it.function as? Expression)?.flatten() ?: emptyList() }
val literalExpressions = allExpressions.mapNotNull { it as? LiteralExpression<*> }
assertThat(literalExpressions.size, `is`(equalTo(318)))
literalExpressions.forEach { println(it.operand?.javaClass) }
}
}
}
Expand Down
41 changes: 8 additions & 33 deletions code/solutions/Flint.plugin/models/Flint.plugin.plugin.mps
Original file line number Diff line number Diff line change
Expand Up @@ -7347,22 +7347,14 @@
</node>
</node>
</node>
<node concept="3clFbF" id="65WHNMBsw$7" role="3cqZAp">
<node concept="2OqwBi" id="65WHNMBsw$4" role="3clFbG">
<node concept="10M0yZ" id="65WHNMBsw$5" role="2Oq$k0">
<ref role="1PxDUh" to="wyt6:~System" resolve="System" />
<ref role="3cqZAo" to="wyt6:~System.out" resolve="out" />
</node>
<node concept="liA8E" id="65WHNMBsw$6" role="2OqNvi">
<ref role="37wK5l" to="guwi:~PrintStream.println(java.lang.String)" resolve="println" />
<node concept="3cpWs3" id="65WHNMBsxJ3" role="37wK5m">
<node concept="37vLTw" id="65WHNMBsxT1" role="3uHU7w">
<ref role="3cqZAo" node="6e6T56s6Z3J" resolve="fillerPath" />
</node>
<node concept="Xl_RD" id="65WHNMBsxiP" role="3uHU7B">
<property role="Xl_RC" value="Filler Path: " />
</node>
</node>
<node concept="2xdQw9" id="5AMZhMOnuhX" role="3cqZAp">
<property role="2xdLsb" value="h1akgim/info" />
<node concept="3cpWs3" id="5AMZhMOnuhY" role="9lYJi">
<node concept="37vLTw" id="5AMZhMOnuhZ" role="3uHU7w">
<ref role="3cqZAo" node="6e6T56s6Z3J" resolve="fillerPath" />
</node>
<node concept="Xl_RD" id="5AMZhMOnui0" role="3uHU7B">
<property role="Xl_RC" value="FlintFillerPath: " />
</node>
</node>
</node>
Expand Down Expand Up @@ -7434,23 +7426,6 @@
<node concept="17QB3L" id="65WHNMBr8bv" role="3clF45" />
<node concept="3Tm1VV" id="65WHNMBr8bw" role="1B3o_S" />
<node concept="3clFbS" id="65WHNMBr8bx" role="3clF47">
<node concept="2xdQw9" id="65WHNMBr8by" role="3cqZAp">
<property role="2xdLsb" value="h1akgim/info" />
<node concept="3cpWs3" id="65WHNMBr8bz" role="9lYJi">
<node concept="Xl_RD" id="65WHNMBr8b$" role="3uHU7B">
<property role="Xl_RC" value="path: " />
</node>
<node concept="2OqwBi" id="65WHNMBr8b_" role="3uHU7w">
<node concept="2YIFZM" id="65WHNMBr8bA" role="2Oq$k0">
<ref role="1Pybhc" to="91gc:2eNuKY2QKZ" resolve="FlintProperties" />
<ref role="37wK5l" to="91gc:7ALI6YWYmu7" resolve="getInstance" />
</node>
<node concept="2S8uIT" id="65WHNMBr8bB" role="2OqNvi">
<ref role="2S8YL0" to="91gc:2eNuKY2R2z" resolve="resourcesFolder" />
</node>
</node>
</node>
</node>
<node concept="3cpWs6" id="65WHNMBr8bC" role="3cqZAp">
<node concept="2OqwBi" id="65WHNMBr8bD" role="3cqZAk">
<node concept="37vLTw" id="65WHNMBr8bE" role="2Oq$k0">
Expand Down

0 comments on commit c62f478

Please sign in to comment.