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



189
190
191
# File 'lib/sub/app.rb', line 189

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

#baseObject



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

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

#coObject



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

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}")
  "Checkout complete."
end

#executeObject



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

def execute
  puts self.send(@command)
end

#extObject

todo: test



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

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 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



193
194
195
196
197
# File 'lib/sub/app.rb', line 193

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

#helpObject



130
131
132
133
134
135
136
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
# File 'lib/sub/app.rb', line 130

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



121
122
123
124
125
126
127
128
# File 'lib/sub/app.rb', line 121

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

#remove_external(dir, name) ⇒ Object



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

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



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

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



209
210
211
212
# File 'lib/sub/app.rb', line 209

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



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

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

#update(root) ⇒ Object



185
186
187
# File 'lib/sub/app.rb', line 185

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

#update_many(roots) ⇒ Object

methods



177
178
179
180
181
182
183
# File 'lib/sub/app.rb', line 177

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