config_for 0.2.0 → 0.2.1

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: 913db2240a68c47d331fd92b549f037e8e51e50a
4
- data.tar.gz: ac3a6cb0e0491c7b0bbe1aa5abb076fa49d08665
3
+ metadata.gz: 4850c2ab8e9a71441f135fd051f6e881c60ca19e
4
+ data.tar.gz: 1acc6f6db0874c566c3320fc47a9a0e278bc4047
5
5
  SHA512:
6
- metadata.gz: 98e1d460ae50bfee669183ffa146e4e9cadf3d26982dda1599e5d15f2eb875646cb37cfd132e87f467b13b961449bb409639ce03fb7366b314c23ac158ed4d17
7
- data.tar.gz: b6bef0f6bc1ae0b202273367e209eac7e41f9816fff8699e7898730062c6fe631ae0e002bc28fa3e043bd20d7028091e9593661de86819638137ad7af3181c5c
6
+ metadata.gz: c4372aa9f7ed61367d6282037b440b0863658fb1b7c407dddf39a65b1f29de34077f0c27e36b00ab14252dd01fca6fb46c6b236b9e2d0b1c04a367d3ec0d98fb
7
+ data.tar.gz: 9af6528f541c9f88e467be6cb18a58aa89a60a09545e015b9970ee074d8c1b7de94485c2870301d26056425ce2d4fc4da896f0cc3ff4ca430693c141d203df66
@@ -8,6 +8,78 @@ require 'yaml'
8
8
  module ConfigFor
9
9
  module Capistrano
10
10
 
11
+ class UploadFileTask < ::Rake::TaskLib
12
+
13
+ attr_reader :path, :tempfile
14
+
15
+ attr_accessor :generator
16
+ # Rake Task generator for uploading files through Capistrano
17
+ #
18
+ # @example generating task for config/unicorn.rb
19
+ # ConfigFor::Capistrano::UploadFileTask.new('config/unicorn.rb', roles: :web) do |file|
20
+ # file.write('some template')
21
+ # end
22
+ #
23
+ # Rake::Task['config/unicorn.rb'].invoke # uploads that file if it does not exist
24
+ # @param [Pathname, String] path the path of the file to be uploaded
25
+ # @param [Hash] options the options
26
+ # @option options [Array<Symbol>,Symbol] :roles (:all) the roles of servers to apply to
27
+ # @yieldparam [Tempfile] file yields the tempfile so you generate the file to be uploaded
28
+ def initialize(path, options = {}, &block)
29
+ @path = path
30
+ @roles = options.fetch(:roles, :all)
31
+ @tempfile = ::Tempfile.new(File.basename(@path))
32
+ @generator = block || ->(_file){ puts 'Did not passed file generator' }
33
+
34
+ define
35
+ end
36
+
37
+ private
38
+
39
+ include ::Capistrano::DSL
40
+
41
+ def define
42
+ desc "Upload file to #{path}"
43
+ remote_file(path => @tempfile.path, roles: @roles)
44
+ desc "Generate file #{@path} to temporary location"
45
+
46
+ generate_file(@tempfile.path, &method(:generate))
47
+ end
48
+
49
+ def generate(*)
50
+ @generator.call(@tempfile)
51
+ @tempfile.close(false)
52
+ end
53
+
54
+ # So it is always generated, because it is always needed
55
+ def generate_file(task, &block)
56
+ GenerateFileTask.define_task(task, &block)
57
+ end
58
+
59
+ # This reimplements Capistrano::DSL::TaskEnhancements#remote_file
60
+ # but uses UploadTask instead of Rake::Task
61
+
62
+ # TODO: can be removed when https://github.com/capistrano/capistrano/pull/1144
63
+ # is merged and released
64
+
65
+ def remote_file(task)
66
+ target_roles = task.delete(:roles)
67
+
68
+ UploadTask.define_task(task) do |t|
69
+ prerequisite_file = t.prerequisites.first
70
+ file = shared_path.join(t.name).to_s.shellescape
71
+
72
+ on roles(target_roles) do
73
+ unless test "[ -f #{file} ]"
74
+ info "Uploading #{prerequisite_file} to #{file}"
75
+ upload! File.open(prerequisite_file), file
76
+ end
77
+ end
78
+
79
+ end
80
+ end
81
+ end
82
+
11
83
  # inspired by https://github.com/rspec/rspec-core/blob/6b7f55e4fb36226cf830983aab8da8308f641708/lib/rspec/core/rake_task.rb
12
84
 
13
85
  # Rake Task generator for generating and uploading config files through Capistrano.
@@ -16,6 +88,10 @@ module ConfigFor
16
88
  # @example changing the folder
17
89
  # ConfigFor::Capistrano::Task.new(:database) { |task| task.folder = 'configuration' }
18
90
  class Task < ::Rake::TaskLib
91
+ # @!attribute config
92
+ # @return [ConfigFor::Capistrano::UploadFileTask] the task used to generate the config
93
+ attr_reader :config
94
+
19
95
  # @!attribute name
20
96
  # @return [String] the name of the task and subtasks namespace
21
97
  attr_accessor :name
@@ -24,10 +100,6 @@ module ConfigFor
24
100
  # @return [String] folder to upload the generated config
25
101
  attr_accessor :folder
26
102
 
27
- # @!attribute tempfile
28
- # @return [Tempfile] temporary file for generating the config before upload
29
- attr_accessor :tempfile
30
-
31
103
  # Generates new tasks with for uploading #name
32
104
  # @param [String, Symbol] name name of this tasks and subtasks
33
105
  # @param &block gets evaluated before defining the tasks
@@ -36,17 +108,18 @@ module ConfigFor
36
108
  @name = name
37
109
  @folder = 'config'
38
110
  @file = "#{name}.yml"
39
- @roles = :all
40
- @tempfile = ::Tempfile.new(@file)
41
111
  @variable = "#{name}_yml".to_sym
112
+ @roles = :all
42
113
 
43
114
  yield(self) if block_given?
44
115
 
116
+ @config = ConfigFor::Capistrano::UploadFileTask.new(path, roles: @roles, &method(:generate))
117
+
45
118
  desc "Generate #{name} uploader" unless ::Rake.application.last_comment
46
119
  define
47
120
  end
48
121
 
49
- # Path where will be the file uploaded
122
+
50
123
  # Is a join of #folder and #file
51
124
  def path
52
125
  ::File.join(@folder, @file)
@@ -66,7 +139,7 @@ module ConfigFor
66
139
  config.deep_stringify_keys
67
140
  else
68
141
  # TODO: remove when dropping rails 3 support
69
- # two level stringify for rails 3 compatability
142
+ # two level stringify for rails 3 compatibility
70
143
  config.stringify_keys.each_value(&:stringify_keys!)
71
144
  end
72
145
  stringified.to_yaml
@@ -76,6 +149,10 @@ module ConfigFor
76
149
 
77
150
  include ::Capistrano::DSL
78
151
 
152
+ def generate(file)
153
+ file.write(yaml)
154
+ end
155
+
79
156
  def define
80
157
  namespace name do
81
158
  desc "Upload #{path} to remote servers"
@@ -100,47 +177,15 @@ module ConfigFor
100
177
 
101
178
  desc "Reset #{name} config"
102
179
  task reset: [:remove, :upload]
103
-
104
- task :generate do
105
- @tempfile.write(yaml)
106
- @tempfile.close(false)
107
- end
108
180
  end
109
181
 
110
- remote_file path => @tempfile.path, roles: @roles
111
- file @tempfile.path => "#{name}:generate"
112
-
113
182
  before 'deploy:check:linked_files', @name
114
183
 
115
184
  desc "Generate #{path}"
116
185
  task(name, &method(:run_task))
117
186
  end
118
-
119
- # This reimplements Capistrano::DSL::TaskEnhancements#remote_file
120
- # but uses UploadTask instead of Rake::Task
121
-
122
- # TODO: can be removed when https://github.com/capistrano/capistrano/pull/1144
123
- # is merged and released
124
-
125
- def remote_file(task)
126
- target_roles = task.delete(:roles)
127
-
128
- UploadTask.define_task(task) do |t|
129
- prerequisite_file = t.prerequisites.first
130
- file = shared_path.join(t.name)
131
-
132
- on roles(target_roles) do
133
- unless test "[ -f #{file} ]"
134
- info "Uploading #{prerequisite_file} to #{file}"
135
- upload! File.open(prerequisite_file), file
136
- end
137
- end
138
-
139
- end
140
- end
141
187
  end
142
188
 
143
-
144
189
  private
145
190
 
146
191
  # Inheriting from Rake::FileCreationTask because it does not scope
@@ -152,5 +197,11 @@ module ConfigFor
152
197
  true
153
198
  end
154
199
  end
200
+
201
+ class GenerateFileTask < Rake::FileTask
202
+ def needed?
203
+ true
204
+ end
205
+ end
155
206
  end
156
207
  end
@@ -60,8 +60,9 @@ module ConfigFor
60
60
 
61
61
  def parse
62
62
  content = read
63
- erb = ::ERB.new(content).result
64
- ::YAML.load(erb, @pathname)
63
+ erb = ::ERB.new(content)
64
+ erb.filename = @pathname.to_s
65
+ ::YAML.load(erb.result, @pathname)
65
66
  rescue ::Psych::SyntaxError => e
66
67
  fail ConfigFor::InvalidConfigError, "YAML syntax error occurred while parsing #{content}. Error: #{e.message}"
67
68
  end
@@ -1,3 +1,3 @@
1
1
  module ConfigFor
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -1,20 +1,50 @@
1
1
  require 'config_for/capistrano'
2
2
 
3
- RSpec.describe ConfigFor::Capistrano do
4
-
5
- end
3
+ module RspecSupport
4
+ def rake_task(task)
5
+ Rake::Task[task]
6
+ end
6
7
 
7
- RSpec.describe ConfigFor::Capistrano::Task do
8
8
  def invoke(task, *args)
9
9
  rake_task(task).invoke(*args)
10
10
  end
11
11
 
12
- def rake_task(task)
13
- Rake::Task[task]
12
+ def self.included(base)
13
+ base.before do
14
+ Rake.application = Rake::Application.new
15
+ end
14
16
  end
17
+ end
18
+
19
+ RSpec.describe ConfigFor::Capistrano do
20
+
21
+ end
22
+
23
+ RSpec.describe ConfigFor::Capistrano::UploadFileTask do
24
+ include RspecSupport
25
+
26
+ let!(:task_name) { self.class.description }
27
+ subject!(:task) { described_class.new(task_name) }
28
+
29
+ context 'config/unicorn.rb' do
30
+ context 'upload_task' do
31
+ subject(:upload_task) { rake_task(task.path) }
32
+ it { expect(upload_task.prerequisites).to eq([task.tempfile.path]) }
33
+ end
34
+
35
+ context 'file_task' do
36
+ subject(:file_task) { rake_task(task.tempfile.path) }
37
+
38
+ it { expect(file_task).to be }
39
+ it { expect(file_task).to be_needed }
40
+ end
41
+ end
42
+ end
43
+
44
+ RSpec.describe ConfigFor::Capistrano::Task do
45
+ include RspecSupport
15
46
 
16
47
  before do
17
- Rake.application = Rake::Application.new
18
48
  Rake::Task.define_task('deploy:check:linked_files')
19
49
  end
20
50
 
@@ -35,14 +65,9 @@ RSpec.describe ConfigFor::Capistrano::Task do
35
65
 
36
66
  let(:prerequisites) { subject.prerequisites }
37
67
 
38
- context 'tempfile' do
39
- subject { rake_task(task.tempfile.path) }
40
- it { expect(prerequisites).to eq(['database:generate']) }
41
- end
42
-
43
68
  context 'remote_file' do
44
69
  subject { rake_task('config/database.yml') }
45
- it { expect(prerequisites).to eq([task.tempfile.path]) }
70
+ it { is_expected.to be }
46
71
  end
47
72
 
48
73
  context 'upload' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Cichra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-06 00:00:00.000000000 Z
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler