Class: GlubyTK::Generator
- Inherits:
-
Object
- Object
- GlubyTK::Generator
- Defined in:
- lib/gluby-tk/generator.rb
Constant Summary collapse
- DIRECTORIES =
[ "assets", "assets/images", "interface", "src", "src/gluby" ]
- TEMPLATES =
[ {:name => "main.rb", :path => nil}, {:name => "includes.rb", :path => "src"}, {:name => "application.rb", :path => "src"}, {:name => "ApplicationWindow.glade", :path => "interface"} ]
Class Method Summary collapse
- .create(app_name) ⇒ Object
- .current_app_name(dir = nil) ⇒ Object
- .generate_ruby_class_for_document(file_name, file_contents, root) ⇒ Object
-
.get_template_contents(template_name) ⇒ Object
Helpers.
- .glubytk_rc_path(dir = nil) ⇒ Object
- .is_glubytk_directory?(dir = nil) ⇒ Boolean
- .rebuild(root = nil) ⇒ Object
- .template_dir ⇒ Object
Class Method Details
.create(app_name) ⇒ Object
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 |
# File 'lib/gluby-tk/generator.rb', line 21 def self.create app_name if File.exist?(app_name) GlubyTK.gputs "#{app_name} already exists!" exit(1) end root = "#{Dir.pwd}/#{app_name}" module_name = "#{app_name.underscore}" # Create root directory GlubyTK.gputs "Creating new project directory..." Dir.mkdir "#{Dir.pwd}/#{app_name}" # Create gluby-tkrc file File.open("#{root}/.gluby-tkrc", "w+") { |file| file.write(module_name) } # Create sub-directories DIRECTORIES.each do |dir| GlubyTK.gputs "Creating directory: #{dir}..." Dir.mkdir "#{root}/#{dir}" end # Generate files from templates TEMPLATES.each do |template| template_dest_file = "#{root}/#{template[:path].nil? ? "" : template[:path] + "/"}#{template[:name]}" GlubyTK.gputs "Creating #{template_dest_file}" File.open(template_dest_file, "w+") { |file| file.write(get_template_contents("#{template[:name]}.template").gsub("gluby-tk_app_id", module_name)) } end rebuild(root) GlubyTK.gputs "Finished creating #{module_name}" GlubyTK.gputs "Done!" end |
.current_app_name(dir = nil) ⇒ Object
160 161 162 |
# File 'lib/gluby-tk/generator.rb', line 160 def self.current_app_name(dir = nil) File.read(glubytk_rc_path(dir)).strip end |
.generate_ruby_class_for_document(file_name, file_contents, root) ⇒ Object
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 |
# File 'lib/gluby-tk/generator.rb', line 83 def self.generate_ruby_class_for_document(file_name, file_contents, root) doc = Nokogiri::XML(file_contents) if doc.css("template").first.nil? main_template = doc.css("object").first class_name = main_template["id"] parent = main_template["class"] else main_template = doc.css("template").first class_name = main_template["class"] parent = main_template["parent"] end parent = parent.gsub("Gtk","") base_file_name = "#{file_name.gsub(File.extname(file_name), "").underscore}" GlubyTK.gputs "Constructing #{base_file_name} Ruby class..." gluby_file_dir = "#{root}/src/gluby" gluby_file_path = "#{gluby_file_dir}/gluby_#{base_file_name}.rb" gluby_class_file_contents = [ "class #{class_name.underscore.humanize} < Gtk::#{parent}", "\ttype_register", "\tclass << self", "\t\tdef init", "\t\t\tset_template(:resource => '/app/#{current_app_name(root)}/#{file_name}')", "\t\t\t#{doc.css("[id]").map{|e| e.attributes["id"].value.to_sym }.select{|e| e != class_name.to_sym }}.each{|child_id| bind_template_child(child_id)}", "\t\tend", "\tend", "end" ].join("\n") File.open(gluby_file_path, "w+") { |file| GlubyTK.gputs "Writing #{gluby_file_path}..." file.write gluby_class_file_contents } # TODO: Need to ensure that the user-facing class (not the gluby_class_name) reflects any updates such as root class name change etc. file_path = "#{root}/src/#{base_file_name}.rb" if !File.exist?(file_path) GlubyTK.gputs "#{file_path} did not exist. Creating..." class_file_contents = [ "class #{class_name.underscore.humanize}", "\tdef initialize(args = nil)", "\t\tsuper(args)", "\tend", "end" ].join("\n") File.open(file_path, "w+") { |file| GlubyTK.gputs "Writing #{file_path}..." file.write class_file_contents } end File.open("#{gluby_file_dir}/gluby_includes.rb", "a+") { |file| contents = file.read g_req = "require 'gluby_#{base_file_name}'" req = "require '#{base_file_name}'" GlubyTK.gputs "Checking require entries for #{base_file_name}..." file.write("#{g_req}\n") if contents.match(g_req).nil? file.write("#{req}\n") if contents.match(req).nil? } end |
.get_template_contents(template_name) ⇒ Object
Helpers
152 153 154 |
# File 'lib/gluby-tk/generator.rb', line 152 def self.get_template_contents(template_name) File.read("#{template_dir}#{template_name}") end |
.glubytk_rc_path(dir = nil) ⇒ Object
172 173 174 |
# File 'lib/gluby-tk/generator.rb', line 172 def self.glubytk_rc_path(dir = nil) "#{dir || Dir.pwd}/.gluby-tkrc" end |
.is_glubytk_directory?(dir = nil) ⇒ Boolean
164 165 166 167 168 169 170 |
# File 'lib/gluby-tk/generator.rb', line 164 def self.is_glubytk_directory?(dir = nil) is_g_project = File.exist?(glubytk_rc_path(dir)) GlubyTK.gputs "No GlubyTK project detected. Please make sure you are in the right directory." if !is_g_project return is_g_project end |
.rebuild(root = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gluby-tk/generator.rb', line 60 def self.rebuild(root = nil) GlubyTK.gputs "Constructing gresource file..." root = root || Dir.pwd interface_files = Dir["#{root}/interface/*.glade"] gresource_file_contents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" gresource_file_contents += "<gresources>\n" interface_files.each { |filename| gresource_file_contents += "<gresource prefix=\"/app/#{current_app_name(root)}\">" gresource_file_contents += "<file preprocess=\"xml-stripblanks\">#{File.basename(filename)}</file>" gresource_file_contents += "</gresource>" generate_ruby_class_for_document(File.basename(filename), File.read(filename), root) } gresource_file_contents += "</gresources>\n" File.open("#{root}/interface/interface.gresource.xml", "w+") { |file| file.write(Nokogiri::XML(gresource_file_contents, &:noblanks)) } end |
.template_dir ⇒ Object
156 157 158 |
# File 'lib/gluby-tk/generator.rb', line 156 def self.template_dir "#{File.dirname(File.dirname(File.dirname(__FILE__)))}/templates/" end |