Module: Smeagol::Console
Overview
Console encapsulates all the public methods smeagol is likely to need, generally it maps to all the CLI commands.
Instance Attribute Summary collapse
-
#wiki_dir ⇒ String
Current wiki directory.
-
#wiki_url ⇒ Object
The wiki git url.
Instance Method Summary collapse
-
#auto_update(server_config) ⇒ Object
Run the auto update process.
-
#catch_signals ⇒ Object
Setup trap signals.
-
#clear_caches(server_config) ⇒ Object
Clear the caches.
-
#clone_wiki ⇒ Object
If a wiki git URI is given, the clone the wiki.
-
#copy_assets ⇒ Object
Copy assets to ‘assets` directory.
- #copy_dir(src_dir, dst_dir) ⇒ Object
- #copy_file(src, dst) ⇒ Object
-
#copy_layouts ⇒ Object
Copy layout templates to ‘_layouts` directory and partial templates to `_partials`.
-
#git ⇒ String
Git executable.
-
#init(*args) ⇒ void
Initialize Gollum wiki for use with Smeagol.
-
#initial_settings(options = {}) ⇒ Settings
When using #init, this provided the initial settings.
-
#preview(options) ⇒ Object
Preview current wiki (from working directory).
- #report(msg) ⇒ Object
-
#run_server(server_config) ⇒ Object
Run the web server.
-
#save_gitignore ⇒ Object
Create or append ‘_site` to .gitignore file.
-
#save_settings(options) ⇒ Object
Save settings.
-
#serve(options) ⇒ Object
Serve up sites defined in smeagol config file.
-
#settings ⇒ Settings
Local wiki settings.
-
#settings_template ⇒ Object
Read in the settings mustache template.
-
#show_repository(server_config) ⇒ Object
Show repositories being served.
-
#site_path ⇒ String
Site directory path.
-
#site_repo ⇒ Repository
Site repository.
-
#update(*args) ⇒ Object
Update wiki repo(s).
-
#wiki ⇒ Wiki
Get and cache Wiki object.
Instance Attribute Details
#wiki_dir ⇒ String
Current wiki directory.
63 64 65 |
# File 'lib/smeagol/console.rb', line 63 def wiki_dir @wiki_dir end |
#wiki_url ⇒ Object
The wiki git url.
60 61 62 |
# File 'lib/smeagol/console.rb', line 60 def wiki_url @wiki_url end |
Instance Method Details
#auto_update(server_config) ⇒ Object
Run the auto update process.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/smeagol/console.rb', line 238 def auto_update(server_config) return unless server_config.auto_update Thread.new do while true do sleep 86400 server_config.repositories.each do |repository| next unless repository.auto_update? out = repository.update out = out[1] if Array === out if out.index('Already up-to-date').nil? $stderr.puts "== Repository updated at #{Time.new()} : #{repository.path} ==" end end end end end |
#catch_signals ⇒ Object
Setup trap signals.
218 219 220 221 222 |
# File 'lib/smeagol/console.rb', line 218 def catch_signals Signal.trap('TERM') do Process.kill('KILL', 0) end end |
#clear_caches(server_config) ⇒ Object
Clear the caches.
258 259 260 261 262 |
# File 'lib/smeagol/console.rb', line 258 def clear_caches(server_config) server_config.repositories.each do |repository| Smeagol::Cache.new(Gollum::Wiki.new(repository.path)).clear() end end |
#clone_wiki ⇒ Object
If a wiki git URI is given, the clone the wiki.
@todo Use grit instead of shelling out.
70 71 72 |
# File 'lib/smeagol/console.rb', line 70 def clone_wiki system "git clone #{wiki_url} #{wiki_dir}" end |
#copy_assets ⇒ Object
Copy assets to ‘assets` directory.
91 92 93 94 95 |
# File 'lib/smeagol/console.rb', line 91 def copy_assets dst_dir = File.join(wiki_dir, 'assets') src_dir = LIBDIR + '/public/assets' copy_dir(src_dir, dst_dir) end |
#copy_dir(src_dir, dst_dir) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/smeagol/console.rb', line 98 def copy_dir(src_dir, dst_dir) FileUtils.mkdir_p(dst_dir) Dir[File.join(src_dir, '**/*')].each do |src| next if File.directory?(src) dst = File.join(dst_dir, src.sub(src_dir, '')) copy_file(src, dst) end end |
#copy_file(src, dst) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/smeagol/console.rb', line 109 def copy_file(src, dst) if File.exist?(dst) report " skip: #{dst.sub(Dir.pwd,'')}" else FileUtils.mkdir_p(File.dirname(dst)) FileUtils.cp(src, dst) report " copy: #{dst.sub(Dir.pwd,'')}" end end |
#copy_layouts ⇒ Object
Copy layout templates to ‘_layouts` directory and partial templates to `_partials`.
78 79 80 81 82 83 84 85 86 |
# File 'lib/smeagol/console.rb', line 78 def copy_layouts dst_dir = File.join(wiki_dir, '_layouts') src_dir = LIBDIR + '/templates/layouts' copy_dir(src_dir, dst_dir) dst_dir = File.join(wiki_dir, '_partials') src_dir = LIBDIR + '/templates/partials' copy_dir(src_dir, dst_dir) end |
#git ⇒ String
Git executable.
363 364 365 |
# File 'lib/smeagol/console.rb', line 363 def git Smeagol.git end |
#init(*args) ⇒ void
Perhaps use a supporting “managed copy” gem in future?
Add –force option to override skips?
This method returns an undefined value.
Initialize Gollum wiki for use with Smeagol. This will clone the wiki repo, if given and it doesn’t already exist and create ‘_settings.yml`, `_layouts/` and `assets/smeagol/`.
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 |
# File 'lib/smeagol/console.rb', line 27 def init(*args) = (Hash === args.last ? args.pop : {}) abort "Too many arguments." if args.size > 2 @wiki_url = args.shift @wiki_dir = args.shift if @wiki_url unless @wiki_dir @wiki_dir = File.basename(@wiki_url).chomp('.git') end @clone = true else @wiki_dir = Dir.pwd if File.exist?(File.join(@wiki_dir, '.git')) @wiki_url = wiki.repo.config['remote.origin.url'].to_s else abort "smeagol: not a git repo." end @clone = false end clone_wiki if @clone save_settings() save_gitignore copy_layouts unless [:no_layouts] copy_assets unless [:no_assets] end |
#initial_settings(options = {}) ⇒ Settings
When using #init, this provided the initial settings.
@returns [Settings]
155 156 157 158 159 160 |
# File 'lib/smeagol/console.rb', line 155 def initial_settings(={}) [:wiki_origin] = wiki_url [:site_origin] = wiki_url.sub('.wiki', '') @settings = Settings.new() end |
#preview(options) ⇒ Object
Preview current wiki (from working directory).
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/smeagol/console.rb', line 173 def preview() repository = {} repository[:path] = Dir.pwd #repository[:cname] = options[:cname] if options[:cname] repository[:secret] = .delete(:secret) if .key?(:secret) [:repositories] = [repository] config = Smeagol::Config.new() catch_signals show_repository(config) run_server(config) end |
#report(msg) ⇒ Object
11 12 13 |
# File 'lib/smeagol/console.rb', line 11 def report(msg) $stderr.puts msg unless $QUIET end |
#run_server(server_config) ⇒ Object
Run the web server.
267 268 269 270 271 272 273 |
# File 'lib/smeagol/console.rb', line 267 def run_server(server_config) #Smeagol::App.set(:git, server_config.git) Smeagol::App.set(:repositories, server_config.repositories) Smeagol::App.set(:cache_enabled, server_config.cache_enabled) Smeagol::App.set(:mount_path, server_config.mount_path) Smeagol::App.run!(:port => server_config.port) end |
#save_gitignore ⇒ Object
Create or append ‘_site` to .gitignore file.
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/smeagol/console.rb', line 122 def save_gitignore file = File.join(wiki_dir, '.gitignore') if File.exist?(file) File.open(file, 'a') do |f| f.write("_site") end else File.open(file, 'w') do |f| f.write("_site") end end end |
#save_settings(options) ⇒ Object
Save settings.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/smeagol/console.rb', line 138 def save_settings() file = File.join(wiki_dir, "_settings.yml") if File.exist?(file) $stderr.puts " skip: #{file}" else text = Mustache.render(settings_template, initial_settings()) File.open(file, 'w') do |f| f.write(text) end end end |
#serve(options) ⇒ Object
Serve up sites defined in smeagol config file.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/smeagol/console.rb', line 192 def serve() config_file = [:config_file] config = Config.load(config_file) config.assign() abort "No repositories configured." if config.repositories.empty? # Set secret on all repositories if passed in by command line option # We can only assume they are all the same, in this case. # # TODO: Maybe only apply if no secret is given in config file? if [:secret] config.repositories.each{ |r| r['secret'] = ['secret'] } end catch_signals show_repository(config) auto_update(config) clear_caches(config) run_server(config) end |
#settings ⇒ Settings
Local wiki settings.
354 355 356 |
# File 'lib/smeagol/console.rb', line 354 def settings @settings ||= Settings.load(wiki_dir) end |
#settings_template ⇒ Object
Read in the settings mustache template.
165 166 167 168 |
# File 'lib/smeagol/console.rb', line 165 def settings_template file = LIBDIR + '/templates/settings.yml' IO.read(file) end |
#show_repository(server_config) ⇒ Object
Show repositories being served
227 228 229 230 231 232 233 |
# File 'lib/smeagol/console.rb', line 227 def show_repository(server_config) $stderr.puts "\n Now serving on port #{server_config.port} at /#{server_config.base_path}:" server_config.repositories.each do |repository| $stderr.puts " * #{repository.path} (#{repository.cname})" end $stderr.puts "\n" end |
#site_path ⇒ String
Site directory path.
318 319 320 |
# File 'lib/smeagol/console.rb', line 318 def site_path settings.site_path end |
#site_repo ⇒ Repository
Site repository.
327 328 329 |
# File 'lib/smeagol/console.rb', line 327 def site_repo settings.site_repo end |
#update(*args) ⇒ Object
Update wiki repo(s).
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/smeagol/console.rb', line 278 def update(*args) = (Hash === args.last ? args.pop : {}) wiki_dir = args.first if wiki_dir dir = File.(wiki_dir) repo = Repository.new(:path=>dir) out = repo.update out = out[1] if Array === out report out else file = [:config_file] config = Config.load(file) abort "No repositories configured." if config.repositories.empty? config.secret = [:secret] config.repositories.each do |repository| report "Updating: #{repository.path}" out = repository.update out = out[1] if Array === out report out end end end |