yop 0.0.2 → 0.0.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: ce8d5a9ab1ad1beba6340296cd5e98942ac659bf
4
- data.tar.gz: 3e33e2d2ac37d54986dfc75e605828dd232d3bb4
3
+ metadata.gz: 48cdd693ddc63341281872a917f15d02c137bef9
4
+ data.tar.gz: 5d3d39003aead9c3f03eff542dde957ed60e9e4d
5
5
  SHA512:
6
- metadata.gz: e6f44612f40053437ba7fae152cb5619f86e4ad71e8596af49ee0fa81a484a599568746788b84ff35394c0258a948b262592c7de061c180e1f4592f52faa5ab7
7
- data.tar.gz: 4a6d1b1e27767dec20b48b162a8a31c57c730d160d388b0c6ada55d2de2d836b072b4aca3fbe799a44ea6a25f3cde25c35c1744128ed0e62f1895dccc0f74332
6
+ metadata.gz: 374870859aa358c2727f47ff5fc8a6f0e93fb3239eee8b6a213e5d608bfb59090a2e262ba4b7dda5ea09020eff5f83508a9c6a8bf8b8599a8b7fb850aa8cffa3
7
+ data.tar.gz: 3ec5f24284bcbfb77d2cf6c3f08d0c949f10563ce08f5a1674fb07e40cee1fffb3e9d4c8c2554443a18a44d7c65d3d60f77a70b798f7f7e4aa6753c76a3ad365
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -9,6 +9,9 @@ class NonExistentTemplate < YopException; end
9
9
  # Raised when a template variable is undefined
10
10
  class UndefinedTemplateVariable < YopException; end
11
11
 
12
+ # Raised when a dynamic template variable is undefined
13
+ class UndefinedDynamicTemplateVariable < UndefinedTemplateVariable; end
14
+
12
15
  # Raised when a template variable has a wrong value
13
16
  class BadTemplateVariableValue < YopException; end
14
17
 
@@ -34,6 +34,9 @@ module Yop
34
34
  # @return [Yop::UI]
35
35
  attr_writer :ui
36
36
 
37
+ # The prefix used for dynamic variables
38
+ DYNAMIC_PREFIX = Regexp.escape "!"
39
+
37
40
  # Create a new template from a base directory
38
41
  # @param base_directory [String] a path to an existing directory which will
39
42
  # be used as a source when this template will be applied
@@ -105,6 +108,10 @@ module Yop
105
108
  [/\.git/, /.~$/, /__pycache__/].any? { |reg| path =~ reg }
106
109
  end
107
110
 
111
+ def dynamic_var?(name)
112
+ name && name =~ /^#{DYNAMIC_PREFIX}/
113
+ end
114
+
108
115
  # Replace vars in a file
109
116
  # @param source [String] the file path
110
117
  def replace_vars(source)
@@ -116,7 +123,12 @@ module Yop
116
123
  def replace_vars_in_string(text)
117
124
  text.gsub(@var_pattern) do
118
125
  name = Regexp.last_match[1]
119
- @vars[name] || @vars[name.to_sym] || get_var(name)
126
+
127
+ if dynamic_var? name
128
+ get_dynamic_var name
129
+ else
130
+ @vars[name] || @vars[name.to_sym] || get_var(name)
131
+ end
120
132
  end
121
133
  end
122
134
 
@@ -131,12 +143,24 @@ module Yop
131
143
  @vars[name] = (@ui ||= Yop::TerminalUI.new).get_var(name)
132
144
  end
133
145
 
146
+ # Return a value for a dynamic variable.
147
+ # @param name [String]
148
+ def get_dynamic_var(name)
149
+ case name[DYNAMIC_PREFIX.size..-1]
150
+ # {(!CURRENT_YEAR)} -> the current year (four digits)
151
+ when "CURRENT_YEAR"
152
+ Time.now.year.to_s
153
+ else
154
+ fail UndefinedDynamicTemplateVariable, name
155
+ end
156
+ end
157
+
134
158
  # Initialize @var_pattern
135
159
  def compile_var_pattern!
136
160
  opening = Regexp.escape(@config[:before_var] || "{(")
137
161
  closing = Regexp.escape(@config[:after_var] || ")}")
138
162
 
139
- @var_pattern = /#{opening}([_A-Z][_A-Z0-9]*)#{closing}/
163
+ @var_pattern = /#{opening}(#{DYNAMIC_PREFIX}?[_A-Z][_A-Z0-9]*)#{closing}/
140
164
  end
141
165
 
142
166
  # Try to mirror the permissions from a file to another
@@ -3,6 +3,6 @@
3
3
  module Yop
4
4
  # @return [String] the current gem's version
5
5
  def self.version
6
- "0.0.2"
6
+ "0.0.3"
7
7
  end
8
8
  end
@@ -207,6 +207,28 @@ class YopTemplatesTests < YopTestCase
207
207
  assert_not_directory "#{dest}/{(SOME_VAR)}"
208
208
  end
209
209
 
210
+ def test_apply_dir_with_dynamic_variable_placeholder
211
+ capture_output!
212
+ set_input "v1"
213
+
214
+ year = Time.now.year.to_s
215
+ mkdir_p "templates/foo/{(!CURRENT_YEAR)}"
216
+ Yop.config! :vars => {:CURRENT_YEAR => "v2"}
217
+ t = Yop.get_template("foo")
218
+ dest = "#{Yop.home}/tmp/test-foo"
219
+ assert_nothing_raised { t.apply dest }
220
+ assert_directory "#{dest}/#{year}"
221
+ assert_not_directory "#{dest}/v1"
222
+ assert_not_directory "#{dest}/v2"
223
+ end
224
+
225
+ def test_apply_dir_with_undefined_dynamic_variable_placeholder
226
+ mkdir_p "templates/foo/{(!SOME_VAR)}"
227
+ t = Yop.get_template("foo")
228
+ dest = "#{Yop.home}/tmp/test-foo"
229
+ assert_raise(UndefinedDynamicTemplateVariable) { t.apply dest }
230
+ end
231
+
210
232
  # placeholders in files
211
233
 
212
234
  def test_apply_file_with_variable_placeholders_in_content
@@ -234,6 +256,19 @@ EOS
234
256
  assert_equal expected, File.read("#{dest}/README")
235
257
  end
236
258
 
259
+ def test_apply_file_with_dynamic_variable_placeholders_in_content
260
+ mkdir_p "templates/foo"
261
+ File.open("#{Yop.home}/templates/foo/LICENSE", "w") do |f|
262
+ f.write "(c) {(!CURRENT_YEAR)} Someone"
263
+ end
264
+ t = Yop.get_template("foo")
265
+ dest = "#{Yop.home}/tmp/test-foo"
266
+ assert_nothing_raised { t.apply dest }
267
+ expected = "(c) #{Time.now.year} Someone"
268
+ assert_true File.file?("#{dest}/LICENSE")
269
+ assert_equal expected, File.read("#{dest}/LICENSE")
270
+ end
271
+
237
272
  # UI variables
238
273
 
239
274
  def test_apply_dir_with_var_in_path_values_from_terminal
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Baptiste Fontaine
@@ -30,7 +30,7 @@ cert_chain:
30
30
  QnOQb8zHyNL+gq2m/mnZGrSehx+6AujokjOfHbmivYMfDATOQQx0eIBI18IhacZm
31
31
  42WxhhIV2bwDtd77
32
32
  -----END CERTIFICATE-----
33
- date: 2015-02-11 00:00:00.000000000 Z
33
+ date: 2015-02-12 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: trollop
metadata.gz.sig CHANGED
@@ -1,4 +1,3 @@
1
- ��X��-�i��#���|g+o$ CGNW�/v�T8a-|*-�3��*
2
- ���:��J�䦔�z��;�PB�������S=ʭ.O���6�"�OM�\c�x�)�m�!�
3
-
4
- LW���]��j{-�:�|m��+*|�,���.�P��
1
+ }�P����O!S#�Tw"��Uj�d ��RkTj2JMu_�O����ԧb�Çf4Kdk���. ѹ=D�10��%v�Q� "�K�g��=�n�e��ԍ��D%Q��%%���J'���
2
+ ��2�z��P��
3
+ ��Q�%���a��XԹ�k!WtQ��"Z�^��lU�/��4�