Module: Smeagol::Console

Extended by:
Console
Included in:
Console
Defined in:
lib/smeagol/console.rb

Overview

Console encapsulates all the public methods smeagol is likely to need, generally it maps to all the CLI commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#wiki_dirString

Current wiki directory.

Returns:

  • (String)

    Returns wiki directory.



63
64
65
# File 'lib/smeagol/console.rb', line 63

def wiki_dir
  @wiki_dir
end

#wiki_urlObject

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_signalsObject

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_wikiObject

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_assetsObject

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_layoutsObject

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

#gitString

Git executable.

Returns:

  • (String)

    Returns git command path.



363
364
365
# File 'lib/smeagol/console.rb', line 363

def git
  Smeagol.git
end

#init(*args) ⇒ void

TODO:

Perhaps use a supporting “managed copy” gem in future?

TODO:

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)
  options = (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(options)
  save_gitignore

  copy_layouts unless options[:no_layouts]
  copy_assets  unless options[:no_assets]
end

#initial_settings(options = {}) ⇒ Settings

When using #init, this provided the initial settings.

@returns [Settings]

Returns:



155
156
157
158
159
160
# File 'lib/smeagol/console.rb', line 155

def initial_settings(options={})
  options[:wiki_origin] = wiki_url
  options[:site_origin] = wiki_url.sub('.wiki', '')

  @settings = Settings.new(options)
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(options)
  repository = {}
  repository[:path]   = Dir.pwd
  #repository[:cname] = options[:cname]  if options[:cname]
  repository[:secret] = options.delete(:secret) if options.key?(:secret)

  options[:repositories] = [repository]

  config = Smeagol::Config.new(options)

  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_gitignoreObject

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(options)
  file = File.join(wiki_dir, "_settings.yml")
  if File.exist?(file)
    $stderr.puts " skip: #{file}"
  else
    text = Mustache.render(settings_template, initial_settings(options)) 
    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(options)
  config_file = options[:config_file]
  config = Config.load(config_file)
  config.assign(options)
  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 options[:secret]
    config.repositories.each{ |r| r['secret'] = options['secret'] }
  end

  catch_signals

  show_repository(config)
  auto_update(config)
  clear_caches(config)

  run_server(config)
end

#settingsSettings

Local wiki settings.

Returns:



354
355
356
# File 'lib/smeagol/console.rb', line 354

def settings
  @settings ||= Settings.load(wiki_dir)
end

#settings_templateObject

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_pathString

Site directory path.

Returns:

  • (String)

    Returns expanded site path.



318
319
320
# File 'lib/smeagol/console.rb', line 318

def site_path
  settings.site_path
end

#site_repoRepository

Site repository.

Returns:



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)
  options  = (Hash === args.last ? args.pop : {})
  wiki_dir = args.first

  if wiki_dir
    dir  = File.expand_path(wiki_dir)
    repo = Repository.new(:path=>dir)
    out  = repo.update
    out  = out[1] if Array === out
    report out
  else
    file   = options[:config_file]
    config = Config.load(file)
    abort "No repositories configured." if config.repositories.empty?

    config.secret = options[:secret]
    config.repositories.each do |repository|
      report "Updating: #{repository.path}"
      out = repository.update
      out = out[1] if Array === out
      report out
    end
  end
end

#wikiWiki

Get and cache Wiki object.

Returns:

  • (Wiki)

    Returns wiki.



345
346
347
# File 'lib/smeagol/console.rb', line 345

def wiki
  @wiki ||= Smeagol::Wiki.new(wiki_dir)
end