assaf-buildr 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. data/CHANGELOG +887 -0
  2. data/DISCLAIMER +7 -0
  3. data/LICENSE +176 -0
  4. data/NOTICE +26 -0
  5. data/README.rdoc +146 -0
  6. data/Rakefile +62 -0
  7. data/_buildr +38 -0
  8. data/addon/buildr/antlr.rb +65 -0
  9. data/addon/buildr/cobertura.rb +236 -0
  10. data/addon/buildr/emma.rb +238 -0
  11. data/addon/buildr/hibernate.rb +142 -0
  12. data/addon/buildr/javacc.rb +85 -0
  13. data/addon/buildr/jdepend.rb +60 -0
  14. data/addon/buildr/jetty.rb +248 -0
  15. data/addon/buildr/jibx.rb +86 -0
  16. data/addon/buildr/nailgun.rb +817 -0
  17. data/addon/buildr/openjpa.rb +90 -0
  18. data/addon/buildr/org/apache/buildr/BuildrNail$Main.class +0 -0
  19. data/addon/buildr/org/apache/buildr/BuildrNail.class +0 -0
  20. data/addon/buildr/org/apache/buildr/BuildrNail.java +41 -0
  21. data/addon/buildr/org/apache/buildr/JettyWrapper$1.class +0 -0
  22. data/addon/buildr/org/apache/buildr/JettyWrapper$BuildrHandler.class +0 -0
  23. data/addon/buildr/org/apache/buildr/JettyWrapper.class +0 -0
  24. data/addon/buildr/org/apache/buildr/JettyWrapper.java +144 -0
  25. data/addon/buildr/xmlbeans.rb +93 -0
  26. data/bin/buildr +28 -0
  27. data/buildr.buildfile +53 -0
  28. data/buildr.gemspec +58 -0
  29. data/doc/css/default.css +228 -0
  30. data/doc/css/print.css +100 -0
  31. data/doc/css/syntax.css +52 -0
  32. data/doc/images/apache-incubator-logo.png +0 -0
  33. data/doc/images/buildr-hires.png +0 -0
  34. data/doc/images/buildr.png +0 -0
  35. data/doc/images/favicon.png +0 -0
  36. data/doc/images/growl-icon.tiff +0 -0
  37. data/doc/images/note.png +0 -0
  38. data/doc/images/project-structure.png +0 -0
  39. data/doc/images/tip.png +0 -0
  40. data/doc/images/zbuildr.tif +0 -0
  41. data/doc/pages/artifacts.textile +207 -0
  42. data/doc/pages/building.textile +240 -0
  43. data/doc/pages/contributing.textile +208 -0
  44. data/doc/pages/download.textile +62 -0
  45. data/doc/pages/extending.textile +175 -0
  46. data/doc/pages/getting_started.textile +273 -0
  47. data/doc/pages/index.textile +42 -0
  48. data/doc/pages/languages.textile +407 -0
  49. data/doc/pages/mailing_lists.textile +17 -0
  50. data/doc/pages/more_stuff.textile +286 -0
  51. data/doc/pages/packaging.textile +427 -0
  52. data/doc/pages/projects.textile +274 -0
  53. data/doc/pages/recipes.textile +103 -0
  54. data/doc/pages/settings_profiles.textile +274 -0
  55. data/doc/pages/testing.textile +212 -0
  56. data/doc/pages/troubleshooting.textile +103 -0
  57. data/doc/pages/whats_new.textile +323 -0
  58. data/doc/print.haml +51 -0
  59. data/doc/print.toc.yaml +29 -0
  60. data/doc/scripts/buildr-git.rb +412 -0
  61. data/doc/scripts/install-jruby.sh +44 -0
  62. data/doc/scripts/install-linux.sh +64 -0
  63. data/doc/scripts/install-osx.sh +52 -0
  64. data/doc/site.haml +56 -0
  65. data/doc/site.toc.yaml +47 -0
  66. data/etc/KEYS +151 -0
  67. data/etc/git-svn-authors +16 -0
  68. data/lib/buildr.rb +35 -0
  69. data/lib/buildr/core.rb +27 -0
  70. data/lib/buildr/core/application.rb +489 -0
  71. data/lib/buildr/core/application_cli.rb +139 -0
  72. data/lib/buildr/core/build.rb +311 -0
  73. data/lib/buildr/core/checks.rb +382 -0
  74. data/lib/buildr/core/common.rb +154 -0
  75. data/lib/buildr/core/compile.rb +596 -0
  76. data/lib/buildr/core/environment.rb +120 -0
  77. data/lib/buildr/core/filter.rb +362 -0
  78. data/lib/buildr/core/generate.rb +195 -0
  79. data/lib/buildr/core/help.rb +118 -0
  80. data/lib/buildr/core/progressbar.rb +156 -0
  81. data/lib/buildr/core/project.rb +892 -0
  82. data/lib/buildr/core/test.rb +715 -0
  83. data/lib/buildr/core/transports.rb +558 -0
  84. data/lib/buildr/core/util.rb +289 -0
  85. data/lib/buildr/groovy.rb +18 -0
  86. data/lib/buildr/groovy/bdd.rb +105 -0
  87. data/lib/buildr/groovy/compiler.rb +138 -0
  88. data/lib/buildr/ide.rb +19 -0
  89. data/lib/buildr/ide/eclipse.rb +212 -0
  90. data/lib/buildr/ide/idea.ipr.template +300 -0
  91. data/lib/buildr/ide/idea.rb +189 -0
  92. data/lib/buildr/ide/idea7x.ipr.template +290 -0
  93. data/lib/buildr/ide/idea7x.rb +210 -0
  94. data/lib/buildr/java.rb +23 -0
  95. data/lib/buildr/java/ant.rb +92 -0
  96. data/lib/buildr/java/bdd.rb +449 -0
  97. data/lib/buildr/java/commands.rb +211 -0
  98. data/lib/buildr/java/compiler.rb +348 -0
  99. data/lib/buildr/java/deprecated.rb +141 -0
  100. data/lib/buildr/java/jruby.rb +117 -0
  101. data/lib/buildr/java/jtestr_runner.rb.erb +116 -0
  102. data/lib/buildr/java/org/apache/buildr/JavaTestFilter.class +0 -0
  103. data/lib/buildr/java/org/apache/buildr/JavaTestFilter.java +119 -0
  104. data/lib/buildr/java/packaging.rb +713 -0
  105. data/lib/buildr/java/pom.rb +178 -0
  106. data/lib/buildr/java/rjb.rb +155 -0
  107. data/lib/buildr/java/test_result.rb +308 -0
  108. data/lib/buildr/java/tests.rb +324 -0
  109. data/lib/buildr/java/version_requirement.rb +172 -0
  110. data/lib/buildr/packaging.rb +21 -0
  111. data/lib/buildr/packaging/artifact.rb +730 -0
  112. data/lib/buildr/packaging/artifact_namespace.rb +972 -0
  113. data/lib/buildr/packaging/artifact_search.rb +140 -0
  114. data/lib/buildr/packaging/gems.rb +102 -0
  115. data/lib/buildr/packaging/package.rb +233 -0
  116. data/lib/buildr/packaging/tar.rb +104 -0
  117. data/lib/buildr/packaging/zip.rb +722 -0
  118. data/lib/buildr/resources/buildr.icns +0 -0
  119. data/lib/buildr/scala.rb +19 -0
  120. data/lib/buildr/scala/compiler.rb +109 -0
  121. data/lib/buildr/scala/tests.rb +203 -0
  122. data/rakelib/apache.rake +191 -0
  123. data/rakelib/changelog.rake +57 -0
  124. data/rakelib/doc.rake +103 -0
  125. data/rakelib/package.rake +73 -0
  126. data/rakelib/release.rake +65 -0
  127. data/rakelib/rspec.rake +83 -0
  128. data/rakelib/rubyforge.rake +53 -0
  129. data/rakelib/scm.rake +49 -0
  130. data/rakelib/setup.rake +86 -0
  131. data/rakelib/stage.rake +48 -0
  132. data/spec/addon/cobertura_spec.rb +77 -0
  133. data/spec/addon/emma_spec.rb +120 -0
  134. data/spec/addon/test_coverage_spec.rb +255 -0
  135. data/spec/core/application_spec.rb +412 -0
  136. data/spec/core/artifact_namespace_spec.rb +646 -0
  137. data/spec/core/build_spec.rb +415 -0
  138. data/spec/core/checks_spec.rb +537 -0
  139. data/spec/core/common_spec.rb +664 -0
  140. data/spec/core/compile_spec.rb +566 -0
  141. data/spec/core/generate_spec.rb +33 -0
  142. data/spec/core/project_spec.rb +754 -0
  143. data/spec/core/test_spec.rb +1091 -0
  144. data/spec/core/transport_spec.rb +500 -0
  145. data/spec/groovy/bdd_spec.rb +80 -0
  146. data/spec/groovy/compiler_spec.rb +239 -0
  147. data/spec/ide/eclipse_spec.rb +243 -0
  148. data/spec/java/ant.rb +28 -0
  149. data/spec/java/bdd_spec.rb +358 -0
  150. data/spec/java/compiler_spec.rb +446 -0
  151. data/spec/java/java_spec.rb +88 -0
  152. data/spec/java/packaging_spec.rb +1103 -0
  153. data/spec/java/tests_spec.rb +466 -0
  154. data/spec/packaging/archive_spec.rb +503 -0
  155. data/spec/packaging/artifact_spec.rb +754 -0
  156. data/spec/packaging/packaging_helper.rb +63 -0
  157. data/spec/packaging/packaging_spec.rb +589 -0
  158. data/spec/sandbox.rb +137 -0
  159. data/spec/scala/compiler_spec.rb +228 -0
  160. data/spec/scala/tests_spec.rb +215 -0
  161. data/spec/spec_helpers.rb +304 -0
  162. data/spec/version_requirement_spec.rb +123 -0
  163. metadata +369 -0
@@ -0,0 +1,195 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+
17
+ require 'buildr/java/pom'
18
+
19
+
20
+ module Buildr
21
+ module Generate #:nodoc:
22
+
23
+ task "generate" do
24
+ script = nil
25
+ choose do |menu|
26
+ menu.header = "To use Buildr you need a buildfile. Do you want me to create one?"
27
+
28
+ menu.choice("From maven2 pom file") { script = Generate.from_maven2_pom(true).join("\n") } if File.exists?("pom.xml")
29
+ menu.choice("From directory structure") { script = Generate.from_directory(true).join("\n") }
30
+ menu.choice("Skip") { }
31
+ end
32
+
33
+ if script
34
+ buildfile = File.expand_path(Buildr::Application::DEFAULT_BUILDFILES.first)
35
+ File.open(buildfile, "w") { |file| file.write script }
36
+ puts "Created #{buildfile}"
37
+ end
38
+ end
39
+
40
+ class << self
41
+
42
+
43
+ HEADER = "# Generated by Buildr #{Buildr::VERSION}, change to your liking\n\n"
44
+
45
+
46
+ def from_directory(root = false)
47
+ name = File.basename(Dir.pwd)
48
+ if root
49
+ script = HEADER.split("\n")
50
+ header = <<-EOF
51
+ # Version number for this release
52
+ VERSION_NUMBER = "1.0.0"
53
+ # Group identifier for your projects
54
+ GROUP = "#{name}"
55
+ COPYRIGHT = ""
56
+
57
+ # Specify Maven 2.0 remote repositories here, like this:
58
+ repositories.remote << "http://www.ibiblio.org/maven2/"
59
+
60
+ desc "The #{name.capitalize} project"
61
+ define "#{name}" do
62
+
63
+ project.version = VERSION_NUMBER
64
+ project.group = GROUP
65
+ manifest["Implementation-Vendor"] = COPYRIGHT
66
+ EOF
67
+ script += header.split("\n")
68
+ else
69
+ script = [ %{define "#{name}" do} ]
70
+ end
71
+ script << " compile.with # Add classpath dependencies" if File.exist?("src/main/java")
72
+ script << " resources" if File.exist?("src/main/resources")
73
+ script << " test.compile.with # Add classpath dependencies" if File.exist?("src/test/java")
74
+ script << " test.resources" if File.exist?("src/test/resources")
75
+ if File.exist?("src/main/webapp")
76
+ script << " package(:war)"
77
+ elsif File.exist?("src/main/java")
78
+ script << " package(:jar)"
79
+ end
80
+ dirs = FileList["*"].exclude("src", "target", "report").
81
+ select { |file| File.directory?(file) && File.exist?(File.join(file, "src")) }
82
+ unless dirs.empty?
83
+ script << ""
84
+ dirs.sort.each do |dir|
85
+ Dir.chdir(dir) { script << from_directory.flatten.map { |line| " " + line } << "" }
86
+ end
87
+ end
88
+ script << "end"
89
+ script.flatten
90
+ end
91
+
92
+ def from_maven2_pom(root = false)
93
+ pom = Buildr::POM.load('pom.xml')
94
+ project = pom.project
95
+
96
+ artifactId = project['artifactId'].first
97
+ description = project['name'] || "The #{artifactId} project"
98
+ project_name = File.basename(Dir.pwd)
99
+
100
+ if root
101
+ script = HEADER.split("\n")
102
+
103
+ settings_file = ENV["M2_SETTINGS"] || File.join(ENV['HOME'], ".m2/settings.xml")
104
+ settings = XmlSimple.xml_in(IO.read(settings_file)) if File.exists?(settings_file)
105
+
106
+ if settings
107
+ proxy = settings['proxies'].first['proxy'].find { |proxy|
108
+ proxy["active"].nil? || proxy["active"].to_s =~ /true/
109
+ } rescue nil
110
+
111
+ if proxy
112
+ url = %{#{proxy["protocol"].first}://#{proxy["host"].first}:#{proxy["port"].first}}
113
+ exclude = proxy["nonProxyHosts"].to_s.gsub("|", ",") if proxy["nonProxyHosts"]
114
+ script << "options.proxy.http = '#{url}'"
115
+ script << "options.proxy.exclude << '#{exclude}'" if exclude
116
+ script << ''
117
+ # In addition, we need to use said proxies to download artifacts.
118
+ Buildr.options.proxy.http = url
119
+ Buildr.options.proxy.exclude << exclude if exclude
120
+ end
121
+ end
122
+
123
+ repositories = project["repositories"].first["repository"].select { |repository|
124
+ legacy = repository["layout"].to_s =~ /legacy/
125
+ !legacy
126
+ } rescue nil
127
+ repositories = [{"name" => "Standard maven2 repository", "url" => "http://www.ibiblio.org/maven2/"}] if repositories.nil? || repositories.empty?
128
+ repositories.each do |repository|
129
+ name, url = repository["name"], repository["url"]
130
+ script << "# #{name}"
131
+ script << "repositories.remote << '#{url}'"
132
+ # In addition we need to use said repositores to download artifacts.
133
+ Buildr.repositories.remote << url.to_s
134
+ end
135
+ script << ""
136
+ else
137
+ script = []
138
+ end
139
+
140
+ script << "desc '#{description}'"
141
+ script << "define '#{project_name}' do"
142
+
143
+ groupId = project['groupId']
144
+ script << " project.group = '#{groupId}'" if groupId
145
+
146
+ version = project['version']
147
+ script << " project.version = '#{version}'" if version
148
+
149
+ #get plugins configurations
150
+ plugins = project['build'].first['plugins'].first['plugin'] rescue {}
151
+ if plugins
152
+ compile_plugin = plugins.find{|pl| (pl['groupId'].nil? or pl['groupId'].first == 'org.apache.maven.plugins') and pl['artifactId'].first == 'maven-compiler-plugin'}
153
+ if compile_plugin
154
+ source = compile_plugin.first['configuration'].first['source'] rescue nil
155
+ target = compile_plugin.first['configuration'].first['target'] rescue nil
156
+
157
+ script << " compile.options.source = '#{source}'" if source
158
+ script << " compile.options.target = '#{target}'" if target
159
+ end
160
+ end
161
+
162
+ compile_dependencies = pom.dependencies
163
+ dependencies = compile_dependencies.sort.map{|d| "'#{d}'"}.join(', ')
164
+ script << " compile.with #{dependencies}" unless dependencies.empty?
165
+
166
+ test_dependencies = (pom.dependencies(['test']) - compile_dependencies).reject{|d| d =~ /^junit:junit:jar:/ }
167
+ #check if we have testng
168
+ use_testng = test_dependencies.find{|d| d =~ /^org.testng:testng:jar:/}
169
+ if use_testng
170
+ script << " test.using :testng"
171
+ test_dependencies = pom.dependencies(['test']).reject{|d| d =~ /^org.testng:testng:jar:/ }
172
+ end
173
+
174
+ test_dependencies = test_dependencies.sort.map{|d| "'#{d}'"}.join(', ')
175
+ script << " test.with #{test_dependencies}" unless test_dependencies.empty?
176
+
177
+ packaging = project['packaging'] ? project['packaging'].first : 'jar'
178
+ if %w(jar war).include?(packaging)
179
+ script << " package :#{packaging}, :id => '#{artifactId}'"
180
+ end
181
+
182
+ modules = project['modules'].first['module'] rescue nil
183
+ if modules
184
+ script << ""
185
+ modules.each do |mod|
186
+ chdir(mod) { script << from_maven2_pom.flatten.map { |line| " " + line } << "" }
187
+ end
188
+ end
189
+ script << "end"
190
+ script.flatten
191
+ end
192
+
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,118 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+
17
+ require 'buildr/core/common'
18
+ require 'buildr/core/project'
19
+
20
+
21
+ module Buildr
22
+
23
+ module Help #:nodoc:
24
+ class << self
25
+
26
+ def <<(arg)
27
+ if arg.respond_to?(:call)
28
+ texters << arg
29
+ else
30
+ texters << lambda { arg }
31
+ end
32
+ end
33
+
34
+ def to_s
35
+ texters.map(&:call).join("\n")
36
+ end
37
+
38
+ protected
39
+ def texters
40
+ @texters ||= []
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ class << self
47
+ def help(&block)
48
+ Help << block if block_given?
49
+ Help
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+
56
+ task 'help' do
57
+ # Greeater.
58
+ Buildr.application.usage
59
+ puts
60
+
61
+ # Show only the top-level projects.
62
+ projects.reject(&:parent).tap do |top_level|
63
+ unless top_level.empty?
64
+ puts 'Top-level projects (buildr help:projects for full list):'
65
+ width = [top_level.map(&:name).map(&:size), 20].flatten.max
66
+ top_level.each do |project|
67
+ puts project.comment.to_s.empty? ? project.name : (" %-#{width}s # %s" % [project.name, project.comment])
68
+ end
69
+ puts
70
+ end
71
+ end
72
+
73
+ # Show all the top-level tasks, excluding projects.
74
+ puts 'Common tasks:'
75
+ task('help:tasks').invoke
76
+ puts
77
+ puts 'For help on command line options:'
78
+ puts ' buildr --help'
79
+ puts Buildr.help.to_s
80
+ end
81
+
82
+
83
+ module Buildr
84
+
85
+ # :call-seq:
86
+ # help() { ... }
87
+ #
88
+ # Use this to enhance the help task, e.g. to print some important information about your build,
89
+ # configuration options, build instructions, etc.
90
+ def help(&block)
91
+ Buildr.help << block
92
+ end
93
+
94
+ end
95
+
96
+
97
+ namespace 'help' do
98
+
99
+ desc 'List all projects defined by this buildfile'
100
+ task 'projects' do
101
+ width = projects.map(&:name).map(&:size).max
102
+ projects.each do |project|
103
+ puts project.comment.to_s.empty? ? " #{project.name}" : (" %-#{width}s # %s" % [project.name, project.comment])
104
+ end
105
+ end
106
+
107
+ desc 'List all tasks available from this buildfile'
108
+ task 'tasks' do
109
+ Buildr.application.tasks.select(&:comment).reject { |task| Project === task }.tap do |tasks|
110
+ width = [tasks.map(&:name).map(&:size), 20].flatten.max
111
+ tasks.each do |task|
112
+ printf " %-#{width}s # %s\n", task.name, task.comment
113
+ end
114
+ puts
115
+ end
116
+ end
117
+
118
+ end
@@ -0,0 +1,156 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+
17
+ class ProgressBar
18
+
19
+ class << self
20
+
21
+ def start(args, &block)
22
+ new(args).start &block
23
+ end
24
+
25
+ def width
26
+ @width ||= $terminal.output_cols || 80
27
+ end
28
+
29
+ end
30
+
31
+ def initialize(args = {})
32
+ @title = args[:title] || ''
33
+ @total = args[:total] || 0
34
+ @mark = args[:mark] || '.'
35
+ @format = args[:format] || default_format
36
+ @output = args[:output] || $stderr unless args[:hidden] || !$stdout.isatty
37
+ clear
38
+ end
39
+
40
+ def start
41
+ @start = @last_time = Time.now
42
+ @count = 0
43
+ @finished = false
44
+ render
45
+ if block_given?
46
+ result = yield(self) if block_given?
47
+ finish
48
+ result
49
+ else
50
+ self
51
+ end
52
+ end
53
+
54
+ def inc(count)
55
+ set @count + count
56
+ end
57
+
58
+ def <<(bytes)
59
+ inc bytes.size
60
+ end
61
+
62
+ def set(count)
63
+ @count = [count, 0].max
64
+ @count = [count, @total].min unless @total == 0
65
+ render if changed?
66
+ end
67
+
68
+ def title
69
+ @title.size > ProgressBar.width / 5 ? (@title[0, ProgressBar.width / 5 - 2] + '..') : @title
70
+ end
71
+
72
+ def count
73
+ human(@count)
74
+ end
75
+
76
+ def total
77
+ human(@total)
78
+ end
79
+
80
+ def percentage
81
+ '%3d%%' % (@total == 0 ? 100 : (@count * 100 / @total))
82
+ end
83
+
84
+ def time
85
+ @finished ? elapsed : eta
86
+ end
87
+
88
+ def eta
89
+ return 'ETA: --:--:--' if @count == 0
90
+ elapsed = Time.now - @start
91
+ eta = elapsed * @total / @count - elapsed
92
+ 'ETA: %s' % duration(eta.ceil)
93
+ end
94
+
95
+ def elapsed
96
+ 'Time: %s' % duration(Time.now - @start)
97
+ end
98
+
99
+ def rate
100
+ '%s/s' % human(@count / (Time.now - @start))
101
+ end
102
+
103
+ def progress(width)
104
+ width -= 2
105
+ marks = @total == 0 ? width : (@count * width / @total)
106
+ "|%-#{width}s|" % (@mark * marks)
107
+ end
108
+
109
+ def human(bytes)
110
+ magnitude = (0..3).find { |i| bytes < (1024 << i * 10) } || 3
111
+ return '%dB' % bytes if magnitude == 0
112
+ return '%.1f%s' % [ bytes.to_f / (1 << magnitude * 10), [nil, 'KB', 'MB', 'GB'][magnitude] ]
113
+ end
114
+
115
+ def duration(seconds)
116
+ '%02d:%02d:%02d' % [seconds / 3600, (seconds / 60) % 60, seconds % 60]
117
+ end
118
+
119
+ def finish
120
+ unless @finished
121
+ @finished = true
122
+ render
123
+ end
124
+ end
125
+
126
+ protected
127
+
128
+ def clear
129
+ return unless @output
130
+ @output.print "\r", " " * (ProgressBar.width - 1), "\r"
131
+ @output.flush
132
+ end
133
+
134
+ def render
135
+ return unless @output
136
+ format, *args = @format
137
+ line = format % args.map { |arg| send(arg) }
138
+ @output.print line.sub('|--|') { progress(ProgressBar.width - line.size + 3) }
139
+ @output.print @finished ? "\n" : "\r"
140
+ @output.flush
141
+ @previous = @count
142
+ @last_time = Time.now
143
+ end
144
+
145
+ def changed?
146
+ return false unless @output && Time.now - @last_time > 0.1
147
+ return human(@count) != human(@previous) if @total == 0
148
+ return true if (@count - @previous) >= @total / 100
149
+ return Time.now - @last_time > 1
150
+ end
151
+
152
+ def default_format
153
+ @total == 0 ? ['%s %8s %s', :title, :count, :elapsed] : ['%s: %s |--| %8s/%s %s', :title, :percentage, :count, :total, :time]
154
+ end
155
+
156
+ end