cuba_genie 0.1.2 → 0.1.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: 6ea6de37fd537e0cf2a0022b2c449fba4d4b95dd
4
- data.tar.gz: 519edffa9fc41d3e2cd2474e7d13c4854e4feef8
3
+ metadata.gz: 50c305a2570c41d098bb165d2fa37aa63ff5a705
4
+ data.tar.gz: 285311530fdf6ed7f3410a898d5a8ce0055f9a84
5
5
  SHA512:
6
- metadata.gz: 440620a683669ad330137c3dcdf041a9a4ce8e7ea7197c253179b6c6401f8c7605b95d7f49b91a489c068add9b848be01602aa620f7929f2e7481807f4a15196
7
- data.tar.gz: 3f5d48e6d95fa781d99df3e9bacae3fb90dbc6bfd961f853030ad1469ce821855ddff6fb114c9a3e11eba759fc6223d087120ccd8ed7a3f8c74d2c485a7aac82
6
+ metadata.gz: 5977cf4d1319eca5dcad76270022543d5f8e06ec9379202b82c102a9d510cf221393de88a22c10a13c375ff57892fa5e2ad53e2b55e90836e6126b669d5c5150
7
+ data.tar.gz: 9839f7a3c762bdbab059a7edfcdfd778296cdab9194f1e56a5a704851cae05cbb169661a85767375c6c555c376b97f802214d978828b3c6c9439f28983d3927b
@@ -13,4 +13,8 @@
13
13
 
14
14
  -- Some serious code refactoring
15
15
 
16
+ ## 0.1.3 (2016-04-16)
16
17
 
18
+ -- Don't have to implement #unexecute in command classes any longer, just set @rollback_msg
19
+ -- added git support
20
+ -- enabled gem as embeddable, i.e. other gems can now incorporate their own functionality into CubaGenie code.
data/README.md CHANGED
@@ -1,19 +1,20 @@
1
1
  # CubaGenie
2
2
 
3
- CubaGenie is a generator gem that sets up all the infrastructure you need in order to be immediately productive with the [Cuba](http://cuba.is/) web application. CubaGenie creates a skeleton Cuba app, configured to your preferences.
3
+ CubaGenie is a generator gem that sets up all the infrastructure you need in order to be immediately productive with the [Cuba](http://cuba.is/) web micro-framework. CubaGenie creates a skeleton Cuba app, configured to your preferences.
4
4
 
5
5
  ## Why use it?
6
6
 
7
7
  [Cuba](http://cuba.is/) is a wonderful DSL that allows us to create simple web-apps quickly and easily. However, for more complex apps we would need to add extra functionality and components to the basic Cuba setup, things like view templates, data stores, static file serving, our favourite JS or CSS framework and so on. This can be a time-consuming task and it's not always obvious how to get everything working together. CubaGenie takes away the hassle by creating everything you need in one fell, quick swoop.
8
8
 
9
9
  ## What does it create?
10
- 1. A basic Cuba application ready to be ran (config, app-file and Gemfile)
11
- 2. Ruby version management support files (.ruby-version and .ruby-gemset)
12
- 3. Standard view templates (layout and home)
13
- 4. A Minitest setup and basic functional test template (optional)
14
- 5. A Capybara setup and basic acceptance test template (optional)
15
- 6. A Rakefile with basic test tasks (optional)
16
- 6. A Twitter Bootstrap installation, hooked into the view templates (optional)
10
+ - A basic Cuba application ready to be ran (config, app-file and Gemfile)
11
+ - Ruby version management support files (.ruby-version and .ruby-gemset)
12
+ - Standard view templates (layout and home)
13
+ - A .git folder (does `git init` in your project)
14
+ - A Minitest setup and basic functional test template (optional)
15
+ - A Capybara setup and basic acceptance test template (optional)
16
+ - A Rakefile with basic test tasks (optional)
17
+ - A Twitter Bootstrap installation, hooked into the view templates (optional)
17
18
 
18
19
 
19
20
  ## Installation
@@ -42,12 +43,78 @@ cuba_genie new <your_app_name>
42
43
 
43
44
  Then simply keep answering the questions CubaGenie asks you.
44
45
 
45
- ## Development
46
+ ## Gem Development
46
47
 
47
48
  After checking out the repo, run `rake test` to run the tests. You can also run `rake console` for an interactive prompt that will allow you to experiment.
48
49
 
49
50
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
51
 
52
+ ## Embedded Development
53
+
54
+ CubaGenie is fully embeddable. This means you can incorporate its run-time in your own code and add your own generator commands to be ran in addition to the existing CubaGenie commands. Embedding CubaGenie in your own code is very simple:
55
+
56
+ ### 1. include CubaGenie
57
+ Add CubaGenie to your *gemspec* file
58
+
59
+ spec.add_dependency "cuba_genie"
60
+
61
+ In your main gem file require CubaGenie
62
+
63
+
64
+ require "my_gem/version"
65
+ require 'cuba_genie'
66
+
67
+ module MyGem
68
+
69
+ end
70
+
71
+
72
+ ### 2. Setup CLI class
73
+
74
+ Under your exe directory create a new file *my_gem*. Make sure *my_gem* has executable privileges, i.e. run `chmod 755 exe/my_gem`. The content of *my_gem* must be:
75
+
76
+
77
+ require 'cuba_genie'
78
+
79
+ CubaGenie::MyCLI.start(ARGV)
80
+
81
+ ### 3. Add your command classes
82
+
83
+ In your source files, open up the CubaGenie module and add your own command classes. A command class must:
84
+
85
+ 1. Derive from *Command* class
86
+ 2. Define a *PRECEDENCE* constant which sets the order in which the class will be executed (if more than one command classes are defined)
87
+ 3. Implement an initializer with a @*description* attribute which calls *super*.
88
+ 4. Implement the #*execute* method, passing a block to *super*.
89
+
90
+ for instance,
91
+
92
+ module CubaGenie
93
+ class MyClass < Command
94
+ PRECEDENCE = 1 #this means this class will be ran before any others you define
95
+
96
+ def initialize
97
+ @description = 'this is MyClass' #this will show when your class runs
98
+ super
99
+ end
100
+
101
+ def execute
102
+ super do
103
+ File.open('some_file', 'w') {|f| f.write("hello world!") }
104
+ # if you create files or dirs maje sure you add them to @files_created and @dirs_created
105
+ @files_created << "#{Dir.pwd}/'some_file'"
106
+ puts "********** executin class A"
107
+ end
108
+ end
109
+
110
+ end
111
+ end
112
+
113
+ ### 4. Build and run your gem
114
+ `bundle install && bundle exec rake install` will build and install your gem locally. `my_gem new my_project_name` will run all the CubaGenie generators and will then run your own generator classes in the order specified by PRECEDENCE. Happy days!
115
+
116
+
117
+
51
118
  ## Roadmap
52
119
 
53
120
  * Add RSpec support
@@ -27,6 +27,10 @@ module CubaGenie
27
27
  @cmds.add_command CubaSetup.new(project_name: project_name, minitest: minitest, capybara: capybara)
28
28
  @cmds.add_command ViewsSetup.new(project_name: project_name, bootstrap_version: choose_bootstrap_version)
29
29
  @cmds.add_command MinitestSetup.new(project_name: project_name, reporter: reporter, capybara_setup: capybara) if minitest
30
+
31
+ # allow users to add their own commands
32
+ run_extensions
33
+
30
34
  puts closing_message(project_name) if @cmds.execute
31
35
  end
32
36
 
@@ -65,6 +69,15 @@ module CubaGenie
65
69
 
66
70
  private
67
71
 
72
+
73
+ # Prompts the user for a boolean input, within a range of potential
74
+ # semantically true or false values (POSITIVE_RESPONSES, NEGATIVE_RESPONSES)
75
+ #
76
+ # @param N/A
77
+ #
78
+ # @return [Boolean] a true or false value
79
+ #
80
+ #
68
81
  def ask_for_yes_no_response
69
82
  response = STDIN.gets.chomp
70
83
  while !(POSITIVE_RESPONSES + NEGATIVE_RESPONSES).include? response
@@ -82,10 +95,10 @@ module CubaGenie
82
95
  #
83
96
  # @return [Fixnum] an integer within the valid range
84
97
  #
85
- # @note character 0 cannot be used as a range value. The reason is that
98
+ # @note character 0 cannot be used as a range value. The reason is that
86
99
  # #to_i will convert any non-numberic character to 0, which the method will
87
100
  # then wrongly interpret as a valid character
88
- #
101
+ #
89
102
  def ask_for_numeric_reponse(valid_range)
90
103
  response = STDIN.gets.chomp.to_i
91
104
  while !valid_range.cover? response
@@ -96,10 +109,62 @@ module CubaGenie
96
109
  response
97
110
  end
98
111
 
112
+
113
+ # Gets all Command-derived classes defined in the CubaGenie
114
+ # module and filters out the classes already defined by the
115
+ # core CubaGenie gem. What is left are the classes added as
116
+ # user-extensions to the gem, and which will be run after the
117
+ # built-in commands have ran.
118
+ #
119
+ # @param N/A
120
+ #
121
+ # @return [Array<Symbol>] all user-added Command-derived classes
122
+ #
123
+ #
124
+ def get_extensions
125
+ existing_klasses = [:CubaSetup, :MinitestSetup, :ViewsSetup]
126
+ all_command_klasses = CubaGenie.constants.select do |c|
127
+ Class === CubaGenie.const_get(c) && CubaGenie.const_get(c).superclass == CubaGenie::Command
128
+ end
129
+ all_command_klasses - existing_klasses
130
+ end
131
+
99
132
  def closing_message(project_name)
100
- "\nYour #{project_name} Cuba app is now set up and ready to go! To start the app, cd into #{project_name} directory, run 'bundle install' and then 'rackup'. You can then see your app running on 'http://localhost:9292'."
133
+ "\nYour #{project_name} Cuba app is now set up and ready to go! To start the app, cd "\
134
+ "into the #{project_name} directory, run 'bundle install' and then 'rackup'. You can then "\
135
+ "see your app running on 'http://localhost:9292'."
101
136
  end
102
137
 
103
138
 
139
+ # Gets all user-defined Command-derived classes within the
140
+ # CubaGenie module, order them according to their PRECEDENCE
141
+ # constant and adds them to the command-list so that they can
142
+ # be executed after the built-in CubaGenie classes
143
+ #
144
+ # @param N/A
145
+ #
146
+ # @return [Array<Symbol>] all user-added Command-derived classes
147
+ #
148
+ #
149
+ def run_extensions
150
+ # first check if someone added extension classes in our module
151
+ command_classes = get_extensions
152
+ unless command_classes.empty?
153
+ #order classes in order of precedence
154
+ command_classes.sort_by! do |item|
155
+ CubaGenie.const_get(item).const_get(:PRECEDENCE)
156
+ end
157
+
158
+ command_classes.each do |klass|
159
+ if CubaGenie.const_get(klass).instance_methods.include? :execute
160
+ @cmds.add_command CubaGenie.const_get(klass).new
161
+ else
162
+ # #execute method is not implemented
163
+ ## TODO: raise exception?
164
+ end
165
+ end
166
+ end
167
+ end
168
+
104
169
  end #class
105
170
  end #module
@@ -1,13 +1,35 @@
1
1
  module CubaGenie
2
+ # Implements the Command pattern. Responsible for providing execute
3
+ # and unexecute (rollback) methods. Every Command-derived class must
4
+ # implement an #execute methods which passes a block to super and
5
+ # (optionally) an unexecute method
2
6
 
7
+ #
3
8
  class Command
4
9
 
5
- attr_reader :description, :error, :project_name
10
+ attr_reader :description, #what this class does
11
+ :error, #fill-in if things go wrong
12
+ :rollback_msg, #what to display if we roll-back
13
+ :project_name
14
+
6
15
 
7
16
  def initialize(**args)
17
+ # arrays used to keep track of files created, so that they
18
+ # can be rolled back, if needed
8
19
  @files_created, @dirs_created = [], []
9
20
  end
10
21
 
22
+
23
+ # Yields to a block where the user puts their command code
24
+ #
25
+ # @param N/A
26
+ #
27
+ # @return [Boolean] true if methods runs without problems,
28
+ # false if an exception is raised
29
+ #
30
+ # @note any files or directories created here should have their
31
+ # path pushed to @files_created and @dirs_created arrays.
32
+ #
11
33
  def execute
12
34
  yield
13
35
  true
@@ -15,8 +37,18 @@ module CubaGenie
15
37
  puts e.message and false
16
38
  end
17
39
 
18
- def unexecute(message)
19
- puts message unless ENV['RACK_ENV'] == 'test'
40
+
41
+ # Deletes all files and directories created with the execute
42
+ # method.
43
+ #
44
+ # @param N/A
45
+ #
46
+ # @return [Boolean] true if methods runs without problems,
47
+ # false if an exception is raised
48
+ #
49
+ #
50
+ def unexecute
51
+ puts @rollback_msg unless ENV['RACK_ENV'] == 'test'
20
52
  @files_created.each do |file_path|
21
53
  File.delete file_path if File.exist? file_path
22
54
  end
@@ -37,7 +37,10 @@ class CommandList
37
37
 
38
38
  private
39
39
  def unexecute(idx)
40
- @commands.slice(0, idx + 1).reverse_each(&:unexecute)
40
+ @commands.slice(0, idx + 1).reverse_each do |cmd|
41
+ cmd.rollback_msg
42
+ cmd.unexecute
43
+ end
41
44
  end
42
45
 
43
46
  alias_method :size, :length
@@ -12,6 +12,7 @@ module CubaGenie
12
12
  @minitest = args[:minitest]
13
13
  @capybara = args[:capybara]
14
14
  @description = "Creating Cuba setup"
15
+ @rollback_msg = "rolling back Cuba basic setup"
15
16
  super
16
17
 
17
18
  end
@@ -24,16 +25,13 @@ module CubaGenie
24
25
  create_app_file
25
26
  create_rack_file
26
27
  create_gem_file
28
+ git_initialize
27
29
  create_ruby_version_file
28
30
  create_ruby_gemset_file
29
31
  end
30
32
  end
31
33
 
32
34
 
33
- def unexecute
34
- super("rolling back Cuba basic setup")
35
- end
36
-
37
35
  private
38
36
 
39
37
  def create_app_file
@@ -75,5 +73,9 @@ module CubaGenie
75
73
  %x(ruby -v).slice(/(\d\.){2}\d/)
76
74
  end
77
75
 
76
+ def git_initialize
77
+ %x(git init)
78
+ end
79
+
78
80
  end #class
79
81
  end #module
@@ -14,6 +14,7 @@ module CubaGenie
14
14
  @description = (args[:capybara_setup] ?
15
15
  "Setting up Minitest functional and acceptance tests" :
16
16
  "Setting up Minitest functional tests")
17
+ @rollback_msg = "rolling back Minitest"
17
18
  @capybara_setup = args[:capybara_setup]
18
19
  @project_name = args[:project_name]
19
20
  @reporter = args[:reporter] || 'Default'
@@ -37,11 +38,6 @@ module CubaGenie
37
38
  end
38
39
 
39
40
 
40
- def unexecute
41
- super("rolling back Minitest")
42
- end
43
-
44
-
45
41
  private
46
42
  def create_test_helper
47
43
 
@@ -1,3 +1,3 @@
1
1
  module CubaGenie
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -19,6 +19,7 @@ module CubaGenie
19
19
  @project_name = args[:project_name]
20
20
  #TODO: allow for choice of different template engines
21
21
  @description = 'Setting up views'
22
+ @rollback_msg = "rolling back Views"
22
23
  @bootstrap_version = args[:bootstrap_version] || 'n'
23
24
  super
24
25
  end
@@ -38,10 +39,6 @@ module CubaGenie
38
39
  end
39
40
  end
40
41
 
41
- def unexecute
42
- super("rolling back views")
43
- end
44
-
45
42
 
46
43
  private
47
44
  def create_view_files(layout_view_formatters)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuba_genie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fred Heath
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-13 00:00:00.000000000 Z
11
+ date: 2016-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -203,7 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
203
  version: '0'
204
204
  requirements: []
205
205
  rubyforge_project:
206
- rubygems_version: 2.4.6
206
+ rubygems_version: 2.4.8
207
207
  signing_key:
208
208
  specification_version: 4
209
209
  summary: CubaGenie is a generator gem that sets up all the infrastructure you need