Class: Jim::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/jim/cli.rb

Overview

CLI handles the command line interface for the ‘jim` binary. It is a `Thor` application.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

create a new instance with the args passed from the command line i.e. ARGV



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jim/cli.rb', line 38

def initialize(*)
  super
  if options[:version]
    say "jim #{Jim::VERSION}", :red
    exit
  end
  # set the default jimhome
  self.jimhome = Pathname.new(options[:jimhome] || ENV['JIMHOME'] || '~/.jim').expand_path
  # parse the options
  self.jimfile = Pathname.new(options[:jimfile] || 'Jimfile').expand_path
  self.force = options[:force]
  self.debug = self.class.respond_to?(:debugging) ? self.class.debugging : options[:debug]
  logger.level = Logger::DEBUG if self.debug
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



9
10
11
# File 'lib/jim/cli.rb', line 9

def debug
  @debug
end

#forceObject

Returns the value of attribute force.



9
10
11
# File 'lib/jim/cli.rb', line 9

def force
  @force
end

#jimfileObject

Returns the value of attribute jimfile.



9
10
11
# File 'lib/jim/cli.rb', line 9

def jimfile
  @jimfile
end

#jimhomeObject

Returns the value of attribute jimhome.



9
10
11
# File 'lib/jim/cli.rb', line 9

def jimhome
  @jimhome
end

Instance Method Details

#available(search = nil) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/jim/cli.rb', line 147

def available(search = nil)
  say "Getting list of all available files in\n#{index.directories.join("\n")}"
  say "Searching for '#{search}'" if search
  list = index.list(search)
  say "Available:"
  print_version_list(list)
end

#bundle(bundle_name = nil) ⇒ Object



95
96
97
# File 'lib/jim/cli.rb', line 95

def bundle(bundle_name = nil)
  make_bundle(bundle_name, false)
end

#compress(bundle_name = nil) ⇒ Object



119
120
121
# File 'lib/jim/cli.rb', line 119

def compress(bundle_name = nil)
  make_bundle(bundle_name, true)
end

#init(dir = nil) ⇒ Object



55
56
57
58
59
# File 'lib/jim/cli.rb', line 55

def init(dir = nil)
  dir = Pathname.new(dir || '')
  jimfile_path = dir + 'Jimfile'
  template('jimfile', jimfile_path)
end

#install(url, name = false, version = false) ⇒ Object



73
74
75
# File 'lib/jim/cli.rb', line 73

def install(url, name = false, version = false)
  Jim::Installer.new(url, jimhome, :force => force, :name => name, :version => version).install
end

#list(search = nil) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/jim/cli.rb', line 134

def list(search = nil)
  say "Getting list of installed files in"
  say("#{installed_index.directories.join(':')}", :yellow)
  say("Searching for '#{search}'", :yellow) if search
  list = installed_index.list(search)
  say "Installed:"
  print_version_list(list)
end

#pack(dir = nil) ⇒ Object



203
204
205
206
207
208
# File 'lib/jim/cli.rb', line 203

def pack(dir = nil)
  say "Packing the Jimfile for this project"
  invoke :vendor, [dir]
  invoke :bundle
  invoke :compress
end

#remove(name, version = nil) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/jim/cli.rb', line 158

def remove(name, version = nil)
  say "Looking for files matching #{name} #{version}"
  files = installed_index.find_all(name, version)
  if files.length > 0
    say "Found #{files.length} matching files"
    removed = 0
    files.each do |filename|
      do_remove = yes?("Remove #{filename}?", :red)
      if do_remove
        say "Removing #{filename}"
        filename.delete
        removed += 1
      else
        say "Skipping #{filename}", :yellow
      end
    end
    say "Removed #{removed} files."
  else
    say "No installed files matched."
  end
end

#resolveObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/jim/cli.rb', line 184

def resolve
  resolved = bundler.resolve!
  say "Files:"
  resolved.each do |bundle_name, requirements|
    say bundle_name, :green
    say "-----------------------", :green
    requirements.each do |path, name, version|
      say [name, version, path].join(" | ") + "\n"
    end
  end
  resolved
rescue Jim::Error => e
  say e.message, :red
end

#update_jimfile(dir = nil) ⇒ Object



241
242
243
244
245
246
# File 'lib/jim/cli.rb', line 241

def update_jimfile(dir = nil)
  dir = Pathname.new(dir || '')
  bundler
  copy_file(dir + 'Jimfile', dir + 'Jimfile.old')
  create_file(dir + 'Jimfile', bundler.jimfile_to_json)
end

#vendor(dir = nil) ⇒ Object



125
126
127
128
129
130
# File 'lib/jim/cli.rb', line 125

def vendor(dir = nil)
  dir = bundler.vendor!(dir, force)
  say("Vendored files to #{dir}", :green)
rescue Jim::Error => e
  say e.message, :red
end

#watch(dir = nil) ⇒ Object



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
# File 'lib/jim/cli.rb', line 213

def watch(dir = nil)
  require 'listen'
  run_update = lambda {|type, path|
    unless bundler.bundle_paths.any? {|p| path.include?(p) }
      say("--> #{path} #{type}")
      system "jim bundle"
    end
  }
  say "Now watching JS files..."
  run_update["started", 'Jimfile']
  dir ||= Dir.pwd
  Listen.to(File.expand_path(dir), :filter => /(\.js$|Jimfile)/) do |modified, added, removed|
    modified.each do |relative|
      run_update["changed", relative]
    end

    added.each do |relative|
      run_update["created", relative]
    end

    removed.each do |relative|
      run_update["deleted", relative]
    end
  end
end