aboutsummaryrefslogtreecommitdiff
path: root/project/mavlink-library/src/main/scala/com/github/jodersky/mavlink/Main.scala
blob: 57d1fcc841e1ffe91ea1543f6cf6b8f101b9ee4c (plain) (blame)
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
package com.github.jodersky.mavlink

import scala.xml.XML
import com.github.jodersky.mavlink.parsing.Protocol
import java.io.FileWriter
import java.io.BufferedWriter
import scalax.file.Path
import java.io.File

object Main {
  
  def prettify(str: String) = str.replaceAll("(\\s*\n)(\\s*\n)+", "\n\n")
  
  private def processTemplates(protocol: Protocol, rootOut: Path) = {
    val root = rootOut / Path.fromString("org/mavlink")
    val mappings: List[(String, Path)] = List(
        org.mavlink.txt.Crc().body -> Path("Crc.scala"),
        org.mavlink.txt.Packet(protocol.messages).body -> Path("Packet.scala"),
        org.mavlink.txt.Parser().body -> Path("Parser.scala"),
        org.mavlink.txt.Assembler().body -> Path("Assembler.scala"),
        org.mavlink.messages.txt.Message(protocol.messages).body -> Path.fromString("messages/messages.scala")
    )
    
    for ((str, file) <- mappings) yield {
      val out = root / file
      out.createFile(true, false)
      out.write(prettify(str))
      out
    }
  }
  
  def run(dialect: File, out: File) = {
    val xml = XML.loadFile(dialect)
    val protocol = Protocol.parse(xml)
    processTemplates(protocol, Path(out)).map(_.fileOption.get)
  }
  
  def main(args: Array[String]): Unit = {
    run(new File(args(0)), new File(args(1)))
  }

}