sprout 0.7.153-darwin

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sprout might be problematic. Click here for more details.

data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Pattern Park
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/TODO ADDED
@@ -0,0 +1,12 @@
1
+
2
+ ---------------------------------
3
+ In the interest of being a better member of our growing community,
4
+ I have extracted the contents of this file to the googlecode site at:
5
+
6
+ http://code.google.com/p/projectsprouts/issues/list?q=label:Beta1.0&can=2
7
+
8
+ Please feel free to check over the available issues and see if any look appealing!
9
+
10
+ If you're considering a contribution, please check in on our mailing list first just to be sure.
11
+ (projectsprouts@googlegroups.com)
12
+
data/bin/sprout ADDED
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Luke Bayes on 2007-4-29.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ begin
7
+ require 'rubygems'
8
+ rescue LoadError
9
+ # no rubygems to load, so we fail silently
10
+ end
11
+
12
+ $:.push(File.dirname(__FILE__) + '/../lib')
13
+ require 'sprout'
14
+ require 'sprout/generator'
15
+ require 'optparse'
16
+
17
+ OPTIONS = {
18
+ :sources => [],
19
+ :project_name => nil,
20
+ :sprout_name => nil,
21
+ :requirements => nil,
22
+ :remove_all => false
23
+ }
24
+
25
+ def fail_with(opts, message)
26
+ puts "[ERROR] #{message}"
27
+ puts ""
28
+ puts opts
29
+ exit
30
+ end
31
+
32
+ parser = OptionParser.new do |opts|
33
+ opts.banner = <<BANNER
34
+ Sprout is an open-source, cross-platform project generation and configuration tool
35
+
36
+ Usage: #{File.basename($0)} [options] [ProjectName]
37
+
38
+ Examples:
39
+
40
+ Create a new ActionScript 3.0 project named 'SomeProject':
41
+ #{File.basename($0)} -n as3 SomeProject
42
+
43
+ Create a new ActionScript 2.0 project named 'OtherProject':
44
+ #{File.basename($0)} -n as2 OtherProject
45
+
46
+ Remove all Sprout gems and cached files:
47
+ #{File.basename($0)} -R
48
+
49
+ Options are:
50
+ BANNER
51
+ opts.separator ""
52
+
53
+ opts.on("-n", "--name NAME", String, "The name of the sprout bundle whose project generator will be executed (e.g., as2 or as3)") do |name|
54
+ OPTIONS[:sprout_name] = name
55
+ end
56
+
57
+ opts.on('--source [URL]', String, "Use URL as the remote source for gems") do |source|
58
+ OPTIONS[:sources] << source
59
+ end
60
+
61
+ opts.on('-r', '--requirements REQUIREMENTS', String, "The specific gem version requirements for the named bundle (e.g., 0.3.2)") do |requirements|
62
+ OPTIONS[:requirements] = requirements
63
+ end
64
+
65
+ opts.on("-R", "--remove-all", "Remove all sprout gems and cached files") do
66
+ OPTIONS[:remove_all] = true
67
+ Sprout::Sprout.remove_all
68
+ end
69
+
70
+ opts.on("-v", "--version", "Display the installed version of the sprout gem") do |version|
71
+ puts "sprout #{Sprout::VERSION::STRING}"
72
+ exit
73
+ end
74
+
75
+ opts.on("-h", "--help", "Show this help message.") do
76
+ puts opts
77
+ exit
78
+ end
79
+
80
+ opts.parse!(ARGV)
81
+
82
+ if(OPTIONS[:sources].size > 0)
83
+ set_sources OPTIONS[:sources]
84
+ end
85
+
86
+ if(ARGV.size > 0)
87
+ if(OPTIONS[:sprout_name])
88
+ OPTIONS[:project_name] = ARGV.pop
89
+ if(OPTIONS[:project_name] == "=")
90
+ fail_with(opts, "Invalid project name #{OPTIONS[:project_name]}")
91
+ end
92
+ else
93
+ fail_with(opts, "You must provide a sprout name argument such as, '-n as2' or '-n as3'")
94
+ end
95
+ elsif(OPTIONS[:remove_all])
96
+ exit
97
+ else
98
+ fail_with(opts, "Expected a sprout bundle name and project name like: 'sprout -n as2 SomeProject'")
99
+ end
100
+
101
+ end
102
+
103
+ #############################################
104
+
105
+ sprout_name = OPTIONS[:sprout_name]
106
+ sprout_requirements = OPTIONS[:requirements]
107
+ project_name = OPTIONS[:project_name]
108
+
109
+ #############################################
110
+
111
+ puts ">> Creating new project '#{project_name}' with #{sprout_name}"
112
+ Sprout::Sprout.project_name = project_name
113
+
114
+ begin
115
+ # Fallback if a version was specified
116
+ if(sprout_requirements)
117
+ sprout(sprout_name, sprout_requirements)
118
+ end
119
+ # Try to run the generator against user home and existing gems first
120
+ Sprout::Sprout.generate(sprout_name, 'project', [project_name])
121
+ rescue Rails::Generator::GeneratorError
122
+ # Failing that, try to download a sprout gem by name
123
+ sprout(sprout_name, sprout_requirements)
124
+ # Then try again....
125
+ Sprout::Sprout.generate(sprout_name, 'project', [project_name])
126
+ end
127
+
128
+ Sprout::Log.flush
data/doc/Bundle ADDED
@@ -0,0 +1,14 @@
1
+
2
+ A Sprout Bundle is a collection of Ruby code that supports a particular interest or technology. At the time of this writing, we have two bundles available.
3
+
4
+ * ActionScript 2 Bundle (link[link:files/bundles/as2/README.html]) which supports ActionScript 2.0 development
5
+ * ActionScript 3 Bundle (link[link:files/bundles/as3/README.html]) which supports ActionScript 3.0, MXML and AIR development
6
+
7
+ Bundles are the named entry point that the +sprout+ shell tool uses to find project generators[link:files/doc/Generator.html].
8
+
9
+ Bundles should be packaged and published to the RubyForge gem repository with very specific names as follows:
10
+
11
+ sprout-#{bundle_name}-bundle where ${bundle_name} is what will be given to the -n parameter of the +sprout+ gem.
12
+
13
+ The as3 bundle is released as sprout-as3-bundle on RubyForge, but we can simply enter the short name when creating new as3 projects.
14
+
data/doc/Generator ADDED
@@ -0,0 +1,35 @@
1
+ A SproutGenerator is a set of specifically configured folders and files that have been placed in a particular, expected location on disk.
2
+ The Sprout generator feature is a minor modification to the standard Rails generators.
3
+
4
+ Sprouts modifies the underlying Rails Generator implementation mainly by changing where it looks for available, named generators. This was an importat change that will help prevent Sprout projects from clobbering generators that are intended for Rails support and vice versa. We also have a slightly different requirement in that we need support for multiple languages or technologies while Rails is able to simply expect that it's generating Ruby code.
5
+
6
+ Generators can exist in multiple different locations on disk, to learn how to create a new generator, see the Rails documentation[http://wiki.rubyonrails.org/rails/pages/UnderstandingGenerators].
7
+
8
+ To use a new or existing generator, simply enter it's name from within a project after calling
9
+ script/generate
10
+
11
+ When a string is passed to the generate command, sprouts will look in the following locations in the following order with 'name' being the generator name that you have requested:
12
+ * #{project_path}/generators/#{name}
13
+ * #{project_path}/script/generators/#{name}
14
+ * #{project_path}/vendor/generators/#{name}
15
+ * #{Sprout::Sprout.sprout_cache}/generators/#{Sprout::ProjectModel.language}/#{name}
16
+ * All Rubygems with a name ending with '-bundle' and with contents that match '/lib/sprout/**/generators/[name]'
17
+
18
+ This means that when you have a new project and enter:
19
+ script/generate foo
20
+
21
+ We will first look in your project for, 'generators/foo', 'script/generators/foo' and 'vendor/generators/foo'.
22
+
23
+ Assuming no viable generator is found in your project, we will then look in your Sprout::Sprout sprout_cache for a folder named 'generators/foo'.
24
+
25
+ Assuming no viable generator is found in your system wide path, we will begin looking inside of installed Ruby Gems. The expected gem will have a file at:
26
+
27
+ lib/sprout/**/generators/foo/foo_generator.rb
28
+
29
+ If no named generator is found in any of these places an exception will be encountered.
30
+
31
+ Sprouts generators can be initiated from one of two places, either from a project directory with script/generate or directly from the Sprout gem.
32
+
33
+ When executing generators directly from the Sprout gem, you must send in a bundle base name and know that only 'project' generators found in that bundle will be executed.
34
+
35
+ When executing generators from a project, the Sprout::ProjectModel language parameter is used to determine the bundle (if necessary), and then the Generator name is used to execute any found generator.
data/doc/Library ADDED
@@ -0,0 +1,63 @@
1
+
2
+ A Library is simply shared code. Some libraries are distributed with only source, others are only pre-compiled binaries (SWC for ActionScript libraries), and still others are made available in both forms.
3
+
4
+ The Sprout::LibraryTask will download and copy a remote library sprout gem.
5
+ The remote archive can include (or reference) either source or a pre-compiled
6
+ file. For ActionScript libraries, this would be a SWC file.
7
+
8
+ This task is integrated with some of the compiler tasks in such
9
+ a way that if an Sprout::MXMLCTask has any number of library tasks in
10
+ it's prerequisites list, each of those libraries will be added
11
+ to the compiler directive appropriately.
12
+
13
+ Following is a simple example of a library task. Using only
14
+ this simple task definition, the Adobe corelib library sprout gem
15
+ will be downloaded, installed and copied to your Sprout::ProjectModel +lib_dir+.
16
+
17
+ library :corelib
18
+
19
+ By adding this named task as a prerequisite to your compilation task,
20
+ that SWC will also be added to the Sprout::MXMLCTask +library_path+ parameter.
21
+
22
+ mxmlc 'bin/SomeProject.swf' => :corelib
23
+
24
+ You can also specify a particular library gem version if the library
25
+ has changed since your project began.
26
+
27
+ library :asunit3 do |t|
28
+ t.version = '3.0.1'
29
+ end
30
+
31
+ This will ensure that only that particular library version is used for this project.
32
+
33
+ You may want to refer to a library using a particular task name, but have it
34
+ use a different library sprout gem. This can be done using the gem_name parameter
35
+ as follows:
36
+
37
+ library :asunit do |t|
38
+ t.gem_name = 'sprout-asunit3-library'
39
+ end
40
+
41
+ This may be useful because now the AsUnit[http://www.asunit.org] sources will be installed to:
42
+ lib/asunit
43
+ instead of:
44
+ lib/asunit3
45
+ and you can now depend on this library as simply +:asunit+ in your compiler tasks.
46
+
47
+ You can easily create your own library gems using the Sprout::GemWrapTask and then
48
+ refer to them by gem name.
49
+
50
+ In order to share your library tasks, you will need to
51
+ do one of the following:
52
+
53
+ * Tell interested developers to manually install your library gem
54
+ * Upload your gem to any Rubyforge[http://www.rubyforge.org] project file releases area.
55
+ If your gem name begins with 'sprout-' and ends with '-library', you (and others) can refer to it by only
56
+ the string in between that prefix and suffix. Otherwise, you (and others) will always have
57
+ to set the gem_name parameter to the full name of your custom library.
58
+ * Submit your library for inclusion from the ProjectSprouts[http://www.projectsprouts.org] project.
59
+ * Add your gem to your own custom gem_server[http://rambleon.org/2007/04/19/creating-your-own-gem-server/], and set up your rakefiles to pull from that server
60
+
61
+ You can search for all available libraries as follows:
62
+ gem search -r sprout-*library
63
+ Only results that begin with 'sprout-' are known, valid libraries.
data/doc/Task ADDED
@@ -0,0 +1,21 @@
1
+ In Sprouts, a Task is referring to a Rake[http://rake.rubyforge.org/] Task.
2
+
3
+ Rake is the automated build to written in Ruby. This tool is similar to Ant[http://ant.apache.org/] and Make[http://www.gnu.org/software/make/] if you're familiar with those technologies.
4
+
5
+ The main thing that differentiates Rake from it's competitors is the fact that Rake tasks are defined and configured in Ruby code rather than XML or C. This lets us more easily avoid repetition throughout a rakefile, and we gain the full power of the Ruby language to apply to our build scripts.
6
+
7
+ Essentially, Rake allows us to write and maintain much smaller, more digestible build scripts.
8
+
9
+ To learn more about Rake, check out Martin Fowler's seminal article[http://martinfowler.com/articles/rake.html] on the subject.
10
+
11
+ At the time of this writing, Sprouts makes the following Rake tasks available:
12
+ * Core Sprout::SFTPTask
13
+ * Core Sprout::ZipTask
14
+ * Core Sprout::LibraryTask
15
+ * As3Bundle Sprout::AsDocTask
16
+ * As3Bundle Sprout::AsUnitTask
17
+ * As3Bundle Sprout::COMPCTask
18
+ * As3Bundle Sprout::MXMLCTask
19
+ * As2Bundle Sprout::MTASCTask
20
+ * As2Bundle Sprout::SWFMillTask
21
+
data/doc/Tool ADDED
@@ -0,0 +1,20 @@
1
+
2
+ A Tool is a Ruby Gem that usually refers to an executable or binary application.
3
+ These applications are either natively cross platform, or the Ruby Gem should include a
4
+ YAML document that tells Sprouts where to go in order to get the appropriate binary for
5
+ which platform the user is currently running.
6
+
7
+ CLI Tools are usually referenced by subclasses of Sprout::ToolTask.
8
+
9
+ Once installed, many Tool Sprouts are made available from your path. For example
10
+ if you install the sprout-mtasc-tool gem, from that point forward you can execute mtasc
11
+ from the terminal as follows:
12
+
13
+ mtasc -help # Should throw an error
14
+ sudo gem install sprout-mtasc-tool
15
+ mtasc -help # Should download and execute mtasc
16
+
17
+ Using just Sprout tools by themselves, we now have have the ability to install and manage
18
+ requisite executables across platforms with zero configuration.
19
+
20
+ In reality, 'Tool Sprouts' are actually nothing more than a naming convention and expected gem configuration, but once those requirements are met, the core Sprout::Sprout can do some important work with them.
data/lib/platform.rb ADDED
@@ -0,0 +1,109 @@
1
+ #
2
+ # platform.rb: naive platform detection for Ruby
3
+ # author: Matt Mower
4
+ #
5
+ # == Platform
6
+ #
7
+ # Platform is a simple module which parses the Ruby constant
8
+ # RUBY_PLATFORM and works out the OS, it's implementation,
9
+ # and the architecture it's running on.
10
+ #
11
+ # The motivation for writing this was coming across a case where
12
+ #
13
+ # +if RUBY_PLATFORM =~ /win/+
14
+ #
15
+ # didn't behave as expected (i.e. on powerpc-darwin-8.1.0)
16
+ #
17
+ # It is hoped that providing a library for parsing the platform
18
+ # means that we can cover all the cases and have something which
19
+ # works reliably 99% of the time.
20
+ #
21
+ # Please report any anomalies or new combinations to the author(s).
22
+ #
23
+ # == Use
24
+ #
25
+ # require "platform"
26
+ #
27
+ # defines
28
+ #
29
+ # Platform::OS (:unix,:win32,:vms,:os2)
30
+ # Platform::impl (:macosx,:linux,:mswin)
31
+ # Platform::arch (:powerpc,:x86,:alpha)
32
+ #
33
+ # if an unknown configuration is encountered any (or all) of
34
+ # these constant may have the value :unknown.
35
+ #
36
+ # To display the combination for your setup run
37
+ #
38
+ # ruby platform.rb
39
+ #
40
+ module Platform #:nodoc:
41
+ os = nil
42
+ impl = nil
43
+ arch = nil
44
+
45
+ if RUBY_PLATFORM =~ /darwin/i
46
+ os = :unix
47
+ impl = :macosx
48
+ elsif RUBY_PLATFORM =~ /linux/i
49
+ os = :unix
50
+ impl = :linux
51
+ elsif RUBY_PLATFORM =~ /freebsd/i
52
+ os = :unix
53
+ impl = :freebsd
54
+ elsif RUBY_PLATFORM =~ /netbsd/i
55
+ os = :unix
56
+ impl = :netbsd
57
+ elsif RUBY_PLATFORM =~ /vista/i
58
+ os = :win32
59
+ impl = :vista
60
+ elsif RUBY_PLATFORM =~ /mswin/i
61
+ os = :win32
62
+ impl = :mswin
63
+ elsif RUBY_PLATFORM =~ /cygwin/i
64
+ os = :win32
65
+ impl = :cygwin
66
+ elsif RUBY_PLATFORM =~ /mingw/i
67
+ os = :win32
68
+ impl = :mingw
69
+ elsif RUBY_PLATFORM =~ /bccwin/i
70
+ os = :win32
71
+ impl = :bccwin
72
+ elsif RUBY_PLATFORM =~ /wince/i
73
+ os = :win32
74
+ impl = :wince
75
+ elsif RUBY_PLATFORM =~ /vms/i
76
+ os = :vms
77
+ impl = :vms
78
+ elsif RUBY_PLATFORM =~ /os2/i
79
+ os = :os2
80
+ impl = :os2 # maybe there is some better choice here?
81
+ else
82
+ os = :unknown
83
+ impl = :unknown
84
+ end
85
+
86
+ # whither AIX, SOLARIS, and the other unixen?
87
+
88
+ if RUBY_PLATFORM =~ /(i\d86)/i
89
+ arch = :x86
90
+ elsif RUBY_PLATFORM =~ /ia64/i
91
+ arch = :ia64
92
+ elsif RUBY_PLATFORM =~ /powerpc/i
93
+ arch = :powerpc
94
+ elsif RUBY_PLATFORM =~ /alpha/i
95
+ arch = :alpha
96
+ else
97
+ arch = :unknown
98
+ end
99
+
100
+ OS = os
101
+ IMPL = impl
102
+ ARCH = arch
103
+ # What about AMD, Turion, Motorola, etc..?
104
+
105
+ end
106
+
107
+ if __FILE__ == $0
108
+ puts "Platform OS=#{Platform::OS}, impl=#{Platform::IMPL}, arch=#{Platform::ARCH}"
109
+ end
@@ -0,0 +1,330 @@
1
+ #
2
+ # Ruby/ProgressBar - a text progress bar library
3
+ #
4
+ # Copyright (C) 2001-2005 Satoru Takabayashi <satoru@namazu.org>
5
+ # All rights reserved.
6
+ # This is free software with ABSOLUTELY NO WARRANTY.
7
+ #
8
+ # You can redistribute it and/or modify it under the terms
9
+ # of Ruby's license.
10
+ #
11
+ # Modified by Luke Bayes to support progress display on
12
+ # multiple simultaneous connections
13
+
14
+ require 'singleton'
15
+
16
+ class ProgressBar # :nodoc:[all]
17
+ VERSION = "0.9"
18
+ def self.new(title, total)
19
+ return ProgressBarManager.instance.add(title, total)
20
+ end
21
+ end
22
+
23
+ class ProgressBarImpl # :nodoc:[all]
24
+
25
+ def initialize (title, total, out = STDERR)
26
+ @title = title
27
+ @total = total
28
+ @out = out
29
+ @terminal_width = 80
30
+ @bar_mark = "."
31
+ @current = 0
32
+ @previous = 0
33
+ @finished_p = false
34
+ @start_time = Time.now
35
+ @previous_time = @start_time
36
+ @title_width = 18
37
+ @format = "%-#{@title_width}s %3d%% %s %s"
38
+ @format_arguments = [:title, :percentage, :bar, :stat]
39
+ clear
40
+ show
41
+ end
42
+ attr_reader :title
43
+ attr_reader :current
44
+ attr_reader :total
45
+ attr_accessor :start_time,
46
+ :title_width,
47
+ :bar_mark
48
+
49
+ def fmt_bar
50
+ bar_width = do_percentage * @terminal_width / 100
51
+ sprintf("|%s%s|",
52
+ @bar_mark * bar_width,
53
+ " " * (@terminal_width - bar_width))
54
+ end
55
+
56
+ def fmt_percentage
57
+ do_percentage
58
+ end
59
+
60
+ def fmt_stat
61
+ if @finished_p then elapsed else eta end
62
+ end
63
+
64
+ def fmt_stat_for_file_transfer
65
+ if @finished_p then
66
+ sprintf("%s %s %s", bytes, transfer_rate, elapsed)
67
+ else
68
+ sprintf("%s %s %s", bytes, transfer_rate, eta)
69
+ end
70
+ end
71
+
72
+ def fmt_title
73
+ @title[0,(@title_width - 1)] + ":"
74
+ end
75
+
76
+ def convert_bytes (bytes)
77
+ if bytes < 1024
78
+ sprintf("%6dB", bytes)
79
+ elsif bytes < 1024 * 1000 # 1000kb
80
+ sprintf("%5.1fKB", bytes.to_f / 1024)
81
+ elsif bytes < 1024 * 1024 * 1000 # 1000mb
82
+ sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
83
+ else
84
+ sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
85
+ end
86
+ end
87
+
88
+ def transfer_rate
89
+ bytes_per_second = @current.to_f / (Time.now - @start_time)
90
+ sprintf("%s/s", convert_bytes(bytes_per_second))
91
+ end
92
+
93
+ def bytes
94
+ convert_bytes(@current)
95
+ end
96
+
97
+ def format_time (t)
98
+ t = t.to_i
99
+ sec = t % 60
100
+ min = (t / 60) % 60
101
+ hour = t / 3600
102
+ sprintf("%02d:%02d:%02d", hour, min, sec);
103
+ end
104
+
105
+ # ETA stands for Estimated Time of Arrival.
106
+ def eta
107
+ if @current == 0
108
+ "ETA: --:--:--"
109
+ else
110
+ elapsed_time = Time.now - @start_time
111
+ estimated_time = elapsed_time * @total / @current - elapsed_time
112
+ sprintf("ETA: %s", format_time(estimated_time))
113
+ end
114
+ end
115
+
116
+ def elapsed
117
+ elapsed_time = Time.now - @start_time
118
+ sprintf("Time: %s", format_time(elapsed_time))
119
+ end
120
+
121
+ def eol
122
+ if @finished_p then "\n" else "\r" end
123
+ end
124
+
125
+ def do_percentage
126
+ if @total.zero?
127
+ 100
128
+ else
129
+ @current * 100 / @total
130
+ end
131
+ end
132
+
133
+ def get_width
134
+ # return 80
135
+ # FIXME: I don't know how portable it is.
136
+ default_width = 80
137
+ begin
138
+ tiocgwinsz = 0x5413
139
+ data = [0, 0, 0, 0].pack("SSSS")
140
+ if @out.ioctl(tiocgwinsz, data) >= 0 then
141
+ unpacked = data.unpack("SSSS")
142
+ cols = unpacked[1]
143
+ # Commented this because Aptana was complaining about
144
+ # The unused variables
145
+ # rows, cols, xpixels, ypixels = data.unpack("SSSS")
146
+ if cols >= 0 then cols else default_width end
147
+ else
148
+ default_width
149
+ end
150
+ rescue Exception
151
+ default_width
152
+ end
153
+ end
154
+
155
+ def show
156
+ arguments = @format_arguments.map {|method|
157
+ method = sprintf("fmt_%s", method)
158
+ send(method)
159
+ }
160
+ line = sprintf(@format, *arguments)
161
+
162
+ width = get_width
163
+ if(line.length == width - 1)
164
+ @out.print(line + eol)
165
+ @out.flush
166
+ elsif(line.length >= width)
167
+ @terminal_width = [@terminal_width - (line.length - width + 1), 0].max
168
+ if @terminal_width == 0 then @out.print(line + eol) else show end
169
+ else # line.length < width - 1
170
+ @terminal_width += width - line.length + 1
171
+ show
172
+ end
173
+ @previous_time = Time.now
174
+ end
175
+
176
+ def show_if_needed
177
+ if @total.zero?
178
+ cur_percentage = 100
179
+ prev_percentage = 0
180
+ else
181
+ cur_percentage = (@current * 100 / @total).to_i
182
+ prev_percentage = (@previous * 100 / @total).to_i
183
+ end
184
+
185
+ # Use "!=" instead of ">" to support negative changes
186
+ if cur_percentage != prev_percentage ||
187
+ Time.now - @previous_time >= 1 || @finished_p
188
+ show
189
+ end
190
+ end
191
+
192
+ public
193
+ def clear
194
+ @out.print "\r"
195
+ @out.print(" " * (get_width - 1))
196
+ @out.print "\r"
197
+ end
198
+
199
+ def finish
200
+ @current = @total
201
+ @finished_p = true
202
+ show
203
+ end
204
+
205
+ def finished?
206
+ @finished_p
207
+ end
208
+
209
+ def file_transfer_mode
210
+ @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
211
+ end
212
+
213
+ def format= (format)
214
+ @format = format
215
+ end
216
+
217
+ def format_arguments= (arguments)
218
+ @format_arguments = arguments
219
+ end
220
+
221
+ def halt
222
+ @finished_p = true
223
+ show
224
+ end
225
+
226
+ def inc (step = 1)
227
+ @current += step
228
+ @current = @total if @current > @total
229
+ show_if_needed
230
+ @previous = @current
231
+ end
232
+
233
+ def set (count)
234
+ if count < 0 || count > @total
235
+ @total = count
236
+ end
237
+ @current = count
238
+ show_if_needed
239
+ @previous = @current
240
+ end
241
+
242
+ def inspect
243
+ "#<ProgressBar:#{@current}/#{@total}>"
244
+ end
245
+ end
246
+
247
+ # Used instead of $stderr when Log.debug == true
248
+ # This helps keep us from junking up unit test
249
+ # output with download status messages
250
+ class MockOutput # :nodoc:[all]
251
+ def print(str)
252
+ end
253
+
254
+ def puts(str)
255
+ end
256
+
257
+ def flush
258
+ end
259
+ end
260
+
261
+ class ReversedProgressBar < ProgressBar # :nodoc:[all]
262
+ def do_percentage
263
+ 100 - super
264
+ end
265
+ end
266
+
267
+ class ProgressBarOutputStream # :nodoc:[all]
268
+ attr_reader :title
269
+
270
+ def initialize(mgr)
271
+ @mgr = mgr
272
+ @msg = ''
273
+ end
274
+
275
+ def print(msg)
276
+ @msg = msg
277
+ end
278
+
279
+ def flush
280
+ @mgr.flush
281
+ end
282
+
283
+ def to_s
284
+ return @msg.clone.split("\n").join("").split("\r").join("")
285
+ end
286
+
287
+ end
288
+
289
+ class ProgressBarManager # :nodoc:[all]
290
+ include Singleton
291
+
292
+ def initialize
293
+ @outio = $stderr
294
+ @finished = {}
295
+ @bars = {}
296
+ @outs = {}
297
+ end
298
+
299
+ def add(title, total1)
300
+ # if(@bars[title])
301
+ # raise StandardError.new
302
+ # end
303
+ @outs[title] = ProgressBarOutputStream.new(self)
304
+ @bars[title] = ProgressBarImpl.new(title, total1, @outs[title])
305
+ end
306
+
307
+ def print(title)
308
+ str = ''
309
+ str += @outs[title].to_s
310
+ str += "\r"
311
+ @outio.print "\r"
312
+ @outio.print str
313
+ end
314
+
315
+ def flush
316
+ @bars.keys.each do |title|
317
+ print(title)
318
+ end
319
+
320
+ @bars.values.each do |bar|
321
+ if(bar.finished?)
322
+ print(bar.title)
323
+ @outio.print "\n"
324
+ @outs.delete(bar.title)
325
+ @bars.delete(bar.title)
326
+ end
327
+ end
328
+ end
329
+
330
+ end