bgem 0.0.13 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/bgem +1 -1
  3. data/lib/bgem.rb +207 -55
  4. metadata +9 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9dd5cdb3ff9091a51601230896ac2ea854d3844c422000ce223b83d69e98d7bc
4
- data.tar.gz: fb6ba38b8f63bb1c0537bee6769654772dc33545744c2507ee14ff0b8ec56aa7
3
+ metadata.gz: 744a132743813a9c03884f2344fa254efb0d7408e912294a6be9ac41a1b2e232
4
+ data.tar.gz: 9bce86aa1f05872cb5a5421bc55751bab95daa2e5bc04ef32d2d9376ff4d4258
5
5
  SHA512:
6
- metadata.gz: 9dac7f98b123fd0f0c13bd4d271672471feada32915cd6a819bb688147f75b358cda34a0e10a2ef7f25fc93d2501e1768ab7a82a288d523b233d21640a2b3e5b
7
- data.tar.gz: 31c9c88df1d4eba032cf5945873c11ced2edbbad73ca0b54556e1add0ca479e36bec4fc10f376698b3a9d685f0cc3863f1ad3b3d9cc19738b40fa4082713bad1
6
+ metadata.gz: 774c5ae4db0e64e4c5e66a3688046b715ed5899119840ba0556b9ee97bb696dbcfef129eb4f452d89875b05b00f762e146fd7fafa311ced3fd0a8b20af801391
7
+ data.tar.gz: '08e34b6531e63485dbb73535c98cbe292428d522f2f9782cfcd8356d56dfe9334f6790256f79f358bc65547b26338793aa79f55114d203b3775cb76fe1953844'
data/bin/bgem CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'bgem'
4
- Bgem.run
4
+ Bgem::CLI.run
data/lib/bgem.rb CHANGED
@@ -1,4 +1,17 @@
1
1
  module Bgem
2
+ module CLI
3
+ def self.run
4
+ case ARGV[0]
5
+ when '-v', '--version'
6
+ puts VERSION
7
+ else
8
+ Bgem.run
9
+ end
10
+ end
11
+ end
12
+
13
+ VERSION = '0.1.1'
14
+
2
15
  require 'pathname'
3
16
  require 'string/indent'
4
17
 
@@ -16,96 +29,231 @@ module Bgem
16
29
  end
17
30
 
18
31
  class Config
32
+ attr_accessor :entry, :output, :scope
19
33
  def initialize config_file
20
34
  @entry, @output, @scope = SOURCE_FILE, 'output.rb', nil
21
35
  DSL.new self, (IO.read config_file)
22
- end
23
36
 
24
- attr_accessor :entry, :output, :scope
37
+ @dir = Pathname File.dirname config_file
38
+ define_macros
39
+ end
25
40
 
41
+ def define_macros
42
+ Output::Ext.file_extensions.map do |type|
43
+ dir = @dir + type.to_s
44
+ MacroDir.new(type, dir) if dir.directory?
45
+ end.compact.each do |macro_dir|
46
+ macro_dir.define_macros
47
+ end
48
+ end
49
+
26
50
  class DSL
27
51
  def initialize config, code
28
52
  @config = config
29
53
  instance_eval code
30
54
  end
31
-
55
+
32
56
  def entry file
33
57
  @config.entry = file
34
58
  end
35
-
59
+
36
60
  def output file
37
61
  @config.output = file
38
62
  end
39
-
63
+
40
64
  def inside *headers
41
65
  @config.scope = headers
42
66
  end
43
67
  end
68
+
69
+ class MacroDir
70
+ def initialize type, dir
71
+ @type, @dir = type, dir
72
+ @constant = Output::Exts.const_get @type.upcase
73
+ end
74
+
75
+ def define_macros
76
+ files = @dir.glob '*.rb'
77
+
78
+ files.each do |file|
79
+ n = file.basename.to_s.split('.').first
80
+ constant = @constant; k = Class.new { include constant }
81
+ to_s = "define_method :to_s do\n#{file.read}\nend"
82
+ k.instance_eval to_s
83
+ constant.const_set n.capitalize, k
84
+ end
85
+ end
86
+ end
44
87
  end
45
88
 
46
89
  class Output
47
90
  class Ext
48
91
  module StandardHooks
49
- def pre
50
- pattern = @dir.join "#{__method__}.#{@name}/*.rb"
51
- concatenate pattern
92
+ module DSL
93
+ def hook hook_name, default: false
94
+ define_method hook_name do
95
+ directory = @dir + "#{hook_name}.#{@name}"
96
+
97
+ if default
98
+ default_directory = @dir + @name
99
+ directory = default_directory unless directory.directory?
100
+ end
101
+
102
+ concatenate sorted_files_in directory
103
+ end
104
+ end
52
105
  end
53
-
54
- def post
55
- patterns = ["#{@name}/*.rb", "post.#{@name}/*.rb"].map do |pattern|
56
- @dir.join pattern
106
+
107
+ module Helpers
108
+ def concatenate files
109
+ files.map do |file|
110
+ Output.new(file, indent: INDENT).to_s
111
+ end.join "\n\n"
112
+ end
113
+
114
+ def sorted_files_in directory
115
+ patterns = Ext.file_extensions.map do |ext|
116
+ directory.join "*.#{ext}"
117
+ end
118
+
119
+ files = Dir[*patterns]
120
+ order_file = directory.join 'order'
121
+
122
+ if order_file.exist?
123
+ sort_with_order_file order_file, files
124
+ else
125
+ files.sort
126
+ end
127
+ end
128
+
129
+ def sort_with_order_file order_file, files
130
+ order = order_file.readlines.map &:chomp
131
+
132
+ files_ordered_to_be_first = order.inject([]) do |array, name|
133
+ files_starting_with_name = files.select do |file|
134
+ basename = File.basename file
135
+ basename.start_with? "#{name}."
136
+ end
137
+ array += files_starting_with_name
138
+ end
139
+
140
+ other_files = files - files_ordered_to_be_first
141
+ files_ordered_to_be_first += other_files.sort
57
142
  end
58
-
59
- concatenate *patterns
60
143
  end
144
+
145
+ extend DSL
146
+ include Helpers
61
147
 
62
- def concatenate *patterns
63
- Dir[*patterns].sort.map do |file|
64
- Output.new(file, indent: INDENT).to_s
65
- end.join "\n\n"
66
- end
148
+ hook :post, default: true
149
+ hook :pre
67
150
  end
68
151
 
152
+ def self.file_extensions
153
+ Exts.constants.map &:downcase
154
+ end
155
+
156
+ def self.new file_extension:, type:, name:, dir:, code:
157
+ parent_constant = Exts.const_get file_extension.upcase
158
+
159
+ type ||= if parent_constant.respond_to? :default
160
+ parent_constant.default
161
+ else
162
+ 'default'
163
+ end
164
+ constant_name = type.capitalize
165
+
166
+ if parent_constant.const_defined? constant_name
167
+ child_constant = parent_constant.const_get constant_name
168
+ else
169
+ fail "Don't know what to do with '#{type}'. #{parent_constant}::#{constant_name} is not defined."
170
+ end
171
+
172
+ child_constant.new file_extension: file_extension, type: type, name: name, dir: dir, code: code
173
+ end
69
174
 
70
- module RB
71
- def self.new dir:, source:, chain:
72
- unless chain.size == 2
73
- fail "#{chain}' size should be 2"
74
- end
175
+ module Common
176
+ def initialize **kwargs
177
+ @file_extension = kwargs[:file_extension]
178
+ @type = kwargs[:type]
179
+ @name = kwargs[:name]
180
+ @dir = kwargs[:dir]
181
+ @code = kwargs[:code]
75
182
 
76
- name, type = chain
77
- constant = type.capitalize
183
+ setup
184
+ end
78
185
 
79
- if self.const_defined? constant
80
- type = self.const_get constant
81
- else
82
- fail "Don't know what to do with '#{type}'. #{self}::#{constant} is not defined."
83
- end
186
+ attr_reader :file_extension, :type, :name, :dir, :code
84
187
 
85
- type.new dir: dir, source: source, name: name
188
+ def ext
189
+ file_extension
86
190
  end
87
191
 
88
- def initialize dir:, source:, name:
89
- @dir, @source, @name = dir, source, name
90
- setup
192
+ def setup
193
+ end
194
+ end
195
+ end
196
+
197
+ module Exts
198
+
199
+ module ERB
200
+ include Ext::Common
201
+
202
+ class Default
203
+ include ERB
204
+
205
+ def to_s
206
+ <<~S
207
+ module #{@name}
208
+ class Context
209
+ def env
210
+ binding
211
+ end
212
+ end
213
+
214
+ def self.[] params
215
+ env = Context.new.env
216
+ params.each do |name, value|
217
+ env.local_variable_set name, value
218
+ end
219
+
220
+ template = <<~TEMPLATE
221
+ #{@code}TEMPLATE
222
+
223
+ require 'erb'
224
+ renderer = ERB.new template
225
+ renderer.result env
226
+ end
227
+ end
228
+ S
229
+ end
91
230
  end
231
+ end
232
+
233
+ module RB
234
+ include Ext::Common
235
+ include Ext::StandardHooks
92
236
 
93
- def to_s
94
- "#{head}#{source}end"
237
+ def self.default
238
+ 'module'
95
239
  end
96
240
 
97
- private
98
- def setup
99
- end
241
+ attr_reader :head
100
242
 
101
- include StandardHooks
243
+ def to_s
244
+ "#{head}#{body}end"
245
+ end
102
246
 
103
- def source
104
- source = @source.indent INDENT
105
- source.prepend "#{pre}\n\n" unless pre.empty?
106
- source.concat "\n#{post}\n" unless post.empty?
107
- source
108
- end
247
+ def body
248
+ wrap code
249
+ end
250
+
251
+ def wrap code
252
+ code = @code.indent INDENT
253
+ code.prepend "#{pre}\n\n" unless pre.empty?
254
+ code.concat "\n#{post}\n" unless post.empty?
255
+ code
256
+ end
109
257
 
110
258
  class Class
111
259
  include RB
@@ -140,18 +288,22 @@ module Bgem
140
288
  def initialize file = SOURCE_FILE, indent: 0
141
289
  file, @indent = (Pathname file), indent
142
290
 
143
- *chain, last = file.basename.to_s.split '.'
144
- name = last.upcase
145
- source = file.read
146
- dir = file.dirname
291
+ parts = file.basename.to_s.split '.'
147
292
 
148
- if Ext.const_defined? name
149
- ext = Ext.const_get name
293
+ case parts.size
294
+ when 3
295
+ name, type, file_extension = parts
296
+ when 2
297
+ name, file_extension = parts
150
298
  else
151
- fail "Don't know what to do with #{file}. Bgem::Output::Ext::#{name} is not defined."
299
+ fail "#{file} has more than two dots in its name."
152
300
  end
153
301
 
154
- @output = ext.new dir: dir, source: source, chain: chain
302
+ if Exts.const_defined? file_extension.upcase
303
+ @output = Ext.new file_extension: file_extension, type: type, name: name, dir: file.dirname, code: file.read
304
+ else
305
+ fail "Don't know what to do with #{file}. Bgem::Output::Exts::#{file_extension.upcase} is not defined."
306
+ end
155
307
  end
156
308
 
157
309
  def to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bgem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoly Chernov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-29 00:00:00.000000000 Z
11
+ date: 2023-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: string-indent
@@ -24,8 +24,8 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.0.1
27
- description:
28
- email:
27
+ description:
28
+ email:
29
29
  executables:
30
30
  - bgem
31
31
  extensions: []
@@ -37,7 +37,7 @@ homepage: https://github.com/ch1c0t/bgem
37
37
  licenses:
38
38
  - ISC
39
39
  metadata: {}
40
- post_install_message:
40
+ post_install_message:
41
41
  rdoc_options: []
42
42
  require_paths:
43
43
  - lib
@@ -52,8 +52,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  requirements: []
55
- rubygems_version: 3.1.6
56
- signing_key:
55
+ rubygems_version: 3.4.21
56
+ signing_key:
57
57
  specification_version: 4
58
- summary: A tool to build single-file Ruby gems with less ends.
58
+ summary: To make Ruby gems from macros.
59
59
  test_files: []