seedbank 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.hound.yml +3 -0
- data/.rubocop.yml +18 -0
- data/.rubocop_todo.yml +29 -0
- data/.travis.yml +10 -9
- data/Gemfile +5 -4
- data/README.md +50 -25
- data/Rakefile +2 -1
- data/TODO.txt +3 -3
- data/gemfiles/rake10.gemfile +1 -4
- data/gemfiles/rake11.gemfile +12 -0
- data/gemfiles/{rake0.9.gemfile → rake12.gemfile} +3 -4
- data/lib/seedbank/dsl.rb +65 -43
- data/lib/seedbank/railtie.rb +2 -7
- data/lib/seedbank/runner.rb +19 -32
- data/lib/seedbank/version.rb +2 -1
- data/lib/seedbank.rb +15 -6
- data/lib/tasks/seed.rake +16 -20
- data/seedbank.gemspec +35 -58
- data/test/dummy/Rakefile +1 -0
- data/test/dummy/app/controllers/application_controller.rb +1 -0
- data/test/dummy/config/application.rb +2 -22
- data/test/dummy/config/boot.rb +2 -1
- data/test/dummy/config/environment.rb +1 -0
- data/test/dummy/config/environments/development.rb +3 -7
- data/test/dummy/config/environments/test.rb +4 -9
- data/test/dummy/config/initializers/secret_token.rb +1 -0
- data/test/dummy/config/initializers/session_store.rb +2 -1
- data/test/dummy/config/initializers/wrap_parameters.rb +2 -1
- data/test/dummy/config/routes.rb +1 -0
- data/test/dummy/config.ru +2 -1
- data/test/dummy/db/seeds/circular1.seeds.rb +2 -1
- data/test/dummy/db/seeds/circular2.seeds.rb +2 -1
- data/test/dummy/db/seeds/dependency.seeds.rb +1 -0
- data/test/dummy/db/seeds/dependency2.seeds.rb +1 -0
- data/test/dummy/db/seeds/dependent.seeds.rb +2 -1
- data/test/dummy/db/seeds/dependent_on_nested.seeds.rb +2 -1
- data/test/dummy/db/seeds/dependent_on_several.seeds.rb +2 -1
- data/test/dummy/db/seeds/development/users.seeds.rb +1 -0
- data/test/dummy/db/seeds/no_block.seeds.rb +2 -1
- data/test/dummy/db/seeds/reference_memos.seeds.rb +2 -1
- data/test/dummy/db/seeds/with_block_memo.seeds.rb +2 -1
- data/test/dummy/db/seeds/with_inline_memo.seeds.rb +1 -0
- data/test/dummy/db/seeds.rb +1 -0
- data/test/dummy/script/rails +3 -2
- data/test/lib/seedbank/dsl_test.rb +58 -75
- data/test/lib/seedbank/runner_test.rb +39 -46
- data/test/lib/tasks/seed_rake_test.rb +85 -48
- data/test/test_helper.rb +13 -11
- metadata +50 -24
- data/test/dummy/lib/tasks/dsl.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e92838099b21443451e9fd5a5bfecdf5f3874e65
|
4
|
+
data.tar.gz: 31061861c8211b79dd619fb2017527d748ea2291
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f9258f988c7ac6f2438a6974057c8b09e5fd6692196af040f60dcbf36c3e24c0d6427a0677d1bd670e73f9c75bc449cfab51c8481f1321da2b2fa9fd95d1968
|
7
|
+
data.tar.gz: 5e65fa43aa95cca1612a970e1fd47f11a2f66eb3aafc4ee0ee5f60669aed750f3decf570c3dfc0fb1e0664f6b3122ebaae7cf0983c13c7b47386f94eaed0b637
|
data/.gitignore
CHANGED
data/.hound.yml
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
Style/StringLiteral:
|
3
|
+
Enabled: true
|
4
|
+
EnforcedStyle: single_quotes
|
5
|
+
Style/MutableConstant:
|
6
|
+
Exclude:
|
7
|
+
- 'lib/seedbank/version.rb'
|
8
|
+
Lint/ScriptPermission:
|
9
|
+
Exclude:
|
10
|
+
- 'test/dummy/Rakefile'
|
11
|
+
Metrics/LineLength:
|
12
|
+
Exclude:
|
13
|
+
- 'test/**/*'
|
14
|
+
Max: 132
|
15
|
+
Style/ConditionalAssignment:
|
16
|
+
Enabled: false
|
17
|
+
Layout/EmptyLineAfterMagicComment:
|
18
|
+
Enabled: false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-02-12 18:38:59 +0700 using RuboCop version 0.47.1.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 6
|
10
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
11
|
+
Metrics/BlockLength:
|
12
|
+
Max: 124
|
13
|
+
|
14
|
+
# Offense count: 1
|
15
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
16
|
+
# SupportedStyles: nested, compact
|
17
|
+
Style/ClassAndModuleChildren:
|
18
|
+
Exclude:
|
19
|
+
- 'test/test_helper.rb'
|
20
|
+
|
21
|
+
# Offense count: 4
|
22
|
+
Style/Documentation:
|
23
|
+
Exclude:
|
24
|
+
- 'spec/**/*'
|
25
|
+
- 'test/**/*'
|
26
|
+
- 'lib/seedbank.rb'
|
27
|
+
- 'lib/seedbank/dsl.rb'
|
28
|
+
- 'lib/seedbank/railtie.rb'
|
29
|
+
- 'lib/seedbank/runner.rb'
|
data/.travis.yml
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
language: ruby
|
2
2
|
|
3
3
|
rvm:
|
4
|
-
-
|
5
|
-
-
|
6
|
-
-
|
7
|
-
- 2.
|
8
|
-
- 2.
|
9
|
-
- jruby-18mode # JRuby in 1.8 mode
|
10
|
-
- jruby-19mode # JRuby in 1.9 mode
|
11
|
-
- rbx-2
|
4
|
+
- 2.1
|
5
|
+
- 2.2
|
6
|
+
- 2.3
|
7
|
+
- 2.4
|
8
|
+
- 2.5
|
12
9
|
|
13
10
|
gemfile:
|
14
|
-
- gemfiles/rake0.9.gemfile
|
15
11
|
- gemfiles/rake10.gemfile
|
12
|
+
- gemfiles/rake11.gemfile
|
13
|
+
- gemfiles/rake12.gemfile
|
14
|
+
|
15
|
+
before_install:
|
16
|
+
- gem install bundler -v '~> 1.11' --no-ri --no-rdoc
|
data/Gemfile
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
source 'http://rubygems.org'
|
2
3
|
|
3
4
|
# Specify your gem's dependencies in seedbank.gemspec
|
4
5
|
gemspec
|
5
6
|
|
6
|
-
gem '
|
7
|
+
gem 'rubocop', group: :development
|
7
8
|
|
8
9
|
# for CRuby, Rubinius, including Windows and RubyInstaller
|
9
|
-
gem
|
10
|
+
gem 'sqlite3', platform: %i[ruby mswin mingw]
|
10
11
|
|
11
12
|
# for JRuby
|
12
|
-
gem 'activerecord-jdbcsqlite3-adapter', :
|
13
|
+
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Seedbank
|
2
2
|
========
|
3
3
|
|
4
|
-
Seedbank allows you to structure your
|
4
|
+
Seedbank allows you to structure your apps seed data instead of having it all dumped into one large file. I find my seed data tended to fall into two categories:
|
5
5
|
|
6
6
|
1. Stuff that the entire application requires.
|
7
7
|
2. Stuff to populate my development and staging environments.
|
@@ -14,6 +14,11 @@ The reason behind Seedbank is laziness. When I checkout or re-visit a project I
|
|
14
14
|
|
15
15
|
To achieve this slothful aim, Seedbank renames the original db:seed rake task to db:seed:original, makes it a dependency for all the Seedbank seeds and adds a new db:seed task that loads all the common seeds in db/seeds plus all the seeds for the current Rails environment.
|
16
16
|
|
17
|
+
Although originally built for Rails, Seedbank can work stand alone thanks to Aleksey Ivanov.
|
18
|
+
|
19
|
+
[](https://travis-ci.org/james2m/seedbank)
|
20
|
+
[](https://houndci.com)
|
21
|
+
|
17
22
|
Example
|
18
23
|
=======
|
19
24
|
|
@@ -46,12 +51,16 @@ will load the seeds in `db/seeds.rb`, `db/seeds/bar.seeds.rb` and `db/seeds/foo.
|
|
46
51
|
|
47
52
|
Installation
|
48
53
|
============
|
54
|
+
|
55
|
+
Seedbank > 0.5.0 uses refinements and no longer supports rubies below 2.x. If you are using an older Ruby you'll have to stick with 0.4.0 and below.
|
56
|
+
|
57
|
+
I have also dropped support for Rubinius and JRuby. I'm happy to accept pull requests for them, but don't have the time to hack together the test environment. If you want to contribute, please ensure that he travis.yml is in line as it's the only way I will test these two environments.
|
58
|
+
|
49
59
|
### Rails 5.x
|
50
60
|
|
51
|
-
Seedbank has not been updated to work with Rails 5. I've
|
52
|
-
people are using it with no problems.
|
61
|
+
Seedbank has not been updated to work with Rails 5. I've used it with 5.x apps and am working on a new version specifically for 5.x. Other people are also reporting using it with no problems.
|
53
62
|
|
54
|
-
### Rails
|
63
|
+
### Rails 4.x and above
|
55
64
|
|
56
65
|
Add the seedbank gem to your Gemfile. In Gemfile:
|
57
66
|
|
@@ -61,21 +70,22 @@ gem "seedbank"
|
|
61
70
|
|
62
71
|
That's it!
|
63
72
|
|
64
|
-
### Rails
|
73
|
+
### Non Rails apps
|
65
74
|
|
66
|
-
|
75
|
+
Although originally built for Rails, Seedbank should work fine in other environments such as Padrino, Grape or the new new hotness. please let us know how you get on.
|
67
76
|
|
68
|
-
|
69
|
-
config.gem 'seedbank'
|
70
|
-
```
|
77
|
+
### Rails 3.x
|
71
78
|
|
72
|
-
|
79
|
+
Seedbank 0.5.0 onwards is no longer tested against Rails 3.x, that isn't to say it will not work. I
|
80
|
+
will not fix issues against Rails 3.x, but will accept tested pull requests.
|
73
81
|
|
74
|
-
|
82
|
+
### Rails 2.x
|
75
83
|
|
76
|
-
|
84
|
+
Seedbank hasn't supported Rails 2.x for some time. You'll need to use the 0.2.1 version. In your Gemfile:
|
77
85
|
|
78
|
-
|
86
|
+
```ruby
|
87
|
+
gem "seedbank", '~> 0.2.1'
|
88
|
+
```
|
79
89
|
|
80
90
|
Then in the bottom of your application's Rakefile:
|
81
91
|
|
@@ -89,7 +99,7 @@ If you vendor the gem you'll need to change the require to the specific path.
|
|
89
99
|
Usage
|
90
100
|
=====
|
91
101
|
|
92
|
-
Seeds files are just plain old Ruby executed in your
|
102
|
+
Seeds files are just plain old Ruby executed in your application environment so anything you could type into the console will work in your seeds. Seeds files have to be named with the '.seeds.rb' extension.
|
93
103
|
|
94
104
|
db/seeds/companies.seeds.rb
|
95
105
|
```ruby
|
@@ -134,24 +144,39 @@ after "development:companies" do
|
|
134
144
|
end
|
135
145
|
```
|
136
146
|
|
147
|
+
*Note* - If you experience any errors like `Don't know how to build task 'db:seed:users'`. Ensure you are specifying `after 'development:companies'` like the above example. This is the usual culprit (YMMV).
|
148
|
+
|
137
149
|
### Defining and using methods
|
138
150
|
|
139
|
-
As seed files are evaluated within
|
151
|
+
As seed files are evaluated within a single runner in dependency order, any methods defined earlier in the run will be available across dependent tasks. I
|
152
|
+
recommend keeping method definitions in the seed file that uses them. Alternatively if you have many common methods, put them into a module and extend the
|
153
|
+
runner with the module.
|
140
154
|
|
155
|
+
db/seeds/support.rb
|
141
156
|
```ruby
|
142
|
-
|
143
|
-
def
|
144
|
-
|
145
|
-
# ...
|
157
|
+
module Support
|
158
|
+
def notify(filename)
|
159
|
+
puts "Seeding: #{filename}"
|
146
160
|
end
|
147
161
|
end
|
162
|
+
```
|
148
163
|
|
149
|
-
|
150
|
-
|
151
|
-
|
164
|
+
db/seeds/common.seeds.rb
|
165
|
+
```ruby
|
166
|
+
require_relative 'support'
|
167
|
+
extend Support
|
168
|
+
|
169
|
+
notify(__FILE__)
|
152
170
|
```
|
153
171
|
|
154
|
-
|
172
|
+
To keep this dry you could make the seeds dependent on a support seed that extends the runner.
|
173
|
+
|
174
|
+
db/seeds/users.seeds.rb
|
175
|
+
```ruby
|
176
|
+
after :common do
|
177
|
+
notify(__FILE__)
|
178
|
+
end
|
179
|
+
```
|
155
180
|
|
156
181
|
Contributors
|
157
182
|
============
|
@@ -171,7 +196,7 @@ git log | grep Author | sort | uniq
|
|
171
196
|
* lulalala
|
172
197
|
* pivotal-cloudplanner
|
173
198
|
* vkill
|
174
|
-
|
199
|
+
* Aleksey Ivanov
|
175
200
|
|
176
201
|
Note on Patches/Pull Request
|
177
202
|
============================
|
@@ -185,4 +210,4 @@ Note on Patches/Pull Request
|
|
185
210
|
|
186
211
|
Copyright
|
187
212
|
=========
|
188
|
-
Copyright (c) 2011-
|
213
|
+
Copyright (c) 2011-2017 James McCarthy, released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'bundler/gem_tasks'
|
2
3
|
require 'rake'
|
3
4
|
require 'rake/testtask'
|
@@ -19,4 +20,4 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
19
20
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
20
21
|
end
|
21
22
|
|
22
|
-
task :
|
23
|
+
task default: ['test']
|
data/TODO.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
* Add generator for seeds directories
|
2
|
-
* Add
|
3
|
-
* Add
|
4
|
-
*
|
2
|
+
* Add platform independent test environment
|
3
|
+
* Add generator for seeds data?
|
4
|
+
* Add ability to add or update records in place?
|
data/gemfiles/rake10.gemfile
CHANGED
@@ -9,7 +9,4 @@ gem 'rake', '~>10.0', :group => :test
|
|
9
9
|
gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
|
10
10
|
|
11
11
|
# for JRuby
|
12
|
-
gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
|
13
|
-
|
14
|
-
gem 'seedbank', :path => '../'
|
15
|
-
|
12
|
+
gem 'activerecord-jdbcsqlite3-adapter', '~>1.3.25', :platform => :jruby
|
@@ -0,0 +1,12 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in seedbank.gemspec
|
4
|
+
gemspec :path=>"../"
|
5
|
+
|
6
|
+
gem 'rake', '~>11.0', :group => :test
|
7
|
+
|
8
|
+
# for CRuby, Rubinius, including Windows and RubyInstaller
|
9
|
+
gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
|
10
|
+
|
11
|
+
# for JRuby
|
12
|
+
gem 'activerecord-jdbcsqlite3-adapter', '~>1.3.25', :platform => :jruby
|
@@ -1,14 +1,13 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in seedbank.gemspec
|
4
|
-
gemspec :path
|
4
|
+
gemspec :path=>"../"
|
5
5
|
|
6
|
-
gem 'rake', '~>
|
6
|
+
gem 'rake', '~>12.0', :group => :test
|
7
7
|
|
8
8
|
# for CRuby, Rubinius, including Windows and RubyInstaller
|
9
9
|
gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
|
10
10
|
|
11
11
|
# for JRuby
|
12
|
-
gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
|
12
|
+
gem 'activerecord-jdbcsqlite3-adapter', '~>1.3.25', :platform => :jruby
|
13
13
|
|
14
|
-
gem 'seedbank', :path => '../'
|
data/lib/seedbank/dsl.rb
CHANGED
@@ -1,64 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Seedbank
|
2
3
|
module DSL
|
4
|
+
refine Object do
|
5
|
+
def override_seed_task(*args)
|
6
|
+
task_name, _, deps = Rake.application.resolve_args(args)
|
7
|
+
seed_task = Rake::Task.task_defined?(task_name) ? Rake::Task[task_name].clear : Rake::Task.define_task(task_name)
|
8
|
+
add_comment_to(seed_task, Rake.application.last_description)
|
9
|
+
add_environment_dependency(seed_task)
|
10
|
+
seed_task.enhance deps
|
11
|
+
end
|
3
12
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
13
|
+
def seed_tasks_matching(*pattern)
|
14
|
+
glob_seed_files_matching(*pattern)
|
15
|
+
.sort
|
16
|
+
.map { |seed_file| seed_task_from_file(seed_file) }
|
17
|
+
end
|
10
18
|
|
11
|
-
|
12
|
-
|
13
|
-
|
19
|
+
def seed_task_from_file(seed_file)
|
20
|
+
scopes = scope_from_seed_file(seed_file)
|
21
|
+
fq_name = scopes.push(File.basename(seed_file, '.seeds.rb')).join(':')
|
14
22
|
|
15
|
-
|
16
|
-
|
23
|
+
define_seed_task(seed_file, fq_name)
|
24
|
+
end
|
17
25
|
|
18
|
-
|
19
|
-
|
20
|
-
|
26
|
+
def glob_seed_files_matching(*args, &block)
|
27
|
+
Dir.glob(File.join(seeds_root, *args), &block)
|
28
|
+
end
|
21
29
|
|
22
|
-
|
23
|
-
|
24
|
-
|
30
|
+
def define_seed_task(seed_file, *args)
|
31
|
+
task = Rake::Task.define_task(*args) do |seed_task|
|
32
|
+
runner.evaluate(seed_task, seed_file) if File.exist?(seed_file)
|
33
|
+
end
|
25
34
|
|
26
|
-
|
27
|
-
|
28
|
-
|
35
|
+
relative_file = Pathname.new(seed_file).relative_path_from(Seedbank.application_root)
|
36
|
+
|
37
|
+
task.add_description "Load the seed data from #{relative_file}"
|
38
|
+
add_environment_dependency(task)
|
39
|
+
task.name
|
29
40
|
end
|
30
41
|
|
31
|
-
|
42
|
+
def original_seeds_file
|
43
|
+
@_seedbank_original ||= existent(Pathname.new('../seeds.rb').expand_path(seeds_root))
|
44
|
+
end
|
32
45
|
|
33
|
-
|
34
|
-
|
35
|
-
elsif Rake::Task.task_defined?(':environment')
|
36
|
-
task.enhance([':environment'])
|
46
|
+
def seeds_root
|
47
|
+
Pathname.new Seedbank.seeds_root
|
37
48
|
end
|
38
49
|
|
39
|
-
|
40
|
-
end
|
50
|
+
private
|
41
51
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
relative = dirname.relative_path_from(seeds_root)
|
46
|
-
relative.to_s.split(File::Separator)
|
47
|
-
end
|
52
|
+
def existent(path)
|
53
|
+
String(path) if path.exist?
|
54
|
+
end
|
48
55
|
|
49
|
-
|
50
|
-
|
51
|
-
|
56
|
+
def scope_from_seed_file(seed_file)
|
57
|
+
dirname = Pathname.new(seed_file).dirname
|
58
|
+
return [] if dirname == seeds_root
|
59
|
+
dirname
|
60
|
+
.relative_path_from(seeds_root)
|
61
|
+
.to_s
|
62
|
+
.split(File::Separator)
|
63
|
+
end
|
52
64
|
|
53
|
-
|
65
|
+
def add_environment_dependency(task)
|
66
|
+
if Rake::Task.task_defined?('db:abort_if_pending_migrations')
|
67
|
+
task.enhance(['db:abort_if_pending_migrations'])
|
68
|
+
elsif defined?(Rails)
|
69
|
+
task.enhance([:environment])
|
70
|
+
end
|
71
|
+
end
|
54
72
|
|
55
|
-
|
56
|
-
|
57
|
-
seed_task.comment = comment
|
58
|
-
else
|
59
|
-
seed_task.send :instance_variable_set, '@full_comment', comment
|
73
|
+
def runner
|
74
|
+
@_seedbank_runner ||= Seedbank::Runner.new
|
60
75
|
end
|
61
|
-
end
|
62
76
|
|
77
|
+
def add_comment_to(seed_task, comment)
|
78
|
+
if seed_task.respond_to?(:clear_comments)
|
79
|
+
seed_task.comment = comment
|
80
|
+
else
|
81
|
+
seed_task.send :instance_variable_set, '@full_comment', comment
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
63
85
|
end
|
64
86
|
end
|
data/lib/seedbank/railtie.rb
CHANGED
@@ -1,13 +1,8 @@
|
|
1
|
-
|
2
|
-
require 'rails'
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
4
2
|
module Seedbank
|
5
3
|
class Railtie < Rails::Railtie
|
6
|
-
|
7
4
|
rake_tasks do
|
8
|
-
Seedbank.seeds_root = File.expand_path('db/seeds', Rails.root)
|
9
5
|
Seedbank.load_tasks
|
10
6
|
end
|
11
|
-
|
12
7
|
end
|
13
|
-
end
|
8
|
+
end
|
data/lib/seedbank/runner.rb
CHANGED
@@ -1,33 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Seedbank
|
2
3
|
class Runner
|
3
|
-
|
4
4
|
def initialize
|
5
5
|
@_memoized = {}
|
6
6
|
end
|
7
7
|
|
8
|
-
def let(name, &block)
|
9
|
-
name = String(name)
|
10
|
-
|
11
|
-
raise ArgumentError.new("#{name} is already defined") if respond_to?(name, true)
|
12
|
-
|
13
|
-
__eigenclass.instance_exec(name) do |name|
|
14
|
-
define_method(name) do
|
15
|
-
@_memoized.fetch(name) { |key| @_memoized[key] = instance_eval(&block) }
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
def let!(name, &block)
|
22
|
-
let(name, &block)
|
23
|
-
send name
|
24
|
-
end
|
25
|
-
|
26
|
-
def evaluate(seed_task, seed_file)
|
27
|
-
@_seed_task = seed_task
|
28
|
-
instance_eval(File.read(seed_file), seed_file)
|
29
|
-
end
|
30
|
-
|
31
8
|
# Run this seed after the specified dependencies have run
|
32
9
|
# @param dependencies [Array] seeds to run before the block is executed
|
33
10
|
#
|
@@ -42,26 +19,36 @@ module Seedbank
|
|
42
19
|
#
|
43
20
|
# Would look for a db/seeds/shared/users.seeds.rb seed and execute it.
|
44
21
|
def after(*dependencies, &block)
|
45
|
-
dependencies.
|
46
|
-
|
47
|
-
dependent_task_name = @_seed_task.name + ':body'
|
22
|
+
depends_on = dependencies.flat_map { |dep| "db:seed:#{dep}" }
|
23
|
+
dependent_task_name = @_seed_task.name + ':body'
|
48
24
|
|
49
25
|
if Rake::Task.task_defined?(dependent_task_name)
|
50
26
|
dependent_task = Rake::Task[dependent_task_name]
|
51
27
|
else
|
52
|
-
dependent_task = Rake::Task.define_task(dependent_task_name =>
|
28
|
+
dependent_task = Rake::Task.define_task(dependent_task_name => depends_on, &block)
|
53
29
|
end
|
54
30
|
|
55
31
|
dependent_task.invoke
|
56
32
|
end
|
57
33
|
|
58
|
-
|
34
|
+
def let(name, &block)
|
35
|
+
name = String(name)
|
36
|
+
|
37
|
+
raise ArgumentError, "#{name} is already defined" if respond_to?(name, true)
|
59
38
|
|
60
|
-
|
61
|
-
|
62
|
-
self
|
39
|
+
define_singleton_method(name) do
|
40
|
+
@_memoized.fetch(name) { |key| @_memoized[key] = instance_exec(&block) }
|
63
41
|
end
|
64
42
|
end
|
65
43
|
|
44
|
+
def let!(name, &block)
|
45
|
+
let(name, &block)
|
46
|
+
public_send name
|
47
|
+
end
|
48
|
+
|
49
|
+
def evaluate(seed_task, seed_file)
|
50
|
+
@_seed_task = seed_task
|
51
|
+
instance_eval(File.read(seed_file), seed_file)
|
52
|
+
end
|
66
53
|
end
|
67
54
|
end
|
data/lib/seedbank/version.rb
CHANGED
data/lib/seedbank.rb
CHANGED
@@ -1,22 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'seedbank/dsl'
|
2
3
|
require 'seedbank/runner'
|
3
4
|
|
4
5
|
module Seedbank
|
5
|
-
|
6
6
|
class << self
|
7
|
+
attr_writer :application_root, :seeds_root, :nesting, :matcher
|
7
8
|
|
8
|
-
|
9
|
+
def application_root
|
10
|
+
@application_root ||= Pathname.new(Rake.application.original_dir)
|
11
|
+
end
|
9
12
|
|
10
13
|
def seeds_root
|
11
|
-
@seeds_root ||= 'db
|
14
|
+
@seeds_root ||= File.join(application_root, 'db', 'seeds')
|
15
|
+
end
|
16
|
+
|
17
|
+
def nesting
|
18
|
+
@nesting ||= 2
|
12
19
|
end
|
13
20
|
|
21
|
+
def matcher
|
22
|
+
@matcher ||= '*.seeds.rb'
|
23
|
+
end
|
14
24
|
end
|
15
25
|
|
16
26
|
def self.load_tasks
|
17
|
-
Dir[File.expand_path(
|
27
|
+
Dir[File.expand_path('../tasks/*.rake', __FILE__)].each { |ext| load ext }
|
18
28
|
end
|
19
29
|
|
20
30
|
require 'seedbank/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
21
|
-
|
22
|
-
end
|
31
|
+
end
|
data/lib/tasks/seed.rake
CHANGED
@@ -1,42 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
namespace :db do
|
2
|
-
|
3
|
-
include Seedbank::DSL
|
4
|
-
|
5
|
-
base_dependencies = ['db:seed:original']
|
3
|
+
using Seedbank::DSL
|
6
4
|
override_dependency = ['db:seed:common']
|
7
5
|
|
8
6
|
namespace :seed do
|
9
7
|
# Create seed tasks for all the seeds in seeds_path and add them to the dependency
|
10
8
|
# list along with the original db/seeds.rb.
|
11
|
-
common_dependencies =
|
9
|
+
common_dependencies = seed_tasks_matching(Seedbank.matcher)
|
10
|
+
|
11
|
+
# Only add the original seeds if db/seeds.rb exists.
|
12
|
+
if original_seeds_file
|
13
|
+
define_seed_task original_seeds_file, :original
|
14
|
+
common_dependencies.unshift('db:seed:original')
|
15
|
+
end
|
12
16
|
|
13
|
-
desc "Load the seed data from db/seeds.rb and db/seeds
|
14
|
-
task 'common' =>
|
17
|
+
desc "Load the seed data from db/seeds.rb and db/seeds/#{Seedbank.matcher}."
|
18
|
+
task 'common' => common_dependencies
|
15
19
|
|
16
20
|
# Glob through the directories under seeds_path and create a task for each adding it to the dependency list.
|
17
21
|
# Then create a task for the environment
|
18
22
|
glob_seed_files_matching('/*/').each do |directory|
|
19
23
|
environment = File.basename(directory)
|
20
24
|
|
21
|
-
environment_dependencies =
|
25
|
+
environment_dependencies = seed_tasks_matching(environment, Seedbank.matcher)
|
22
26
|
|
23
|
-
desc "Load the seed data from db/seeds.rb, db/seeds
|
27
|
+
desc "Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/#{environment}/#{Seedbank.matcher}."
|
24
28
|
task environment => ['db:seed:common'] + environment_dependencies
|
25
29
|
|
26
30
|
override_dependency << "db:seed:#{environment}" if defined?(Rails) && Rails.env == environment
|
27
31
|
end
|
28
|
-
|
29
|
-
if Rails.version > '4'
|
30
|
-
original_seeds_file = Rails.application.paths["db/seeds.rb"].existent.first
|
31
|
-
elsif Rails.version > '3.1'
|
32
|
-
original_seeds_file = Rails.application.paths["db/seeds"].existent.first
|
33
|
-
else
|
34
|
-
original_seeds_file = Rails.root.join("db","seeds").children.first.to_s
|
35
|
-
end
|
36
|
-
define_seed_task original_seeds_file, :original if original_seeds_file
|
37
32
|
end
|
38
33
|
|
39
34
|
# Override db:seed to run all the common and environments seeds plus the original db:seed.
|
40
|
-
desc
|
41
|
-
|
35
|
+
desc %(Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/ENVIRONMENT/#{Seedbank.matcher}.
|
36
|
+
ENVIRONMENT is the current Rails.env.)
|
37
|
+
override_seed_task seed: override_dependency
|
42
38
|
end
|