Class: Linguist::Command::Base

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

Direct Known Subclasses

Auth, BaseWithApp, Collaborator, Help, Project, Translations, Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#ask, #confirm, #confirm_command, #deprecate, #display, #error, #format_date, #git, #has_git?, #home_directory, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #shell

Constructor Details

#initialize(args, linguist = nil) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
# File 'lib/linguist_ruby/commands/base.rb', line 10

def initialize(args, linguist=nil)
  @args             = args
  @linguist         = linguist
  @autodetected_project_name = false
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



7
8
9
# File 'lib/linguist_ruby/commands/base.rb', line 7

def args
  @args
end

#autodetected_appObject (readonly)

Returns the value of attribute autodetected_app.



8
9
10
# File 'lib/linguist_ruby/commands/base.rb', line 8

def autodetected_app
  @autodetected_app
end

Instance Method Details

#app_urls(name) ⇒ Object



97
98
99
# File 'lib/linguist_ruby/commands/base.rb', line 97

def app_urls(name)
#      "#{web_url(name)} | #{git_url(name)}"
end

#escape(value) ⇒ Object



101
102
103
# File 'lib/linguist_ruby/commands/base.rb', line 101

def escape(value)
  linguist.escape(value)
end

#extract_app_from_git_configObject



55
56
57
58
# File 'lib/linguist_ruby/commands/base.rb', line 55

def extract_app_from_git_config
  remote = git("config heroku.remote")
  remote == "" ? nil : remote
end

#extract_option(options, default = true) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/linguist_ruby/commands/base.rb', line 77

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

#extract_project_title_from_argsObject

Raises:



30
31
32
33
34
# File 'lib/linguist_ruby/commands/base.rb', line 30

def extract_project_title_from_args
  project_title = extract_option('--project', false)
  raise(CommandFailed, "You must specify a project title after --project") if project_title == false
  project_title
end

#extract_project_title_from_dir_nameObject



36
37
38
39
# File 'lib/linguist_ruby/commands/base.rb', line 36

def extract_project_title_from_dir_name
  dir = Dir.pwd
  File.basename(dir)
end

#extract_project_title_from_gitObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/linguist_ruby/commands/base.rb', line 41

def extract_project_title_from_git
  dir = Dir.pwd
  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
    return apps.first if apps.size == 1
  end
end

#git_remotes(base_dir = Dir.pwd) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/linguist_ruby/commands/base.rb', line 60

def git_remotes(base_dir=Dir.pwd)
  remotes      = { }
  original_dir = Dir.pwd
  Dir.chdir(base_dir)

  # TODO
#      git("remote -v").split("\n").each do |remote|
#        name, url, method = remote.split(/\s/)
#        if url =~ /^git@#{heroku.host}:([\w\d-]+)\.git$/
#          remotes[name] = $1
#        end
#      end

  Dir.chdir(original_dir)
  remotes
end

#git_url(name) ⇒ Object



93
94
95
# File 'lib/linguist_ruby/commands/base.rb', line 93

def git_url(name)
  "git@#{heroku.host}:#{name}.git"
end

#linguistObject



16
17
18
# File 'lib/linguist_ruby/commands/base.rb', line 16

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

#project(title = nil) ⇒ Object



105
106
107
108
# File 'lib/linguist_ruby/commands/base.rb', line 105

def project(title=nil)
  title ||= project_title
  @project ||= linguist.project(title) 
end

#project_title(force = true) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/linguist_ruby/commands/base.rb', line 20

def project_title(force=true)
  project_title = extract_project_title_from_args
  unless project_title
    project_title = extract_project_title_from_git || extract_project_title_from_dir_name ||
      raise(CommandFailed, "No project specified.\nRun this command from project folder or set it adding --project <title>") if force
    @autodetected_project_name = true
  end
  project_title
end