common.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. require 'json'
  2. require 'fileutils'
  3. $res_root = (lambda {
  4. dir = File.dirname(__FILE__)
  5. loop do
  6. parent_dir = File.expand_path('..', dir)
  7. break if dir.empty? || parent_dir == dir
  8. dir = parent_dir
  9. break if File.exist?("#{dir}/project.godot")
  10. end
  11. return dir
  12. }).call
  13. CONFIG_ROOT = "#{$res_root}/.tbl"
  14. def get_options(tool_file)
  15. options = {}
  16. name = File.basename(tool_file, ".rb")
  17. JSON.parse(File.read("#{CONFIG_ROOT}/#{name}.json"))['options'].each do |unit|
  18. key, value = unit['name'], unit['value']
  19. value.gsub!('res:', $res_root) if value.is_a?(String)
  20. options[key] = value
  21. end
  22. return options
  23. end
  24. def convert_case(source)
  25. return source.split('_').collect(&:capitalize).join
  26. end
  27. def origin_case(source)
  28. source.gsub(/::/, '/').
  29. gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  30. gsub(/([a-z\d])([A-Z])/,'\1_\2').
  31. tr("-", "_").
  32. downcase
  33. end
  34. def ensure_dir(dir)
  35. return if Dir.exist?(dir)
  36. FileUtils.mkdir_p dir
  37. end
  38. def recreate_dir(dir)
  39. FileUtils.rm_rf dir
  40. FileUtils.mkdir_p dir
  41. end