Module: Sup

Defined in:
lib/sup/differ/differ.rb,
lib/sup/api.rb,
lib/sup/base.rb,
lib/sup/help.rb,
lib/sup/command.rb

Overview

TODO: check git status for added un-tracked files

Defined Under Namespace

Modules: Api, Command, Differ Classes: Project

Constant Summary collapse

VERSION =
'0.1.6'
GLOBAL_CONFIG_PATH =
'~/.utsup/config.yml'
GLOBAL_PROJECT_CONFIG_PATH =
'~/.utsup/projects.yml'
PROJECT_CONFIG_PATH =
'.git/utsup.yml'
API_URL =
"https://utsup.heroku.com"
SIGNUP_URL =
"http://www.utsup.com"
GIT_HOOKS =

Init

{
  "post-checkout" => "sup git checkout $@",
  "post-commit"   => "sup git commit",
  "post-merge"    => "sup git merge $@",
  "post-receive"  => "sup git receive $@"
}
HELP_TEXT =
<<-eos
=======================================
UtSup Client v.#{VERSION}
by Nick Merwin (Lemur Heavy Industries)
=======================================

=== Examples:
  sup setup

  cd /some-project/ && sup init
  sup in "whatup"
  sup
  sup "just chillin"
  sup out "later"

=== Commands:

  help                      # show this message
  version                   # show version

  setup <api_key>           # initializes global config file, if no api_key specified, will let you login

  init <project name>       # initilize current directory

  "<message>"               # send status update for current project
  nm                        # destroy your last supdate

  (no command)              # get all user's current status
  all                       # get all user's statuses over the past day

  in "<message>"            # check in to project
  out "<message>"           # check out of project

  users                     # get list of users in company
  <user name>               # get last day's worth of status updates from specified user

  push                      # triggers a git push + update

  start                     # starts differ
  stop                      # stops differ
eos

Class Method Summary collapse

Class Method Details

.check_in(message) ⇒ Object

Check In/Out



138
139
140
# File 'lib/sup/base.rb', line 138

def check_in(message)
  Api::Status.add :status_type => "StatusIn", :message => message
end

.check_out(message) ⇒ Object



142
143
144
# File 'lib/sup/base.rb', line 142

def check_out(message)
  Api::Status.add :status_type => "StatusOut", :message => message
end

.configureObject

Configure



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sup/base.rb', line 117

def configure
  begin
    global_config = Yamlize.new GLOBAL_CONFIG_PATH
    project_config = Yamlize.new(File.join(Dir.pwd, PROJECT_CONFIG_PATH)) rescue {}
    global_project_config = Yamlize.new GLOBAL_PROJECT_CONFIG_PATH
  
    raise unless global_config['api_key']
  rescue 
    setup
    exit 0
  end
  
  # --- configure API
  Api::Base.project_id = project_config['project_id']
  Api::Base.password = project_config['api_key'] || global_config['api_key']
  Api::Base.site = project_config['domain'] || global_config['domain'] || API_URL
end

.current_branch_nameObject

Git Update



158
159
160
# File 'lib/sup/base.rb', line 158

def current_branch_name
  `git branch 2> /dev/null | grep -e ^*`[/^\* (.*?)\n/,1]
end

.get_statuses(opts = {}) ⇒ Object

Get Statuses



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/sup/base.rb', line 219

def get_statuses(opts={})
  name = opts[:name]
  
  statuses = Api::Status.find :all, :params => {
    :name => name, 
    :today => opts[:today]
  }
  
  width = statuses.map{|s| s.to_command_line.gsub(/\\e\[.*?m/,'').size}.max + 2
  line = (" "*width).as_underline.in_white

  render "\nThis is ".in_green+"UtSup?".in_magenta+"#{" with #{name.in_yellow}".in_green if name}:"
  render line
  puts "\n"
  statuses.each do |status|
    render "  "+status.to_command_line
  end
  render line

  puts "\n"
end

.get_usersObject



241
242
243
244
245
246
247
248
# File 'lib/sup/base.rb', line 241

def get_users
  users = Api::User.find :all
  
  puts "#{users.first.company.title} Users:"
  users.each do |user|
    puts user.to_command_line
  end
end

.git_update(*args) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/sup/base.rb', line 162

def git_update(*args)
  git = Git.open(Dir.pwd)

  args.flatten!
  
  case args.first.strip
            
  when "checkout"
    previous_head = args[1]
    next_head = args[2]
    branch = args[3] == '1'
    
    # TODO: get previous branch name from ref
    
    Api::Status.add :status_type => "StatusCheckout",
      :message => current_branch_name
    
  when "push"
    resp = `git push #{args[1..-1]*' '} 2>&1`
    puts resp
    unless resp =~ /Everything up-to-date/
      Api::Status.add :status_type => "StatusPush", :message => "pushed"
    end
    
  when "receive"
    Api::Status.add :status_type => "StatusReceive",:message => "received"
  
  when "merge"
    Api::Status.add :status_type => "StatusMerge", :message => "merged"
  
  when "commit"
    
    commit = git.object('HEAD')
    sha = commit.sha
  
    Api::Status.add :status_type => "StatusCommit", 
      :message => commit.message, 
      :ref => sha, :text => `git diff HEAD~ HEAD`
              
  else
    puts "WTF git status is that?"
  end
    
end

.init(project_title) ⇒ Object



78
79
80
81
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
# File 'lib/sup/base.rb', line 78

def init(project_title)      
  # --- project init
  project_title = File.basename(Dir.pwd) if project_title.blank? || project_title == "."
  project = Api::Project.create :title => project_title
  
  if project.valid?
    # add project id to .git
    Yamlize.new File.join(Dir.pwd, PROJECT_CONFIG_PATH) do |project_config|
      project_config.project_id = project.id
    end

    # add project path and id to global project config (for differ)
    Yamlize.new GLOBAL_PROJECT_CONFIG_PATH, Array do |global_project_config|
      global_project_config << {'path'=>Dir.pwd, 'id'=>project.id}
      global_project_config.uniq!
    end

    # --- write git hooks
    GIT_HOOKS.each do |hook, command|
      path = File.join(Dir.pwd, '.git/hooks/', hook)
      exists = File.exists?(path)

      next if exists && File.read(path) =~ /#{Regexp.quote(command)}/

      File.open(path, (exists ? 'a' : 'w'), 0775) do |f|
        f.puts command
      end
    end
            
  else
    #// project creation faild
    render project.errors.full_messages.map &:in_red
  end
end

.setup(api_key = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sup/base.rb', line 39

def setup(api_key=nil)
  require 'fileutils'
  
  # --- global init
  global_config_path = File.expand_path(GLOBAL_CONFIG_PATH)
  
  # for back-compat with old config file
  oldpath = File.dirname(global_config_path)
  if File.exists?(oldpath) && !File.directory?(oldpath)
    FileUtils.mv oldpath, oldpath+'.bak'
    FileUtils.mkdir File.dirname(global_config_path)
    FileUtils.mv oldpath+'.bak', global_config_path
  end
  
  FileUtils.mkdir File.dirname(global_config_path) rescue nil
  global_config = Yamlize.new global_config_path
  
  unless global_config['api_key']
    global_config.api_key = api_key || 
    global_config.save
    render "Success!".in_green+" Added API Key to #{GLOBAL_CONFIG_PATH}\n" +
      "  - Lemur Heavy Industries ".in_yellow +
      "http://lemurheavy.com".in_blue.as_underline
  else
    render "You're good to go.".in_green
  end
end

.sign_inObject

Setup



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sup/base.rb', line 17

def 
  render "Please enter your "+"UtSup?".in_magenta+" credentials. " + 
    "Don't have an account yet? ".in_red + 
    "Create one at ".in_green + "#{SIGNUP_URL}".in_blue.as_underline
  
  loop do
    print "Email: ".in_yellow.escape
    email = gets.chomp
    print "Password: ".in_yellow.escape
    system "stty -echo"
    password = gets.chomp
    system "stty echo"
    puts ""
    
    if api_key = Api::User.get_api_key(email, password)
      return api_key
    else
      render "Couldn't find that email/password combo...".in_red
    end
  end
end

.socket_errorObject



250
251
252
# File 'lib/sup/base.rb', line 250

def socket_error
  render "UtSup? v.#{VERSION} Offline.".in_black.on_red
end

.undoObject

undo



150
151
152
# File 'lib/sup/base.rb', line 150

def undo
  Api::Status.delete :undo
end

.update(message) ⇒ Object

Update



211
212
213
# File 'lib/sup/base.rb', line 211

def update(message)
  Api::Status.add :status_type => "StatusUpdate", :message => message
end