Class: Sub

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

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION

Constant Summary collapse

DEFAULT_BASE_URL =
"svn+ssh://rubyforge.org/var/svn"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {:verbosity => NORMAL}, args = []) ⇒ Sub

Returns a new instance of Sub.



48
49
50
51
52
53
54
55
# File 'lib/sub/app.rb', line 48

def initialize(options = {:verbosity => NORMAL}, args = [])
  options = defaults.merge(options)
  @verbosity = options[:verbosity]
  @clean = options[:clean]
  @command = options[:command]
  @url = options[:url] || ENV['SUB_BASE_URL'] || ENV['SVN'] || DEFAULT_BASE_URL
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



45
46
47
# File 'lib/sub/app.rb', line 45

def args
  @args
end

#cleanObject (readonly)

Returns the value of attribute clean.



45
46
47
# File 'lib/sub/app.rb', line 45

def clean
  @clean
end

#commandObject (readonly)

Returns the value of attribute command.



45
46
47
# File 'lib/sub/app.rb', line 45

def command
  @command
end

#urlObject (readonly)

Returns the value of attribute url.



45
46
47
# File 'lib/sub/app.rb', line 45

def url
  @url
end

#verbosityObject

Returns the value of attribute verbosity.



46
47
48
# File 'lib/sub/app.rb', line 46

def verbosity
  @verbosity
end

Class Method Details

.from_args(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sub/app.rb', line 12

def self.from_args(args)
  verbosity = NORMAL
  still_parsing_options = true
  command = :up
  options = {}
  while still_parsing_options
    case args[0]
    when 'up', 'co', 'help'
      options[:command] = args[0]
    when '-v', '--verbose'
      options[:verbosity] = VERBOSE
    when '-q', '--quiet'
      options[:verbosity] = QUIET
    when '-h', '--help'
      options[:command] = 'help'
      still_parsing_options = false
    when '-c', '--clean'
      options[:clean] = true
    when '--url'
      args.shift
      options[:url] = args[0]
    when '--version'
      puts "Sub version #{Sub::VERSION::STRING}"
      exit 0
    else
      still_parsing_options = false
    end
    args.shift if still_parsing_options
  end

  Sub.new(options, args)
end

Instance Method Details

#coObject



76
77
78
79
80
81
82
83
84
# File 'lib/sub/app.rb', line 76

def co
  if @args.empty?
    raise "Please specify a project to check out"
  end

  project = args.shift
  dir_name = args.shift || project
  svn("co #{url}/#{project}/trunk #{dir_name}")
end

#defaultsObject



57
58
59
60
61
62
# File 'lib/sub/app.rb', line 57

def defaults
  {
    :verbosity => NORMAL,
    :command => :up,
  }
end

#executeObject



64
65
66
# File 'lib/sub/app.rb', line 64

def execute
  self.send(@command)
end

#externals(root) ⇒ Object



131
132
133
# File 'lib/sub/app.rb', line 131

def externals(root)
  Root.new(root, self, clean).externals
end

#helpObject



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
112
113
114
115
# File 'lib/sub/app.rb', line 86

def help
  puts """
sub - Alex's wrapper for subversion

Version: #{Sub::VERSION::STRING}

Usage:
sub co project_name [dir_name]
  checks out [base_url]/project_name/trunk into ./project_name (or dir_name if specified)
sub up [dir]*
  fast update (default command, so 'sub dir...' or just 'sub' work too)
sub help
  prints this message

Options:
--verbose, -v
    lots of output
--quiet, -q
    no output at all except for errors
--help, -h
    prints this message
--clean, -c
    'up' command removes all unversioned files and directories
--url [base_url]
    sets base repository url for 'co' command
    (default is ENV['SUB_BASE_URL'] or ENV['SVN'] or #{DEFAULT_BASE_URL})
--version
    prints release version
"""
end

#parse_externals(st) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sub/app.rb', line 135

def parse_externals(st)
  exts = []
  st.split("\n").select do |line|
    line =~ /^X/
  end.collect do |line|
    line.gsub(/^X */, '').gsub(/\/[^\/]*$/, '')
  end.uniq.collect do |parent|
    prop = `svn propget svn:externals #{parent}`
    prop.split("\n").each do |external|
      next if external.strip.empty?
      exts << External.new(parent, external, self)
    end
  end
  exts
end

#run(cmd, return_output = false) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/sub/app.rb', line 161

def run(cmd, return_output = false)
  say("\t#{cmd}") if verbosity == VERBOSE
  if (return_output)
    `#{cmd}`
  else
    cmd += ">/dev/null"if verbosity == QUIET
    system(cmd)
  end
end

#say(msg) ⇒ Object



151
152
153
# File 'lib/sub/app.rb', line 151

def say(msg)
  puts msg if verbosity > QUIET
end

#svn(cmd) ⇒ Object



155
156
157
158
159
# File 'lib/sub/app.rb', line 155

def svn(cmd)
  svncmd = "svn"
  svncmd += " --quiet" if (cmd =~ /^(up|co)/ && verbosity == QUIET)
  run("#{svncmd} #{cmd}")
end

#upObject

commands



69
70
71
72
73
74
# File 'lib/sub/app.rb', line 69

def up
  if @args.empty?
    @args = [`pwd`.chomp]
  end
  update_many(@args)
end

#update(root) ⇒ Object



127
128
129
# File 'lib/sub/app.rb', line 127

def update(root)
  Root.new(root, self, clean).update
end

#update_many(roots) ⇒ Object

methods



119
120
121
122
123
124
125
# File 'lib/sub/app.rb', line 119

def update_many(roots)
  roots.each do |root|
    say "Updating #{root}"
    b = Benchmark.measure { update(root) }
    say "Updated %s in %.2f sec" % [root, b.real]
  end
end