-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
148 lines (107 loc) · 3.43 KB
/
Rakefile
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
@env = ARGV[1].nil? ? 'development' : ARGV[1]
task @env do ; end unless ARGV[1].nil?
namespace :db do
desc "Run DB migrations on db/migrate directory"
task :migrate do
require 'sequel'
require_relative 'helpers/environment_helper'
Sequel.extension :migration
env ||= ENV['RACK_ENV'] || "development"
db = BaseApp::Helpers.init_environment(env)
puts 'Running migrations...'
Sequel::Migrator.run(db, "db/migrate")
puts 'Done!'
end
namespace :schema do
desc "Dump the DB schema to db/schema.rb"
task :dump do
require 'sequel'
require_relative 'helpers/environment_helper'
env ||= ENV['RACK_ENV'] || "development"
db = BaseApp::Helpers.init_environment(env)
db.extension :schema_dumper
puts "Dumping schema to db/schema.rb..."
File.open("db/schema.rb", "w") do |f|
f.puts db.dump_schema_migration
end
puts "Done!"
end
desc "Load the DB schema defined in db/schema.rb"
task :load do
require 'sequel'
require_relative 'helpers/environment_helper'
Sequel.extension :migration
puts "Loading schema..."
env ||= ENV['RACK_ENV'] || "development"
db = BaseApp::Helpers.init_environment(env)
migration = eval(File.read('./db/schema.rb'))
puts "Dropping old tables..."
db.drop_table *db.tables, cascade: true
puts "Applying new schema..."
migration.apply(db, :up)
puts "Done!"
end
end
namespace :test do
desc "Prepares test DB by copying current dev schema"
task :prepare do
require 'sequel'
env_val = ENV['RACK_ENV']
ENV['RACK_ENV'] = 'development'
Rake::Task["db:schema:dump"].invoke
ENV['RACK_ENV'] = 'test'
Rake::Task["db:schema:load"].invoke
end
end
end
namespace :test do
@env = 'test'
desc "Runs all tests in test/{models/helpers/routes/lib}"
task :all do
load_files "test/models/*.rb"
load_files "test/helpers/*.rb"
load_files "test/routes/*.rb"
load_files "test/lib/*.rb"
end
end
desc "Setup application for new name"
task :setup do
fail "Operating System not supported" if /windows/ =~ RUBY_PLATFORM.downcase
puts "Please, enter your app name. (e.g. MyApp)" unless ARGV[1]
app_name = ARGV[1] || STDIN.readline.strip
sed_command = (/darwin/ =~ RUBY_PLATFORM.downcase) ? 'sed -i ""' : 'sed -i'
puts
if app_name == ""
puts "\e[31m[ERROR] The application name can't be blank\e[0m"
exit 1
end
puts "Setting up #{app_name}..."
file_name = app_name.gsub(/App/,"").downcase.strip + ".rb"
puts "Setting up files..."
`rm -rf .git`
`mv routes/base.rb routes/#{file_name}`
`find ./ -type f | xargs #{sed_command} -e 's/BaseApp/#{app_name}/'`
`cp env.sh.sample development.env.sh`
puts "Do you have a git repository already? [y/N]"
add_remote = STDIN.readline.strip
if add_remote.downcase == "y"
puts "Enter your git repository name [origin]"
remote_name = STDIN.readline.strip
remote_name = "origin" if remote_name == ""
puts "Enter your git repository URL"
remote_url = STDIN.readline.strip
if remote_url == ""
puts "\e[33m[WARNING] URL is blank. Not adding git remote\e[0m"
else
`git init`
`git remote add #{remote_name} #{remote_url}`
`git add *`
`git commit -m "Initial layout"`
end
end
puts "\e[32mDone."
puts "Happy Coding!\e[0m"
end
def load_files(dir)
Dir[dir].each { |file| load file }
end