Class: Serif::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/serif/commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Commands

Returns a new instance of Commands.



7
8
9
# File 'lib/serif/commands.rb', line 7

def initialize(argv)
  @argv = argv.dup
end

Instance Method Details

#generate_site(source_dir) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/serif/commands.rb', line 46

def generate_site(source_dir)

  site = Serif::Site.new(source_dir)

  begin
    site.generate
  rescue Serif::PostConflictError => e
    puts "Error! Unable to generate because there is a conflict."
    puts
    puts "Conflicts at:"
    puts

    site.conflicts.each do |url, ary|
      puts url
      ary.each do |e|
        puts "\t#{e.path}"
      end
    end

    exit 1
  end
end

#initialize_admin_server(source_dir) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/serif/commands.rb', line 28

def initialize_admin_server(source_dir)
  # need to cd to the directory before requiring the admin
  # server, because otherwise Dir.pwd won't be right when
  # the admin server class is defined at require time.
  FileUtils.cd(source_dir)
  require "serif/admin_server"

  server = Serif::AdminServer.new(source_dir)
  server.start
end

#initialize_dev_server(source_dir) ⇒ Object



39
40
41
42
43
44
# File 'lib/serif/commands.rb', line 39

def initialize_dev_server(source_dir)
  FileUtils.cd(source_dir)

  server = Serif::DevelopmentServer.new(source_dir)
  server.start
end


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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/serif/commands.rb', line 97

def print_help
  puts "USAGE\n\n  serif [-h | --help]\n  serif [COMMAND]\n\nOPTIONS\n\n  -h, --help  Display this help message and exit immediately.\n\nCOMMANDS\n\n  serif generate    Generate the site in the current directory.\n\n  serif new         Create a site skeleton to get started. Will\n                    only run if the current directory is empty.\n\n  serif admin       Start the admin server on localhost:4567.\n\n  serif dev         Start a simple dev server on localhost:8000.\n                    Serves up the generated static files, but loads\n                    some files (like CSS) from source (instead of\n                    out of the _site/ directory).\n\nENVIRONMENT VARIABLES\n\n  ENV               Set to 'production' if the command is being run\n                    as part of serving up a live site.\n\n                        $ ENV=production serif generate\n\n                        $ ENV=production serif admin\n\n                    Note that this by and large doesn't change much,\n                    but in future it may provide extra features.\n\n                    The main benefit is that the `file_digest` tag\n                    will return a hex digest of the given file's\n                    contents only when ENV is set to production.\n\nEXAMPLES\n\n  $ serif generate\n\n    Generate the site.\n\n  $ serif admin\n\n    Start the admin server on localhost:4567.\n\n"
end

#processObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/serif/commands.rb', line 11

def process
  command = @argv.shift
  case command
  when "-h", "--help", nil
    print_help
    exit 0
  when "admin"
    initialize_admin_server(Dir.pwd)
  when "generate"
    generate_site(Dir.pwd)
  when "dev"
    initialize_dev_server(Dir.pwd)
  when "new"
    produce_skeleton(Dir.pwd)
  end
end

#produce_skeleton(dir) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/serif/commands.rb', line 76

def produce_skeleton(dir)
  if !Dir[File.join(dir, "*")].empty?
    abort "Directory is not empty."
  end

  FileUtils.cd(File.join(File.dirname(__FILE__), "..", "..", "statics", "skeleton"))
  files = Dir["*"]
  files.each do |f|
    FileUtils.cp_r(f, dir, verbose: true)
  end
  FileUtils.mkdir(File.join(dir, "_posts"))

  generate_site(dir)

  puts
  puts "*** NOTE ***"
  puts
  puts "You should now edit the username and password in _config.yml"
  puts
end

#verify_directory(dir) ⇒ Object



69
70
71
72
73
74
# File 'lib/serif/commands.rb', line 69

def verify_directory(dir)
  unless Dir.exist?(dir)
    puts "No such directory: #{dir}'"
    exit 1
  end
end