Class: SSS

Inherits:
Object
  • Object
show all
Defined in:
lib/sss.rb,
lib/sss/version.rb

Constant Summary collapse

HELP =
<<-EOH
SSS performs SCM commands on all projects in your workspace. Set the
SSS_WORKSPACE environment variable if your workspace is not ~/workspace.

Usage:
sss COMMAND

Commands:
pull, up, update  Update to the latest changes
status, st        Check the status for any uncommitted changes
push              (git, hg) Push all committed changes to central repo
out, outgoing     (git, hg) Show outgoing changes, not pushed to central repo
in, incoming      (git, hg) Show incoming changes, not updated
wtf               (git) Compare local to tracked remote branch
EOH
GIT =

SCMs

:git
MERCURIAL =
:hg
SUBVERSION =
:svn
COMMANDS =
{
  "status" => {
    GIT => "git status",
    MERCURIAL => "hg status",
    SUBVERSION => "svn status",
  },
  "push" => {
    GIT => "git push",
    MERCURIAL => "hg push",
  },
  "pull" => {
    GIT => "git pull",
    MERCURIAL => "hg pull -u",
    SUBVERSION => "svn up",
  },
  "incoming" => {
    GIT => "git wtf",
    MERCURIAL => "hg incoming",
  },
  "outgoing" => {
    GIT => "git wtf",
    MERCURIAL => "hg outgoing",
  },
  "wtf" => {
    GIT => "git wtf",
  },
}
VERSION =
"1.0.4"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = nil) ⇒ SSS

Returns a new instance of SSS.



57
58
59
60
# File 'lib/sss.rb', line 57

def initialize(command = nil)
  self.command = command
  self.workspace = ENV['SSS_WORKSPACE'] || "~/workspace/"
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/sss.rb', line 5

def command
  @command
end

#workspaceObject

Returns the value of attribute workspace.



6
7
8
# File 'lib/sss.rb', line 6

def workspace
  @workspace
end

Class Method Details

.run(command) ⇒ Object



76
77
78
# File 'lib/sss.rb', line 76

def self.run(command)
  new(command).run
end

Instance Method Details

#directoriesObject



86
87
88
89
90
91
92
# File 'lib/sss.rb', line 86

def directories
  if File.directory? File.expand_path workspace
    Dir["#{File.expand_path(workspace)}/*"].map { |dir| File.expand_path(dir) }
  else
    raise ArgumentError, "#{workspace} is not a directory. Please set the WORKSPACE environment variable to the directory containing your projects."
  end
end

#directory_string(directory) ⇒ Object



133
134
135
136
# File 'lib/sss.rb', line 133

def directory_string(directory)
  last_directory = directory.split("/").last
  embolden last_directory
end

#display(string, output = STDOUT) ⇒ Object



125
126
127
# File 'lib/sss.rb', line 125

def display(string, output = STDOUT)
  output.puts string
end

#display_commandObject



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

def display_command
  "Attempting to perform #{command}"
end

#embolden(string) ⇒ Object



129
130
131
# File 'lib/sss.rb', line 129

def embolden(string)
  "\033[1m#{string}\033[0m"
end

#perform_command(directory) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sss.rb', line 94

def perform_command(directory)
  command_for_directory = scm_command_for directory
  if command_for_directory
    display "Performing #{command} in #{directory_string directory} (#{scm_for directory})"
    stdout, stderr, status = Open3.capture3 command_for_directory
    display stdout
    display stderr, STDERR
  else
    display "Skipping #{directory_string directory}, no available command"
  end
end

#runObject



80
81
82
83
84
# File 'lib/sss.rb', line 80

def run
  if COMMANDS.include? command
    directories.each { |directory| perform_command(directory) }
  end
end

#scm_command_for(directory) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/sss.rb', line 106

def scm_command_for(directory)
  scm_command = COMMANDS[command][scm_for(directory)]
  if scm_command
    "cd #{directory} && #{scm_command}"
  end
rescue
  nil
end

#scm_for(directory) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/sss.rb', line 115

def scm_for(directory)
  if File.exists?("#{directory}/.git")
    GIT
  elsif File.exists?("#{directory}/.hg")
    MERCURIAL
  elsif File.exists?("#{directory}/.svn")
    SUBVERSION
  end
end