Module: Teamster::CLI::ClassMethods
- Defined in:
- lib/teamster-cli.rb
Instance Method Summary collapse
- #adapter_helper_placeholder_for(filename, class_name) ⇒ Object
- #adapter_placeholder_for(filename, class_name) ⇒ Object
- #add_adapter_to_config(name, filename) ⇒ Object
- #ask_user(question) ⇒ Object
- #b64_enc(obj) ⇒ Object
- #config_ru ⇒ Object
- #create_adapter_for(name) ⇒ Object
- #create_config ⇒ Object
- #create_file(filename, method, *args) ⇒ Object
- #create_user ⇒ Object
- #detailed_usage ⇒ Object
- #export_adapter(name, files) ⇒ Object
- #import_adapter_for(name) ⇒ Object
- #quit(msg, code = 0) ⇒ Object
- #show_version ⇒ Object
- #teamster_adapters ⇒ Object
- #update_adapter(name) ⇒ Object
- #update_existing_adapters ⇒ Object
- #update_new_adapters ⇒ Object
- #usage ⇒ Object
- #view_placeholder_for(filename, class_name) ⇒ Object
- #view_summary_placeholder_for(filename, class_name) ⇒ Object
Instance Method Details
#adapter_helper_placeholder_for(filename, class_name) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/teamster-cli.rb', line 323 def adapter_helper_placeholder_for(filename, class_name) <<-CODE module Teamster module Adapters module #{class_name}Helper def #{filename}_summary? true end def #{filename}_summary erb :#{filename}_summary end end end end CODE end |
#adapter_placeholder_for(filename, class_name) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/teamster-cli.rb', line 284 def adapter_placeholder_for(filename, class_name) <<-CODE require_relative \"#{filename}/#{filename}_helper\" module Teamster module Adapters class #{class_name} < Sinatra::Base \# Class methods that contain Teamster-Adapter specific helpers. include BaseAdapter \# --{ Stuff that needs to be done before registration with core. }-- \# Add adapters here (comma separated) if there are helper adapters. has_helpers #{class_name}Helper \# Location of the views folder views_at \"\#\{File.dirname(__FILE__)\}/#{filename}/views\" under_development \# Remove this line when development is finished. \# ------------------------------------------------------------------ \# Register this class so it can be used. register self, "#{filename}" configure do enable :logging \# To log, use \"logger.info <MSG>\". Log messages are captured \# in the file specified in config.ru. end get '/#{filename.gsub('_', '-')}/?' do erb :#{filename} end end end end CODE end |
#add_adapter_to_config(name, filename) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/teamster-cli.rb', line 80 def add_adapter_to_config(name, filename) @config = YAML.load_file CONFIG_FILE adapter = { 'name' => name, 'files' => [ "lib/teamster-adapters/#{filename}.rb", "lib/teamster-adapters/#{filename}/#{filename}_helper.rb", "lib/teamster-adapters/#{filename}/views/#{filename}.erb", "lib/teamster-adapters/#{filename}/views/#{filename}_summary.erb" ] } @config.tap do |hsh| adapters = hsh.fetch('adapters') adapters << adapter end File.open(CONFIG_FILE, 'w') {|fh| fh.write @config.to_yaml} end |
#ask_user(question) ⇒ Object
32 33 34 35 |
# File 'lib/teamster-cli.rb', line 32 def ask_user(question) print "#{question}: " STDIN.gets.strip end |
#b64_enc(obj) ⇒ Object
28 29 30 |
# File 'lib/teamster-cli.rb', line 28 def b64_enc(obj) Base64.strict_encode64(obj) end |
#config_ru ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/teamster-cli.rb', line 260 def config_ru <<-CODE require 'teamster' Dir.glob("lib/*.rb").each {|file| require File.absolute_path(file)} Dir.mkdir("log") unless Dir.exists?("log") log_file = File.new("log/teamster-#{Time.now.strftime("%Y%m%d-%H%M")}.log", "a+") $stdout.reopen(log_file) $stderr.reopen(log_file) $stdout.sync=true $stderr.sync=true run Teamster::Core::App CODE end |
#create_adapter_for(name) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/teamster-cli.rb', line 98 def create_adapter_for(name) puts "Creating placeholders for adapter #{name}...\n" filename = name.split(' ').map{|a| a.downcase}.join('_') class_name = name.split(' ').map{|a| a.capitalize}.join('') FileUtils.mkdir_p "lib/teamster-adapters/#{filename}/views" create_file "lib/teamster-adapters/#{filename}.rb", "adapter_placeholder_for", filename, class_name create_file "lib/teamster-adapters/#{filename}/#{filename}_helper.rb", "adapter_helper_placeholder_for", filename, class_name create_file "lib/teamster-adapters/#{filename}/views/#{filename}.erb", "view_placeholder_for", filename, class_name create_file "lib/teamster-adapters/#{filename}/views/#{filename}_summary.erb", "view_summary_placeholder_for", filename, class_name puts "\nBasic adapter creation done!" puts "Controller : \"lib/teamster-adapters/#{filename}.rb\"" puts "Helper : \"lib/teamster-adapters/#{filename}/#{filename}_helper.rb\"" puts "View : \"lib/teamster-adapters/#{filename}/views/#{filename}.erb\"" puts "Summary View : \"lib/teamster-adapters/#{filename}/views/#{filename}_summary.erb\"" add_adapter_to_config name, filename end |
#create_config ⇒ Object
13 14 15 16 17 |
# File 'lib/teamster-cli.rb', line 13 def create_config @config['title'] = ask_user "What is your team name" @config['adapters'] = [] File.open(CONFIG_FILE, 'w') {|fh| fh.write @config.to_yaml} end |
#create_file(filename, method, *args) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/teamster-cli.rb', line 145 def create_file(filename, method, *args) case [File.exists?(filename), !!@opts[:overwrite]] when [true, false] puts "File \"#{filename}\" exists. Run with --overwrite to overwrite file." else puts "Creating file #{filename}..." File.open(filename, "w") {|fh| fh.write(send(method.to_sym, *args))} end end |
#create_user ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/teamster-cli.rb', line 19 def create_user puts "Creating default user.." users = [{"name" => "Administrator", "pass" => b64_enc(BCrypt::Password.create("password"))}] File.open("./data/users", "w") do |fh| fh.write({"users" => users}.to_yaml) end FileUtils.chmod 0600, './data/users' end |
#detailed_usage ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/teamster-cli.rb', line 176 def detailed_usage <<-DETAIL Teamster is a simple and extensible web portal for teams. Current version: #{VERSION} Usage: teamster [COMMAND] [OPTIONS] Commands: init, list, create, delete, export, import, update, start, stop, restart Options (standalone): --help Display this detailed help. --version Version of the teamster used. Command \"init\" : Initializes the web app. Creates required files for web app. Options: --overwrite If app is already initialized, --overwrite will recreate all the base files. Command \"list\" Lists all teamster adapters. Command \"create\" NAME Creates a skeleton of a teamster adapter. Creates a new entry in the config file. Command \"update\" NAME Updates the files for a teamster adapter in the config file. NAME is optional. If NAME is not given, all new and existing adapters are updated. Command \"delete\" NAME Deletes all the files of the adapter based on the config file. Option: --no-update Does not update new and existing adapters in the config files before deletion. Command \"remove\" NAME Same as DELETE above. Command \"export\" NAME Zips all the files listed in the config file for the specified adapter. Take care to scrub all information in config files, etc. Option: --no-update Does not update new and existing adapters in the config files before deletion. Command \"import\" NAME Import is not yet implemented. Command \"start\" : Used to run the teamster web app. Options: --prod Binds a unix socket to be used by a web server (eg. Nginx) and creates a state file that is used by the Puma app server. --socket-file FILE Required with --prod. Relative/absolute path to the UNIX socket file. --state-file FILE Required with --prod. Relative/absolute path to the Puma state file. Command \"stop\" : Used to stop the teamster web app in production mode. Options: --socket-file FILE Required. Relative/absolute path to the UNIX socket file. --state-file FILE Required. Relative/absolute path to the Puma state file. Command \"restart\" : Used to restart the teamster web app in production mode. Options: --socket-file FILE Required. Relative/absolute path to the UNIX socket file. --state-file FILE Required. Relative/absolute path to the Puma state file. DETAIL end |
#export_adapter(name, files) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/teamster-cli.rb', line 130 def export_adapter(name, files) zip_file = "#{name.gsub(' ', '_')}.zip" puts "\nExporting adapter #{name} to zip: #{zip_file}\n\n" puts "The following files will be exported into a zip file:" files.each {|f| puts "- #{f}"} puts if `which zip`.length != 0 if system "zip -r #{zip_file} #{files.join(' ')}" quit "\nExport successful!", 0 else quit "\nError occurred when zipping!", 1 end end end |
#import_adapter_for(name) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/teamster-cli.rb', line 115 def import_adapter_for(name) puts "Importing adapter: #{name}" zip_file = "#{name}.zip" if File.exists?(zip_file) if `which unzip`.length != 0 `unzip #{zip_file}` puts "\nSuccessfully imported #{name}. Please restart teamster to use." else puts "\nUnable to import adapter. Export depends on cli tool \"unzip\"." end else puts "Unable to find file: #{zip_file}" end end |
#quit(msg, code = 0) ⇒ Object
8 9 10 11 |
# File 'lib/teamster-cli.rb', line 8 def quit(msg, code = 0) warn msg exit code end |
#show_version ⇒ Object
155 156 157 |
# File 'lib/teamster-cli.rb', line 155 def show_version "Current version of teamster: #{VERSION}" end |
#teamster_adapters ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/teamster-cli.rb', line 276 def teamster_adapters <<-CODE Dir.glob(File.dirname(__FILE__) + '/teamster-adapters/*.rb').each do |mdl| require mdl end CODE end |
#update_adapter(name) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/teamster-cli.rb', line 37 def update_adapter(name) config = YAML.load_file(CONFIG_FILE) adapter = config.fetch('adapters').find {|e| e.fetch("name") == name} quit "No local adapter found with name: #{name}" unless adapter list_from_fs = Dir.glob("lib/teamster-adapters/#{name.gsub(' ', '_')}/**/*.*") list_from_conf = adapter.fetch('files') list_from_conf.concat(list_from_fs).uniq! File.open(CONFIG_FILE, 'w') {|fh| fh.write config.to_yaml} end |
#update_existing_adapters ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/teamster-cli.rb', line 65 def update_existing_adapters config = YAML.load_file(CONFIG_FILE) adapters = config.fetch('adapters') current_adapters = Dir.glob("lib/teamster-adapters/*.rb").map do |f| f.split('/').last.gsub('.rb', '').gsub('_', ' ') end current_adapters.each do |name| adapter = adapters.find {|e| e.fetch("name") == name} list_from_fs = Dir.glob("lib/teamster-adapters/#{name.gsub(' ', '_')}/**/*.*") list_from_conf = adapter.fetch('files') list_from_conf.concat(list_from_fs).uniq! end File.open(CONFIG_FILE, 'w') {|fh| fh.write config.to_yaml} end |
#update_new_adapters ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/teamster-cli.rb', line 47 def update_new_adapters config = YAML.load_file(CONFIG_FILE) adapters = config.fetch('adapters') current_adapters = Dir.glob("lib/teamster-adapters/*.rb").map do |f| f.split('/').last.gsub('.rb', '').gsub('_', ' ') end current_adapters.each do |name| next if adapters.find {|e| e.fetch("name") == name} adapters << {}.tap do |hsh| hsh["name"] = name file_list = Dir.glob("lib/teamster-adapters/#{name.gsub(' ', '_')}/**/*.*") file_list << "lib/teamster-adapters/#{name.gsub(' ', '_')}.rb" hsh["files"] = file_list end end File.open(CONFIG_FILE, 'w') {|fh| fh.write config.to_yaml} end |
#usage ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/teamster-cli.rb', line 159 def usage <<-HELP Initialize application: teamster init Create an adapter: teamster create my amazing adapter Run web application: teamster start Verify by opening browser and navigating to "http://localhost:9292". For more detailed help, please run "teamster --help". HELP end |
#view_placeholder_for(filename, class_name) ⇒ Object
341 342 343 344 345 346 |
# File 'lib/teamster-cli.rb', line 341 def view_placeholder_for(filename, class_name) <<-CODE <h1 style='text-align: center'>PLACEHOLDER FOR #{filename.split('_').map{|a| a.upcase}.join(' ')}</h1> <p style='text-align: center'>Page under construction. Please check back later!</p> CODE end |
#view_summary_placeholder_for(filename, class_name) ⇒ Object
348 349 350 351 352 |
# File 'lib/teamster-cli.rb', line 348 def view_summary_placeholder_for(filename, class_name) <<-CODE <p>Under development right now..</p> CODE end |