forked from spree/spree
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextensions.rake
210 lines (186 loc) · 8.65 KB
/
extensions.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require 'rake/testtask'
require 'spree/extension'
require 'custom_fixtures'
namespace :db do
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
task :migrate => :environment do
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
# spree
ActiveRecord::Migrator.migrate("#{SPREE_ROOT}/db/migrate/", version)
# extensions
Spree::Extension.descendants.select{|ext| ext.root.starts_with?(SPREE_ROOT)}.each do |extension|
ActiveRecord::Migrator.migrate("#{extension.root}/db/migrate/", version)
end
Spree::Extension.descendants.select{|ext| !ext.root.starts_with?(SPREE_ROOT)}.each do |extension|
ActiveRecord::Migrator.migrate("#{extension.root}/db/migrate/", version)
end
Spree::Extension.descendants.each do |extension|
ActiveRecord::Migrator.migrate("#{extension.root}/db/migrate/", version)
end
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
desc "Loading db/loadfrom for spree and each extension where you specify dir by rake db:load_dir[loadfrom]"
task :load_dir , [:dir] => :environment do |t , args|
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
dir = args.dir
fixtures = {}
Dir.glob(File.join(SPREE_ROOT, "db", dir , '*.{yml,csv}')).each do |fixture_file|
#puts "spree " + fixture_file + " " + File.basename(fixture_file, '.*')
fixtures[File.basename(fixture_file, '.*')] = fixture_file
end
Spree::ExtensionLoader.instance.db_paths(dir).each do |dir|
Dir.glob(File.join(dir, '*.{yml,csv}')).each do |fixture_file|
#puts "ext " + fixture_file + " " + File.basename(fixture_file, '.*')
fixtures[File.basename(fixture_file, '.*')] = fixture_file
end
end
fixtures.each do |fixture , fixture_file|
# an invoke will only execute the task once
Rake::Task["db:load_file"].execute( Rake::TaskArguments.new([:file], [fixture_file]) )
end
end
namespace :migrate do
desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x'
task :redo => [ 'db:rollback', 'db:migrate' ]
desc 'Resets your database using your migrations for the current environment'
task :reset => ["db:drop", "db:create", "db:migrate"]
desc 'Runs the "up" for a given migration VERSION.'
task :up => :environment do
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
raise "VERSION is required" unless version
# spree
ActiveRecord::Migrator.run(:up, "#{SPREE_ROOT}/db/migrate/", version)
# extensions
Spree::Extension.descendants.select{|ext| ext.root.starts_with?(SPREE_ROOT)}.each do |extension|
ActiveRecord::Migrator.migrate("#{extension.root}/db/migrate/", version)
end
Spree::Extension.descendants.select{|ext| !ext.root.starts_with?(SPREE_ROOT)}.each do |extension|
ActiveRecord::Migrator.migrate("#{extension.root}/db/migrate/", version)
end
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
desc 'Runs the "down" for a given migration VERSION.'
task :down => :environment do
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
raise "VERSION is required" unless version
# spree
ActiveRecord::Migrator.run(:down, "#{SPREE_ROOT}/db/migrate/", version)
# extensions
Spree::Extension.descendants.select{|ext| ext.root.starts_with?(SPREE_ROOT)}.each do |extension|
ActiveRecord::Migrator.migrate("#{extension.root}/db/migrate/", version)
end
Spree::Extension.descendants.select{|ext| !ext.root.starts_with?(SPREE_ROOT)}.each do |extension|
ActiveRecord::Migrator.migrate("#{extension.root}/db/migrate/", version)
end
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
end
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
task :rollback => :environment do
raise "rollback is not currently supported in Spree (due to complications with extensions)"
#step = ENV['STEP'] ? ENV['STEP'].to_i : 1
#ActiveRecord::Migrator.rollback('db/migrate/', step)
#Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
desc "Raises an error if there are pending migrations"
task :abort_if_pending_migrations => :environment do
if defined? ActiveRecord
pending_migrations = []
# spree
pending_migrations += ActiveRecord::Migrator.new(:up, "#{SPREE_ROOT}/db/migrate/").pending_migrations
# extensions
Spree::Extension.descendants.select{|ext| ext.root.starts_with?(SPREE_ROOT)}.each do |extension|
pending_migrations += ActiveRecord::Migrator.new(:up, "#{extension.root}/db/migrate/").pending_migrations
end
Spree::Extension.descendants.select{|ext| !ext.root.starts_with?(SPREE_ROOT)}.each do |extension|
pending_migrations += ActiveRecord::Migrator.new(:up, "#{extension.root}/db/migrate/").pending_migrations
end
if pending_migrations.any?
puts "You have #{pending_migrations.size} pending migrations:"
pending_migrations.each do |pending_migration|
puts ' %4d %s' % [pending_migration.version, pending_migration.name]
end
abort %{Run "rake db:migrate" to update your database then try again.}
end
end
end
end
def find_extension_roots
current_extensions_path = File.join(File.expand_path(Dir.pwd), 'vendor/extensions')
all_extension_names = Spree::Extension.descendants
all_extension_names.collect!{ |x| x.extension_name.tr(' ', '').underscore }
verified_extension_paths = []
all_extension_names.each do |ext_name|
extension_path = File.join(current_extensions_path, ext_name)
if File.directory?(extension_path)
verified_extension_paths << extension_path
end
end
extension_roots = verified_extension_paths
if ENV["EXT"]
extension_roots = extension_roots.select {|x| /\/(\d+_)?#{ENV["EXT"]}$/ === x }
if extension_roots.empty?
puts "Sorry, that extension is not installed."
end
end
extension_roots
end
namespace :spec do
desc "Runs specs on all available extensions in the current /vendor/extensions directory, pass EXT=extension_name to test a single extension"
task :extensions => "db:test:prepare" do
extension_roots = find_extension_roots
extension_roots.each do |directory|
if File.directory?(File.join(directory, 'spec'))
chdir directory do
if RUBY_PLATFORM =~ /win32/
system "rake.cmd spec SPREE_ENV_FILE=#{RAILS_ROOT}/config/environment"
else
system "rake spec SPREE_ENV_FILE=#{RAILS_ROOT}/config/environment"
end
end
end
end
end
end
namespace :extensions do
namespace :test do
desc "Runs functional tests on all available extensions in the current /vendor/extensions directory, pass EXT=extension_name to test a single extension"
task :functionals => "db:test:prepare" do
extension_roots = find_extension_roots
extension_roots.each do |directory|
if File.directory?(File.join(directory, 'test/functional'))
chdir directory do
system "rake test:functionals SPREE_ENV_FILE=#{RAILS_ROOT}/config/environment"
end
end
end
end
desc "Runs unit tests on all available extensions in the current /vendor/extensions directory, pass EXT=extension_name to test a single extension"
task :units => "db:test:prepare" do
extension_roots = find_extension_roots
extension_roots.each do |directory|
if File.directory?(File.join(directory, 'test/unit'))
chdir directory do
system "rake test:units SPREE_ENV_FILE=#{RAILS_ROOT}/config/environment"
end
end
end
end
end
desc "Runs all tests on all available extensions in the current /vendor/extensions directory, pass EXT=extension_name to test a single extension"
task :test => "db:test:prepare" do
extension_roots = find_extension_roots
extension_roots.each do |directory|
if File.directory?(File.join(directory, 'test'))
chdir directory do
system "rake test SPREE_ENV_FILE=#{RAILS_ROOT}/config/environment"
end
end
end
end
end
# Load any custom rakefiles from extensions
[RAILS_ROOT, SPREE_ROOT].uniq.each do |root|
Dir[root + '/vendor/extensions/*/lib/tasks/*.rake'].sort.each { |ext| load ext }
end