Module: Cyborg::Command

Extended by:
Command
Included in:
Command
Defined in:
lib/cyborg/command.rb

Instance Method Summary collapse

Instance Method Details

#build(options = {}) ⇒ Object

Build assets



97
98
99
100
101
102
# File 'lib/cyborg/command.rb', line 97

def build(options={})
  puts Cyborg.production? ? 'Building for production…' : 'Building…'
  require_rails
  clean if Cyborg.production?
  Cyborg.plugin.build(options)
end

#cleanObject



82
83
84
85
86
# File 'lib/cyborg/command.rb', line 82

def clean
  FileUtils.rm_rf(Cyborg.rails_path('tmp/cache/'))
  FileUtils.rm_rf('.sass-cache')
  FileUtils.rm_rf(Cyborg.rails_path('.sass-cache'))
end

#dispatch(command, *args) ⇒ Object

Handles running threaded commands



90
91
92
93
94
# File 'lib/cyborg/command.rb', line 90

def dispatch(command, *args)
  @threads = []
  send(command, *args)
  @threads.each { |thr| thr.join }
end

#from_root(command = nil, &blk) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/cyborg/command.rb', line 124

def from_root(command=nil, &blk)
  unless dir = Cyborg.gem_path
    abort "Command must be run from the root of a Cyborg Plugin (adjacent to the gemspec)."
  end

  Dir.chdir(dir) do
    if command
      system command
    else
      blk.call
    end
  end
end

#gem_buildObject



50
51
52
53
54
55
# File 'lib/cyborg/command.rb', line 50

def gem_build
  @production = true
  FileUtils.rm_rf('public')
  dispatch(:build)
  system "bundle exec rake build"
end

#gem_installObject



57
58
59
60
61
62
# File 'lib/cyborg/command.rb', line 57

def gem_install
  @production = true
  FileUtils.rm_rf('public')
  dispatch(:build)
  system "bundle exec rake install"
end

#gem_releaseObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cyborg/command.rb', line 64

def gem_release
  @production = true
  FileUtils.rm_rf('public')
  dispatch(:build)

  if key = ENV['RUBYGEMS_API_KEY']
    gem = "#{Cyborg.plugin.gem_name}-#{Cyborg.plugin.version}.gem"
    system "bundle exec rake build"
    system "curl --data-binary @#{gem} -H 'Authorization:#{key}' https://rubygems.org/api/v1/gems"
  else
    system 'bundle exec rake release'
  end
end

#production?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/cyborg/command.rb', line 46

def production?
  @production == true
end

#require_railsObject



78
79
80
# File 'lib/cyborg/command.rb', line 78

def require_rails
  require File.join(Dir.pwd, Cyborg.rails_path('config/application'))
end

#run(options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cyborg/command.rb', line 9

def run(options)
  @production = options[:production]

  if options[:help]
    version
    puts options[:help]
    return
  end

  case options[:command]
  when 'new', 'n'
    Scaffold.new(options)
  when 'build', 'b'
    from_root { dispatch(:build, options) }
  when 'watch', 'w'
    from_root { dispatch(:watch, options) }
  when 'server', 's'
    from_root { dispatch(:server, options) }
  when 'clean', 'c'
    from_root { clean }
  when 'version'
    version
  when 'gem:build'
    from_root { gem_build }
  when 'gem:install'
    from_root { gem_install }
  when 'gem:release'
    from_root { gem_release }
  else
    puts "Command `#{options[:command]}` not recognized"
  end
end

#server(options = {}) ⇒ Object

Run rails server and watch assets



118
119
120
121
122
# File 'lib/cyborg/command.rb', line 118

def server(options={})
  options[:port] ||= 3000
  @threads << Thread.new { system "#{Cyborg.rails_path('bin/rails')} server -p #{options[:port]}" }
  watch(options) if options[:watch]
end

#versionObject



42
43
44
# File 'lib/cyborg/command.rb', line 42

def version
  puts "Cyborg version #{Cyborg::VERSION}\n\n"
end

#watch(options = {}) ⇒ Object

Watch assets for changes and build



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cyborg/command.rb', line 105

def watch(options={})
  build(options)
  require 'listen'

  trap("SIGINT") { 
    puts "\nCyborg watcher stopped. Have a nice day!"
    exit! 
  }

  @threads.concat Cyborg.plugin.watch(options)
end