因此,想法是如果任何任务失败,因为不再执行太多任务。
示例:rails db:如果有任何用户登录数据库,drop会给出错误
示例:如果import_package没有.csv文件或其他文件,则会显示错误消息。
这是开发人员任务:
require 'csv'
namespace :dev do
desc "Configuration of the development environment."
task setup: :environment do
if Rails.env.development?
show_spinner("Deleting database...") { %x(rails dev:drop_db) }
show_spinner("Creating database...") { %x(rails db:create) }
show_spinner("Running migrates...") { %x(rails db:migrate) }
show_spinner("Importing Packages...") { %x(rails dev:import_packages) }
show_spinner("Importing Products...") { %x(rails dev:import_products) }
show_spinner("Running seeds...") { %x(rails db:seed) }
else
puts "You are not in a development environment."
end
end
desc "Import packages from csv file"
task import_packages: :environment do
file = "db/packages.csv"
CSV.foreach(file, :headers => true) do |row|
Package.create!(row.to_hash)
end
end
desc "Import Products from csv file"
task import_products: :environment do
file = "db/products.csv"
CSV.foreach(file, :headers => true) do |row|
Product.create!(row.to_hash)
end
end
# Spinner load method
def show_spinner(start_msg, end_msg = "Finished.")
spinner = TTY::Spinner.new("[:spinner] #{start_msg}")
spinner.auto_spin
yield
spinner.success("#{end_msg}")
end
end