Skip to content
This repository was archived by the owner on Mar 7, 2018. It is now read-only.

Commit d0eef2d

Browse files
committed
moving cli to lib and updating bin file
1 parent c3a7279 commit d0eef2d

File tree

9 files changed

+473
-282
lines changed

9 files changed

+473
-282
lines changed

bin/dashing

Lines changed: 5 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,7 @@
11
#!/usr/bin/env ruby
2+
require "pathname"
3+
bin_file = Pathname.new(__FILE__).realpath
4+
$:.unshift File.expand_path("../../lib", bin_file)
25

3-
require 'thor'
4-
require 'net/http'
5-
require 'json'
6-
require 'open-uri'
7-
8-
class MockScheduler
9-
def method_missing(*args)
10-
yield
11-
end
12-
end
13-
14-
SCHEDULER = MockScheduler.new
15-
16-
module Dashing
17-
18-
class CLI < Thor
19-
include Thor::Actions
20-
21-
class << self
22-
attr_accessor :auth_token
23-
24-
def hyphenate(str)
25-
return str.downcase if str =~ /^[A-Z-]+$/
26-
str.gsub('_', '-').gsub(/\B[A-Z]/, '-\&').squeeze('-').downcase
27-
end
28-
end
29-
30-
attr_accessor :name
31-
32-
no_tasks do
33-
['widget', 'dashboard', 'job'].each do |type|
34-
define_method "generate_#{type}" do |name|
35-
@name = Thor::Util.snake_case(name)
36-
directory type.to_sym, File.join("#{type}s")
37-
end
38-
end
39-
end
40-
41-
def self.source_root
42-
File.expand_path('../../templates', __FILE__)
43-
end
44-
45-
desc "new PROJECT_NAME", "Sets up ALL THE THINGS needed for your dashboard project."
46-
def new(name)
47-
@name = Thor::Util.snake_case(name)
48-
directory :project, @name
49-
end
50-
51-
desc "generate (widget/dashboard/job) NAME", "Creates a new widget, dashboard, or job."
52-
def generate(type, name)
53-
send("generate_#{type}".to_sym, name)
54-
rescue NoMethodError => e
55-
puts "Invalid generator. Either use widget, dashboard, or job"
56-
end
57-
map "g" => :generate
58-
59-
desc "install GIST_ID", "Installs a new widget from a gist."
60-
def install(gist_id)
61-
public_url = "https://gist.github.com/#{gist_id}"
62-
gist = JSON.parse(open("https://api.github.com/gists/#{gist_id}").read)
63-
64-
gist['files'].each do |filename, contents|
65-
if filename.end_with?(".rb")
66-
create_file File.join(Dir.pwd, 'jobs', filename), contents['content']
67-
elsif filename.end_with?(".coffee", ".html", ".scss")
68-
widget_name = File.basename(filename, '.*')
69-
create_file File.join(Dir.pwd, 'widgets', widget_name, filename), contents['content']
70-
end
71-
end
72-
73-
print set_color("Don't forget to edit the ", :yellow)
74-
print set_color("Gemfile ", :yellow, :bold)
75-
print set_color("and run ", :yellow)
76-
print set_color("bundle install ", :yellow, :bold)
77-
say set_color("if needed. More information for this widget can be found at #{public_url}", :yellow)
78-
79-
rescue OpenURI::HTTPError => e
80-
say set_color("Could not find gist at #{public_url}"), :red
81-
end
82-
map "i" => :install
83-
84-
desc "start", "Starts the server in style!"
85-
method_option :job_path, :desc => "Specify the directory where jobs are stored"
86-
def start(*args)
87-
port_option = args.include?('-p')? '' : ' -p 3030'
88-
args = args.join(" ")
89-
command = "bundle exec thin -R config.ru start #{port_option} #{args}"
90-
command.prepend "export JOB_PATH=#{options[:job_path]}; " if options[:job_path]
91-
system(command)
92-
end
93-
map "s" => :start
94-
95-
desc "stop", "Stops the thin server"
96-
def stop
97-
command = "bundle exec thin stop"
98-
system(command)
99-
end
100-
101-
desc "job JOB_NAME AUTH_TOKEN(optional)", "Runs the specified job. Make sure to supply your auth token if you have one set."
102-
def job(name, auth_token = "")
103-
Dir[File.join(Dir.pwd, 'lib/**/*.rb')].each {|file| require file }
104-
self.class.auth_token = auth_token
105-
f = File.join(Dir.pwd, "jobs", "#{name}.rb")
106-
require f
107-
end
108-
109-
end
110-
end
111-
112-
Dashing::CLI.start
6+
require 'dashing'
7+
Dashing::CLI.start(ARGV)

dashing.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ Gem::Specification.new do |s|
2929
s.add_development_dependency('rake', '~> 10.1.0')
3030
s.add_development_dependency('haml', '~> 4.0.4')
3131
s.add_development_dependency('minitest', '~> 5.2.0')
32-
32+
s.add_development_dependency('mocha', '~> 0.14.0')
33+
s.add_development_dependency('fakeweb', '~> 1.3.0')
3334
end

lib/dashing.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
require 'json'
88
require 'yaml'
99

10+
require 'dashing/cli'
11+
require 'dashing/downloader'
12+
1013
SCHEDULER = Rufus::Scheduler.new
1114

1215
set :root, Dir.pwd

lib/dashing/cli.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
require 'thor'
2+
require 'open-uri'
3+
4+
module Dashing
5+
class CLI < Thor
6+
include Thor::Actions
7+
8+
attr_reader :name
9+
10+
class << self
11+
attr_accessor :auth_token
12+
13+
def CLI.hyphenate(str)
14+
return str.downcase if str =~ /^[A-Z-]+$/
15+
str.gsub('_', '-').gsub(/\B[A-Z]/, '-\&').squeeze('-').downcase
16+
end
17+
end
18+
19+
no_tasks do
20+
%w(widget dashboard job).each do |type|
21+
define_method "generate_#{type}" do |name|
22+
@name = Thor::Util.snake_case(name)
23+
directory(type.to_sym, "#{type}s")
24+
end
25+
end
26+
end
27+
28+
desc "new PROJECT_NAME", "Sets up ALL THE THINGS needed for your dashboard project."
29+
def new(name)
30+
@name = Thor::Util.snake_case(name)
31+
directory(:project, @name)
32+
end
33+
34+
desc "generate (widget/dashboard/job) NAME", "Creates a new widget, dashboard, or job."
35+
def generate(type, name)
36+
public_send("generate_#{type}".to_sym, name)
37+
rescue NoMethodError => e
38+
puts "Invalid generator. Either use widget, dashboard, or job"
39+
end
40+
41+
desc "install GIST_ID", "Installs a new widget from a gist."
42+
def install(gist_id)
43+
gist = Downloader.get_gist(gist_id)
44+
public_url = "https://gist.github.com/#{gist_id}"
45+
46+
gist['files'].each do |file, details|
47+
if file =~ /\.(html|coffee|scss)\z/
48+
widget_name = File.basename(file, '.*')
49+
new_path = File.join(Dir.pwd, 'widgets', widget_name, file)
50+
create_file(new_path, details['content'])
51+
elsif file.end_with?('.rb')
52+
new_path = File.join(Dir.pwd, 'jobs', file)
53+
create_file(new_path, details['content'])
54+
end
55+
end
56+
57+
print set_color("Don't forget to edit the ", :yellow)
58+
print set_color("Gemfile ", :yellow, :bold)
59+
print set_color("and run ", :yellow)
60+
print set_color("bundle install ", :yellow, :bold)
61+
say set_color("if needed. More information for this widget can be found at #{public_url}", :yellow)
62+
rescue OpenURI::HTTPError => http_error
63+
say set_color("Could not find gist at #{public_url}"), :red
64+
end
65+
66+
desc "start", "Starts the server in style!"
67+
method_option :job_path, :desc => "Specify the directory where jobs are stored"
68+
def start(*args)
69+
port_option = args.include?('-p') ? '' : ' -p 3030'
70+
args = args.join(' ')
71+
command = "bundle exec thin -R config.ru start#{port_option} #{args}"
72+
command.prepend "export JOB_PATH=#{options[:job_path]}; " if options[:job_path]
73+
run_command(command)
74+
end
75+
76+
desc "stop", "Stops the thin server"
77+
def stop
78+
command = "bundle exec thin stop"
79+
run_command(command)
80+
end
81+
82+
desc "job JOB_NAME AUTH_TOKEN(optional)", "Runs the specified job. Make sure to supply your auth token if you have one set."
83+
def job(name, auth_token = "")
84+
Dir[File.join(Dir.pwd, 'lib/**/*.rb')].each {|file| require_file(file) }
85+
self.class.auth_token = auth_token
86+
f = File.join(Dir.pwd, "jobs", "#{name}.rb")
87+
require_file(f)
88+
end
89+
90+
# map some commands
91+
map 'g' => :generate
92+
map 'i' => :install
93+
map 's' => :start
94+
95+
private
96+
97+
def run_command(command)
98+
system(command)
99+
end
100+
101+
def require_file(file)
102+
require file
103+
end
104+
end
105+
end

lib/dashing/downloader.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'net/http'
2+
require 'open-uri'
3+
require 'json'
4+
5+
module Dashing
6+
module Downloader
7+
extend self
8+
9+
def get_gist(gist_id)
10+
get_json("https://api.github.com/gists/#{gist_id}")
11+
end
12+
13+
def get_json(url)
14+
response = open(url).read
15+
JSON.parse(response)
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)