Top Level Namespace

Defined Under Namespace

Classes: RuneCMS

Constant Summary collapse

CONFIG =
"config.txt"

Instance Method Summary collapse

Instance Method Details

#command?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/runecms/commands.rb', line 66

def command?(cmd)
  self.respond_to?("run_#{cmd}", true)
end

#execute(cmd) ⇒ Object



70
71
72
# File 'lib/runecms/commands.rb', line 70

def execute(cmd)
  send("run_#{cmd}")
end

#fix_extension(file) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/runecms/support.rb', line 71

def fix_extension(file)
  if lt3?(file)
    file2 = file.sub(/.lt3$/, ".html")
  else
    file2 = file
  end
  file2
end

#lt3?(file) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/runecms/support.rb', line 67

def lt3?(file)
  file.end_with?(".lt3")
end

#read_configObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/runecms/support.rb', line 80

def read_config
  config = "config.txt"
  here = Dir.pwd
  loop do
    raise if Dir.pwd == "/"  # shouldn't get this far
    break if File.exist?(config)
    Dir.chdir("..")
  end
  lines = File.readlines("config.txt")
  # Example: "user: hal9000"
  lines.each do |line|
    var, val = line.split(": ")
    instance_variable_set("@" + var, val.chomp.strip)
  end
  return true
rescue => e
  puts "Can't read config file: here = #{here}  pwd = #{Dir.pwd}"
  puts e
  return false
end

#run_browseObject



62
63
64
# File 'lib/runecms/commands.rb', line 62

def run_browse
  system("open #@server")
end

#run_checkObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/runecms/commands.rb', line 10

def run_check
  list = stale_files("source")
  if stale.empty? 
    puts "No stale files"
  else
    puts "Stale files:"
    stale.each {|x| puts "  " + x }
    puts
  end
end

#run_configObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/runecms/commands.rb', line 21

def run_config
  unless read_config
    File.open("config.txt", "w") do |f|
      f.puts "server: "
      f.puts "path: "
      f.puts "user: "
    end
  end
  system("vi config.txt")
end

#run_generateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/runecms/commands.rb', line 32

def run_generate
  verify_dirs    # handle missing subdirectories
  list = stale_files("source") 
  if stale.empty? 
    puts "Nothing to do"
  else
    puts "Stale files:"
    stale.each do |file| 
      puts "  " + x
      update_target(file)
    end
    puts
  end
end

#run_publishObject



57
58
59
60
# File 'lib/runecms/commands.rb', line 57

def run_publish
  cmd = "rsync -r -z target/ #@user@#@server:#@path/"
  system(cmd)
end

#run_updateObject



47
48
49
50
# File 'lib/runecms/commands.rb', line 47

def run_update
  run_generate
  run_publish
end

#run_versionObject



6
7
8
# File 'lib/runecms/commands.rb', line 6

def run_version
  puts RuneCMS::VERSION
end

#run_viewObject



52
53
54
55
# File 'lib/runecms/commands.rb', line 52

def run_view
  # FIXME index is hardcoded...
  system("open target/index.html")
end

#stale?(file) ⇒ Boolean

without source/ or target/

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/runecms/support.rb', line 26

def stale?(file)  # without source/ or target/
  return false if File.directory?(file)
  if file.end_with?(".lt3")
    file1 = file
    file2 = file.sub(/.lt3$/, ".html")
  else
    file1 = file2 = file
  end
  return true if ! File.exist?("target/#{file2}")

  t1 = File.mtime("source/#{file1}")
  t2 = File.mtime("target/#{file2}")
  t1 > t2
end

#stale_files(dir) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/runecms/support.rb', line 17

def stale_files(dir)
  list = nil
  path = "#{Dir.pwd}/#{dir}/"
  list = Find.find(path).to_a
  list = list.select {|x| ! File.directory?(x) }
  list.map! {|x| x.sub(path, "") }
  list.select {|x| stale?(x) }
end

#update_target(file) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/runecms/support.rb', line 41

def update_target(file)
  file2 = fix_extension(file)
  if lt3?(file) 
    cmd = "livetext source/#{file} > target/#{file2}"
  else
    if File.directory?(file)
      cmd = "mkdir target/#{file2}"
    else
      cmd = "cp source/#{file} target/#{file2}"
    end
  end
  system(cmd)
end

#usage_messageObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/runecms/support.rb', line 2

def usage_message
  puts
  puts "    rcms config    Initialize config.txt if necessary and edit with vi\n    rcms check     List stale files, but do nothing else\n    rcms generate  Find stale files under source/ and generate them under target/\n    rcms view      View the current state of target/ via browser (local files)\n    rcms publish   Publish target/ to the remote server\n    rcms update    Shortcut - Like a generate followed by a push\n    rcms browse    Browse the current state of the remote server\n  TEXT\n  puts\n  exit\nend\n"

#verify_dirsObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/runecms/support.rb', line 55

def verify_dirs
  src_dirs = []
  Find.find("source") do |path|
    src_dirs << path if File.directory?(path)
  end

  src_dirs.each do |path|
    tdir = path.sub("source", "target")
    Dir.mkdir(tdir) unless Dir.exist?(tdir)
  end
end