Module: SparkEngine::Command

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

Instance Method Summary collapse

Instance Method Details

#build(options = {}) ⇒ Object

Build assets



134
135
136
137
138
# File 'lib/spark_engine/command.rb', line 134

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

#cleanObject



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

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

#dispatch(command, *args) ⇒ Object

Handles running threaded commands



127
128
129
130
131
# File 'lib/spark_engine/command.rb', line 127

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

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



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/spark_engine/command.rb', line 160

def from_root(command=nil, &blk)
  unless SparkEngine.gem_path
    abort "Command must be run from the root of an engine (adjacent to the gemspec)."
  end

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

#gem_buildObject



68
69
70
71
72
73
74
# File 'lib/spark_engine/command.rb', line 68

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

#gem_bump(v) ⇒ Object



105
106
107
# File 'lib/spark_engine/command.rb', line 105

def gem_bump(v)
  system "bump #{v} --no-commit"
end

#gem_installObject



76
77
78
79
80
81
# File 'lib/spark_engine/command.rb', line 76

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

#gem_releaseObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spark_engine/command.rb', line 83

def gem_release
  @production = true
  gem_build
  spec = SparkEngine.plugin_spec
  gem_file = "./pkg/#{spec.name}-#{spec.version}.gem"

  if File.exists?(gem_file)
    system "git commit -m v#{spec.version}"
    gem_tag
    system "git push origin $(git rev-parse --abbrev-ref HEAD) --tag"

    if key = ENV['RUBYGEMS_API_KEY']
      gem = "#{spec.name}-#{spec.version}.gem"
      system "bundle exec rake build"
      system "curl --data-binary @./pkg/#{gem} -H 'Authorization:#{key}' https://rubygems.org/api/v1/gems"
    else
      system "gem push #{gem_file}"
      system "rm ./public/*.gz"
    end
  end
end

#gem_tagObject



109
110
111
# File 'lib/spark_engine/command.rb', line 109

def gem_tag
  system "git tag v#{SparkEngine.plugin_spec.version}"
end

#production?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/spark_engine/command.rb', line 64

def production?
  @production == true
end

#require_railsObject



113
114
115
116
# File 'lib/spark_engine/command.rb', line 113

def require_rails
  return if defined?(Rails)
  require File.join(Dir.pwd, SparkEngine.rails_path('config/application'))
end

#run(options) ⇒ Object



5
6
7
8
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spark_engine/command.rb', line 5

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

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

  if options[:clean] || SparkEngine.production?
    from_root { clean }
  end

  case options[:command]
  when 'new', 'n'
    require "spark_engine/scaffold"
    Scaffold.new(options)
  when 'generate', 'g'
    from_root { 
      require "spark_engine/scaffold"
      require_rails
      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 }
  when 'gem:tag'
    from_root { gem_tag }
  when 'gem:bump:patch'
    from_root { gem_bump('patch') }
  when 'gem:bump:minor'
    from_root { gem_bump('minor') }
  when 'gem:bump:major'
    from_root { gem_bump('major') }
  else
    puts "Command `#{options[:command]}` not recognized"
  end
end

#server(options = {}) ⇒ Object

Run rails server and watch assets



154
155
156
157
158
# File 'lib/spark_engine/command.rb', line 154

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

#versionObject



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

def version
  spec = SparkEngine.plugin_spec
  puts "spark_engine v #{SparkEngine::VERSION}"
  puts " - #{spec.name} v #{spec.version}\n" if spec
  puts ""
end

#watch(options = {}) ⇒ Object

Watch assets for changes and build



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/spark_engine/command.rb', line 141

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

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

  @threads.concat SparkEngine.load_plugin.watch(options)
end