Class: Foresite::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/foresite/cli.rb

Overview

Cli class.

For CLI functionality to generate static content.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Ensure that failures exit with a status of zero.

Returns:

  • (Boolean)


11
12
13
# File 'lib/foresite/cli.rb', line 11

def self.exit_on_failure?
  true
end

Instance Method Details

#buildObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/foresite/cli.rb', line 82

def build
  unless Foresite.subdirectories_exist?
    warn("Missing subdirectories, try running `foresite init`")
    exit(1)
  end

  # Wipe all output files.
  Dir.glob(File.join(Foresite.get_path_to_out, "*.html")).each { File.delete(_1) }
  File.delete(Foresite.get_path_to_index_file) if File.exist?(Foresite.get_path_to_index_file)

  markdown_paths = Dir.glob(File.join(Foresite.get_path_to_md, "*.md"))

  if markdown_paths.count == 0
    warn("No markdown files, try running `foresite touch`")
    exit(1)
  end

  links = markdown_paths.map do |markdown_path|
    markdown_content = File.read(markdown_path)
    title = markdown_content.split("\n").first { |line| /^# [a-z]/i =~ line }.gsub(/^#/, "").strip

    filename_markdown = File.basename(markdown_path)
    html_path = Foresite.get_path_to_out_file(filename_markdown.gsub(/\.md$/, ".html"))

    File.write(html_path, Foresite.render_wrapped(title, markdown_content))
    $stdout.puts("Created #{Foresite.relative_path(html_path)}")

    # Extract date if it exists.
    match_data = /\d{4}-\d{2}-\d{2}/.match(filename_markdown)

    {
      date_ymd: match_data.nil? ? "" : match_data[0],
      href: Foresite.relative_path(html_path),
      title: title
    }
  end

  # Generate index file.
  index_html_path = Foresite.get_path_to_index_file
  File.write(index_html_path, Foresite.render_wrapped_index(links))

  $stdout.puts("Created #{Foresite.relative_path(index_html_path)}")
end

#initObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/foresite/cli.rb', line 29

def init
  unless Foresite.root_exists?
    warn("Nonexistent directory #{Foresite.get_path_to_root}")
    exit(1)
  end

  unless Foresite.root_writable?
    warn("Cannot write to directory #{Foresite.get_path_to_root}")
    exit(1)
  end

  Foresite.touch_directories.map { $stdout.puts(_1) }
  Foresite.copy_templates.map { $stdout.puts(_1) }
end

#touch(title) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/foresite/cli.rb', line 56

def touch(title)
  unless Foresite.subdirectories_exist?
    warn("Missing subdirectories, try running `foresite init`")
    exit(1)
  end

  date_ymd = Time.now.strftime("%F")
  slug = title.downcase.gsub(/[^a-z]/i, " ").strip.gsub(/ +/, "-")

  path = Foresite.get_path_to_md_file("#{date_ymd}-#{slug}.md")

  if File.exist?(path)
    $stdout.puts("File #{Foresite.relative_path(path)} already exists")
  else
    File.write(path, Foresite.render_post(title, date_ymd))
    $stdout.puts("Created #{Foresite.relative_path(path)}")
  end
end

#versionObject



16
17
18
# File 'lib/foresite/cli.rb', line 16

def version
  $stdout.puts(Foresite::VERSION)
end

#watchObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/foresite/cli.rb', line 132

def watch
  dirs_to_watch = [
    Foresite::DIRNAME_MARKDOWN,
    Foresite::DIRNAME_ERB
  ]

  $stdout.puts("Watching #{dirs_to_watch.map { "./#{_1}" }.join(", ")} for changes... (Ctrl+C to exit)")

  Filewatcher.new(dirs_to_watch).watch do |changes|
    changes.each do |filename, event|
      $stdout.puts("#{File.basename(filename)} #{event}")
    end

    build
  end
end