Module: Diary::CLI::Commands

Included in:
Diary::CLI
Defined in:
lib/diary/cli/commands.rb,
lib/diary/cli/commands/edit.rb,
lib/diary/cli/commands/init.rb,
lib/diary/cli/commands/sync.rb,
lib/diary/cli/commands/server.rb,
lib/diary/cli/commands/compile.rb,
lib/diary/cli/commands/new_item.rb

Instance Method Summary collapse

Instance Method Details

#command(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/diary/cli/commands.rb', line 8

def command(*args)
  options = Diary::CLI.parse(args)

  if args.size == 0
    return
  end

  if respond_to?(args.first)
    self.send(args.shift, args, options)
  elsif
    Diary.message :error, "No command found", :red
  end
end

#compile(args, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/diary/cli/commands/compile.rb', line 4

def compile(args, options)
  if (args.first == "all") or (args.size == 0)
    Post.all.each { |p| p.output(options.force) }
    Page.all.each { |p| p.output(options.force) }
  elsif args.size >= 2
    const_get(args.shift.capitalize).find(args.first).output(options.force)
  else
    const_get(args.shift.capitalize).all.each { |p| p.output(options.force) }
  end
end

#edit(args, options) ⇒ Object



4
5
6
# File 'lib/diary/cli/commands/edit.rb', line 4

def edit(args, options)
  `#{editor} #{const_get(args.shift.capitalize).find(args.first).path}`
end

#init(args, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/diary/cli/commands/init.rb', line 4

def init(args, options)
  # Create directories
  %w{ templates posts pages snippets public }.each do |directory|
    create_dir directory
  end

  # Create files
  create_file "templates/index.html.erb", "<%= content %>"
  create_file "config.yml", "sync:\n  user: \n  server: \n  path: \n"

  unless File.exists?(".git")
    if options.git
      Diary.message :git, "Initial commit", :magenta
      init_git!
    else
      Diary.message :skip, "git init", :red
    end
  else
    Diary.message :git, "Already a git repository", :yellow
  end
end

#new(args, options) ⇒ Object



4
5
6
# File 'lib/diary/cli/commands/new_item.rb', line 4

def new(args, options)
  const_get(args.shift.capitalize).create(args.first)
end

#server(args, options) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/diary/cli/commands/server.rb', line 4

def server(args, options)
  Diary.message :start, "Diary server is starting...", :green
  `ruby #{File.expand_path("../../../server.rb", __FILE__)}`
rescue Interrupt
  Diary.message :close, "Diary server is closing...", :red
  sleep(2)
end

#sync(args, options) ⇒ Object



4
5
6
7
# File 'lib/diary/cli/commands/sync.rb', line 4

def sync(args, options)
  Diary.message :sync, sync_path, :magenta
  `rsync --verbose  --progress --stats --compress --recursive --times --perms --links --delete --exclude "*bak" ./public/* #{sync_path}`
end