Class: Sub

Inherits:
Object
  • Object
show all
Includes:
Sys::Client
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

Methods included from Sys::Client

#run, #say, #svn, #sys, #verbosity, #verbosity=

Constructor Details

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

Returns a new instance of Sub.



59
60
61
62
63
64
65
66
67
68
# File 'lib/sub/app.rb', line 59

def initialize(options = {:verbosity => NORMAL}, args = [])
  options = Sub.defaults.merge(options)
  sys.verbosity = options[:verbosity]
  @clean = options[:clean]
  @command = options[:command]
  @url = options[:url]
  @revision = options[:revision]
  @dir = options[:dir] || '.'
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#cleanObject (readonly)

Returns the value of attribute clean.



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

def clean
  @clean
end

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#dirObject (readonly)

Returns the value of attribute dir.



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

def dir
  @dir
end

#revisionObject (readonly)

Returns the value of attribute revision.



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

def revision
  @revision
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.defaultsObject



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

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

.from_args(args) ⇒ Object



11
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
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sub/app.rb', line 11

def self.from_args(args)
  still_parsing_options = true
  command = defaults[:command]
  options = {}
  real_args = []
  while !args.empty?
    case args[0]

    # commands
    when 'up', 'co', 'help', 'ext', 'ignore'
      options[:command] = args[0]

    # no-param options
    when '--version'
      puts "Sub version #{Sub::VERSION::STRING}"
      exit 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

    # one-param options
    when '--url'
      args.shift
      options[:url] = args[0]
    when '--revision', '-r'
      args.shift
      options[:revision] = args[0]
    when '--directory', '--dir', '-d'
      args.shift
      options[:dir] = args[0]

    else
      real_args << args[0]
    end
    args.shift
  end

  Sub.new(options, real_args)
end

Instance Method Details

#all_externals(root) ⇒ Object



196
197
198
# File 'lib/sub/app.rb', line 196

def all_externals(root)
  Root.new(root, clean).all_externals
end

#baseObject



102
103
104
# File 'lib/sub/app.rb', line 102

def base
  url || ENV['SUB_BASE_URL'] || ENV['SVN'] || DEFAULT_BASE_URL    
end

#coObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/sub/app.rb', line 91

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

  project = args.shift
  dir_name = args.shift || project
  svn("co #{base}/#{project}/trunk #{dir_name}")
  verbosity == QUIET ? nil : "Checkout complete."
end

#executeObject



77
78
79
80
# File 'lib/sub/app.rb', line 77

def execute
  result = self.send(@command)
  puts result unless result.nil?
end

#extObject

todo: test



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sub/app.rb', line 107

def ext
  external_name = args.shift
  if external_name.nil?
    External.externals_of(dir).join("\n")
  else
    old_external = get_external(dir, external_name)
    if old_external.nil?
      "No external found named #{external_name} in directory #{dir}" + 
        ((dir == '.') ? "\nDid you mean to use the --dir parameter?" : "")
    elsif url || revision
      revision_to_set = revision == 'HEAD' ? nil : revision
      set_external(dir, external_name, url || old_external.url, revision_to_set)
    else
      old_external
    end
      
  end
end

#get_external(dir, external_name) ⇒ Object



200
201
202
203
204
# File 'lib/sub/app.rb', line 200

def get_external(dir, external_name)
  External.externals_of(dir).select do |external|
    external.name == external_name
  end.first
end

#helpObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
# File 'lib/sub/app.rb', line 137

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

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

Usage:

sub co [--url base_url] project [dir]
  checks out [base_url]/project/trunk into ./project (or dir if specified)
  default base_url is ENV['SUB_BASE_URL'] or ENV['SVN']
    or #{DEFAULT_BASE_URL}

sub up [--clean] [dir]*
  fast update (default command, so 'sub dir...' or just 'sub' work too)
  if --clean is specified, removes all unversioned files and directories

sub help
  prints this message

sub ext [--dir directory] [external_name [--revision rev] [--url url]]]
  prints the value of the named svn:external property for directory dir
  if no dir is specified, uses '.'
  if no external_name is specified, prints all externals in that directory
  if --url or --revision is specified, sets the value and commits the change
  if rev is HEAD then the revision is removed from that external

sub ignore [--dir directory] [pattern]
  prints the value of the svn:ignore property for directory dir
  if pattern is specified, ignores that pattern
  if no dir is specified, uses '.'

Options:
--verbose, -v
    lots of output
--quiet, -q
    no output at all except for errors
--help, -h
    prints this message
--version
    prints release version
"""
end

#ignoreObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/sub/app.rb', line 126

def ignore
  now_ignoring = svn("propget svn:ignore #{dir}", true).strip
  if args.empty?
    now_ignoring
  else
    to_ignore = now_ignoring.split("\n") + args
    svn("propset svn:ignore '#{to_ignore.join("\n")}' #{dir}")
    "#{dir} now ignoring #{args.join(", ")}"
  end
end

#remove_external(dir, name) ⇒ Object



221
222
223
224
225
# File 'lib/sub/app.rb', line 221

def remove_external(dir, name)
  set_externals(dir, External.externals_of(dir).select { |x| x.name != name })
  svn("ci --non-recursive -m 'removing external #{name}' #{dir}")
  FileUtils.rm_rf("#{dir}/#{name}") # TODO: unless modified
end

#set_external(dir, external_name, url, revision = nil) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/sub/app.rb', line 206

def set_external(dir, external_name, url, revision = nil)
  if get_external(dir, external_name)
    remove_external(dir, external_name)
  end
  new_external = External.new(dir, external_name, url, revision)
  externals = External.externals_of(dir) << new_external
  set_externals(dir, externals)
  new_external
end

#set_externals(externals_path, externals) ⇒ Object



216
217
218
219
# File 'lib/sub/app.rb', line 216

def set_externals(externals_path, externals)
  property_lines = externals.collect { |x| x.to_s }
  svn "propset svn:externals '#{property_lines.join("\n")}' #{externals_path}"
end

#upObject

commands



83
84
85
86
87
88
89
# File 'lib/sub/app.rb', line 83

def up
  if @args.empty?
    @args = [`pwd`.chomp]
  end
  update_many(@args)
  verbosity == QUIET ? nil : "Update complete."
end

#update(root) ⇒ Object



192
193
194
# File 'lib/sub/app.rb', line 192

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

#update_many(roots) ⇒ Object

methods



184
185
186
187
188
189
190
# File 'lib/sub/app.rb', line 184

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