Module: CLIHelper

Included in:
ProjectUtility
Defined in:
lib/joe_utils/helpers/cli_helper.rb

Constant Summary collapse

MESSAGES =
{
    add_git: 'Adding to git...',
    commit_git: 'Commiting to git...',
    push_rubygems: 'Pushing to rubygems.org...',
    install_gem: 'Installing gem and jgem locally...',
    build_gem: 'Building gem...',
    build_gem_failed: 'The gem build failed. Please confirm the gem name and try again.',
    deploy_heroku: 'Deploying to heroku.',
    commit_github: 'Commiting to github.',
    commit_default_message: 'Commit by script',
    rake_test: 'Running tests...',
}

Instance Method Summary collapse

Instance Method Details

#build_gem(gem_name, options = {}) ⇒ Boolean

Builds the named gem locally, in case of provided options pushes and installs the gem

Parameters:

  • gem_name (String)
  • options, (Hash)

    supported options: :push for pushing to rubygems.org, :install for installing the gem locally

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/joe_utils/helpers/cli_helper.rb', line 27

def build_gem(gem_name, options = {})
  puts MESSAGES[:build_gem]
  success = true
  puts(gem_built_name = `gem build "#{gem_name}.gemspec"`)
  gem_built_name = gem_built_name.match(/File: /).post_match
  if gem_built_name && !gem_built_name.empty?
    success = push_rubygems(gem_built_name) if options[:push]
    options[:install] ? install_gem(gem_built_name) && install_jgem(gem_built_name) && success : success
  else
    puts MESSAGES[:build_gem_failed]
  end
end

#commit_git(message) ⇒ Boolean

Commits to git

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/joe_utils/helpers/cli_helper.rb', line 18

def commit_git(message)
    execute(:add_git, 'git add -u') &&
        execute(:commit_git, "git commit -m \"#{message || MESSAGES[:commit_default_message]}\"")
end

#commit_githubBoolean

Returns:

  • (Boolean)


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

def commit_github
  execute(:commit_github, 'git push origin master')
end

#deploy_herokuBoolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/joe_utils/helpers/cli_helper.rb', line 67

def deploy_heroku
  execute(:deploy_heroku, 'git push heroku master')
end

#execute(message_key, command, no_success_pattern = nil) ⇒ Boolean

Parameters:

  • message_key (Symbol)
  • command (String)

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/joe_utils/helpers/cli_helper.rb', line 79

def execute(message_key, command, no_success_pattern = nil)
  puts MESSAGES[message_key] if message_key
  puts (result = `#{command}`)
  result !~ no_success_pattern || RegExpHelper::NO_SUCCESS
end

#install_gem(gem_built_name) ⇒ Boolean

Installs the named gem locally

Parameters:

  • gem_built_name (String)

Returns:

  • (Boolean)


50
51
52
# File 'lib/joe_utils/helpers/cli_helper.rb', line 50

def install_gem(gem_built_name)
  execute(:install_gem, "gem install #{gem_built_name}")
end

#install_jgem(gem_built_name) ⇒ Boolean

Installs the named jgem locally

Parameters:

  • gem_built_name (String)

Returns:

  • (Boolean)


57
58
59
# File 'lib/joe_utils/helpers/cli_helper.rb', line 57

def install_jgem(gem_built_name)
  execute(:install_jgem, "jgem install #{gem_built_name}")
end

#push_rubygems(gem_built_name) ⇒ Boolean

Pushes the named gem to rubygems.org

Parameters:

  • gem_built_name (String)

Returns:

  • (Boolean)


43
44
45
# File 'lib/joe_utils/helpers/cli_helper.rb', line 43

def push_rubygems(gem_built_name)
  execute(:push_rubygems, "gem push #{gem_built_name}")
end

#rake_testBoolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/joe_utils/helpers/cli_helper.rb', line 72

def rake_test
  execute(:rake_test, 'rake test')
end