Module: Jiveapps::Helpers

Included in:
Command, Command::Base
Defined in:
lib/jiveapps/helpers.rb

Instance Method Summary collapse

Instance Method Details

#askObject

Ask user for input, trim the response



62
63
64
# File 'lib/jiveapps/helpers.rb', line 62

def ask
  gets.strip
end

#catch_args(*list) ⇒ Object



70
71
72
73
74
75
# File 'lib/jiveapps/helpers.rb', line 70

def catch_args(*list)
  while current_arg = args.shift
    instance_variable_set "@#{list.shift}", current_arg
  end
  raise Jiveapps::Command::CommandFailed, "Missing #{list.length} parameter#{list.length > 1 ? 's' : ''}: #{list.map{|l| '<' + l.to_s + '>'}.join(' ')}#{@usage}" if list.length > 0
end

#check_git_versionObject



132
133
134
135
136
137
# File 'lib/jiveapps/helpers.rb', line 132

def check_git_version
  minimum_git_version = Gem::Version.new("1.7")
  if user_git_version < minimum_git_version
    error("Running Git version #{user_git_version}. Please install Git #{minimum_git_version} or higher. http://git-scm.com/download")
  end
end

#confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object



29
30
31
32
# File 'lib/jiveapps/helpers.rb', line 29

def confirm(message="Are you sure you wish to continue? (y/n)?")
  display("#{message} ", false)
  ask.downcase == 'y'
end

#confirm_command(app = app) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jiveapps/helpers.rb', line 34

def confirm_command(app = app)
  if extract_option('--force')
    display("Warning: The --force switch is deprecated, and will be removed in a future release. Use --confirm #{app} instead.")
    return true
  end

  raise(Jiveapps::Command::CommandFailed, "No app specified.\nRun this command from app folder or set it adding --app <app name>") unless app

  confirmed_app = extract_option('--confirm', false)
  if confirmed_app
    unless confirmed_app == app
      raise(Jiveapps::Command::CommandFailed, "Confirmed app #{confirmed_app} did not match the selected app #{app}.")
    end
    return true
  else
    display "\n !    Potentially Destructive Action"
    display " !    To proceed, type \"#{app}\" or re-run this command with --confirm #{@app}"
    display "> ", false
    if ask.downcase != app
      display " !    Input did not match #{app}. Aborted."
      false
    else
      true
    end
  end
end

#debug(msg) ⇒ Object



104
105
106
107
108
# File 'lib/jiveapps/helpers.rb', line 104

def debug(msg)
  if debug_mode?
    puts "DEBUG: #{msg}"
  end
end

#debug_mode?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
# File 'lib/jiveapps/helpers.rb', line 93

def debug_mode?
  return @debug_mode if @debug_mode.nil? == false

  if args.include?('--debug')
    args.delete('--debug')
    @debug_mode = true
  else
    @debug_mode = false
  end
end

#display(msg, newline = true) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/jiveapps/helpers.rb', line 15

def display(msg, newline=true)
  if newline
    puts(msg)
  else
    print(msg)
    STDOUT.flush
  end
end

#display_oauth_services(oauth_services, app_name) ⇒ Object

Display Oauth Service list Example Output:

2 OAuth services for app-name

  1. “foo” Service

    Consumer Key:     bar
    Consumer Secret:  baz
    
  2. “foo2” Service

    Consumer Key:     bar
    Consumer Secret:  baz
    


148
149
150
151
152
153
154
155
156
157
# File 'lib/jiveapps/helpers.rb', line 148

def display_oauth_services(oauth_services, app_name)
  if oauth_services.empty?
    display "No OAuth Services for #{app_name}"
  else
    display "=== #{oauth_services.size} OAuth Service#{'s' if oauth_services.size > 1} for #{app_name}"
    oauth_services.each_with_index do |oauth_service, index|
      display "#{index+1}. \"#{oauth_service['name']}\" Service\n     Consumer Key:     #{oauth_service['key']}\n     Consumer Secret:  #{oauth_service['secret']}"
    end
  end
end

#error(msg) ⇒ Object



24
25
26
27
# File 'lib/jiveapps/helpers.rb', line 24

def error(msg)
  STDERR.puts(msg)
  exit 1
end

#get_app_prop_with_default(title, default = "") ⇒ Object

Prompt for an app property. If user enters a value, return it, otherwise return the default

Example:

get_app_prop_with_default("App Name", "foobarbaz")

Example Output:

Enter App Name or hit enter for default [foobarbaz]:


187
188
189
190
191
# File 'lib/jiveapps/helpers.rb', line 187

def get_app_prop_with_default(title, default="")
  display "  Enter #{title} or hit enter for default [#{default}]: ", false
  val = gets.strip
  val.blank? ? default : val
end

#get_or_set_git_prop(prop, title) ⇒ Object

Checks for existance of a git property. If it exists, return it. If it doesn’t, prompt for it, set it, and return it.

Example:

get_or_set_git_prop("--global user.name", "Author Name")


165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jiveapps/helpers.rb', line 165

def get_or_set_git_prop(prop, title)
  val = `git config #{prop}`.to_s.strip
  while val.blank?
    display "Enter #{title}: ", false
    val = gets.strip
    if val.blank?
      display "#{title} cannot be blank."
    else
      `git config #{prop} "#{val}"`
    end
  end
  val
end

#git_versionObject



121
122
123
# File 'lib/jiveapps/helpers.rb', line 121

def git_version
  `git --version`
end

#has_program?(program) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
# File 'lib/jiveapps/helpers.rb', line 110

def has_program?(program)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory|
    path = File.join(directory, program.to_s)
    if running_on_windows?
      File.exists?("#{path}.cmd") || File.exists?("#{path}.exe")
    else
      File.executable?(path)
    end
  end
end

#home_directoryObject



3
4
5
# File 'lib/jiveapps/helpers.rb', line 3

def home_directory
  running_on_windows? ? ENV['USERPROFILE'] : ENV['HOME']
end

#run(command, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jiveapps/helpers.rb', line 77

def run(command, options={})
  options[:exec] ||= false

  puts "DEBUG: $ #{command}" if debug_mode?

  if options[:exec] == true
    exec command
  else
    sh(command)
  end
end

#running_on_a_mac?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/jiveapps/helpers.rb', line 11

def running_on_a_mac?
  RUBY_PLATFORM =~ /-darwin\d/
end

#running_on_windows?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/jiveapps/helpers.rb', line 7

def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end

#sh(command) ⇒ Object



89
90
91
# File 'lib/jiveapps/helpers.rb', line 89

def sh(command)
  Shell.new(command).run
end

#usage(text) ⇒ Object



66
67
68
# File 'lib/jiveapps/helpers.rb', line 66

def usage(text)
  @usage = "\n  Usage:\n  $ #{text}"
end

#user_git_versionObject



125
126
127
128
129
130
# File 'lib/jiveapps/helpers.rb', line 125

def user_git_version
  return @git_version if @git_version
  error("Git not found. Please install Git 1.7 or higher. http://git-scm.com/download") unless has_program?("git")
  version_string = git_version[/\d{1,2}\.\d{1,2}/]
  @git_version = Gem::Version.new(version_string)
end