Class: Junk::Command
- Inherits:
-
Object
- Object
- Junk::Command
- Defined in:
- lib/junk/command.rb
Constant Summary collapse
- PROXY_COMMANDS =
%w(add commit diff remote push pull log)
- SUB_COMMANDS =
%w(init clone track link unlink help status) + PROXY_COMMANDS
- HELP_STRING =
<<-EOS usage: junk [-v|--version] [--home] [--drawer] [-h|--help] COMMAND [ARGS] Commands: init Initialize a new junk drawer for the current directory clone Clones a git repo into ~/.junkd track Moves a file to the junk drawer and symlinks it from it's old location link Links in the junk drawer with the same name as the current directory unlink Removes any symlinks pointing to the current junk drawer help Displays information about a command Proxy Commands (passed to git): status #{PROXY_COMMANDS.inject("") { |str, c| str << " #{c}\n" }} EOS
- CMD_HELP_STRINGS =
{ "init" => "usage: junk init\n\nInitialize a new junk drawer for the current directory", "clone" => "usage: junk clone REMOTE\n\nClone REMOTE into ~/.junkd", "track" => "usage: junk track FILE\n\nMoves FILE to the junk drawer and symlinks it from it's old location", "link" => "usage: junk link\n\nCreates symlinks to to all the files in the junk drawer with the same name as the current directory", "unlink" => "usage: junk unlink\n\nRemoves any symlinks pointing to the current junk drawer", "status" => "usage: junk status\n\nRuns `git status` in the current junk drawer", "help" => "usage: junk help COMMAND\n\nShows usage information for COMMAND" }
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Class Method Summary collapse
Instance Method Summary collapse
- #clone ⇒ Object
- #help ⇒ Object
- #init ⇒ Object
-
#initialize(*args) ⇒ Command
constructor
A new instance of Command.
- #link ⇒ Object
- #proxy_command(cmd) ⇒ Object
- #run ⇒ Object
- #status ⇒ Object
- #track ⇒ Object
- #unlink ⇒ Object
Constructor Details
#initialize(*args) ⇒ Command
Returns a new instance of Command.
43 44 45 |
# File 'lib/junk/command.rb', line 43 def initialize(*args) @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
42 43 44 |
# File 'lib/junk/command.rb', line 42 def args @args end |
Class Method Details
.run(*args) ⇒ Object
47 48 49 |
# File 'lib/junk/command.rb', line 47 def self.run(*args) new(*args).run end |
Instance Method Details
#clone ⇒ Object
209 210 211 212 213 |
# File 'lib/junk/command.rb', line 209 def clone Dir.chdir(ENV["HOME"]) do exec_git_or_hub("clone #{@args.join(" ")} .junkd") end end |
#help ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/junk/command.rb', line 227 def help if @args.empty? puts HELP_STRING return end cmd = @args.shift if CMD_HELP_STRINGS[cmd] puts CMD_HELP_STRINGS[cmd] else error "unknown command '#{cmd}'." end end |
#init ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/junk/command.rb', line 93 def init setup unless prefix_is_setup? drawer_name = get_drawer_name(Dir.pwd) Dir.chdir(junk_home) do if File.exists?(File.join(junk_home, drawer_name)) error "There is already a junk drawer called #{drawer_name}." exit(1) end Dir.mkdir drawer_name end File.symlink(File.join(junk_home, drawer_name), File.join(Dir.pwd, ".junk")) add_to_git_ignore(".junk") say "Alright, #{Dir.pwd} now has a junk drawer." end |
#link ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/junk/command.rb', line 155 def link junk_drawer = junk_drawer_for_directory(Dir.pwd) unless File.directory? junk_drawer if File.exists? junk_home error "#{junk_drawer} doesn't exist. Are you in the root directory of your project?" else error "#{junk_drawer} doesn't exist. Have you cloned your junk repo yet?" end exit(1) end say "found junk drawer #{junk_drawer}" unless File.exists? ".junk" say "linking #{junk_drawer} => .junk" File.symlink(junk_drawer, ".junk") end junk_drawer_path = Pathname.new(junk_drawer) Find.find(junk_drawer) do |path| unless File.directory? path rel_path = Pathname.new(path).relative_path_from(junk_drawer_path).to_s unless File.exists? rel_path say "linking #{path} => #{rel_path}" File.symlink(path, rel_path) end end end end |
#proxy_command(cmd) ⇒ Object
221 222 223 224 225 |
# File 'lib/junk/command.rb', line 221 def proxy_command(cmd) Dir.chdir(junk_repo!) do exec_git_or_hub("#{cmd} #{@args.join(" ")}") end end |
#run ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/junk/command.rb', line 51 def run parser = Trollop::Parser.new do version "Junk #{Junk::VERSION}" "#{HELP_STRING}\nOptions:" opt :home, "Prints junk's home directory", :short => :none opt :drawer, "Prints the junk drawer for the current directory", :short => :none stop_on SUB_COMMANDS end @global_opts = Trollop::with_standard_exception_handling parser do o = parser.parse @args raise Trollop::HelpNeeded if ARGV.empty? o end if @global_opts[:home] puts junk_home exit(0) end if @global_opts[:drawer] puts junk_repo! exit(0) end check_for_git! cmd = @args.shift @cmd_opts = case cmd when *SUB_COMMANDS else error "unknown command '#{cmd}'." exit(1) end if PROXY_COMMANDS.include? cmd proxy_command(cmd) else self.send(cmd) end end |
#status ⇒ Object
215 216 217 218 219 |
# File 'lib/junk/command.rb', line 215 def status Dir.chdir(junk_repo!) do exec_git_or_hub("status .") end end |
#track ⇒ Object
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 150 151 152 153 |
# File 'lib/junk/command.rb', line 114 def track file = @args.shift Dir.chdir(parent_with_junk_drawer!) do unless File.exists? file error "#{file} doesn't seem to exist." exit(1) end if file[0] == '/' pwd = Pathname.new(Dir.pwd) file_path = Pathname.new(file) relative_path = file_path.relative_path_from(pwd).to_s else relative_path = file end if File.directory? relative_path error "junk doesn't support adding directories, only single files :(." exit(1) end dir, filename = File.split(relative_path) new_path = File.join(junk_drawer_for_directory(Dir.pwd), dir) unless File.exists? new_path FileUtils.mkpath(new_path) end new_path = File.join(junk_drawer_for_directory(Dir.pwd), relative_path) if File.exists? new_path error "it looks like you've already added #{file}" exit(1) end FileUtils.mv(relative_path, new_path) File.symlink(new_path, relative_path) add_to_git_ignore(relative_path) end end |
#unlink ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/junk/command.rb', line 186 def unlink junk_repo = junk_repo! # junk_repo! uses Dir.chdir without a block. Will fix this later maybe Dir.chdir(parent_with_junk_drawer!) do |dir| Find.find(Dir.pwd) do |path| if File.directory? path if File.basename(path)[0] == ?. Find.prune end elsif File.symlink? path if File.readlink(path).start_with? junk_repo puts "unlinking #{path}" File.unlink(path) end end end if File.exists? ".junk" puts "unlinking #{dir}/.junk" File.unlink(".junk") end end end |