Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 4
- Log:
Initial import of Radiant 0.50 sources from a downloaded Tarball.
Radiant is a Ruby On Rails based lightweight content management
system.
- Author:
- adh
- Date:
- Sat Jul 22 16:47:40 +0100 2006
- Size:
- 8487 Bytes
- Properties:
- Property svn:executable is set
1 | #!/usr/bin/env ruby |
2 | |
3 | RAILS_ENV = 'development' |
4 | |
5 | require 'optparse' |
6 | |
7 | class DatabaseSetupApplication |
8 | def self.run(args = ARGV) |
9 | new.run(args) |
10 | end |
11 | |
12 | attr_reader :params, :force, :help, :env |
13 | |
14 | def run(args = []) |
15 | parse_args(args) |
16 | announce_additional_options |
17 | load_environment |
18 | overwrite_check |
19 | create_tables |
20 | create_admin_user |
21 | initialize_configuration |
22 | if @template_filename |
23 | @template = load_template(@template_filename) |
24 | else |
25 | select_template |
26 | end |
27 | create_records_from_template |
28 | announce_finished |
29 | end |
30 | |
31 | def parse_args(args) |
32 | args = args.dup |
33 | @env = args.pop.downcase if args.last =~ /^(development|production|test)$/i |
34 | @env ||= 'development' |
35 | @overwrite_warning = true |
36 | @template_filename = nil |
37 | |
38 | opts = OptionParser.new do |opts| |
39 | opts.banner = "Usage: #{File.basename($0)} [options] [environment]" |
40 | opts.separator "Setup Radiant database for the appropriate environment. Environment may be 'development', 'production', or 'test'." |
41 | opts.separator "" |
42 | |
43 | opts.separator "Options:" |
44 | |
45 | opts.on("-o", "--overwrite", "Overwrite all data in tables. Do not display the overwrite warning.") do |
46 | @overwrite_warning = false |
47 | end |
48 | |
49 | opts.on("-t", "--template FILENAME", "Use the template specified in FILENAME.") do |filename| |
50 | @template_filename = filename |
51 | end |
52 | |
53 | opts.on("-?", "--help") do |
54 | puts opts |
55 | exit |
56 | end |
57 | end |
58 | |
59 | begin |
60 | opts.parse(args) |
61 | rescue OptionParser::ParseError => e |
62 | puts e.message.capitalize |
63 | puts |
64 | puts opts |
65 | exit |
66 | end |
67 | |
68 | RAILS_ENV.replace(@env) |
69 | end |
70 | |
71 | def announce_additional_options |
72 | puts "Run `#{File.basename($0)} --help` for information on additional options." |
73 | puts |
74 | end |
75 | |
76 | def load_environment |
77 | announce "Loading #{env} environment" do |
78 | require File.join(File.dirname(__FILE__), '..', 'config', 'environment') |
79 | @templates_dir = File.join(RADIANT_ROOT, 'db', 'templates') |
80 | end |
81 | end |
82 | |
83 | def overwrite_check |
84 | if @overwrite_warning |
85 | puts |
86 | if ask_yes_or_no "WARNING! This script will overwrite information currently stored in the\n" + |
87 | "database #{(ActiveRecord::Base.configurations[env]['database']).inspect}. " + |
88 | "Are you sure you want to continue" |
89 | puts |
90 | else |
91 | puts |
92 | puts "Setup canceled." |
93 | exit |
94 | end |
95 | end |
96 | end |
97 | |
98 | def create_tables |
99 | announce "Creating tables" do |
100 | puts |
101 | ActiveRecord::Schema.define(:version => 9) do |
102 | |
103 | drop_table "config" rescue nil |
104 | create_table "config", :force => true do |t| |
105 | t.column "key", :string, :limit => 40, :default => "", :null => false |
106 | t.column "value", :string, :default => "" |
107 | end |
108 | add_index "config", ["key"], :name => "key", :unique => true |
109 | |
110 | drop_table "layouts" rescue nil |
111 | create_table "layouts", :force => true do |t| |
112 | t.column "name", :string, :limit => 100 |
113 | t.column "content", :text |
114 | t.column "content_type", :string, :limit => 40 |
115 | t.column "created_at", :datetime |
116 | t.column "updated_at", :datetime |
117 | t.column "created_by", :integer |
118 | t.column "updated_by", :integer |
119 | end |
120 | |
121 | drop_table "page_parts" rescue nil |
122 | create_table "page_parts", :force => true do |t| |
123 | t.column "name", :string, :limit => 100 |
124 | t.column "filter_id", :string, :limit => 25 |
125 | t.column "content", :text |
126 | t.column "page_id", :integer |
127 | end |
128 | |
129 | drop_table "pages" rescue nil |
130 | create_table "pages", :force => true do |t| |
131 | t.column "title", :string |
132 | t.column "slug", :string, :limit => 100 |
133 | t.column "breadcrumb", :string, :limit => 160 |
134 | t.column "parent_id", :integer |
135 | t.column "layout_id", :integer |
136 | t.column "behavior_id", :string, :limit => 25 |
137 | t.column "status_id", :integer, :default => 1, :null => false |
138 | t.column "created_at", :datetime |
139 | t.column "updated_at", :datetime |
140 | t.column "published_at", :datetime |
141 | t.column "created_by", :integer |
142 | t.column "updated_by", :integer |
143 | t.column "virtual", :boolean, :default => false, :null => false |
144 | end |
145 | |
146 | drop_table "snippets" rescue nil |
147 | create_table "snippets", :force => true do |t| |
148 | t.column "name", :string, :limit => 100, :default => "", :null => false |
149 | t.column "filter_id", :string, :limit => 25 |
150 | t.column "content", :text |
151 | t.column "created_at", :datetime |
152 | t.column "updated_at", :datetime |
153 | t.column "created_by", :integer |
154 | t.column "updated_by", :integer |
155 | end |
156 | add_index "snippets", ["name"], :name => "name", :unique => true |
157 | |
158 | drop_table "users" rescue nil |
159 | create_table "users", :force => true do |t| |
160 | t.column "name", :string, :limit => 100 |
161 | t.column "email", :string |
162 | t.column "login", :string, :limit => 40, :default => "", :null => false |
163 | t.column "password", :string, :limit => 40 |
164 | t.column "admin", :boolean, :default => false, :null => false |
165 | t.column "developer", :boolean, :default => false, :null => false |
166 | t.column "created_at", :datetime |
167 | t.column "updated_at", :datetime |
168 | t.column "created_by", :integer |
169 | t.column "updated_by", :integer |
170 | end |
171 | add_index "users", ["login"], :name => "login", :unique => true |
172 | end |
173 | end |
174 | end |
175 | |
176 | def create_admin_user |
177 | announce "Creating user 'admin' with password 'radiant'" do |
178 | @admin = User.create :name => 'Administrator', :login => 'admin', :password => 'radiant', :password_confirmation => 'radiant', :admin => true |
179 | @admin = User.find(@admin.id) |
180 | UserActionObserver.current_user = @admin |
181 | @admin.created_by = @admin |
182 | @admin.save |
183 | end |
184 | end |
185 | |
186 | def initialize_configuration |
187 | announce "Initializing configuration" do |
188 | config = Radiant::Config |
189 | config['admin.title' ] = 'Radiant CMS' |
190 | config['admin.subtitle'] = 'Publishing for Small Teams' |
191 | config['default.parts' ] = 'body, extended' |
192 | end |
193 | end |
194 | |
195 | def select_template |
196 | templates = Dir[File.join(@templates_dir, '*.yml')] |
197 | templates.map! { |t| load_template(t) } |
198 | templates = templates.sort_by { |t| t['name'] } |
199 | loop do |
200 | puts |
201 | puts "Select a database template:" |
202 | templates.each_with_index do |t, i| |
203 | puts |
204 | puts " #{i + 1}) #{t['name']}" |
205 | t['description'].each_line { |line| puts " #{line.strip}" } |
206 | end |
207 | puts |
208 | print "[1-#{templates.size}] " |
209 | selection = $1.to_i if gets.strip =~ /^(\d+)$/ |
210 | case selection |
211 | when (1..templates.size) |
212 | @template = templates[selection-1] |
213 | puts |
214 | break |
215 | else |
216 | puts |
217 | invalid_option |
218 | end |
219 | end |
220 | end |
221 | |
222 | def load_template(template) |
223 | YAML.load_file(template) |
224 | end |
225 | |
226 | def create_records_from_template |
227 | records = @template['records'] |
228 | if records |
229 | records.keys.each do |key| |
230 | announce "Creating #{key.to_s.underscore.humanize}" do |
231 | const = Object.const_get(key.to_s.singularize) |
232 | record_pairs = records[key].map { |name, record| [record['id'], record] }.sort { |a, b| a[0] <=> b[0] } |
233 | record_pairs.each do |id, record| |
234 | const.new(record).save |
235 | end |
236 | end |
237 | end |
238 | end |
239 | end |
240 | |
241 | def announce_finished |
242 | puts |
243 | puts "Finished." |
244 | end |
245 | |
246 | def print(*args) |
247 | $defout << args |
248 | $defout.flush |
249 | end |
250 | |
251 | def puts(*args) |
252 | args << '' if args.size == 0 |
253 | print *(args.map { |a| "#{a}\n" }) |
254 | end |
255 | |
256 | def announce(something) |
257 | print "#{something}..." |
258 | yield |
259 | puts "OK" |
260 | rescue Exception => e |
261 | puts "FAILED" |
262 | raise e |
263 | end |
264 | |
265 | def invalid_option |
266 | puts "Invalid option." |
267 | puts |
268 | end |
269 | |
270 | def gets |
271 | $stdin.gets |
272 | end |
273 | |
274 | def ask_yes_or_no(question, default = :yes) |
275 | prompt = (default == :yes) ? "[Yn]" : "[yN]" |
276 | loop do |
277 | print "#{question}? #{prompt} " |
278 | case gets.strip.downcase |
279 | when "yes", "y" |
280 | break true |
281 | when "no", "n" |
282 | break false |
283 | when "" |
284 | break default == :yes |
285 | else |
286 | puts |
287 | invalid_option |
288 | end |
289 | end |
290 | end |
291 | end |
292 | |
293 | trap('INT') do |
294 | puts |
295 | exit |
296 | end |
297 | DatabaseSetupApplication.run |