Class: BPM::CLI::Base

Inherits:
Thor
  • Object
show all
Defined in:
lib/bpm/cli/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.help(shell, subcommand = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bpm/cli/base.rb', line 17

def help(shell, subcommand = false)
  shell.say <<LONGDESC
bpm (v#{BPM::VERSION}) - the browser package manager

BPM is a tool for aiding in development of JavaScript-based web applications.
It manages dependencies, custom file formats, minification and more.

Sample Usage:

  bpm init my_app
  cd my_app
  bpm add my_dependency
  bpm preview

LONGDESC

  super shell, subcommand
end

.start(given_args = ARGV, config = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bpm/cli/base.rb', line 54

def start(given_args=ARGV, config={})
  if given_args.include?('--verbose') || given_args.include?('-V')
    BPM.show_deprecations = true
  end

  super

  if BPM.deprecation_count > 0
    puts "[WARN] #{BPM.deprecation_count} deprecation warnings were hidden. Run with --verbose to see them."
  end
end

.task_help(shell, task_name) ⇒ Object

Hacked so long description isn’t wrapped



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

def task_help(shell, task_name)
  meth = normalize_task_name(task_name)
  task = all_tasks[meth]
  handle_no_task_error(meth) unless task

  shell.say "Usage:"
  shell.say "  #{banner(task)}"
  shell.say
  class_options_help(shell, nil => task.options.map { |_, o| o })
  if task.long_description
    shell.say "Description:"
    shell.print_wrapped(task.long_description, :ident => 2)
  else
    shell.say task.description
  end
end

Instance Method Details

#add(*package_names) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/bpm/cli/base.rb', line 150

def add(*package_names)
  # map to dependencies
  if package_names.empty?
    abort "You must specify at least one package"
  else
    if package_names.size > 1 && options[:version]
      abort "You can only name one package with the version option"
    end

    deps = {}
    package_names.each do |name|
      vers = options[:version] || (options[:prerelease] ? '>= 0.pre' : '>= 0')
      if name =~ /^(.+?)(-(\d[\w\.]*))?\.bpkg$/
        name = $1
        vers = $3 if $3
      end
      deps[name] = vers
    end
  end

  # find project
  project = find_project

  project.add_dependencies deps, options[:development], true
  project.build options[:mode], true
end

#debug(option) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
# File 'lib/bpm/cli/base.rb', line 464

def debug(option)
  case option
  when 'build'
    say JSON.pretty_generate find_project.build_settings(options[:mode].to_sym)
  when 'repair'
    say "Verifying and repairing project..."
    find_project.verify_and_repair options[:mode].to_sym, true
  else
    abort "Do not know how to display #{option}"
  end
end

#fetch(*packages) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bpm/cli/base.rb', line 90

def fetch(*packages)
  project = find_project(false) if packages.empty?
  if project
    success = project.fetch_dependencies options[:verbose]
    if !success
      abort project.errors * "\n"
    else
      say "Fetched dependent packages for #{project.name}"
    end

  else
    report_arity_error("fetch") and return if packages.size.zero?

    begin
      packages.each do |package|
        installed = BPM::Remote.new.install(package, options[:version], options[:prerelease])
        installed.each do |spec|
          say "Successfully fetched #{spec.name} (#{spec.version})"
        end
      end
    rescue LibGems::InstallError => e
      raise BPM::Error.new("Fetch error: #{e}")
    rescue LibGems::GemNotFoundException => e
      abort "Can't find package #{e.name} #{e.version} available for fetch"
    rescue Errno::EACCES, LibGems::FilePermissionError => e
      raise BPM::Error.new(e.message)
    end
  end
end

#fetched(*packages) ⇒ Object



122
123
124
125
126
# File 'lib/bpm/cli/base.rb', line 122

def fetched(*packages)
  local = BPM::Local.new
  index = local.installed(packages)
  print_specs(packages, index)
end

#help(*args) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/bpm/cli/base.rb', line 67

def help(*args)
  if args.first == "owner"
    CLI::Owner.start( [ "help" ] + args[1..-1] )
  else
    super
  end
end

#init(*paths) ⇒ Object

method_option :package, :type => :string, :default => nil, :desc => ‘Specify a package template to build from’



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/bpm/cli/base.rb', line 374

def init(*paths)
  paths = [Dir.pwd] if paths.empty?
  paths.map!{|p| File.expand_path(p) }

  if paths.length > 1 && options[:name]
    abort "Can't specify a name with multiple paths"
  end

  paths.each do |path|
    name = options[:name] || File.basename(path)

    # if someone specified both a name and path assume they meant
    # exactly what they said
    if name == File.basename(path)
      new_path = File.join(File.dirname(path), File.basename(path))
      path = new_path if !File.directory?(path)
    end

    if File.directory?(path)
      run_init(name, options[:app], path)
    else
      #package = install_package(options[:package])
      package = nil
      template_path = package ? package.template_path(:project) : nil
      generator = get_generator(:project, package)
      success = generator.new(self, name, path, template_path, package).run
      run_init(name, true, path, package) if success
    end
  end
end

#list(*packages) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/bpm/cli/base.rb', line 340

def list(*packages)
  if options[:remote]
    remote = BPM::Remote.new
    index  = remote.list_packages(packages, options[:all], options[:prerelease])
    print_specs(packages, index)
  else
    packages = nil if packages.size == 0
    project  = find_project
    project.verify_and_repair

    deps = options[:development] ? project.sorted_development_deps : project.sorted_runtime_deps
    deps.each do |dep|
      next if packages && !packages.include?(dep.name)
      say "#{dep.name} (#{dep.version})"
    end
  end
end

#loginObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/bpm/cli/base.rb', line 242

def 
  email = options[:username]
  password = options[:password]

  unless email && password
    require 'highline'
    highline = HighLine.new
    say "Enter your BPM credentials."

    begin
      email ||= highline.ask "\nEmail:" do |q|
        next unless STDIN.tty?
        q.readline = true
      end

      password ||= highline.ask "\nPassword:" do |q|
        next unless STDIN.tty?
        q.echo = "*"
      end
    rescue Interrupt => ex
      abort "Cancelled login."
    end
  end

  say "\nLogging in as #{email}..."

  if BPM::Remote.new.(email, password)
    say "Logged in!"
  else
    say "Incorrect email or password."
     unless options[:username] && options[:password]
  end
end

#pack(package_path = nil) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/bpm/cli/base.rb', line 415

def pack(package_path=nil)
  package_path ||= Dir.pwd
  local = BPM::Local.new
  package = local.pack(File.join(package_path, "package.json"), options[:email])

  if package.errors.empty?
    say "Successfully built package: #{package.file_name}"
  else
    failure_message = "BPM encountered the following problems building your package:"
    package.errors.each do |error|
      failure_message << "\n* #{error}"
    end
    abort failure_message
  end
end

#previewObject



215
216
217
218
219
# File 'lib/bpm/cli/base.rb', line 215

def preview
  project = find_project
  project.verify_and_repair options[:mode], options[:verbose]
  BPM::Server.start project, :Port => options[:port], :mode => options[:mode].to_sym
end

#push(package) ⇒ Object



284
285
286
287
288
289
290
291
# File 'lib/bpm/cli/base.rb', line 284

def push(package)
  remote = BPM::Remote.new
  if remote.logged_in?
    say remote.push(package)
  else
    say LOGIN_MESSAGE
  end
end

#rebuildObject



234
235
236
237
# File 'lib/bpm/cli/base.rb', line 234

def rebuild
  find_project.fetch_dependencies(true) if options[:update]
  find_project.build options[:mode].to_sym, true
end

#remove(*package_names) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/bpm/cli/base.rb', line 187

def remove(*package_names)

  # map to dependencies
  if package_names.size.zero?
    abort "You must specify at least one package"
  end

  project = find_project
  project.unbuild options[:verbose]
  project.remove_dependencies package_names, true
  project.build options[:mode], true
end

#unpack(*paths) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/bpm/cli/base.rb', line 440

def unpack(*paths)
  local = BPM::Local.new

  paths.each do |path|
    begin
      package     = local.unpack(path, options[:target])
      unpack_path = File.expand_path(File.join(Dir.pwd, options[:target], package.full_name))
      say "Unpacked package into: #{unpack_path}"
    rescue Errno::EACCES, LibGems::FilePermissionError => ex
      abort "There was a problem unpacking #{path}:\n#{ex.message}"
    end
  end
end

#yank(package) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/bpm/cli/base.rb', line 304

def yank(package)
  if options[:version]
    remote = BPM::Remote.new
    if remote.logged_in?
      if options[:undo]
        say remote.unyank(package, options[:version])
      else
        say remote.yank(package, options[:version])
      end
    else
      say LOGIN_MESSAGE
    end
  else
    say "Version required"
  end
end