Class: Jiveapps::Command::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/jiveapps/commands/base.rb

Direct Known Subclasses

App, Auth, BaseWithApp, Help, Keys

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#ask, #catch_args, #check_git_version, #confirm, #confirm_command, #debug, #debug_mode?, #display, #display_oauth_services, #error, #get_app_prop_with_default, #get_or_set_git_prop, #git_version, #has_program?, #home_directory, #run, #running_on_a_mac?, #running_on_windows?, #sh, #usage, #user_git_version

Constructor Details

#initialize(args, jiveapps = nil) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
# File 'lib/jiveapps/commands/base.rb', line 7

def initialize(args, jiveapps=nil)
  @args = args
  @jiveapps = jiveapps
  @autodetected_app = false
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



5
6
7
# File 'lib/jiveapps/commands/base.rb', line 5

def args
  @args
end

#autodetected_appObject (readonly)

Returns the value of attribute autodetected_app.



6
7
8
# File 'lib/jiveapps/commands/base.rb', line 6

def autodetected_app
  @autodetected_app
end

Instance Method Details

#extract_app(force = true) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
# File 'lib/jiveapps/commands/base.rb', line 17

def extract_app(force=true)
  app = extract_option('--app', false)
  raise(CommandFailed, "You must specify an app name after --app") if app == false
  unless app
    app = extract_app_in_dir(Dir.pwd) ||
    raise(CommandFailed, "No app specified.\nRun this command from app folder or set it adding --app <app name>") if force
    @autodetected_app = true
  end
  app
end

#extract_app_from_git_configObject



47
48
49
50
# File 'lib/jiveapps/commands/base.rb', line 47

def extract_app_from_git_config
  remote = %x{ git config jiveapps.remote }.strip
  remote == "" ? nil : remote
end

#extract_app_in_dir(dir) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jiveapps/commands/base.rb', line 28

def extract_app_in_dir(dir)
  return unless remotes = git_remotes(dir)

  if remote = extract_option('--remote')
    remotes[remote]
  elsif remote = extract_app_from_git_config
    remotes[remote]
  else
    apps = remotes.values.uniq
    case apps.size
      when 0; return nil
      when 1; return apps.first
      else
        current_dir_name = dir.split('/').last.downcase
        apps.select { |a| a.downcase == current_dir_name }.first
    end
  end
end

#extract_option(options, default = true) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jiveapps/commands/base.rb', line 72

def extract_option(options, default=true)
  values = options.is_a?(Array) ? options : [options]
  return unless opt_index = args.select { |a| values.include? a }.first
  opt_position = args.index(opt_index) + 1
  if args.size > opt_position && opt_value = args[opt_position]
    if opt_value.include?('--')
      opt_value = nil
    else
      args.delete_at(opt_position)
    end
  end
  opt_value ||= default
  args.delete(opt_index)
  block_given? ? yield(opt_value) : opt_value
end

#git_remotes(base_dir) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jiveapps/commands/base.rb', line 52

def git_remotes(base_dir)
  git_config = "#{base_dir}/.git/config"
  unless File.exists?(git_config)
    parent = base_dir.split('/')[0..-2].join('/')
    return git_remotes(parent) unless parent.empty?
  else
    remotes = {}
    current_remote = nil
    File.read(git_config).split(/\n/).each do |l|
      current_remote = $1 if l.match(/\[remote \"([\w\d-]+)\"\]/)
      app = (l.match(/url = git@#{jiveapps.git_host}:([\w\d-]+)\.git/) || [])[1]
      if current_remote && app
        remotes[current_remote.downcase] = app
        current_remote = nil
      end
    end
    return remotes
  end
end

#jiveappsObject



13
14
15
# File 'lib/jiveapps/commands/base.rb', line 13

def jiveapps
  @jiveapps ||= Jiveapps::Command.run_internal('auth:client', args)
end