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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use std::io;
use std::io::BufRead;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::*;
use std::fs::create_dir_all;
use std::fs::OpenOptions;
use std::io::BufReader;
use ndata::dataobject::*;

use crate::datastore::*;

pub fn build_all() {
  let libs = fs::read_dir("data").unwrap();
  let store = DataStore::new();
  for db in libs {
    let lib = db.unwrap().file_name().into_string().unwrap();
    if !store.exists(&lib, "controls") {
      println!("No controls in library {}", &lib);
    }
    else {
      let controls = store.get_data(&lib, "controls");
      let list = controls.get_object("data").get_array("list");
      for control in list.objects() {
        let control = control.object();
        let ctl = control.get_string("name");
        let id = control.get_string("id");
        if !store.exists(&lib, &id) {
          println!("No control file for {}:{}", &lib, &id);
        }
        else {
          let ctldata = store.get_data(&lib, &id);
          let d = ctldata.get_object("data");
          if d.has("cmd") {
            let cmdlist = d.get_array("cmd");
            for command in cmdlist.objects() {
              let command = command.object();
              let cmd = command.get_string("name");
              build(&lib, &ctl, &cmd);
            }
          }
        }
      }
    }
  }    
}

pub fn build(lib:&str, ctl:&str, cmd:&str) {
  let store = DataStore::new();
  let id = &store.lookup_cmd_id(lib, ctl, cmd);
  
  if store.exists(lib, id) {
    let meta = store.get_data(lib, id);
    let data = meta.get_object("data");
    let typ = data.get_string("type");
    
    if typ == "rust" {
      let id = &data.get_string("rust");
      let mut meta = store.get_data(lib, id);
      
      let path = store.get_data_file(lib, &(id.to_owned()+".rs"));
      let src = store.read_file(path);
      let path = store.root.parent().unwrap().join("cmd").join("src").join(lib).join(ctl);
      
      meta.put_str("lib", lib);
      meta.put_str("ctl", ctl);
      meta.put_str("cmd", cmd);
      
      // FIXME - Don't rebuild if current
      
      println!("Building Rust: {}:{}:{}", lib, ctl, cmd);
      build_rust(path, meta, &src);
    }
    else if typ == "python" {
      let cid = &data.get_string("python");
      let mut meta = store.get_data(lib, cid);
      
      let path = store.get_data_file(lib, &(cid.to_owned()+".python"));
      
      let src = store.read_file(path);
      let pypath = store.root.parent().unwrap().join("lib_python");
      let path = store.root.parent().unwrap()
                                    .join("generated")
                                    .join("com")
                                    .join("newbound")
                                    .join("robot")
                                    .join("published")
                                    .join(lib);
      
      meta.put_str("ctlid", id);
      meta.put_str("lib", lib);
      meta.put_str("ctl", ctl);
      meta.put_str("cmd", cmd);
      
      // FIXME - Don't rebuild if current
      
      println!("Building Python: {}:{}:{}", lib, ctl, cmd);
      build_python(pypath, path, meta, &src);
    }
  }
}

fn lookup_type(t:&str) -> String {
  let typ = match t {
    "Any" => "Data",
    "Integer" => "i64",
    "Float" => "f64",
    "String" => "String",
    "File" => "String",
    "Boolean" => "bool",
    "JSONArray" => "DataArray",
    "InputStream" => "DataBytes",
    _ => "DataObject"
  };
  typ.to_string()
}

fn file_contains(path2:&PathBuf, m:&str) -> bool{
  let file = File::open(&path2).unwrap();
  let lines = io::BufReader::new(file).lines();
  for line in lines {
    if let Ok(ip) = line {
      if ip == m {
        return true;
      }
    }
  }
  false
}

fn build_mod(path2:&PathBuf, m:&str) {
  if path2.exists() {
    if !file_contains(path2, m) {
      let mut file = OpenOptions::new()
        .write(true)
        .append(true)
        .open(&path2)
        .unwrap();
      let _x = file.write_all(b"\n");
      let _x = file.write_all(m.as_bytes());
    }
  }
  else {
    let mut file = File::create(&path2).unwrap();
    let _x = file.write_all(m.as_bytes());
  }
}

fn build_rust(path:PathBuf, meta:DataObject, src:&str) {
  let _x = create_dir_all(&path);
  let id = &meta.get_string("id");
  let lib = &meta.get_string("lib");
  let ctl = &meta.get_string("ctl");
  let cmd = &meta.get_string("cmd");
  let data = meta.get_object("data");
  let import = &data.get_string("import");
  let returntype = &lookup_type(&data.get_string("returntype"));
  let params = &data.get_array("params");
  
  let path2 = &path.join(cmd.to_string()+".rs");
  let mut file = File::create(&path2).unwrap();

  let _x = file.write_all(b"use ndata::dataobject::*;\n");
  let _x = file.write_all(import.as_bytes());
  
  let n = params.len();
  let _x = file.write_all(b"\npub fn execute(");
  if n == 0 { let _x = file.write_all(b"_"); }
  let _x = file.write_all(b"o: DataObject) -> DataObject {\n");
  
  let mut index = 0;
  let mut invoke1 = "let ax = ".to_string()+cmd+"(";
  let mut invoke2 = "pub fn ".to_string()+cmd+"(";
  for v in params.objects() {
    let v = v.object();
    let name = &v.get_string("name");
    let t = &v.get_string("type");
    let typ = &lookup_type(t);
    let typ2;
    if typ == "DataObject" { typ2 = "object".to_string(); }
    else if typ == "DataArray" { typ2 = "array".to_string(); }
    else if typ == "DataBytes" { typ2 = "bytes".to_string(); }
    else if typ == "Data" { typ2 = "property".to_string(); }
    else { typ2 = typ.to_lowercase(); }
    let line = "let a".to_string() + &index.to_string() + " = o.get_" + &typ2 + "(\"" + name + "\");\n";
    let _x = file.write_all(line.as_bytes());
    if index > 0 {
      invoke1 = invoke1 + ", ";
      invoke2 = invoke2 + ", ";
    }
    invoke1 = invoke1 + "a" + &index.to_string();
    invoke2 = invoke2 + name + ":" + typ;
    index += 1;
  }
  invoke1 = invoke1 + ");\n";
  invoke2 = invoke2 + ") -> " + returntype + " {\n";

  let _x = file.write_all(invoke1.as_bytes());
  let _x = file.write_all(b"let mut o = DataObject::new();\n");
  if returntype == "Data" {
    let _x = file.write_all(b"o.set_property(\"a\", ax);\n");
  }
  else {
    let _x = file.write_all(b"o.put_");
    if returntype == "String" {
      let _x = file.write_all(b"str");
      let _x = file.write_all(b"(\"a\", &ax);\n");
    }
    else if returntype == "f64" {
      let _x = file.write_all(b"float");
      let _x = file.write_all(b"(\"a\", ax);\n");
    }
    else if returntype == "DataObject" {
      let _x = file.write_all(b"object");
      let _x = file.write_all(b"(\"a\", ax);\n");
    }
    else if returntype == "DataArray" {
      let _x = file.write_all(b"array");
      let _x = file.write_all(b"(\"a\", ax);\n");
    }
    else if returntype == "DataBytes" {
      let _x = file.write_all(b"bytes");
      let _x = file.write_all(b"(\"a\", ax);\n");
    }
    else {
      let _x = file.write_all(returntype.as_bytes());
      let _x = file.write_all(b"(\"a\", ax);\n");
    }
  }
  let _x = file.write_all(b"o\n");
  let _x = file.write_all(b"}\n\n");
  let _x = file.write_all(invoke2.as_bytes());
  let _x = file.write_all(src.as_bytes());
  let _x = file.write_all(b"\n}\n\n");
  
  let m = "pub mod ".to_string()+cmd+";";
  let path2 = &path.join("mod.rs");
  build_mod(path2, &m);  
  
  let m = "pub mod ".to_string()+ctl+";";
  let path2 = &path2.parent().unwrap().parent().unwrap().join("mod.rs");
  build_mod(path2, &m);  
  
  let m = "    state.cmds.push((\"".to_string()+id+"\".to_string(), "+lib+"::"+ctl+"::"+cmd+"::execute, \"\".to_string()));";
  let mm = "pub mod ".to_string()+lib+";";
  let path2 = &path2.parent().unwrap().parent().unwrap().join("lib.rs");
  if path2.exists() {
    let file = File::open(&path2).unwrap();
    let lines = io::BufReader::new(file).lines();
    let mut part1 = Vec::<String>::new();
    let mut part2 = Vec::<String>::new();
    let mut a = true;
    let mut b = true;
    let mut c = true;
    let begin = "    state.cmds.clear();";
    for line in lines {
      if let Ok(ip) = line {
        if ip == m {
          b = false;
          break;
        }
        
        if ip == mm {
          a = false;
        }
        
        if c {
          part1.push(ip.to_string());
        }
        else {
          part2.push(ip.to_string());
        }
        
        if ip == begin {
          c = false;
        }
      }
    }
    if b {
      let mut file = File::create(&path2).unwrap();
      if a {
        let _x = file.write_all(mm.as_bytes());
        let _x = file.write_all(b"\n");
      }
      for line in part1 {
        let _x = file.write_all(line.as_bytes());
        let _x = file.write_all(b"\n");
      }
      let _x = file.write_all(m.as_bytes());
      let _x = file.write_all(b"\n");
      for line in part2 {
        let _x = file.write_all(line.as_bytes());
        let _x = file.write_all(b"\n");
      }
    }
  }
  else {
      let mut file = File::create(&path2).unwrap();
      let _x = file.write_all(mm.as_bytes());
      let _x = file.write_all(b"\nuse flowlang::rustcmd::*;\n\n#[derive(Debug)]\npub struct Initializer {\n    pub data_ref: (&'static str, ((usize,usize),(usize,usize),(usize,usize))),\n    pub cmds: Vec<(String, Transform, String)>,\n}\n\n#[no_mangle]\npub fn mirror(state: &mut Initializer) {\n    flowlang::mirror(state.data_ref);\n    state.cmds.clear();\n");
      let _x = file.write_all(m.as_bytes());
      let _x = file.write_all(b"\n    for q in &state.cmds { RustCmd::add(q.0.to_owned(), q.1, q.2.to_owned()); }\n}\n");
  }
}

fn build_python(pypath:PathBuf, path:PathBuf, meta:DataObject, src:&str) {
  let _x = create_dir_all(&pypath);
  let _x = create_dir_all(&path);
//  let id = meta["id"].as_str().unwrap();
//  let lib = meta["lib"].as_str().unwrap();
//  let ctl = meta["ctl"].as_str().unwrap();
  let ctlid = &meta.get_string("ctlid");
//  let cmd = meta["cmd"].as_str().unwrap();
  let data = meta.get_object("data");
  let import = data.get_string("import");
  let import = import.replace("\r", "\n");
//  let returntype = &lookup_type(data["returntype"].as_str().unwrap());
  let params = data.get_array("params");
  
  let path2 = &path.join(ctlid.to_string()+"-f.py");
  let mut file = File::create(&path2).unwrap();

  let _x = file.write_all(b"import sys\nsys.path.append(\"");
  let _x = file.write_all(pypath.canonicalize().unwrap().to_str().unwrap().as_bytes());
  let _x = file.write_all(b"\")\n\n");
  let _x = file.write_all(import.as_bytes());
  let _x = file.write_all(b"\ndef execute(args):\n  return ");
  let _x = file.write_all(ctlid.as_bytes());
  let _x = file.write_all(b"(**args)\n");
  let _x = file.write_all(b"\ndef ");
  let _x = file.write_all(ctlid.as_bytes());
  let _x = file.write_all(b"(");
  
  let mut invoke = "".to_string();
  for param in params.objects() {
    let param = param.object();
    if invoke != "" { invoke += ", "; }
    invoke += &param.get_string("name");
  }
  let _x = file.write_all(invoke.as_bytes());
  let _x = file.write_all(b"):\n");
  
  let src = indent(src.to_string());
  let _x = file.write_all(src.as_bytes());
  let _x = file.write_all(b"\n");
}

fn indent(src:String) -> String {
  let mut s = "".to_string();
  let mut lines = BufReader::new(src.as_bytes()).lines();
  while let Some(line) = lines.next() {
    s += "  ";
    s += &line.unwrap();
    s += "\n";
  }
  s
}