Module: Bundlegem

Defined in:
lib/bundlegem.rb,
lib/bundlegem/cli.rb,
lib/bundlegem/cli/gem.rb,
lib/bundlegem/strings.rb,
lib/bundlegem/version.rb,
lib/bundlegem/configurator.rb,
lib/bundlegem/friendly_errors.rb,
lib/bundlegem/template_manager.rb

Defined Under Namespace

Classes: CLI, Configurator, TemplateManager

Constant Summary collapse

HELP_MSG =
<<-HEREDOC
                  BundleGem version #{Bundlegem::VERSION}
Use bundlegem to start a new project folder based on a predefined template.

Usage Examples:

# Make a new ruby gem
$  bundlegem your_gem_name

# List available teplates
$  bundlegem --list

# Create a ruby gem project using the built in service template
$ bundlegem your_gem_name -t service

# Download all my template files (configured in ~/.bundlegem/config)
$ bundlegem --install-best-templates

# not implemented, should create a new gem template in ~/.bundlegem/templates
# that you'll love customizing to your personal preference
$ bundlegem --newtemplate

$ bundlegem --help              # shows this message
HEREDOC
VERSION =
"0.0.14"

Class Method Summary collapse

Class Method Details

.convert_grouped_hashes_to_output(available_templates) ⇒ Object

input: [ { “predefined” => [“default”, “service”] },

  { "MISC" => ["my_thing"] }
]


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bundlegem.rb', line 99

def convert_grouped_hashes_to_output(available_templates)
  s = ""
  available_templates.each do |hash|
    k = hash.first.upcase
    a = hash.last
    s << " #{k}:\n"
    a.each do |el|
      s << "   #{el}\n"
    end
    s << "\n"
  end
  s
end

.gem(options, gem_name) ⇒ Object



56
57
58
59
60
61
# File 'lib/bundlegem.rb', line 56

def gem(options, gem_name)
  require 'bundlegem/cli'
  require 'bundlegem/cli/gem'

  Bundlegem::CLI::Gem.new(options, gem_name).run
end

.group_hashes_by_key(available_templates) ⇒ Object

input: [ { “predefined” => “default” },

  { "MISC" => "my_thing" },
  { "prdefined" => "service" }
]

output: [ { “predefined” => [“default”, “service”] },

  { "MISC" => ["my_thing"] }
]


85
86
87
88
89
90
91
92
93
94
# File 'lib/bundlegem.rb', line 85

def group_hashes_by_key(available_templates)
  h = {}
  available_templates.each do |hash|
    k = hash.first.first
    v = hash.first.last
    h[k] = [] unless h.has_key?(k)
    h[k] << v
  end
  h
end

.install_best_templatesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bundlegem.rb', line 38

def install_best_templates
  configurator = Configurator.new
  config_file_data = configurator.config_file_data
  puts "Downloading templates from the following locations: \n  #{config_file_data['best_templates'].split(" ").join("\n  ")}"
  config_file_data['best_templates'].split.each do |url|
    uri = URI.parse(url)
    template_folder_name = File.basename(uri.path).sub(/\.git$/, "")
    if !File.exist?("#{ENV['HOME']}/.bundlegem/templates/#{template_folder_name}")
      cmd = "cd #{ENV['HOME']}/.bundlegem/templates && git clone #{url}"
      cmd += " 2> /dev/null" if $test_env
      `#{cmd}`
    else
      # TODO:
      # Prompt to update the repo if they have a clean working state.
    end
  end
end

.issues_url(exception) ⇒ Object



74
75
76
77
# File 'lib/bundlegem/friendly_errors.rb', line 74

def self.issues_url(exception)
  'TODO: Change the link that was here' \
  "#{CGI.escape(exception.message)}&type=Issues"
end

.listObject

lists available templates



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bundlegem.rb', line 19

def list
  configurator = Configurator.new
  # search through builtin
  available_templates = [ { "predefined" => "newgem" },
                          { "predefined" => "c_extension_gem" },
                          { "predefined" => "cli_gem" }]

  # search through user downloaded
  available_templates += configurator.user_downloaded_templates

  # search through user defined
  available_templates += configurator.user_defined_templates

  available_templates = group_hashes_by_key(available_templates)
  output_string = convert_grouped_hashes_to_output(available_templates)

  mark_default_template(output_string, configurator.default_template)
end

.mark_default_template(output_string, default_template) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/bundlegem.rb', line 113

def mark_default_template(output_string, default_template)
  output_string.lines.reverse.map do |l|
    if l.strip == default_template
      l[1]= "*"
      "#{l.chomp}       (default)\n"
    else
      l
    end
  end.reverse.join
end

.new_template(args) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/bundlegem.rb', line 63

def new_template(args)
  template_name = args[1]
  template_name = prompt_for_template_name if template_name.nil?

  # Copy newgem from within the repo to ~/.bundlegem/templates/#{template_name}
  TemplateManager.create_new_template(template_name)
end

.prompt_for_template_nameObject



71
72
73
74
# File 'lib/bundlegem.rb', line 71

def prompt_for_template_name
  puts "Please specify a name for your template:  "
  template_name = STDIN.gets.chomp.strip.gsub(" ", "_")
end

.request_issue_report_for(e) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bundlegem/friendly_errors.rb', line 45

def self.request_issue_report_for(e)
  Bundlegem.ui.info <<-EOS.gsub(/^ {6}/, '')
    #{'――― ERROR REPORT TEMPLATE ―――――――――――――――――――――――――――――――――――――――――――――――――――――――'}
    - What did you do?
    - What did you expect to happen?
    - What happened instead?

    Error details

        #{e.class}: #{e.message}
        #{e.backtrace.join("\n          ")}

    #{Bundlegem::Env.new.report(:print_gemfile => false).gsub(/\n/, "\n      ").strip}
    #{'――― TEMPLATE END ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――'}

  EOS

  Bundlegem.ui.error "Unfortunately, an unexpected error occurred, and Bundlegem cannot continue."

  Bundlegem.ui.warn <<-EOS.gsub(/^ {6}/, '')

    First, try this link to see if there are any existing issue reports for this error:
    #{issues_url(e)}

    If there aren't any reports for this error yet, please create copy and paste the report template above into a new issue. Don't forget to anonymize any private data! The new issue form is located at:
    TODO: Change the link that was here
  EOS
end

.versionObject



14
15
16
# File 'lib/bundlegem.rb', line 14

def version
  Bundlegem::VERSION
end

.which(executable) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bundlegem.rb', line 124

def which(executable)
  if File.file?(executable) && File.executable?(executable)
    executable
  elsif ENV['PATH']
    path = ENV['PATH'].split(File::PATH_SEPARATOR).find do |p|
      abs_path = File.join(p, executable)
      File.file?(abs_path) && File.executable?(abs_path)
    end
    path && File.expand_path(executable, path)
  end
end

.with_friendly_errorsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bundlegem/friendly_errors.rb', line 6

def self.with_friendly_errors
  yield
rescue Bundlegem::Dsl::DSLError => e
  Bundlegem.ui.error e.message
  exit e.status_code
rescue Bundlegem::BundlegemError => e
  Bundlegem.ui.error e.message, :wrap => true
  Bundlegem.ui.trace e
  exit e.status_code
rescue Thor::AmbiguousTaskError => e
  Bundlegem.ui.error e.message
  exit 15
rescue Thor::UndefinedTaskError => e
  Bundlegem.ui.error e.message
  exit 15
rescue Thor::Error => e
  Bundlegem.ui.error e.message
  exit 1
rescue LoadError => e
  raise e unless e.message =~ /cannot load such file -- openssl|openssl.so|libcrypto.so/
  Bundlegem.ui.error "\nCould not load OpenSSL."
  Bundlegem.ui.warn <<-WARN, :wrap => true
    You must recompile Ruby with OpenSSL support or change the sources in your \
    Gemfile from 'https' to 'http'. Instructions for compiling with OpenSSL \
    using RVM are available at http://rvm.io/packages/openssl.
  WARN
  Bundlegem.ui.trace e
  exit 1
rescue Interrupt => e
  Bundlegem.ui.error "\nQuitting..."
  Bundlegem.ui.trace e
  exit 1
rescue SystemExit => e
  exit e.status
rescue Exception => e
  request_issue_report_for(e)
  exit 1
end