Class: GlubyTK::Generator
- Inherits:
-
Object
- Object
- GlubyTK::Generator
- Defined in:
- lib/gluby-tk/generator.rb
Constant Summary collapse
- DIR_ASSETS =
"assets"
- DIR_ASSETS_IMAGES =
"#{DIR_ASSETS}/images"
- DIR_INTERFACE =
"interface"
- DIR_SRC =
"src"
- DIR_SRC_GLUBY =
"#{DIR_SRC}/gluby"
- DIRECTORIES =
[ DIR_ASSETS, DIR_ASSETS_IMAGES, DIR_INTERFACE, DIR_SRC, DIR_SRC_GLUBY ]
- TEMPLATES =
[ {:name => "main.rb", :path => nil}, {:name => "includes.rb", :path => "src"}, {:name => "application.rb", :path => "src"}, {:name => "ApplicationWindow.glade", :path => "interface"} ]
- SAMPLE_CODE_LINES =
[ "# Start GlubyTK sample code", "# Label Reference: https://ruby-gnome2.osdn.jp/hiki.cgi?pango-markup", "welcome_label.margin = 100", "welcome_label.use_markup = true", "welcome_label.markup = '<span weight=\"heavy\" foreground=\"white\" size=\"x-large\"><b>Generated with GlubyTK</b></span>'", "# End GlubyTK sample code" ]
Class Method Summary collapse
- .add_sample_code_to_new_project(root) ⇒ Object
- .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
.add_sample_code_to_new_project(root) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gluby-tk/generator.rb', line 81 def self.add_sample_code_to_new_project(root) app_window_path = "#{root}/#{DIR_SRC}/application_window.rb" contents = StringIO.open(File.read(app_window_path)) new_contents = "" contents.each do |line| new_contents << line if line.include?("super(args)") tab = "\t\t" nl = "\n" SAMPLE_CODE_LINES.each do |scl| new_contents << "#{tab}#{scl}#{nl}" end end end File.open(app_window_path, "wb").write new_contents end |
.create(app_name) ⇒ Object
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 |
# File 'lib/gluby-tk/generator.rb', line 37 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 # Construct gresource file & ruby classes rebuild(root) # Add in example code add_sample_code_to_new_project root GlubyTK.gputs "Finished creating #{module_name}" GlubyTK.gputs "Done!" end |
.current_app_name(dir = nil) ⇒ Object
198 199 200 |
# File 'lib/gluby-tk/generator.rb', line 198 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
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 |
# File 'lib/gluby-tk/generator.rb', line 121 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
190 191 192 |
# File 'lib/gluby-tk/generator.rb', line 190 def self.get_template_contents(template_name) File.read("#{template_dir}#{template_name}") end |
.glubytk_rc_path(dir = nil) ⇒ Object
210 211 212 |
# File 'lib/gluby-tk/generator.rb', line 210 def self.glubytk_rc_path(dir = nil) "#{dir || Dir.pwd}/.gluby-tkrc" end |
.is_glubytk_directory?(dir = nil) ⇒ Boolean
202 203 204 205 206 207 208 |
# File 'lib/gluby-tk/generator.rb', line 202 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/gluby-tk/generator.rb', line 98 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
194 195 196 |
# File 'lib/gluby-tk/generator.rb', line 194 def self.template_dir "#{File.dirname(File.dirname(File.dirname(__FILE__)))}/templates/" end |