bundlegem 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43a7ecbc5c9caf18ef4fd7dfd978a173d9b6aecb
4
- data.tar.gz: 03d52dc639b8a2c2fa3ee266810911a808f5da2a
3
+ metadata.gz: dbdb68afebfbeb334228842740af8526fa5504cc
4
+ data.tar.gz: 42d17ac31d85fa50bdcfef6c5a1bbef3b864546a
5
5
  SHA512:
6
- metadata.gz: 60ecbdf011e4fea8998857e06b49af05f66ed26d301c651766680056bf6b96fc1f8d5fa61ac062465658ee8ef910594e9b74347c611fc56fb838960baa490316
7
- data.tar.gz: 3c830a24a7fd96398cd6093e4d215598aa87adcd843fe4d89f66e4a10f365a793bbfb0697b12d38786039ffd546710718bbf5ecb40043e6a046ee61a46c46a1c
6
+ metadata.gz: 66bc0d28dfe1a4ec6515e2bfbf9e9a7c82f273b3e8d6fbbfa73f35adb083c6b26f974b6d603b9754d8bbca55e3bfe7bdd8f47e5dd13fcf7945ea66d5c88c202a
7
+ data.tar.gz: 570a2bf84fac4530e10884ecef11008d6555e8a7b3736f6128c37d90d28144380254488c9741168209a97500d5c0b15381c6358853e06b0c1168b0660c8b3f10
data/README.md CHANGED
@@ -1,13 +1,12 @@
1
+ # BundleGem: A Gem Project Generator with User Defined Templates
1
2
 
2
- # BundleGem: a gem project genorator with user defined templates
3
+ I've more or less taken the code out of Bundler's `bundle gem` command, expanded on it and made it available as this repos (so credits to Bundler team).
3
4
 
4
- I've more or less taken the code out of Bundler's `bundle gem` command, expanded on it and made it available as this repos.
5
+ The goal to the project is to allow users to define templates in the most native form for all technologist: Directory Structures, short command, and helpful commands which make the gem's usage completely visible!
5
6
 
6
- The benefits of using this repo to create gems rather than bundler is that you can choose to create 'classes' of gems.
7
- By classes, I mean that there are different typs of 'gems', there are basic library gems, that are just code and can only be used from other code, there are command line application gems, those are gems that are run on the command line and include a binary file, and there are also web interface gems, those are gems which spin up a simple web interface and request that the user connect to it's server on what ever port it has spun up on.
7
+ The benefits of using this repo to create gems rather than bundler is that you can choose to create 'classes' of gems. By classes, I mean that there are different typs of 'gems', there are basic library gems, that are just code and can only be used from other code, there are command line application gems, those are gems that are run on the command line and include a binary file, and there are also web interface gems, those are gems which spin up a simple web interface and request that the user connect to it's server on what ever port it has spun up on. Depending on your field of specialty, you may be able to imagine other classes of gems that will aid you in your work.
8
8
 
9
- All of these 'classes of gems' as I refer to them, start out with a different code base, consistent with all other gems of there class.
10
- This 'class based' aproach to gem creation is different from the addative approach that other gem genorators are based on.
9
+ All of these 'classes of gems' as I refer to them, start out with a different code base, consistent with all other gems of the same class. This 'class based' aproach to gem creation is different from the addative approach that other gem genorators are based on.
11
10
 
12
11
  The most benificial aspect of this gem is that it allows users to specify exactly how they want their 'default starting gem' to look like, rather than rely on what someone else thought their default starting gem should look like.
13
12
 
@@ -18,13 +17,30 @@ First install it:
18
17
  gem install bundlegem
19
18
  ```
20
19
 
21
- Then create a new project structure:
20
+ Then create a new template for a gem class you expect to use more than once:
22
21
  ```
23
- $ bundlegem template new
22
+ $ bundlegem --newtemplate
24
23
  Specify a name for your gem template: my_service
25
- Cloning base project structure into ~/.bundlegem/templates/my_service
24
+ Specify description:
25
+ Specify template tag name [MISC]:
26
+ Cloning base project structure into ~/.bundlegem/gem_templates/my_service
26
27
  ...
27
- Complete!
28
+ Complete!
29
+ ```
30
+ You can now get to work making changes to the my_service gem. All you need to know is that any file that ends in .tt in that folder will be copied into new projects when you create a new project based on that template, however the .tt extension will obviously be stripped out of the resulting file name. Also, you can specify the `category` of the gem by editing the .bundlegem file in each template's root. Categories are just used for organizing the output when you run `bundlegem --list`. Here, I'll show you an example:
31
+
32
+ ```
33
+ $ bundlegem --list
34
+ PREDEFINED:
35
+ default - A basic ruby gem
36
+ service - A gem that allows installation as a service to run as a daemon
37
+
38
+ MISC:
39
+ my_service -
40
+
41
+ EMBEDDED:
42
+ arduino
43
+
28
44
  ```
29
45
 
30
46
  You'll now find a project skeleton in ~/.bundlegem/templates/my_service that you can customize to your liking.
data/bin/bundlegem CHANGED
@@ -29,6 +29,11 @@ if ARGV[0] == "--help"
29
29
  exit 0
30
30
  end
31
31
 
32
+ if ARGV[0] == "--list" or ARGV[0] == "-l"
33
+ puts Bundlegem.list
34
+ exit 0
35
+ end
36
+
32
37
  if ARGV[0] == "--newtemplate"
33
38
  puts "Sorry, I haven't been implemented, I don't have a lot of free time atm :("
34
39
  exit 0
data/changelog CHANGED
@@ -4,11 +4,21 @@
4
4
  * CLI with web client
5
5
  - Restore the original UI that was in bundler
6
6
  - Ability to load templates from github
7
- - CMD: --list to list templates (with descriptions)
8
7
  - CMD: --newtemplate to start new template
9
- - Behavior: gems in the ~/.bundlegem/templates folder take precidence over
10
- built in gems
8
+ - CMD: --templateize-current-directory to make the current directory into a
9
+ new template
10
+ - Behavior: gems in the ~/.bundlegem/gem_templates folder take precidence over
11
+ built in gems?
12
+ - Tell the user what files are being created
13
+
14
+
15
+ ** 0.0.3 **
16
+ - CMD: --list to list templates (with descriptions)
17
+ - Raise error when project name already exists, do not overwrite!
18
+ - Raises error if none of the files in the template directory end in .tt
19
+ - Create even empty folders
11
20
 
21
+
12
22
  ** 0.0.2 **
13
23
  - Added default gem template 'service'
14
24
  - Better syntax of bundlegem gem_name -t template_name
@@ -17,5 +27,5 @@
17
27
 
18
28
  ** 0.0.1 **
19
29
  - Very basic prototype that allows you to specify templates in
20
- ~/.bundlegem/templates
30
+ ~/.bundlegem/templates
21
31
 
@@ -18,6 +18,8 @@ module Bundlegem
18
18
 
19
19
  def run
20
20
  # Bundler.ui.confirm "Creating gem '#{name}'..."
21
+
22
+ raise_project_with_that_name_already_exists! if File.exists?(target)
21
23
 
22
24
  underscored_name = name.tr('-', '_')
23
25
  namespaced_path = name.tr('-', '/')
@@ -107,11 +109,15 @@ module Bundlegem
107
109
  end
108
110
 
109
111
  template_src = match_template_src
112
+ template_directories = dynamically_generate_template_directories
110
113
  templates = dynamically_generate_templates || templates
111
114
 
115
+ create_template_directories(template_directories, target)
116
+
112
117
  templates.each do |src, dst|
113
118
  template("#{template_src}/#{src}", target.join(dst), config)
114
119
  end
120
+
115
121
 
116
122
  # Bundler.ui.info "Initializing git repo in #{target}"
117
123
  Dir.chdir(target) { `git init`; `git add .` }
@@ -123,31 +129,51 @@ module Bundlegem
123
129
  end
124
130
 
125
131
  private
132
+
133
+ def dynamically_generate_template_directories
134
+ return nil if options["template"].nil?
135
+
136
+ template_src = get_template_src
137
+
138
+ template_dirs = {}
139
+ Dir.glob("#{template_src}/**").each do |f|
140
+ next unless File.directory? f
141
+ base_path = f[template_src.length+1..-1]
142
+ template_dirs.merge!(base_path => base_path.gsub('#{name}', "#{name}") )
143
+ end
144
+ template_dirs
145
+ end
126
146
 
127
147
  def dynamically_generate_templates
128
148
  return nil if options["template"].nil?
129
149
 
130
150
  template_src = get_template_src
131
-
151
+
132
152
  templates = {}
133
153
  Dir.glob("#{template_src}/**/*.tt").each do |f|
134
154
  base_path = f[template_src.length+1..-1]
135
155
  templates.merge!(base_path => base_path.gsub(/\.tt$/, "").gsub('#{name}', "#{name}") )
136
156
  end
157
+
158
+ raise_no_files_in_template_error! if templates.empty?
137
159
 
138
160
  templates
139
161
  end
162
+
163
+ def create_template_directories(template_directories, target)
164
+ template_directories.each do |k,v|
165
+ FileUtils.mkdir_p("#{target}/#{v}")
166
+ end
167
+ end
140
168
 
141
169
  def match_template_src
142
170
  template_src = get_template_src
143
171
 
144
- return template_src if template_exists_within_repo?(template_src) or File.exists?(template_src) # 'newgem' refers to the built in template that comes with the gem
145
-
146
- # else message the user that the template could not be found
147
- err_missing_template = "Could not find template folder #{options["template"]} in `~/.bundle/gem_templates/`. Please check to make sure your desired template exists."
148
- puts err_missing_template
149
- Bundler.ui.error err_missing_template
150
- exit 1
172
+ if template_exists_within_repo?(template_src) or File.exists?(template_src)
173
+ return template_src # 'newgem' refers to the built in template that comes with the gem
174
+ else
175
+ raise_templet_not_found! # else message the user that the template could not be found
176
+ end
151
177
  end
152
178
 
153
179
  def get_template_src
@@ -156,7 +182,7 @@ module Bundlegem
156
182
  if template_exists_within_repo?(template_name) # if template_exists_within_repo?(template_name)
157
183
  gem_template_location = get_internal_template_location
158
184
  else
159
- gem_template_location = File.expand_path("~/.bundle/gem_templates")
185
+ gem_template_location = File.expand_path("~/.bundlegem/gem_templates")
160
186
  end
161
187
  template_src = "#{gem_template_location}/#{template_name}"
162
188
  end
@@ -289,6 +315,31 @@ module Bundlegem
289
315
  FileUtils.mkdir_p(File.dirname(destination))
290
316
  File.open(destination, "wb") { |f| f.write block.call }
291
317
  end
318
+
319
+
320
+
321
+ def raise_no_files_in_template_error!
322
+ puts "Ooops, the template was found for '#{options['template']}' in ~/.bundlegem/gem_templates, "
323
+ puts "but no files within it ended in .tt. Did you forget to rename the extensions of your files?"
324
+ puts
325
+ puts "Exiting..."
326
+ exit
327
+ end
328
+
329
+ def raise_project_with_that_name_already_exists!
330
+ puts "Ooops, a project with the name #{target} already exists."
331
+ puts "Can't make project. Either delete that folder or choose a new project name"
332
+ puts
333
+ puts "Exiting..."
334
+ exit
335
+ end
336
+
337
+ def raise_templet_not_found!
338
+ err_missing_template = "Could not find template folder '#{options["template"]}' in `~/.bundle/gem_templates/`. Please check to make sure your desired template exists."
339
+ puts err_missing_template
340
+ Bundler.ui.error err_missing_template
341
+ exit 1
342
+ end
292
343
 
293
344
  end
294
345
  end
@@ -0,0 +1,62 @@
1
+ require 'fileutils'
2
+
3
+ module Bundlegem
4
+
5
+ class Configurator
6
+ attr_accessor :user_defined_templates, :user_downloaded_templates
7
+
8
+ def initialize
9
+ @config_directory_root = "#{ENV['HOME']}/.bundlegem"
10
+ @config_file = "#{@config_directory_root}/config"
11
+ @user_defined_templates_path = "#{@config_directory_root}/gem_templates"
12
+
13
+ create_config_file_if_needed!
14
+
15
+ @user_defined_templates = get_user_defined_templates
16
+
17
+ @user_downloaded_templates = get_user_downloaded_templates
18
+
19
+ # load configurations from config file if needed...
20
+ # perhaps it would contain a list of remote templates specified by the user
21
+ end
22
+
23
+ # not implemented yet
24
+ def get_user_downloaded_templates
25
+ []
26
+ end
27
+
28
+ def get_user_defined_templates
29
+ user_definition_directory = @user_defined_templates_path
30
+ template_dirs = Dir.entries(user_definition_directory).select do |entry|
31
+ File.directory?(File.join(user_definition_directory, entry)) and !(entry =='.' || entry == '..')
32
+ end
33
+ binding.pry
34
+
35
+ pairs = []
36
+ template_dirs.each do |dir|
37
+ # open the dir and read the .bundlegem file to see what class of file it is
38
+ # If there's no .bundlegem file in there, mark it misc
39
+
40
+ begin
41
+ f = File.read("#{@user_defined_templates_path}/#{dir}/.bundlegem")
42
+ /category:\s*([\w\s]*$)/ =~ f
43
+
44
+ category = $1.chomp
45
+ rescue
46
+ category = "MISC"
47
+ end
48
+
49
+ category = "MISC" if category.nil?
50
+
51
+ pairs << {category => dir }
52
+ end
53
+ pairs
54
+ end
55
+
56
+ def create_config_file_if_needed!
57
+ FileUtils.mkdir_p @user_defined_templates_path
58
+ FileUtils.touch @config_file
59
+ end
60
+
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module Bundlegem
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/bundlegem.rb CHANGED
@@ -1,12 +1,66 @@
1
1
  require "bundlegem/version"
2
-
3
-
4
-
2
+ require 'bundlegem/configurator'
5
3
 
6
4
  module Bundlegem
7
- # Your code goes here...
8
5
 
9
6
  class << self
7
+
8
+ # lists available templates
9
+ def list
10
+ configurator = Configurator.new
11
+ # search through builtin
12
+ available_templates = [ { "predefined" => "newgem" },
13
+ { "predefined" => "service" }]
14
+
15
+ # search through user downloaded
16
+ available_templates += configurator.user_downloaded_templates
17
+
18
+ # search through user defined
19
+ available_templates += configurator.user_defined_templates
20
+
21
+ available_templates = group_hashes_by_key(available_templates)
22
+ output_string = convert_grouped_hashes_to_output(available_templates)
23
+
24
+ output_string
25
+ end
26
+
27
+ # input: [ { "predefined" => "default" },
28
+ # { "MISC" => "my_thing" },
29
+ # { "prdefined" => "service" }
30
+ # ]
31
+ #
32
+ # output: [ { "predefined" => ["default", "service"] },
33
+ # { "MISC" => ["my_thing"] }
34
+ # ]
35
+ #
36
+ def group_hashes_by_key(available_templates)
37
+ h = {}
38
+ available_templates.each do |hash|
39
+ k = hash.first.first
40
+ v = hash.first.last
41
+ h[k] = [] unless h.has_key?(k)
42
+ h[k] << v
43
+ end
44
+ h
45
+ end
46
+
47
+ # input: [ { "predefined" => ["default", "service"] },
48
+ # { "MISC" => ["my_thing"] }
49
+ # ]
50
+ def convert_grouped_hashes_to_output(available_templates)
51
+ s = ""
52
+ available_templates.each do |hash|
53
+ k = hash.first.upcase
54
+ a = hash.last
55
+ s << " #{k}:\n"
56
+ a.each do |el|
57
+ s << " #{el}\n"
58
+ end
59
+ s << "\n"
60
+ end
61
+ s
62
+ end
63
+
10
64
  def which(executable)
11
65
  if File.file?(executable) && File.executable?(executable)
12
66
  executable
@@ -1,13 +1,34 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Bundlegem do
4
+
5
+ before :each do
6
+ clear_templates
7
+ end
8
+
4
9
  it 'has a version number' do
5
10
  expect(Bundlegem::VERSION).not_to be nil
6
11
  end
7
12
 
8
13
  it 'does something useful' do
9
14
  require 'bundlegem/cli'
10
- binding.pry
15
+ end
11
16
 
17
+ it 'creates a config file if needed and lists properly' do
18
+ create_user_defined_template
19
+
20
+ list_output = Bundlegem.list
21
+
22
+ expect(list_output).to eq " PREDEFINED:\n newgem\n service\n\n MISC:\n empty_template\n\n"
23
+ expect(File.exists?("#{ENV['HOME']}/.bundlegem")).to be true
24
+ end
25
+
26
+ it "lists with good categories" do
27
+ create_user_defined_template("ARDUINO")
28
+
29
+ list_output = Bundlegem.list
30
+
31
+ binding.pry
12
32
  end
33
+
13
34
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,23 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'bundlegem'
3
-
3
+ require 'fileutils'
4
4
  require 'pry'
5
+
6
+ # Mock our home directory
7
+ ENV['HOME'] = "/tmp/bundlegem_mock_home"
8
+
9
+
10
+ def create_user_defined_template(category = nil)
11
+ template_root = "/tmp/bundlegem_mock_home/.bundlegem/gem_templates/empty_template"
12
+ FileUtils.mkdir_p template_root
13
+
14
+ File.open("#{template_root}/.bundlegem", "w+") do |f|
15
+ f.puts "category: #{category}" unless category.nil?
16
+ end
17
+
18
+ end
19
+
20
+
21
+ def clear_templates
22
+ FileUtils.rm_rf "/tmp/bundlegem_mock_home/.bundlegem"
23
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bundlegem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - TheNotary
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-08 00:00:00.000000000 Z
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -101,6 +101,7 @@ files:
101
101
  - lib/bundlegem.rb
102
102
  - lib/bundlegem/cli.rb
103
103
  - lib/bundlegem/cli/gem.rb
104
+ - lib/bundlegem/configurator.rb
104
105
  - lib/bundlegem/friendly_errors.rb
105
106
  - lib/bundlegem/templates/Executable
106
107
  - lib/bundlegem/templates/Executable.standalone