Sha256: 5a7c16579f4f68f883939f85ae85ed17d3929ab19622e3164aec2c51226f2e6b

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

# Utility methods.

# This module is used inside the `katapult` binary and thus should not
# require any gems in order to prevent version conflicts

require_relative '../../katapult/version'
require 'bundler'
require 'io/console'

module Katapult
  module BinaryUtil
    extend self

    def git_commit(message, options = nil)
      message.gsub! /'/, "" # remove single quotes
      system "git add --all; git commit -m '#{ message }' --author='katapult <katapult@makandra.com>' #{ options }"
    end

    def create_rails_app(name)
      version = Katapult::RAILS_VERSION
      options = %w[
        --database postgresql
        --webpack

        --skip-test
        --skip-system-test
        --skip-turbolinks
      ]

      success = run "rails _#{version}_ new #{name} " + options.join(' ')
      success or fail 'Failed to create Rails app'
    end

    def pink(*args, linefeed: true)
      message = "> #{ args.join ' ' }"
      message.prepend($/) if linefeed
      message << (linefeed ? $/ : ' ')

      pink_message = "\e[35m#{ message }\e[0m"
      print pink_message
    end

    def ask(question)
      pink(question, linefeed: false)
      gets.chomp
    end

    # With clean Bundler env
    def run(command)
      success = Bundler.with_clean_env { system command }
      success or fail 'Something went wrong'
    end

    def job(do_something, done = 'Done.', &job)
      pink "About to #{do_something}. [C]ontinue, [s]kip or [e]xit?"

      case $stdin.getch
      when 's' then puts('Skipped.')
      when 'e' then fail('Cancelled.')
      else
        job.call
        puts done
      end
    end

    def fail(message)
      puts "x #{message}"
      exit(1)
    end

    def snake_case(string)
      string.gsub(/([a-z])([A-Z])/,'\1_\2').downcase
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
katapult-0.5.0 lib/katapult/support/binary_util.rb
katapult-0.4.1 lib/katapult/support/binary_util.rb
katapult-0.4.0 lib/katapult/support/binary_util.rb