Add Application class

This commit is contained in:
Josh Holtrop 2018-02-27 20:18:06 -05:00
parent 6f55b3ddae
commit 090daf39a0
3 changed files with 78 additions and 74 deletions

View File

@ -1,79 +1,8 @@
require_relative "svi/ansi"
require_relative "svi/application"
require_relative "svi/cli"
require_relative "svi/config"
require_relative "svi/svn_runner"
require_relative "svi/util"
require_relative "svi/version"
require "svi/svi"
module Svi
class << self
def checkout(url)
checkout_directory = []
if url =~ %r{/([^/]+)/trunk$}
checkout_directory << $1
end
last_checkout_message = ""
checked_out_paths = []
clear_message = lambda do
if last_checkout_message.size > 0
clear = ""
lines = (last_checkout_message.size + C.screen_width - 1) / C.screen_width
if lines > 1
clear += Ansi.cursor_up(lines - 1)
end
clear += Ansi.cursor_back(999)
clear += Ansi.erase_cursor_to_eos
$stdout.write(clear)
last_checkout_message = ""
end
end
SvnRunner.run_svn("checkout", [url] + checkout_directory, allow_interactive: true) do |line|
if line =~ /^A.{4}(.*)$/
path = $1
checked_out_paths << path
clear_message[]
last_checkout_message = "Checking out #{path}..."
$stdout.write(last_checkout_message)
$stdout.flush
elsif line =~ /^\sU\s{3}/
# Ignore the 'U'pdate line of the checkout directory itself.
elsif line =~ /^Checked out revision (\d+)/
revision = $1
clear_message[]
n_files = 0
n_directories = 0
checked_out_paths.uniq.each do |path|
if File.directory?(path)
n_directories += 1
else
n_files += 1
end
end
$stdout.puts "Checked out revision #{revision}: #{n_files} file#{n_files == 1 ? '' : 's'}, #{n_directories} director#{n_directories == 1 ? 'y' : 'ies'}"
else
clear_message[]
$stdout.puts line
end
end
0
end
def status
config = Config.new
SvnRunner.run_svn("status", []) do |line|
if line =~ %r{^([ACDIMRX\?!~ ])[CM ][L ][\+ ][SX ][KOTB ]..(.+)$}
action, path = $1, $2
if action == "?" and Util.is_path_ignored?(path, config)
# Path is ignored
else
puts line
end
end
end
0
end
end
end

71
lib/svi/application.rb Normal file
View File

@ -0,0 +1,71 @@
module Svi
class Application
def checkout(url)
checkout_directory = []
if url =~ %r{/([^/]+)/trunk$}
checkout_directory << $1
end
last_checkout_message = ""
checked_out_paths = []
clear_message = lambda do
if last_checkout_message.size > 0
clear = ""
lines = (last_checkout_message.size + C.screen_width - 1) / C.screen_width
if lines > 1
clear += Ansi.cursor_up(lines - 1)
end
clear += Ansi.cursor_back(999)
clear += Ansi.erase_cursor_to_eos
$stdout.write(clear)
last_checkout_message = ""
end
end
SvnRunner.run_svn("checkout", [url] + checkout_directory, allow_interactive: true) do |line|
if line =~ /^A.{4}(.*)$/
path = $1
checked_out_paths << path
clear_message[]
last_checkout_message = "Checking out #{path}..."
$stdout.write(last_checkout_message)
$stdout.flush
elsif line =~ /^\sU\s{3}/
# Ignore the 'U'pdate line of the checkout directory itself.
elsif line =~ /^Checked out revision (\d+)/
revision = $1
clear_message[]
n_files = 0
n_directories = 0
checked_out_paths.uniq.each do |path|
if File.directory?(path)
n_directories += 1
else
n_files += 1
end
end
$stdout.puts "Checked out revision #{revision}: #{n_files} file#{n_files == 1 ? '' : 's'}, #{n_directories} director#{n_directories == 1 ? 'y' : 'ies'}"
else
clear_message[]
$stdout.puts line
end
end
0
end
def status
config = Config.new
SvnRunner.run_svn("status", []) do |line|
if line =~ %r{^([ACDIMRX\?!~ ])[CM ][L ][\+ ][SX ][KOTB ]..(.+)$}
action, path = $1, $2
if action == "?" and Util.is_path_ignored?(path, config)
# Path is ignored
else
puts line
end
end
end
0
end
end
end

View File

@ -17,6 +17,10 @@ Commands:
checkout/co check out Subversion URL
EOS
def initialize
@application = Application.new
end
def run(params)
options = {
version: {},
@ -61,13 +65,13 @@ EOS
return 1
end
url, = args
Svi.checkout(url)
@application.checkout(url)
end
def cmd_status(params)
options = {}
opts, args = Yawpa.parse(params, options, posix_order: true)
Svi.status
@application.status
end
end