Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 7
- Log:
Initial import of RForum 0.2 sources from a downloaded Tarball.
RForum is a Ruby On Rails based forum and mail gateway service.
- Author:
- adh
- Date:
- Sat Jul 22 18:43:13 +0100 2006
- Size:
- 6581 Bytes
- Properties:
- Property svn:executable is set
1 | #!/usr/bin/env ruby |
2 | |
3 | require 'rubygems' |
4 | require 'active_record' |
5 | require 'config/environment' |
6 | |
7 | # Print error message, ask for response |
8 | def error( msg ) |
9 | STDERR.puts "Error: #{msg}" |
10 | STDERR.print "Contine? (y/n) " |
11 | STDERR.flush |
12 | answer = STDIN.gets |
13 | return answer[0,1].downcase == "y" |
14 | end |
15 | |
16 | def copy_file( source_file_name, destination_file_name ) |
17 | sfn = File.open( source_file_name ) |
18 | dfn = File.open( destination_file_name, "w+" ) |
19 | dfn << sfn.read |
20 | sfn.close |
21 | dfn.close |
22 | true |
23 | end |
24 | |
25 | # Prints usage |
26 | def usage() |
27 | "Usage: ruby script/console [install|remove] modulename\n\n" |
28 | end |
29 | |
30 | # Crawl modules/views, document which templates are already modded |
31 | def current_modded_templates() |
32 | dirs = Dir["modules/views/[_a-z]*"] |
33 | files = Hash.new{ |hh,kk| hh[kk] = Array.new } |
34 | dirs.each{ |ii| Dir.entries("#{ii}/").each{ |kk| files["views#{ii.split("views").last}"] << kk unless kk=="." || kk==".."} } |
35 | files |
36 | end |
37 | |
38 | # Returns a hash of directories => [template files ] of module named "name" |
39 | def module_templates( name ) |
40 | dirs = Dir["modules/#{name}/views/[_a-z]*"] |
41 | files = Hash.new{ |hh,kk| hh[kk] = Array.new } |
42 | dirs.each{ |ii| Dir.entries("#{ii}/").each{ |kk| files["views#{ii.split("views").last}"] << kk unless kk=="." || kk==".."} } |
43 | files |
44 | end |
45 | |
46 | # Install (aka copy) all template for module "module_name" into modules/views/ |
47 | def install_templates( module_name ) |
48 | hsh = module_templates( module_name ) |
49 | hsh.each_key do |key| |
50 | hsh[key].each do |file| |
51 | # First, we need to make sure the destination directory exists |
52 | unless FileTest.exists?( "modules/#{key}") |
53 | Dir.mkdir( "modules/#{key}" ) |
54 | end |
55 | # If this template already exists, make a backup |
56 | if FileTest.exists?( "modules/#{key}/#{file}" ) |
57 | copy_file( "modules/#{key}/#{file}", "modules/#{key}/#{file}.old" ) |
58 | end |
59 | copy_file( "modules/#{module_name}/#{key}/#{file}", "modules/#{key}/#{file}") |
60 | STDOUT.puts "...\tCopied modules/#{module_name}/#{key}/#{file} to modules/#{key}/#{file}" |
61 | end |
62 | end |
63 | end |
64 | |
65 | # Reverts all templates for module_name in modules/views/, replacing them with their filename.old counterpart |
66 | def revert_templates( module_name ) |
67 | # Get a list of all files ending in ".old" |
68 | hsh = module_templates( module_name ) |
69 | hsh.each_key do |key| |
70 | hsh[key].each do |file| |
71 | if FileTest.exists?( "modules/#{key}/#{file}.old" ) |
72 | copy_file( "modules/#{key}/#{file}.old", "modules/#{key}/#{file}" ) |
73 | File.delete( "modules/#{key}/#{file}.old" ) |
74 | else # Else, we should just remove the template |
75 | File.delete( "modules/#{key}/#{file}" ) if FileTest.exists?("modules/#{key}/#{file}") |
76 | end |
77 | STDOUT.puts "...\tReverted template modules/#{key}/#{file}" |
78 | end |
79 | end |
80 | end |
81 | |
82 | def add_module_to_modules_config( module_name ) |
83 | arr = [] |
84 | File.open( "modules/modules.rb" ){ |f| f.each_line{ |line| arr.push line.strip } } |
85 | arr.uniq! |
86 | File.open( "modules/modules.rb", "w+" ) do |f| |
87 | f << arr.join("\n") |
88 | f << "\nrequire '#{module_name}/#{module_name}'" |
89 | end |
90 | end |
91 | |
92 | def remove_module_from_modules_config( module_name ) |
93 | arr = [] |
94 | File.open( "modules/modules.rb" ){ |f| f.each_line{ |line| arr.push line.strip if line.strip.size > 0 } } |
95 | arr.uniq! |
96 | arr.delete_if{ |ii| ii.include?( "require '#{module_name}/#{module_name}'" ) } |
97 | File.open( "modules/modules.rb", "w+" ){ |f| f << arr.join("\n") } |
98 | end |
99 | |
100 | #------------------------------------------------------------------------------- |
101 | # Main routine |
102 | #------------------------------------------------------------------------------- |
103 | todo = ARGV[0] |
104 | mname = ARGV[1] |
105 | |
106 | # Check to make sure correct arguments were provided |
107 | if todo.nil? || mname.nil? |
108 | STDERR.puts usage() |
109 | exit |
110 | end |
111 | |
112 | # Crawl modules/views, document which templates are already modded |
113 | modded_templates = current_modded_templates |
114 | |
115 | case todo |
116 | when "install" |
117 | # Compare module to be installed against modded_templates to detect conflicts |
118 | templates_to_install = module_templates( mname ) |
119 | templates_to_install.each_key{ |key| |
120 | tmp = (modded_templates[key] & templates_to_install[key]) |
121 | if tmp.size > 0 |
122 | exit unless error( "There is a conflict with a previously installed module. Conflicted templates for #{key}:\n\t#{tmp.join("\n\t")}" ) |
123 | # If they said continue, lets make backup of their old templates, save as .old |
124 | end |
125 | } |
126 | |
127 | # Install all views for this module. |
128 | begin |
129 | install_templates( mname ) |
130 | # Perform necessary DB migrations |
131 | if FileTest.exists?( "modules/#{mname}/migrate.rb" ) |
132 | begin # Try out migrations |
133 | require "modules/#{mname}/migrate.rb" |
134 | klass = instance_eval( "#{mname.capitalize}Migration" ) |
135 | klass.up |
136 | rescue => e |
137 | STDERR.puts "ERROR! Migration Failed!" |
138 | raise e |
139 | end |
140 | end |
141 | # Add line to modules.rb |
142 | add_module_to_modules_config( mname ) |
143 | rescue => e |
144 | STDERR.puts "ERROR! Installation failed! Undoing install..." |
145 | # Back out of installation |
146 | # Revert templates to old versions |
147 | revert_templates( mname ) |
148 | # Remove modules.rb config |
149 | remove_module_from_modules_config( mname ) |
150 | raise e |
151 | end |
152 | when "remove" |
153 | # Check to make sure no other mods need the templates from this mod |
154 | # Remove template files |
155 | revert_templates( mname ) |
156 | # Perform necessary DB migrations |
157 | if FileTest.exists?( "modules/#{mname}/migrate.rb" ) |
158 | begin |
159 | require "modules/#{mname}/migrate.rb" |
160 | STDOUT.puts "...\tPerforming Migration.down" |
161 | klass = instance_eval( "#{mname.capitalize}Migration" ) |
162 | klass.down |
163 | rescue => e |
164 | STDERR.puts "ERROR: There was a problem migrating down from this mod install. Please refer to the modules manual removal instructions." |
165 | raise e |
166 | end |
167 | end |
168 | # remove line from modules.rb |
169 | remove_module_from_modules_config( mname ) |
170 | # remove entire mod directory structure |
171 | STDERR.puts "Please delete modules/#{mname}" |
172 | else |
173 | STDERR.puts usage() |
174 | end # end case statement |