write_crate.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from sccd.cd.parser.xml import *
  2. from sccd.test.parser.xml import *
  3. from sccd.util.indenting_writer import *
  4. from functools import partial
  5. import sccd
  6. RUST_DIR = os.path.dirname(sccd.__file__) + "/../../rust"
  7. # Quick and dirty high-level function, taking a statechart / class diagram / test model in XML format and generating from it a Rust crate, which is written to the filesystem as a directory.
  8. def write_crate(src, target):
  9. path = os.path.dirname(src)
  10. basename = os.path.splitext(os.path.basename(src))[0]
  11. globals = Globals()
  12. sc_parser_rules = partial(statechart_parser_rules, path=path, load_external=True)
  13. rules = {
  14. "statechart": sc_parser_rules(globals),
  15. "single_instance_cd": cd_parser_rules(sc_parser_rules),
  16. "test": test_parser_rules(sc_parser_rules),
  17. }
  18. parsed = parse_f(src, rules)
  19. if not os.path.isdir(target):
  20. os.mkdir(target)
  21. with open(target+"/%s.rs" % basename, 'w') as file:
  22. w = IndentingWriter(out=file)
  23. w.writeln("#![allow(non_camel_case_types)]")
  24. w.writeln("#![allow(non_snake_case)]")
  25. w.writeln("#![allow(unused_labels)]")
  26. w.writeln("#![allow(unused_variables)]")
  27. w.writeln("#![allow(dead_code)]")
  28. w.writeln("#![allow(unused_parens)]")
  29. w.writeln("#![allow(unused_macros)]")
  30. w.writeln("#![allow(non_upper_case_globals)]")
  31. w.writeln("#![allow(unused_mut)]")
  32. w.writeln("#![allow(unused_imports)]")
  33. w.writeln()
  34. w.writeln("#[cfg(target_arch = \"wasm32\")]")
  35. w.writeln("use wasm_bindgen::prelude::*;")
  36. w.writeln()
  37. if isinstance(parsed, Statechart):
  38. from sccd.statechart.codegen.rust import StatechartRustGenerator
  39. gen = StatechartRustGenerator(w, globals)
  40. parsed.accept(gen)
  41. else:
  42. from sccd.test.codegen.rust import TestRustGenerator
  43. # can parse Class Diagrams and Tests:
  44. gen = TestRustGenerator(w)
  45. parsed.accept(gen)
  46. with open(target+"/Cargo.toml", 'w') as file:
  47. w = IndentingWriter(out=file)
  48. w.writeln("[package]")
  49. w.writeln("name = \"%s\"" % basename)
  50. w.writeln("version = \"0.1.0\"")
  51. w.writeln("edition = \"2018\"")
  52. w.writeln()
  53. w.writeln("[dependencies]")
  54. w.writeln("sccd = { path = \"%s\" }" % RUST_DIR)
  55. w.writeln()
  56. w.writeln("# Only depend on wasm-bindgen when building for wasm32 architecture")
  57. w.writeln("[target.'cfg(target_arch = \"wasm32\")'.dependencies]")
  58. w.writeln("wasm-bindgen = \"0.2\"")
  59. w.writeln()
  60. if isinstance(parsed, Test):
  61. # Tests are compiled to binaries
  62. w.writeln("[[bin]]")
  63. else:
  64. # Everything else becomes a library
  65. w.writeln("[lib]")
  66. w.writeln("name = \"%s\"" % basename)
  67. w.writeln("path = \"%s.rs\"" % basename)
  68. w.writeln()
  69. with open(target+"/.gitignore", 'w') as file:
  70. w = IndentingWriter(out=file)
  71. w.writeln("/target/")