aboutsummaryrefslogtreecommitdiffstats
path: root/backend/src/compiler.rs
blob: ebd6c791ad78d63551ccf3a18b9843a9ce5504f6 (plain)
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
43
use rustc_serialize::json::{ToJson, Json};
use std::collections::BTreeMap;
use std::process::Command;
use error::PoeError;
use config;
use run_result;
use snippet::Snippet;

#[derive(Debug)]
pub struct Compiler {
    pub id: String,
    pub lang: String,
    pub version: String,
    pub version_command: String,
    pub commandline: Vec<String>,
}

impl Compiler {
    pub fn run(&self, snippet: &Snippet) -> Result<(), PoeError> {
        let output = Command::new(config::runner())
                          .arg(config::basedir())
                          .arg(self.overlay_path())
                          .arg(snippet.code_file())
                          .args(&self.commandline)
                          .output()?;
        run_result::parse_and_save(&snippet, &self, output)
    }

    pub fn overlay_path(&self) -> String {
        format!("{}/env/{}/{}", config::datadir(), &self.lang, &self.id)
    }

    pub fn render(&self) -> Json {
        let mut map = BTreeMap::new();
        map.insert("id".to_string(), self.id.to_json());
        map.insert("lang".to_string(), self.lang.to_json());
        map.insert("version".to_string(), self.version.to_json());
        map.insert("version_command".to_string(), self.version_command.to_json());
        map.insert("commandline".to_string(), self.commandline.to_json());

        Json::Object(map)
    }
}