drakon_gen.tcl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env tclsh8.6
  2. set use_log 0
  3. proc require { package errors } {
  4. if { [ catch {
  5. package require $package
  6. } ] } {
  7. foreach error $errors {
  8. puts $error
  9. }
  10. exit
  11. }
  12. }
  13. require msgcat {
  14. "This script requires MsgCat package."
  15. "Consider installing tk8.6 or later."
  16. }
  17. namespace import ::msgcat::mc
  18. set script_path [ file dirname [ file normalize [ info script ] ] ]
  19. source [ file join $script_path scripts/art.tcl ]
  20. source [ file join $script_path scripts/utils.tcl ]
  21. source [ file join $script_path scripts/generators.tcl ]
  22. source [ file join $script_path scripts/graph.tcl ]
  23. source [ file join $script_path scripts/auto.tcl ]
  24. source [ file join $script_path scripts/model.tcl ]
  25. source [ file join $script_path scripts/dedit.tcl ]
  26. source [ file join $script_path scripts/back.tcl ]
  27. source [ file join $script_path scripts/version.tcl ]
  28. source [ file join $script_path scripts/search.tcl ]
  29. source [ file join $script_path scripts/colors.tcl ]
  30. source [ file join $script_path scripts/graph2.tcl ]
  31. source [ file join $script_path scripts/icon.links.tcl ]
  32. source [ file join $script_path scripts/newfor.tcl ]
  33. source [ file join $script_path scripts/highlight.tcl ]
  34. source [ file join $script_path generators/c.tcl ]
  35. source [ file join $script_path generators/cpp.tcl ]
  36. source [ file join $script_path generators/cycle_body.tcl ]
  37. source [ file join $script_path generators/node_sorter.tcl ]
  38. source [ file join $script_path generators/python.tcl ]
  39. source [ file join $script_path generators/tcl.tcl ]
  40. source [ file join $script_path structure/struct.tcl ]
  41. source [ file join $script_path structure/tables.tcl ]
  42. source [ file join $script_path structure/tables_tcl.tcl ]
  43. source [ file join $script_path structure/tables_cs.tcl ]
  44. source [ file join $script_path structure/tables_c.tcl ]
  45. load_sqlite
  46. load_generators
  47. proc print_usage { } {
  48. puts "\nThis utility generates code from a .drn file."
  49. puts "Usage: tclsh8.6 drakon_gen.tcl <options>"
  50. puts "Options:"
  51. puts "-in <filename> The input filename."
  52. puts "-out <dir> The output directory. Optional."
  53. puts "or:"
  54. puts "tclsh8.6 drakon_gen.tcl -folder some_folder -ext js"
  55. }
  56. namespace eval mw {
  57. proc set_status { ignored } {
  58. }
  59. }
  60. proc get_argument { name optional } {
  61. global argv
  62. if { [ llength $argv ] % 2 != 0 } {
  63. puts "Error in command line arguments."
  64. print_usage
  65. exit 1
  66. }
  67. array set arguments $argv
  68. if { ![ info exists arguments($name) ] } {
  69. if { $optional } { return "" }
  70. puts "Error: $name argument missing."
  71. print_usage
  72. exit 1
  73. }
  74. return $arguments($name)
  75. }
  76. proc run { src_filename dst_filename } {
  77. mod::close db
  78. set result [ mod::open db $src_filename drakon ]
  79. set message [ lindex $result 1 ]
  80. if { $message != "" } {
  81. puts $message
  82. exit 1
  83. }
  84. mwc::init db
  85. set success [ gen::generate_no_gui $dst_filename]
  86. if { !$success } {
  87. exit 1
  88. }
  89. }
  90. proc file_time { file } {
  91. if { [ file exists $file ] } {
  92. return [ file mtime $file ]
  93. } else {
  94. return 0
  95. }
  96. }
  97. proc get_files { dir pattern } {
  98. set files [ glob -nocomplain -directory $dir $pattern ]
  99. return $files
  100. }
  101. set folder [ get_argument -folder 1 ]
  102. set folder [ file normalize $folder ]
  103. if { $folder != "" } {
  104. set ext [ get_argument -ext 0 ]
  105. puts "Building \"$ext\" files: \"$folder\""
  106. set files [ get_files $folder *.drn ]
  107. set dst_mask ".$ext"
  108. foreach file $files {
  109. set dst [ string map [list .drn $dst_mask] $file ]
  110. set dst_time [ file_time $dst ]
  111. set drn_time [ file_time $file ]
  112. if { $dst_time < $drn_time } {
  113. puts $file
  114. run $file $dst
  115. }
  116. }
  117. } else {
  118. set in [ get_argument -in 0 ]
  119. set out [ get_argument -out 1 ]
  120. set src_filename [ file normalize $in ]
  121. if { $out == "" } {
  122. set out [ file dirname $in ]
  123. }
  124. set out_dir [ file normalize $out ]
  125. set name [ file tail $src_filename ]
  126. set dst_filename [ file join $out_dir $name ]
  127. run $src_filename $dst_filename
  128. }