From cdd8ff258582f5eba7e3941a5a18007e7aabbbfa Mon Sep 17 00:00:00 2001 From: Daniel Beauchamp Date: Thu, 2 Aug 2012 13:38:19 -0400 Subject: Better generators, sample widgets, and more! --- bin/att | 66 -------------------------------------------------- bin/things | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 66 deletions(-) delete mode 100755 bin/att create mode 100755 bin/things (limited to 'bin') diff --git a/bin/att b/bin/att deleted file mode 100755 index c90fc48..0000000 --- a/bin/att +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env ruby - -require 'thor' -require 'net/http' -require 'json' - -class MockScheduler - def method_missing(*args) - yield - end -end - -def send_event(id, data) - req = Net::HTTP::Post.new("/widgets/#{id}") - req["content-type"] = "application/json" - req.body = JSON.unparse(data.merge(:auth_token => AllTheThings::CLI.auth_token)) - res = Net::HTTP.new('localhost', 3000).start { |http| http.request(req) } - puts "Data Sent to #{id}: #{data}" -end - -SCHEDULER = MockScheduler.new - -module AllTheThings - - class CLI < Thor - include Thor::Actions - - class << self - attr_accessor :auth_token - end - - attr_accessor :name - - def self.source_root - File.expand_path('../../templates', __FILE__) - end - - desc "install PROJECT_NAME", "Sets up ALL THE THINGS needed for your dashboard project structure." - def install(name) - @name = Thor::Util.snake_case(name) - directory :project, @name - end - - desc "new_widget WIDGET_NAME", "Creates a new widget with all the fixins'" - def new_widget(name) - @name = "widget_#{Thor::Util.snake_case(name)}" - directory :widget, File.join('widgets', @name) - end - - desc "start", "Starts the server in style!" - def start(*args) - args = args.join(" ") - system("bundle exec thin -R config.ru start #{args}") - end - - desc "job JOB_NAME AUTH_TOKEN(optional)", "Runs the specified job." - def job(name, auth_token = "") - self.class.auth_token = auth_token - f = File.join(Dir.pwd, "jobs", "#{name}.rb") - require f - end - - end -end - -AllTheThings::CLI.start \ No newline at end of file diff --git a/bin/things b/bin/things new file mode 100755 index 0000000..0ad9eb3 --- /dev/null +++ b/bin/things @@ -0,0 +1,81 @@ +#!/usr/bin/env ruby + +require 'thor' +require 'net/http' +require 'json' + +class MockScheduler + def method_missing(*args) + yield + end +end + +def send_event(id, data) + req = Net::HTTP::Post.new("/widgets/#{id}") + req["content-type"] = "application/json" + req.body = JSON.unparse(data.merge(:auth_token => AllTheThings::CLI.auth_token)) + res = Net::HTTP.new('localhost', 3000).start { |http| http.request(req) } + puts "Data Sent to #{id}: #{data}" +end + +SCHEDULER = MockScheduler.new + +module AllTheThings + + class CLI < Thor + include Thor::Actions + + class << self + attr_accessor :auth_token + end + + attr_accessor :name + + no_tasks do + ['widget', 'dashboard', 'job'].each do |type| + define_method "generate_#{type}" do |name| + @name = Thor::Util.snake_case(name) + directory type.to_sym, File.join("#{type}s") + end + end + end + + def self.source_root + File.expand_path('../../templates', __FILE__) + end + + desc "new PROJECT_NAME", "Sets up ALL THE THINGS needed for your dashboard project structure." + def new(name) + @name = Thor::Util.snake_case(name) + directory :project, @name + end + + desc "generate GENERATOR NAME", "Creates a new widget with all the fixins'" + def generate(type, name) + send("generate_#{type}".to_sym, name) + rescue NoMethodError => e + puts "Invalid generator. Either use widget, dashboard, or job" + end + map "g" => :generate + + desc "start", "Starts the server in style!" + method_option :job_path, :desc => "Specify the directory where jobs are stored" + def start(*args) + args = args.join(" ") + command = "bundle exec thin -R config.ru start #{args}" + command.prepend "export JOB_PATH=#{options[:job_path]}; " if options[:job_path] + system(command) + end + + desc "job JOB_NAME AUTH_TOKEN(optional)", "Runs the specified job." + def job(name, auth_token = "") + Dir[File.join(Dir.pwd, 'lib/**/*.rb')].each {|file| require file } + self.class.auth_token = auth_token + f = File.join(Dir.pwd, "jobs", "#{name}.rb") + require f + end + + end +end + +AllTheThings::CLI.start \ No newline at end of file -- cgit v1.2.3