dslable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/dslable ADDED
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ #!/usr/bin/env ruby
3
+ require 'dslable/version'
4
+ require 'dslable_core'
5
+ require "thor"
6
+
7
+ module Dslable
8
+ #= Dslable CLI
9
+ class CLI < Thor
10
+ class_option :help, :type => :boolean, :aliases => 'h', :desc => 'help message.'
11
+ class_option :version, :type => :boolean, :aliases => 'v', :desc => 'version'
12
+
13
+ desc "generate", "generate gem template"
14
+ def generate(*args)
15
+ Dslable::Core.new.generate
16
+ end
17
+
18
+ desc "init", "generate Dsldefine template"
19
+ def init(*args)
20
+ Dslable::Core.new.init
21
+ end
22
+
23
+ desc "version", "version"
24
+ def version
25
+ p Dslable::VERSION
26
+ end
27
+ end
28
+ end
29
+
30
+ Dslable::CLI.start(ARGV)
data/dslable.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dslable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dslable"
8
+ spec.version = Dslable::VERSION
9
+ spec.authors = ["tbpgr@tbpgr.jp"]
10
+ spec.email = ["tbpgr@tbpgr.jp"]
11
+ spec.description = %q{Dslable is generator for simple gem-code template.}
12
+ spec.summary = %q{Dslable is generator for simple gem-code template.}
13
+ spec.homepage = "https://github.com/tbpgr/dslable"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "thor", "~> 0.18.1"
22
+ spec.add_runtime_dependency "activesupport", "~> 4.0.1"
23
+ spec.add_runtime_dependency "rspec_piccolo", "~> 0.0.6"
24
+ spec.add_runtime_dependency "bundler", "~> 1.3"
25
+ spec.add_runtime_dependency "rspec", "~> 2.14.1"
26
+
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "simplecov", "~> 0.8.2"
29
+ end
@@ -0,0 +1,3 @@
1
+ module Dslable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ module Dslable
4
+ class Args
5
+ attr_accessor :_desc
6
+ attr_accessor :_args_name
7
+ attr_accessor :_klass
8
+ attr_accessor :_required
9
+ attr_accessor :_default_value
10
+
11
+ def initialize
12
+ @_desc = ""
13
+ @_required = false
14
+ end
15
+
16
+ def desc(_desc)
17
+ return if _desc.nil?
18
+ @_desc = _desc
19
+ end
20
+
21
+ def args_name(_args_name)
22
+ raise InvalidArgsError.new("args_name not allow nil") if _args_name.nil?
23
+ raise InvalidArgsError.new("args_name not allow empty") if _args_name.empty?
24
+ raise InvalidArgsError.new("args_name allow /^[a-z0-9_]+$/") unless _args_name =~ /^[a-z0-9_]+$/
25
+ @_args_name = _args_name
26
+ end
27
+
28
+ def klass(_klass)
29
+ raise InvalidArgsError.new("klass not allow nil") if _klass.nil?
30
+ raise InvalidArgsError.new("klass only allow String, Array and Hash") unless [String, Hash, Array].include? (_klass)
31
+ @_klass = _klass
32
+ end
33
+
34
+ def required
35
+ @_required = true
36
+ end
37
+
38
+ def default_value(_default_value)
39
+ @_default_value = _default_value
40
+ end
41
+ end
42
+
43
+ class InvalidArgsError < StandardError;end
44
+ end
@@ -0,0 +1,79 @@
1
+ require "dslable/version"
2
+ require "dslable_field"
3
+ require "dslable_args"
4
+ require "dslable_dsl"
5
+ require "generators/gem_template"
6
+ require "generators/product_codes/cli"
7
+ require "generators/product_codes/core"
8
+ require "generators/product_codes/dsl"
9
+ require "generators/product_codes/dsl_model"
10
+ require "generators/rspec/spec_template"
11
+ require "generators/rspec/specs"
12
+ require "generators/settings/gemfile"
13
+
14
+ module Dslable
15
+ class Core
16
+ DSLDEFINE_FILE = "Dsldefine"
17
+
18
+ DSLDEFINE_TEMPLATE =<<-EOS
19
+ # encoding: utf-8
20
+
21
+ # set your gem name. this is use in rb-filename and class-name
22
+ gem_name "TODO: set your gem_name"
23
+
24
+ # set your bin name
25
+ bin_name "TODO: set your bin_name"
26
+
27
+ # set your dsl filed
28
+ field :field_name1 do |f|
29
+ # set your field description
30
+ f.desc "field1 description"
31
+ f.args :args_name do |a|
32
+ # set your args description
33
+ a.desc "args description"
34
+ # you can use String, Array and Hash
35
+ a.klass String
36
+ # if you want not required, comment out following line
37
+ a.required
38
+ # if you comment out following line, default => nil
39
+ a.default_value "args_value2"
40
+ end
41
+ end
42
+
43
+ # field :field_name2 do |f|
44
+ # f.desc "field2 description"
45
+ # f.args :args_name do |a|
46
+ # a.desc "args description"
47
+ # a.klass String
48
+ # a.required
49
+ # a.default_value "args_value2"
50
+ # end
51
+ # end
52
+ EOS
53
+
54
+ def init
55
+ File.open(DSLDEFINE_FILE, "w") {|f|f.puts DSLDEFINE_TEMPLATE}
56
+ end
57
+
58
+ def generate
59
+ src = read_dsl_define
60
+ # DSLからパラメータの取得
61
+ dsl = Dslable::Dsl.new
62
+ dsl.instance_eval src
63
+ Dslable::Generators::GemTemplate.new(dsl).generate
64
+ Dslable::Generators::Settings::Gemfile.new(dsl).generate
65
+ Dslable::Generators::ProductCodes::Core.new(dsl).generate
66
+ Dslable::Generators::ProductCodes::Dsl.new(dsl).generate
67
+ Dslable::Generators::ProductCodes::DslModel.new(dsl).generate
68
+ Dslable::Generators::ProductCodes::CLI.new(dsl).generate
69
+ Dslable::Generators::RSpec::SpecTemplate.new(dsl).generate
70
+ Dslable::Generators::RSpec::Specs.new(dsl).generate
71
+ end
72
+
73
+ private
74
+ def read_dsl_define
75
+ File.open(DSLDEFINE_FILE) {|f|f.read}
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ require 'active_support/inflector'
3
+
4
+ module Dslable
5
+ class Dsl
6
+ attr_accessor :_gem_name
7
+ attr_accessor :_gem_desc
8
+ attr_accessor :_bin_name
9
+ attr_accessor :fields
10
+
11
+ def initialize
12
+ @fields = []
13
+ end
14
+
15
+ def gem_name(_gem_name)
16
+ raise InvalidDslError.new("gem_name not allow nil") if _gem_name.nil?
17
+ raise InvalidDslError.new("gem_name not allow empty") if _gem_name.empty?
18
+ raise InvalidDslError.new("gem_name allow /^[a-z0-9_]+$/") unless _gem_name =~ /^[a-z0-9_]+$/
19
+ @_gem_name = _gem_name
20
+ end
21
+
22
+ def gem_desc(_gem_desc)
23
+ raise InvalidDslError.new("gem_desc not allow nil") if _gem_desc.nil?
24
+ raise InvalidDslError.new("gem_desc not allow empty") if _gem_desc.empty?
25
+ @_gem_desc = _gem_desc
26
+ end
27
+
28
+ def bin_name(_bin_name)
29
+ raise InvalidDslError.new("bin_name not allow nil") if _bin_name.nil?
30
+ raise InvalidDslError.new("bin_name not allow empty") if _bin_name.empty?
31
+ raise InvalidDslError.new("bin_name allow /^[a-z0-9_]+$/") unless _bin_name =~ /^[a-z0-9_]+$/
32
+ @_bin_name = _bin_name
33
+ end
34
+
35
+ def field(_field_name)
36
+ raise InvalidDslError.new("field name not allow nil") if _field_name.nil?
37
+ raise InvalidDslError.new("field name not allow empty") if _field_name.empty?
38
+ _field = Field.new
39
+ _field.field_name _field_name
40
+ yield _field
41
+ @fields << _field
42
+ end
43
+
44
+ def camelized_gem_name
45
+ _gem_name.camelize
46
+ end
47
+ end
48
+
49
+ class InvalidDslError < StandardError;end
50
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ require "dslable_args"
3
+
4
+ module Dslable
5
+ class Field
6
+ attr_accessor :_field_name
7
+ attr_accessor :_desc
8
+ attr_accessor :_args
9
+
10
+ def initialize
11
+ @_desc = ""
12
+ end
13
+
14
+ def field_name(_name)
15
+ return if _name.nil?
16
+ @_field_name = _name
17
+ end
18
+
19
+ def desc(_desc)
20
+ return if _desc.nil?
21
+ @_desc = _desc
22
+ end
23
+
24
+ def args(args_name)
25
+ raise InvalidFieldError.new("args_name not allow nil") if args_name.nil?
26
+ raise InvalidFieldError.new("args_name not allow empty") if args_name.empty?
27
+ raise InvalidFieldError.new("args_name allow /^[a-z0-9_]+$/. your input is #{args_name}") unless args_name =~ /^[a-z0-9_]+$/
28
+ dslable_args = Dslable::Args.new
29
+ dslable_args._args_name = args_name
30
+ yield dslable_args
31
+ @_args = dslable_args
32
+ end
33
+ end
34
+
35
+ class InvalidFieldError < StandardError;end
36
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require 'generators/generators'
3
+ require 'fileutils'
4
+
5
+ module Dslable::Generators
6
+ # Dslable::Generators Gem Template Generator
7
+ class GemTemplate
8
+ attr_accessor :dsl
9
+
10
+ #== initialize generate gem template
11
+ #=== Params
12
+ #- _dsl: input from dsl
13
+ def initialize(_dsl)
14
+ @dsl = _dsl
15
+ end
16
+
17
+ #== generate gem template
18
+ def generate
19
+ `bundle gem #{@dsl._gem_name}`
20
+ Dir.chdir(@dsl._gem_name)
21
+ FileUtils.rm_rf("./lib/#{@dsl._gem_name}.rb")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ module Dslable
4
+ module Generators
5
+ end
6
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+ require "generators/generators"
3
+ require "erb"
4
+ require 'active_support/inflector'
5
+ require "dslable_dsl"
6
+ require "fileutils"
7
+
8
+ module Dslable::Generators::ProductCodes
9
+ class CLI
10
+ CLI_TEMPLATE =<<-EOF
11
+ #!/usr/bin/env ruby
12
+ # encoding: utf-8
13
+
14
+ require "<%=gem_name%>_core"
15
+ require "<%=gem_name%>/version"
16
+ require "thor"
17
+
18
+ module <%=gem_name_camel%>
19
+ #= <%=gem_name_camel%> CLI
20
+ class CLI < Thor
21
+ class_option :help, :type => :boolean, :aliases => '-h', :desc => 'help message.'
22
+ class_option :version, :type => :boolean, :desc => 'version'
23
+
24
+ desc "execute", "TODO: write your desc"
25
+ def execute
26
+ <%=gem_name_camel%>::Core.new.execute
27
+ end
28
+
29
+ desc "init", "generate <%=dsl_file_name%>"
30
+ def init
31
+ <%=gem_name_camel%>::Core.new.init
32
+ end
33
+
34
+ desc "version", "version"
35
+ def version
36
+ p <%=gem_name_camel%>::VERSION
37
+ end
38
+ end
39
+ end
40
+
41
+ <%=gem_name_camel%>::CLI.start(ARGV)
42
+ EOF
43
+
44
+ attr_accessor :dsl
45
+
46
+ #== initialize dsl model
47
+ #=== Params
48
+ #- _dsl: input from dsl
49
+ def initialize(_dsl)
50
+ raise InvalidDslError.new("dsl not allow nil") if _dsl.nil?
51
+ @dsl = _dsl
52
+ end
53
+
54
+ def generate
55
+ cli_src = adapt_template(@dsl.camelized_gem_name)
56
+ generate_cli_src cli_src
57
+ end
58
+
59
+ private
60
+ def adapt_template(gem_name_camel)
61
+ gem_name = @dsl._gem_name
62
+ dsl_file_name = "#{@dsl._gem_name.camelize.downcase.camelize}file"
63
+ erb = ERB.new(CLI_TEMPLATE)
64
+ erb.result(binding)
65
+ end
66
+
67
+ def generate_cli_src(cli_src)
68
+ FileUtils.mkdir_p("./bin") unless File.exists?("./bin")
69
+ File.open("./bin/#{@dsl._bin_name}", "w") {|f|f.puts cli_src}
70
+ end
71
+ end
72
+
73
+ class InvalidDslError < StandardError;end
74
+ end
@@ -0,0 +1,134 @@
1
+ # encoding: utf-8
2
+ require "dslable_dsl"
3
+ require "generators/generators"
4
+ require "erb"
5
+ require 'active_support/inflector'
6
+
7
+ module Dslable::Generators::ProductCodes
8
+ class Core
9
+ attr_accessor :dsl
10
+ CORE_TEMPLATE =<<-END
11
+ # encoding: utf-8
12
+ require '<%=gem_name%>_dsl'
13
+
14
+ module <%=gem_name_camel%>
15
+ # <%=gem_name_camel%> Core
16
+ class Core
17
+ <%=gem_name_upper%>_FILE = "<%=dsl_file_name%>"
18
+ <%=gem_name_upper%>_TEMPLATE =<<-EOS
19
+ # encoding: utf-8
20
+
21
+ <%=fields%>
22
+ EOS
23
+
24
+ #== generate <%=dsl_file_name%> to current directory.
25
+ def init
26
+ File.open(<%=gem_name_upper%>_FILE, "w") {|f|f.puts <%=gem_name_upper%>_TEMPLATE}
27
+ end
28
+
29
+ #== TODO: write your gem's specific contents
30
+ def execute
31
+ src = read_dsl
32
+ dsl = <%=gem_name_camel%>::Dsl.new
33
+ dsl.instance_eval src
34
+
35
+ # TODO: implement your gem's specific logic
36
+
37
+ end
38
+
39
+ private
40
+ def read_dsl
41
+ File.open(<%=gem_name_upper%>_FILE) {|f|f.read}
42
+ end
43
+ end
44
+ end
45
+ END
46
+
47
+ #== initialize core
48
+ #=== Params
49
+ #- _dsl: input from dsl
50
+ def initialize(_dsl)
51
+ raise InvalidDslError.new("dls not allow nil") if _dsl.nil?
52
+ @dsl = _dsl
53
+ end
54
+
55
+ def generate
56
+ gem_name_camel = @dsl.camelized_gem_name
57
+ gem_name_upper = @dsl._gem_name.upcase
58
+ dsl_file_name = "#{@dsl._gem_name.camelize.downcase.camelize}file"
59
+ fields = get_fields
60
+
61
+ core_src = adapt_template(gem_name_camel, gem_name_upper, dsl_file_name, fields, @dsl._gem_name)
62
+ generate_core_src core_src
63
+ end
64
+
65
+ private
66
+ def get_fields
67
+ field_dsls_all = []
68
+ @dsl.fields.each do |field|
69
+ field_dsls = []
70
+ field_dsls << get_field_desc(field)
71
+ field_dsls << get_field_required(field) if field._args._required
72
+ field_dsls << get_field_klass(field)
73
+ field_dsls << get_field_default(field) unless field._args._default_value.nil?
74
+ field_dsls << get_field_call(field)
75
+ field_dsls << [""]
76
+ field_dsls_all << field_dsls.join("\n")
77
+ end
78
+ field_dsls_all.join("\n")
79
+ end
80
+
81
+ def get_field_desc(field)
82
+ "# #{field._desc}"
83
+ end
84
+
85
+ def get_field_required(field)
86
+ "# #{field._args._args_name} is required"
87
+ end
88
+
89
+ def get_field_klass(field)
90
+ "# #{field._args._args_name} allow only #{field._args._klass}"
91
+ end
92
+
93
+ def get_field_default(field)
94
+ "# #{field._args._args_name}'s default value => #{get_default(field)}"
95
+ end
96
+
97
+ def get_field_call(field)
98
+ "#{field._field_name} #{get_default(field)}"
99
+ end
100
+
101
+ def get_default(field)
102
+ klass = field._args._klass
103
+ default_value = field._args._default_value
104
+ if default_value.nil?
105
+ method("nil_default_#{klass.to_s.downcase}").call
106
+ else
107
+ klass == String ? "\"#{default_value}\"" : default_value
108
+ end
109
+ end
110
+
111
+ def nil_default_string
112
+ "\"your value\""
113
+ end
114
+
115
+ def nil_default_array
116
+ ["your value"]
117
+ end
118
+
119
+ def nil_default_hash
120
+ {"your key" => "your value"}
121
+ end
122
+
123
+ def adapt_template(gem_name_camel, gem_name_upper, dsl_file_name, fields, gem_name)
124
+ erb = ERB.new(CORE_TEMPLATE)
125
+ erb.result(binding)
126
+ end
127
+
128
+ def generate_core_src(core_src)
129
+ File.open("./lib/#{@dsl._gem_name}_core.rb", "w") {|f|f.puts core_src}
130
+ end
131
+ end
132
+
133
+ class InvalidDslError < StandardError;end
134
+ end