bgem 0.0.8 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/bgem.rb +126 -81
  3. metadata +23 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f58220570f36a054b4389676b2fc0e1e234e74f4
4
- data.tar.gz: 499f65c4d8c68f3ba4783bc18228d3338a6d56ab
2
+ SHA256:
3
+ metadata.gz: 9dd5cdb3ff9091a51601230896ac2ea854d3844c422000ce223b83d69e98d7bc
4
+ data.tar.gz: fb6ba38b8f63bb1c0537bee6769654772dc33545744c2507ee14ff0b8ec56aa7
5
5
  SHA512:
6
- metadata.gz: d5ce15f902ab43ec3dce419e54cd9aece4a2f389408381d5a90ae524a8f43e627f92eba429e049607b4aaca2248b733ce060f2cabe50d6c28e88cf27bc4a6dbd
7
- data.tar.gz: 8a1717e4608a8aa7d47b1ebfd2a8b40662b416c1555f419dcfa0a0ba567f0c288c3aff57e83eded4dd11d0477536a6adc062ebce526ce4d22f03781195c1922b
6
+ metadata.gz: 9dac7f98b123fd0f0c13bd4d271672471feada32915cd6a819bb688147f75b358cda34a0e10a2ef7f25fc93d2501e1768ab7a82a288d523b233d21640a2b3e5b
7
+ data.tar.gz: 31c9c88df1d4eba032cf5945873c11ced2edbbad73ca0b54556e1add0ca479e36bec4fc10f376698b3a9d685f0cc3863f1ad3b3d9cc19738b40fa4082713bad1
data/lib/bgem.rb CHANGED
@@ -1,13 +1,6 @@
1
1
  module Bgem
2
2
  require 'pathname'
3
-
4
- using Module.new {
5
- refine String do
6
- def indent number
7
- lines.map { |line| "#{' '*number}#{line}" }.join
8
- end
9
- end
10
- }
3
+ require 'string/indent'
11
4
 
12
5
  INDENT = 2
13
6
  SOURCE_FILE = Dir['src/*.rb'][0]
@@ -16,39 +9,24 @@ module Bgem
16
9
  class << self
17
10
  def run config_file = CONFIG_FILE
18
11
  config = Config.new config_file
19
- target = TargetFile.new config.output, config.scope&.reverse
20
- target.write SourceFile.new(config.entry).to_s
21
- target
22
- end
23
- end
24
-
25
- module CLI
26
- def self.run
27
- options = parse_argv
28
- target = TargetFile.new *options[:output], options[:scope]
29
- target.write SourceFile.new(*options[:input]).to_s
30
- end
31
-
32
- def self.parse_argv
33
- ARGV.map do |string|
34
- key, _colon, value = string.partition ':'
35
- [key.to_sym, value]
36
- end.to_h
12
+ write = Write.new config
13
+ write[Output.new(config.entry).to_s]
14
+ write
37
15
  end
38
16
  end
39
17
 
40
18
  class Config
41
19
  def initialize config_file
42
20
  @entry, @output, @scope = SOURCE_FILE, 'output.rb', nil
43
- DSL.new self, config_file
21
+ DSL.new self, (IO.read config_file)
44
22
  end
45
23
 
46
24
  attr_accessor :entry, :output, :scope
47
25
 
48
26
  class DSL
49
- def initialize config, file
27
+ def initialize config, code
50
28
  @config = config
51
- instance_eval IO.read file
29
+ instance_eval code
52
30
  end
53
31
 
54
32
  def entry file
@@ -59,79 +37,146 @@ module Bgem
59
37
  @config.output = file
60
38
  end
61
39
 
62
- def inside *array_of_strings
63
- @config.scope = array_of_strings
40
+ def inside *headers
41
+ @config.scope = headers
64
42
  end
65
43
  end
66
44
  end
67
45
 
68
- class SourceFile
69
- def initialize file = SOURCE_FILE, indent: 0
70
- @file, @indent = (Pathname file), indent
71
- @source = @file.read
72
- head, @type, _rb = @file.basename.to_s.split '.'
73
- @constant, _colon, @ancestor = head.partition ':'
74
- end
75
-
76
- def to_s
77
- "#{head}#{source}end".indent @indent
78
- end
79
-
80
- private
81
- def head
82
- if @ancestor.empty?
83
- "#{@type} #{@constant}\n"
84
- else
85
- "#{@type} #{@constant} < #{@ancestor}\n"
46
+ class Output
47
+ class Ext
48
+ module StandardHooks
49
+ def pre
50
+ pattern = @dir.join "#{__method__}.#{@name}/*.rb"
51
+ concatenate pattern
52
+ end
53
+
54
+ def post
55
+ patterns = ["#{@name}/*.rb", "post.#{@name}/*.rb"].map do |pattern|
56
+ @dir.join pattern
57
+ end
58
+
59
+ concatenate *patterns
60
+ end
61
+
62
+ def concatenate *patterns
63
+ Dir[*patterns].sort.map do |file|
64
+ Output.new(file, indent: INDENT).to_s
65
+ end.join "\n\n"
86
66
  end
87
67
  end
88
68
 
89
- def source
90
- source = @source.indent INDENT
91
- source.prepend "#{before}\n\n" unless before.empty?
92
- source.concat "\n#{after}\n" unless after.empty?
93
- source
94
- end
95
69
 
96
- def self.concatenate_source_files *symbols
97
- symbols.each do |symbol|
98
- define_method symbol do
99
- pattern = @file.dirname.join "#{__method__}.#{@constant}/*.rb"
100
- Dir[pattern].sort.map do |file|
101
- self.class.new(file, indent: INDENT).to_s
102
- end.join "\n\n"
70
+ module RB
71
+ def self.new dir:, source:, chain:
72
+ unless chain.size == 2
73
+ fail "#{chain}' size should be 2"
74
+ end
75
+
76
+ name, type = chain
77
+ constant = type.capitalize
78
+
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
84
+
85
+ type.new dir: dir, source: source, name: name
86
+ end
87
+
88
+ def initialize dir:, source:, name:
89
+ @dir, @source, @name = dir, source, name
90
+ setup
91
+ end
92
+
93
+ def to_s
94
+ "#{head}#{source}end"
95
+ end
96
+
97
+ private
98
+ def setup
99
+ end
100
+
101
+ include StandardHooks
102
+
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
109
+
110
+ class Class
111
+ include RB
112
+
113
+ def setup
114
+ @name, _colon, @parent = @name.partition ':'
115
+ end
116
+
117
+ def head
118
+ if subclass?
119
+ "class #{@name} < #{@parent}\n"
120
+ else
121
+ "class #{@name}\n"
122
+ end
123
+ end
124
+
125
+ def subclass?
126
+ not @parent.empty?
127
+ end
128
+ end
129
+
130
+ class Module
131
+ include RB
132
+
133
+ def head
134
+ "module #{@name}\n"
103
135
  end
104
136
  end
105
137
  end
138
+ end
139
+
140
+ def initialize file = SOURCE_FILE, indent: 0
141
+ file, @indent = (Pathname file), indent
106
142
 
107
- concatenate_source_files :before, :after
143
+ *chain, last = file.basename.to_s.split '.'
144
+ name = last.upcase
145
+ source = file.read
146
+ dir = file.dirname
147
+
148
+ if Ext.const_defined? name
149
+ ext = Ext.const_get name
150
+ else
151
+ fail "Don't know what to do with #{file}. Bgem::Output::Ext::#{name} is not defined."
152
+ end
153
+
154
+ @output = ext.new dir: dir, source: source, chain: chain
155
+ end
156
+
157
+ def to_s
158
+ @output.to_s.indent @indent
159
+ end
108
160
  end
109
161
 
110
- class TargetFile
111
- def initialize path = 'output.rb', scope = nil
112
- @path = Pathname path
113
- @scope = case scope
114
- when Array
115
- scope
116
- when String
117
- eval scope
118
- end
162
+ class Write
163
+ def initialize config
164
+ @file = Pathname config.output
165
+ @scope = config.scope
119
166
  end
120
167
 
121
- def write string
122
- @path.dirname.mkpath
168
+ attr_reader :file
169
+
170
+ def [] string
171
+ file.dirname.mkpath
123
172
 
124
173
  if @scope
125
- @scope.reverse_each do |head_of_constant_definition|
126
- string = "#{head_of_constant_definition}\n#{string.indent INDENT}\nend"
174
+ @scope.each do |header|
175
+ string = "#{header}\n#{string.indent INDENT}\nend"
127
176
  end
128
177
  end
129
178
 
130
- @path.write "#{string}\n"
131
- end
132
-
133
- def file
134
- @path
179
+ file.write "#{string}\n"
135
180
  end
136
181
  end
137
182
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bgem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
- - Anatoly Chernow
7
+ - Anatoly Chernov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-10 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: string-indent
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
13
27
  description:
14
28
  email:
15
29
  executables:
@@ -19,8 +33,9 @@ extra_rdoc_files: []
19
33
  files:
20
34
  - bin/bgem
21
35
  - lib/bgem.rb
22
- homepage:
23
- licenses: []
36
+ homepage: https://github.com/ch1c0t/bgem
37
+ licenses:
38
+ - ISC
24
39
  metadata: {}
25
40
  post_install_message:
26
41
  rdoc_options: []
@@ -37,9 +52,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
52
  - !ruby/object:Gem::Version
38
53
  version: '0'
39
54
  requirements: []
40
- rubyforge_project:
41
- rubygems_version: 2.6.8
55
+ rubygems_version: 3.1.6
42
56
  signing_key:
43
57
  specification_version: 4
44
- summary: Build gems without too much hassle.
58
+ summary: A tool to build single-file Ruby gems with less ends.
45
59
  test_files: []