Module: Bananajour::Commands

Included in:
Bananajour
Defined in:
lib/bananajour/commands.rb

Instance Method Summary collapse

Instance Method Details

#advertise!Object



33
34
35
# File 'lib/bananajour/commands.rb', line 33

def advertise!
  fork { Bananajour::Bonjour::Advertiser.new.go! }
end

#check_git!Object



3
4
5
6
7
# File 'lib/bananajour/commands.rb', line 3

def check_git!
  if (version = `git --version`.strip) =~ /git version 1\.[12345]/
    abort "You have #{version}, you need at least 1.6"
  end
end

#check_git_config!Object



9
10
11
12
13
# File 'lib/bananajour/commands.rb', line 9

def check_git_config!
  config_message = lambda {|key, example| "You haven't set your #{key} in your git config yet. To set it: git config --global #{key} '#{example}'"}
  abort(config_message["user.name", "My Name"]) if config.name.empty?
  abort(config_message["user.email", "[email protected]"]) if config.email.empty?
end

#clone!(url, clone_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bananajour/commands.rb', line 72

def clone!(url, clone_name)
  dir = clone_name || File.basename(url).chomp('.git')

  if File.exists?(dir)
    abort "Can't clone #{url} to #{dir}, the directory already exists."
  end

  `git clone #{url} #{dir}`
  if $? != 0
    abort clone_failure_message(url, repo.dirname)
  else
    puts clone_success_message(url, dir)
    init!(dir, dir)
  end
end

#clone_failure_message(source_repo_url, repo_dirname) ⇒ Object



92
93
94
# File 'lib/bananajour/commands.rb', line 92

def clone_failure_message(source_repo_url, repo_dirname)
  "Failed to clone Bananajour repository #{source_repo_url} to #{repo_dirname}."
end

#clone_success_message(source_repo_url, repo_dirname) ⇒ Object



88
89
90
# File 'lib/bananajour/commands.rb', line 88

def clone_success_message(source_repo_url, repo_dirname)
  "Bananajour repository #{source_repo_url} cloned to #{repo_dirname}."
end

#init!(dir, name = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bananajour/commands.rb', line 37

def init!(dir, name = nil)
  dir = Fancypath(dir)

  unless dir.join(".git").directory?
    abort "Can't init project #{dir}, no .git directory found."
  end

  if name.nil?
    default_name = dir.basename.to_s
    print "Project Name?".foreground(:yellow) + " [#{default_name}] "
    name = (STDIN.gets || "").strip
    name = default_name if name.empty?
  end

  repo = Bananajour::Repository.for_name(name)

  if repo.exists?
    abort "You've already a project #{repo}."
  end

  repo.init!
  Dir.chdir(dir) { `git remote add banana #{repo.path.expand_path}` }
  puts init_success_message(repo.dirname)

  repo
end

#init_success_message(repo_dirname) ⇒ Object



64
65
66
# File 'lib/bananajour/commands.rb', line 64

def init_success_message(repo_dirname)
  plain_init_success_message(repo_dirname).gsub("git push banana master", "git push banana master".foreground(:yellow))
end

#plain_init_success_message(repo_dirname) ⇒ Object



68
69
70
# File 'lib/bananajour/commands.rb', line 68

def plain_init_success_message(repo_dirname)
  "Bananajour repository #{repo_dirname} initialised and remote banana added.\nNext: git push banana master"
end

#serve_git!Object



23
24
25
26
# File 'lib/bananajour/commands.rb', line 23

def serve_git!
  fork { exec "git daemon --base-path=#{repositories_path} --export-all" }
  puts "* Started " + "#{git_uri}".foreground(:yellow)
end

#serve_web!Object



15
16
17
18
19
20
21
# File 'lib/bananajour/commands.rb', line 15

def serve_web!
  if repositories.empty?
    STDERR.puts "Warning: you don't have any bananajour repositories. Use: bananajour init"
  end
  fork { exec "/usr/bin/env ruby #{File.dirname(__FILE__)}/../../sinatra/app.rb -p #{web_port} -e production" }
  puts "* Started " + web_uri.foreground(:yellow)
end