trailblazer-generator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +4 -0
  5. data/Guardfile +42 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +67 -0
  8. data/Rakefile +10 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/bin/trailblazer +9 -0
  12. data/lib/trailblazer/generator.rb +16 -0
  13. data/lib/trailblazer/generator/builder.rb +7 -0
  14. data/lib/trailblazer/generator/builder/cell.rb +42 -0
  15. data/lib/trailblazer/generator/builder/cell/edit.erb +4 -0
  16. data/lib/trailblazer/generator/builder/cell/generic.erb +4 -0
  17. data/lib/trailblazer/generator/builder/cell/index.erb +7 -0
  18. data/lib/trailblazer/generator/builder/cell/item.erb +4 -0
  19. data/lib/trailblazer/generator/builder/cell/new.erb +6 -0
  20. data/lib/trailblazer/generator/builder/cell/show.erb +4 -0
  21. data/lib/trailblazer/generator/builder/contract.rb +33 -0
  22. data/lib/trailblazer/generator/builder/contract/generic.erb +7 -0
  23. data/lib/trailblazer/generator/builder/operation.rb +29 -0
  24. data/lib/trailblazer/generator/builder/operation/create.erb +10 -0
  25. data/lib/trailblazer/generator/builder/operation/delete.erb +8 -0
  26. data/lib/trailblazer/generator/builder/operation/generic.erb +19 -0
  27. data/lib/trailblazer/generator/builder/operation/index.erb +7 -0
  28. data/lib/trailblazer/generator/builder/operation/show.erb +3 -0
  29. data/lib/trailblazer/generator/builder/operation/update.erb +10 -0
  30. data/lib/trailblazer/generator/builder/view.rb +29 -0
  31. data/lib/trailblazer/generator/builder/view/generic.erb +2 -0
  32. data/lib/trailblazer/generator/cell.rb +38 -0
  33. data/lib/trailblazer/generator/cli.rb +11 -0
  34. data/lib/trailblazer/generator/cli/generate.rb +10 -0
  35. data/lib/trailblazer/generator/cli/generate/cell.rb +16 -0
  36. data/lib/trailblazer/generator/cli/generate/contract.rb +17 -0
  37. data/lib/trailblazer/generator/cli/generate/operation.rb +29 -0
  38. data/lib/trailblazer/generator/inflector.rb +45 -0
  39. data/lib/trailblazer/generator/macro.rb +36 -0
  40. data/lib/trailblazer/generator/output.rb +20 -0
  41. data/lib/trailblazer/generator/version.rb +5 -0
  42. data/trailblazer-generator.gemspec +35 -0
  43. metadata +228 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 581643780930a5755ea935f17e700e1edcdff8aa
4
+ data.tar.gz: 3b5cd01cd379be68220b5b536421d87132ba541f
5
+ SHA512:
6
+ metadata.gz: 35a72610f78d5f6336d94314fd729d7ad9c6c6e90faa9b6a0661f83895fffbfb839a592a135c242756f05d3db02ea5ffb5258783cb4da4c220ff4559270b4af2
7
+ data.tar.gz: 175a55cf36c55581c17b980ecd2bdb1c27b87e247b97f7cdc0d102875637402b9cfd56260150d33fb926f1e025d4adad17e5d35e126a273cadf2cbbced40d236
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
11
+ /app
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in trailblazer-generator.gemspec
4
+ gemspec
@@ -0,0 +1,42 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :minitest do
19
+ # with Minitest::Unit
20
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
21
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
22
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
23
+
24
+ # with Minitest::Spec
25
+ # watch(%r{^spec/(.*)_spec\.rb$})
26
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
27
+ # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
28
+
29
+ # Rails 4
30
+ # watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
31
+ # watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
32
+ # watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
33
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
34
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
35
+ # watch(%r{^test/.+_test\.rb$})
36
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
37
+
38
+ # Rails < 4
39
+ # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
40
+ # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
41
+ # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
42
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Celso Fernandes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Trailblazer::Generator
2
+
3
+ Generate trailblazer files, this is just a prototype, soon will be a WIP, check back soon ;)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'trailblazer-generator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install trailblazer-generator
20
+
21
+ ## Usage
22
+
23
+ It includes a binary called `trailblazer`
24
+
25
+ run trailblazer and check usage
26
+
27
+ ```bash
28
+ ○ trailblazer
29
+ Commands:
30
+ trailblazer generate COMMANDS # Generates trailblazer file
31
+ trailblazer help [COMMAND] # Describe available commands or one specific command
32
+ ```
33
+
34
+ ## Examples
35
+
36
+ Generating some operations:
37
+
38
+ ```shell
39
+ trailblazer generate operation BlogPost --actions index,create
40
+ ```
41
+
42
+ Generating some cells:
43
+
44
+ ```shell
45
+ trailblazer generate cell BlogPost --actions index,edit
46
+ ```
47
+
48
+ As bonus, we get some views:
49
+ * index.erb
50
+ * item.erb (to be used as a item render on index collection)
51
+ * edit.erb
52
+
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ 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).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/trailblazer/trailblazer-generator.
63
+
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "trailblazer/generator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "trailblazer/generator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ Trailblazer::Generator::Cli.start( ARGV )
@@ -0,0 +1,16 @@
1
+ require 'cells'
2
+ require 'cells-erb'
3
+ require 'trailblazer'
4
+ require "trailblazer/generator/version"
5
+ require "trailblazer/generator/cli"
6
+ require "trailblazer/generator/cell"
7
+ require "trailblazer/generator/inflector"
8
+ require "trailblazer/generator/macro"
9
+ require "trailblazer/generator/output"
10
+ require "trailblazer/generator/builder"
11
+
12
+ module Trailblazer
13
+ module Generator
14
+ # Your code goes here...
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module Trailblazer::Generator::Builder
2
+ end
3
+
4
+ require_relative 'builder/cell'
5
+ require_relative 'builder/contract'
6
+ require_relative 'builder/operation'
7
+ require_relative 'builder/view'
@@ -0,0 +1,42 @@
1
+ class Trailblazer::Generator::Builder::Cell < Trailblazer::Operation
2
+ class Cell < Trailblazer::Generator::Cell
3
+ end
4
+
5
+ step Trailblazer::Generator::Macro::ValidateClassName()
6
+ step :generate_actions!
7
+ step :generate_views!
8
+ failure Trailblazer::Generator::Macro::Failure()
9
+
10
+ def generate_actions!(options, params:)
11
+ actions = params[:options]['actions'].split(',')
12
+ actions.each do |action|
13
+ generate_file(options, name: params[:name], action: action)
14
+ generate_file(options, name: params[:name], action: 'item') if action == 'Index'
15
+ end
16
+ end
17
+
18
+ def generate_views!(options, params:)
19
+ options_dup = params[:options].dup
20
+ actions = params[:options]['actions'].dup
21
+ if actions.match /index/i
22
+ actions << ',item'
23
+ options_dup['actions'] = actions
24
+ end
25
+ Trailblazer::Generator::Builder::View.(name: params[:name], options: options_dup)
26
+ true
27
+ end
28
+
29
+ private
30
+ def generate_file(options, name:, action:)
31
+ model = Trailblazer::Generator::Cell.build_model(
32
+ name: name, action: action
33
+ )
34
+ params = options['params'][:options]
35
+ content = Cell.(model, params)
36
+
37
+ name = Trailblazer::Generator::Inflector.underscore(name)
38
+ path = File.join('app', 'concepts', name, 'cell', "#{action}.rb")
39
+
40
+ Trailblazer::Generator::Output.new(path: path, content: content).save
41
+ end
42
+ end
@@ -0,0 +1,4 @@
1
+ module <%= name %>::Cell
2
+ class <%= action_class %> < Trailblazer::Cell
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module <%= name %>::Cell
2
+ class <%= action_class %> < Trailblazer::Cell
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module <%= name %>::Cell
2
+ class <%= action_class %> < Trailblazer::Cell
3
+ def total
4
+ return "No element" if model.size == 0
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module <%= name %>::Cell
2
+ class <%= action_class %> < Trailblazer::Cell
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module <%= name %>::Cell
2
+ class <%= action_class %> < Trailblazer::Cell
3
+ include ActionView::RecordIdentifier
4
+ include ActionView::Helpers::FormOptionsHelper
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ module <%= name %>::Cell
2
+ class <%= action_class %> < Trailblazer::Cell
3
+ end
4
+ end
@@ -0,0 +1,33 @@
1
+ class Trailblazer::Generator::Builder::Contract < Trailblazer::Operation
2
+ class Cell < Trailblazer::Generator::Cell
3
+ def properties
4
+ return [] unless options.key?("properties")
5
+ options["properties"]
6
+ end
7
+ end
8
+
9
+ step Trailblazer::Generator::Macro::ValidateClassName()
10
+ step :generate_actions!
11
+ failure Trailblazer::Generator::Macro::Failure()
12
+
13
+ def generate_actions!(options, params:)
14
+ actions = params[:options]['actions'].split(',')
15
+ actions.each do |action|
16
+ generate_file(options, name: params[:name], action: action)
17
+ end
18
+ end
19
+
20
+ private
21
+ def generate_file(options, name:, action:)
22
+ model = Trailblazer::Generator::Cell.build_model(
23
+ name: name, action: action
24
+ )
25
+ params = options['params'][:options]
26
+ content = Cell.(model, params)
27
+
28
+ name = Trailblazer::Generator::Inflector.underscore(name)
29
+ path = File.join('app', 'concepts', name, 'contract', "#{action}.rb")
30
+
31
+ Trailblazer::Generator::Output.new(path: path, content: content).save
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ module <%= name %>::Contract
2
+ class <%= action_class %> < Reform::Form
3
+ <% properties.each do |property| -%>
4
+ property :<%= property + "\n" %>
5
+ <%- end %>
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ class Trailblazer::Generator::Builder::Operation < Trailblazer::Operation
2
+ class Cell < Trailblazer::Generator::Cell
3
+ end
4
+
5
+ step Trailblazer::Generator::Macro::ValidateClassName()
6
+ step :generate_actions!
7
+ failure Trailblazer::Generator::Macro::Failure()
8
+
9
+ def generate_actions!(options, params:)
10
+ actions = params[:options]['actions'].split(',')
11
+ actions.each do |action|
12
+ generate_file(options, name: params[:name], action: action)
13
+ end
14
+ end
15
+
16
+ private
17
+ def generate_file(options, name:, action:)
18
+ model = Trailblazer::Generator::Cell.build_model(
19
+ name: name, action: action
20
+ )
21
+ params = options['params'][:options]
22
+ content = Cell.(model, params)
23
+
24
+ name = Trailblazer::Generator::Inflector.underscore(name)
25
+ path = File.join('app', 'concepts', name, 'operation', "#{action}.rb")
26
+
27
+ Trailblazer::Generator::Output.new(path: path, content: content).save
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ class <%= name %>::<%= action_class %> < Trailblazer::Operation
2
+ class Present < Trailblazer::Operation
3
+ step Model(<%= name %>, :new)
4
+ step Contract::Build( constant: <%= name %>::Contract::Create )
5
+ end
6
+
7
+ step Nested( Present )
8
+ step Contract::Validate( key: :<%= underscore_name %> )
9
+ step Contract::Persist( )
10
+ end
@@ -0,0 +1,8 @@
1
+ class <%= name %>::<%= action_class %> < Trailblazer::Operation
2
+ step Model(<%= name %>, :find_by)
3
+ step :delete!
4
+
5
+ def delete!(options, model:, **)
6
+ model.destroy
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ class <%= name %>::<%= action_class %> < Trailblazer::Operation
2
+ <%- if options[:present] %>
3
+ class Present < Trailblazer::Operation
4
+ step Model( <%= name %>, :new )
5
+ step :decorate!
6
+ step Contract::Build( constant: Form::Create )
7
+
8
+ def decorate!(options, model:, **)
9
+ options["model"] = Twin::Create.new(model)
10
+ end
11
+ end
12
+ <% end %>
13
+
14
+ <%- if options[:present] %>
15
+ step Nested( Present )
16
+ <% end %>
17
+ step Contract::Validate()
18
+ step Contract::Persist()
19
+ end
@@ -0,0 +1,7 @@
1
+ class <%= name %>::<%= action_class %> < Trailblazer::Operation
2
+ step :model!
3
+
4
+ def model!(options, *)
5
+ options["model"] = ::<%= name %>.all
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class <%= name %>::<%= action_class %> < Trailblazer::Operation
2
+ step Model(<%= name %>, :find_by)
3
+ end
@@ -0,0 +1,10 @@
1
+ class <%= name %>::<%= action_class %> < Trailblazer::Operation
2
+ class Present < Trailblazer::Operation
3
+ step Model(<%= name %>, :find_by)
4
+ step Contract::Build( constant: <%= name %>::Contract::Update )
5
+ end
6
+
7
+ step Nested( Present )
8
+ step Contract::Validate( key: :<%= underscore_name %> )
9
+ step Contract::Persist( )
10
+ end
@@ -0,0 +1,29 @@
1
+ class Trailblazer::Generator::Builder::View < Trailblazer::Operation
2
+ class Cell < Trailblazer::Generator::Cell
3
+ end
4
+
5
+ step Trailblazer::Generator::Macro::ValidateClassName()
6
+ step :generate_actions!
7
+ failure Trailblazer::Generator::Macro::Failure()
8
+
9
+ def generate_actions!(options, params:)
10
+ actions = params[:options]['actions'].split(',')
11
+ actions.each do |action|
12
+ generate_file(options, name: params[:name], action: action)
13
+ end
14
+ end
15
+
16
+ private
17
+ def generate_file(options, name:, action:)
18
+ model = Trailblazer::Generator::Cell.build_model(
19
+ name: name, action: action
20
+ )
21
+ params = options['params'][:options]
22
+ content = Cell.(model, params)
23
+
24
+ name = Trailblazer::Generator::Inflector.underscore(name)
25
+ path = File.join('app', 'concepts', name, 'view', "#{action}.erb")
26
+
27
+ Trailblazer::Generator::Output.new(path: path, content: content).save
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ <h1><%= name %>#<%= action %></h1>
2
+ <p>Find me in app/concepts/<%= underscore_name %>/view/<%= action %>.erb</p>
@@ -0,0 +1,38 @@
1
+ require 'ostruct'
2
+
3
+ module Trailblazer
4
+ module Generator
5
+ class Cell < Cell::ViewModel
6
+ module ClassMethods
7
+ def build_model(name:, action:)
8
+ OpenStruct.new(name: name, action: action)
9
+ end
10
+ end
11
+ extend ClassMethods
12
+
13
+ include ::Cell::Erb
14
+ self.view_paths = [File.join(File.dirname(__FILE__), 'builder')]
15
+
16
+ def self.controller_path
17
+ util.underscore(name.sub(/Cell$/, '')).split("/")[-1]
18
+ end
19
+
20
+ property :name
21
+ property :action
22
+
23
+ def show
24
+ render action
25
+ rescue ::Cell::TemplateMissingError
26
+ render :generic
27
+ end
28
+
29
+ def action_class
30
+ Trailblazer::Generator::Inflector.camelize(action)
31
+ end
32
+
33
+ def underscore_name
34
+ Trailblazer::Generator::Inflector.underscore(name)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ require 'thor'
2
+ require 'trailblazer/generator/cli/generate'
3
+
4
+ module Trailblazer
5
+ module Generator
6
+ class Cli < Thor
7
+ desc "generate COMMANDS", "Generates trailblazer file"
8
+ subcommand "generate", Trailblazer::Generator::Generate
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module Trailblazer
2
+ module Generator
3
+ class Generate < Thor
4
+ end
5
+ end
6
+ end
7
+
8
+ require 'trailblazer/generator/cli/generate/cell'
9
+ require 'trailblazer/generator/cli/generate/contract'
10
+ require 'trailblazer/generator/cli/generate/operation'
@@ -0,0 +1,16 @@
1
+ module Trailblazer
2
+ module Generator
3
+ class Generate < Thor
4
+ desc "cell NAME", "Generates cell file"
5
+ long_desc <<-CELL_LONG_DESC
6
+
7
+ `generate cell` generate cell file
8
+
9
+ CELL_LONG_DESC
10
+ options actions: :required
11
+ def cell(name)
12
+ Trailblazer::Generator::Builder::Cell.(name: name, options: options)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Trailblazer
2
+ module Generator
3
+ class Generate < Thor
4
+ desc "contract NAME", "Generates contract file"
5
+ long_desc <<-CONTRACT_LONG_DESC
6
+
7
+ `generate contract` generate contract file
8
+
9
+ CONTRACT_LONG_DESC
10
+ options actions: :required
11
+ options properties: :array
12
+ def contract(name)
13
+ Trailblazer::Generator::Builder::Contract.(name: name, options: options)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module Trailblazer
2
+ module Generator
3
+ class Generate < Thor
4
+ desc "operation NAME", "Generates operation file"
5
+ long_desc <<-OPERATION_LONG_DESC
6
+
7
+ `generate operation` generate operation file
8
+
9
+ Actions Available:
10
+ * index
11
+ * create (includes present for new action)
12
+ * show
13
+ * update (includes present for edit action)
14
+ * delete
15
+ * generic (if name doesn't match above, is generic)
16
+
17
+ Examples:
18
+ `generate operation BlogPost --actions index,foo`
19
+
20
+ Will generate an index operation for BlogPost and a foo operation (using generic template)
21
+
22
+ OPERATION_LONG_DESC
23
+ options actions: :required
24
+ def operation(name)
25
+ Trailblazer::Generator::Builder::Operation.(name: name, options: options)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,45 @@
1
+ class Trailblazer::Generator::Inflector
2
+ # Converts strings to UpperCamelCase.
3
+ # If the +uppercase_first_letter+ parameter is set to false, then produces
4
+ # lowerCamelCase.
5
+ #
6
+ # Also converts '/' to '::' which is useful for converting
7
+ # paths to namespaces.
8
+ #
9
+ # camelize('trail_blazer') # => "TrailBlazer"
10
+ # camelize('trail_blazer/operation') # => "TrailBlazer::Operation"
11
+ #
12
+ # As a rule of thumb you can think of +camelize+ as the inverse of
13
+ # #underscore, though there are cases where that does not hold:
14
+ #
15
+ # camelize(underscore('SSLError')) # => "SslError"
16
+ def self.camelize(term)
17
+ string = term.to_s
18
+ string.gsub!(/\/([a-zA-Z])/) { |m| "::#{$1.upcase}" }
19
+ string.gsub!(/_([a-zA-Z])/) { |m| "#{$1.upcase}" }
20
+ string.gsub!(/^([a-zA-Z])/) { |m| "#{$1.upcase}" }
21
+ string.gsub!("/".freeze, "::".freeze)
22
+ string
23
+ end
24
+
25
+ # Makes an underscored, lowercase form from the expression in the string.
26
+ #
27
+ # Changes '::' to '/' to convert namespaces to paths.
28
+ #
29
+ # underscore('TrailBlazer') # => "trail_blazer"
30
+ # underscore('TrailBlazer::Operation') # => "trail_blazer/operation"
31
+ #
32
+ # As a rule of thumb you can think of +underscore+ as the inverse of
33
+ # #camelize, though there are cases where that does not hold:
34
+ #
35
+ # camelize(underscore('SSLError')) # => "SslError"
36
+ def self.underscore(camel_cased_word)
37
+ return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
38
+ word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze)
39
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
40
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
41
+ word.tr!("-".freeze, "_".freeze)
42
+ word.downcase!
43
+ word
44
+ end
45
+ end
@@ -0,0 +1,36 @@
1
+ module Trailblazer::Generator::Macro
2
+ REGEXP = /^[A-Z][A-Za-z]{1,}(::[A-Z][A-Za-z]{1,})?/
3
+ # Plase, keep error codes sorted alphabetically
4
+ ERROR_INVALID_CLASS_NAME = 1
5
+
6
+ def self.ValidateClassName()
7
+ step = ->(input, options) do
8
+ if options["params"][:name].match REGEXP
9
+ true
10
+ else
11
+ options['failure_message'] = 'You provided an invalid class name'
12
+ options['error_code'] = ERROR_INVALID_CLASS_NAME
13
+ false
14
+ end
15
+ end
16
+
17
+ [ step, name: "validate_class_name" ]
18
+ end
19
+
20
+ def self.Output()
21
+ step = ->(input, options) do
22
+ Trailblazer::Generator::Output.new(path: options['path'], content: options['content']).save
23
+ end
24
+
25
+ [ step, name: "output" ]
26
+ end
27
+
28
+ def self.Failure()
29
+ step = ->(input, options) do
30
+ puts "Error: " + options['failure_message']
31
+ options['error_code'] ? exit(options['error_code']) : exit(1)
32
+ end
33
+
34
+ [ step, name: "failure" ]
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ module Trailblazer
2
+ module Generator
3
+ class Output
4
+ attr_reader :path, :content
5
+
6
+ def initialize(path:, content:)
7
+ @path = path
8
+ @content = content
9
+ end
10
+
11
+ def save
12
+ FileUtils.mkdir_p(File.dirname(path))
13
+ File.open(path, 'w') do |f|
14
+ f.puts content
15
+ end
16
+ true
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Trailblazer
2
+ module Generator
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trailblazer/generator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trailblazer-generator"
8
+ spec.version = Trailblazer::Generator::VERSION
9
+ spec.authors = ["Celso Fernandes", "Nick Sutterer"]
10
+ spec.email = ["celso.fernandes@gmail.com", "apotonick@gmail.com"]
11
+
12
+ spec.summary = %q{Generators for trailblazer.}
13
+ spec.description = %q{Generate trailblazer files from your command file}
14
+ spec.homepage = "https://github.com/trailblazer/trailblazer-generator"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "bin"
21
+ spec.executables = ["trailblazer"]
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "cells"
25
+ spec.add_dependency "cells-erb"
26
+ spec.add_dependency "trailblazer"
27
+ spec.add_dependency "thor"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.14"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "minitest", "~> 5.0"
32
+ spec.add_development_dependency "guard"
33
+ spec.add_development_dependency "guard-minitest"
34
+ spec.add_development_dependency "fakefs"
35
+ end
metadata ADDED
@@ -0,0 +1,228 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trailblazer-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Celso Fernandes
8
+ - Nick Sutterer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-11-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cells
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: cells-erb
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: trailblazer
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: thor
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: bundler
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1.14'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.14'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '10.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '10.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: minitest
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '5.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '5.0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: guard
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: guard-minitest
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: fakefs
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ description: Generate trailblazer files from your command file
155
+ email:
156
+ - celso.fernandes@gmail.com
157
+ - apotonick@gmail.com
158
+ executables:
159
+ - trailblazer
160
+ extensions: []
161
+ extra_rdoc_files: []
162
+ files:
163
+ - ".gitignore"
164
+ - ".travis.yml"
165
+ - Gemfile
166
+ - Guardfile
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bin/console
171
+ - bin/setup
172
+ - bin/trailblazer
173
+ - lib/trailblazer/generator.rb
174
+ - lib/trailblazer/generator/builder.rb
175
+ - lib/trailblazer/generator/builder/cell.rb
176
+ - lib/trailblazer/generator/builder/cell/edit.erb
177
+ - lib/trailblazer/generator/builder/cell/generic.erb
178
+ - lib/trailblazer/generator/builder/cell/index.erb
179
+ - lib/trailblazer/generator/builder/cell/item.erb
180
+ - lib/trailblazer/generator/builder/cell/new.erb
181
+ - lib/trailblazer/generator/builder/cell/show.erb
182
+ - lib/trailblazer/generator/builder/contract.rb
183
+ - lib/trailblazer/generator/builder/contract/generic.erb
184
+ - lib/trailblazer/generator/builder/operation.rb
185
+ - lib/trailblazer/generator/builder/operation/create.erb
186
+ - lib/trailblazer/generator/builder/operation/delete.erb
187
+ - lib/trailblazer/generator/builder/operation/generic.erb
188
+ - lib/trailblazer/generator/builder/operation/index.erb
189
+ - lib/trailblazer/generator/builder/operation/show.erb
190
+ - lib/trailblazer/generator/builder/operation/update.erb
191
+ - lib/trailblazer/generator/builder/view.rb
192
+ - lib/trailblazer/generator/builder/view/generic.erb
193
+ - lib/trailblazer/generator/cell.rb
194
+ - lib/trailblazer/generator/cli.rb
195
+ - lib/trailblazer/generator/cli/generate.rb
196
+ - lib/trailblazer/generator/cli/generate/cell.rb
197
+ - lib/trailblazer/generator/cli/generate/contract.rb
198
+ - lib/trailblazer/generator/cli/generate/operation.rb
199
+ - lib/trailblazer/generator/inflector.rb
200
+ - lib/trailblazer/generator/macro.rb
201
+ - lib/trailblazer/generator/output.rb
202
+ - lib/trailblazer/generator/version.rb
203
+ - trailblazer-generator.gemspec
204
+ homepage: https://github.com/trailblazer/trailblazer-generator
205
+ licenses:
206
+ - MIT
207
+ metadata: {}
208
+ post_install_message:
209
+ rdoc_options: []
210
+ require_paths:
211
+ - lib
212
+ required_ruby_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ required_rubygems_version: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - ">="
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ requirements: []
223
+ rubyforge_project:
224
+ rubygems_version: 2.6.13
225
+ signing_key:
226
+ specification_version: 4
227
+ summary: Generators for trailblazer.
228
+ test_files: []