├── var ├── name ├── title ├── version ├── created ├── organization ├── summary ├── requirements ├── repositories ├── authors ├── copyrights ├── description └── resources ├── work ├── deprecated │ └── meta │ │ ├── name │ │ ├── version │ │ ├── created │ │ ├── license │ │ ├── collection │ │ ├── released │ │ ├── homepage │ │ ├── repository │ │ ├── summary │ │ ├── authors │ │ └── description ├── reference │ └── script-0.3 │ │ ├── doc │ │ ├── created.rid │ │ ├── index.html │ │ ├── classes │ │ │ ├── Script.src │ │ │ │ ├── M000006.html │ │ │ │ ├── M000005.html │ │ │ │ ├── M000002.html │ │ │ │ ├── M000001.html │ │ │ │ ├── M000004.html │ │ │ │ └── M000003.html │ │ │ ├── Kernel.src │ │ │ │ ├── M000008.html │ │ │ │ ├── M000006.html │ │ │ │ └── M000007.html │ │ │ ├── Module.src │ │ │ │ ├── M000007.html │ │ │ │ ├── M000005.html │ │ │ │ └── M000006.html │ │ │ ├── Script │ │ │ │ └── MissingFile.html │ │ │ ├── Kernel.html │ │ │ ├── Module.html │ │ │ └── Script.html │ │ ├── fr_class_index.html │ │ ├── fr_file_index.html │ │ ├── fr_method_index.html │ │ ├── files │ │ │ ├── RELEASE-NOTES.html │ │ │ ├── README.html │ │ │ ├── lib │ │ │ │ ├── autoscript_rb.html │ │ │ │ └── script_rb.html │ │ │ ├── sim_rb.html │ │ │ └── intro_txt.html │ │ └── rdoc-style.css │ │ ├── examples │ │ ├── locals │ │ │ ├── my-script-sub.rb │ │ │ ├── my-script.rb │ │ │ └── prog.rb │ │ ├── scripts │ │ │ ├── simple-script.rb │ │ │ ├── lib │ │ │ │ └── x-accessor.rb │ │ │ ├── exception-raiser.rb │ │ │ ├── sub-script.rb │ │ │ └── script.rb │ │ ├── plugins │ │ │ ├── plug1.rb │ │ │ └── plug2.rb │ │ ├── program1.rb │ │ ├── exception-rescuer.rb │ │ ├── program2.rb │ │ ├── plugin.rb │ │ └── autoscript-example.rb │ │ ├── README │ │ ├── script │ │ └── mkrdoc │ │ ├── RELEASE-NOTES │ │ ├── lib │ │ ├── autoscript.rb │ │ ├── script-0.rb │ │ ├── script.rb │ │ ├── ~script.rb │ │ └── script-1.rb │ │ ├── intro.txt │ │ └── install.rb ├── faq.rdoc └── consider │ ├── autoscript.rb │ └── script.rb ├── Gemfile ├── .yardopts ├── .gitignore ├── .travis.yml ├── demo ├── applique │ └── env.rb └── 01_example.rdoc ├── MANIFEST ├── Assembly ├── DEMO.rdoc ├── HISTORY.rdoc ├── .ruby ├── lib ├── capsule │ └── autoimport.rb └── capsule.rb ├── COPYING.rdoc ├── .gemspec └── README.rdoc /var/name: -------------------------------------------------------------------------------- 1 | capsule -------------------------------------------------------------------------------- /var/title: -------------------------------------------------------------------------------- 1 | Capsule -------------------------------------------------------------------------------- /var/version: -------------------------------------------------------------------------------- 1 | 1.1.1 -------------------------------------------------------------------------------- /var/created: -------------------------------------------------------------------------------- 1 | 2005-01-01 -------------------------------------------------------------------------------- /var/organization: -------------------------------------------------------------------------------- 1 | Rubyworks -------------------------------------------------------------------------------- /work/deprecated/meta/name: -------------------------------------------------------------------------------- 1 | capsule 2 | -------------------------------------------------------------------------------- /work/deprecated/meta/version: -------------------------------------------------------------------------------- 1 | 1.0.0 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source :rubygems 2 | gemspec 3 | -------------------------------------------------------------------------------- /var/summary: -------------------------------------------------------------------------------- 1 | Encapsulate Scripts in Modules -------------------------------------------------------------------------------- /work/deprecated/meta/created: -------------------------------------------------------------------------------- 1 | 2005-01-01 2 | -------------------------------------------------------------------------------- /work/deprecated/meta/license: -------------------------------------------------------------------------------- 1 | LGPLv3 2 | -------------------------------------------------------------------------------- /work/deprecated/meta/collection: -------------------------------------------------------------------------------- 1 | rubyworks 2 | -------------------------------------------------------------------------------- /work/deprecated/meta/released: -------------------------------------------------------------------------------- 1 | 2009-06-26 2 | -------------------------------------------------------------------------------- /var/requirements: -------------------------------------------------------------------------------- 1 | --- 2 | - detroit (build) 3 | - qed (test) 4 | -------------------------------------------------------------------------------- /.yardopts: -------------------------------------------------------------------------------- 1 | --private 2 | --protected 3 | lib 4 | - 5 | [A-Z]*.* 6 | -------------------------------------------------------------------------------- /work/deprecated/meta/homepage: -------------------------------------------------------------------------------- 1 | http://rubyworks.github.com/capsule 2 | -------------------------------------------------------------------------------- /var/repositories: -------------------------------------------------------------------------------- 1 | --- 2 | upstream: git://github.com/rubyworks/capsule.git 3 | -------------------------------------------------------------------------------- /work/deprecated/meta/repository: -------------------------------------------------------------------------------- 1 | git://github.com/rubyworks/capsule.git 2 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/created.rid: -------------------------------------------------------------------------------- 1 | Sun Dec 05 18:50:20 PST 2004 2 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/locals/my-script-sub.rb: -------------------------------------------------------------------------------- 1 | x = 5 2 | y = 6 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .fire/digest 2 | .yardoc 3 | doc 4 | log 5 | pkg 6 | tmp 7 | web 8 | -------------------------------------------------------------------------------- /var/authors: -------------------------------------------------------------------------------- 1 | --- 2 | - Thomas Sawyer 3 | - Joel VanderWerf 4 | -------------------------------------------------------------------------------- /work/deprecated/meta/summary: -------------------------------------------------------------------------------- 1 | Encapsulate scripts and functional modules. 2 | 3 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/scripts/simple-script.rb: -------------------------------------------------------------------------------- 1 | VALUE = [1,2,3] 2 | -------------------------------------------------------------------------------- /work/deprecated/meta/authors: -------------------------------------------------------------------------------- 1 | Joel VanderWerf 2 | trans 3 | 4 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/locals/my-script.rb: -------------------------------------------------------------------------------- 1 | x = 4 2 | require 'my-script-sub' 3 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/plugins/plug1.rb: -------------------------------------------------------------------------------- 1 | class Plug1 2 | end 3 | 4 | K = 42 5 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/plugins/plug2.rb: -------------------------------------------------------------------------------- 1 | class Plug2 2 | end 3 | 4 | K = 43 5 | -------------------------------------------------------------------------------- /var/copyrights: -------------------------------------------------------------------------------- 1 | --- 2 | - (c) 2007 Thomas Sawyer (BSD-2) 3 | - (c) 2004 Joel VanderWerf (Ruby) 4 | -------------------------------------------------------------------------------- /var/description: -------------------------------------------------------------------------------- 1 | Capsule is subclass of Module. It encapsulates an extenal script as a functions module. -------------------------------------------------------------------------------- /work/deprecated/meta/description: -------------------------------------------------------------------------------- 1 | Capsule is subclass of Module. It encapsulates an 2 | extenal script as a functions module. 3 | -------------------------------------------------------------------------------- /work/reference/script-0.3/README: -------------------------------------------------------------------------------- 1 | To install: 2 | 3 | ruby install.rb config 4 | ruby install.rb setup 5 | ruby install.rb install 6 | -------------------------------------------------------------------------------- /work/reference/script-0.3/script/mkrdoc: -------------------------------------------------------------------------------- 1 | rdoc $* -m intro.txt -o doc -t 'Script' -f html \ 2 | intro.txt lib/*.rb README RELEASE-NOTES 3 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/program1.rb: -------------------------------------------------------------------------------- 1 | require 'script' 2 | 3 | script = Script.load("scripts/simple-script.rb") 4 | 5 | p script::VALUE 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | script: "bundle exec qed" 3 | rvm: 4 | - 1.8.7 5 | - 1.9.2 6 | - 1.9.3 7 | - rbx-2.0 8 | - jruby 9 | - ree 10 | 11 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/scripts/lib/x-accessor.rb: -------------------------------------------------------------------------------- 1 | puts "in #{__FILE__}, line #{__LINE__}" 2 | 3 | def x 4 | @x # @x is assigned in subworld.rb 5 | end 6 | -------------------------------------------------------------------------------- /demo/applique/env.rb: -------------------------------------------------------------------------------- 1 | require 'capsule' 2 | 3 | When "say we have a Ruby script called '(((.*)))'" do |file, text| 4 | File.open(file, 'w'){ |f| f << text } 5 | end 6 | 7 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/scripts/exception-raiser.rb: -------------------------------------------------------------------------------- 1 | class Foo 2 | def foo 3 | raise ArgumentError, "I want to have an argument!" 4 | end 5 | end 6 | 7 | f = Foo.new 8 | f.foo 9 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/exception-rescuer.rb: -------------------------------------------------------------------------------- 1 | require 'script' 2 | 3 | begin 4 | script = Script.load("scripts/exception-raiser.rb") 5 | rescue Exception => ex 6 | puts ex.message 7 | end 8 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | #!mast .ruby .yardopts bin lib meta spec test [A-Z]*.* 2 | .ruby 3 | .yardopts 4 | lib/capsule/autoimport.rb 5 | lib/capsule.rb 6 | HISTORY.rdoc 7 | README.rdoc 8 | QED.rdoc 9 | COPYING.rdoc 10 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/scripts/sub-script.rb: -------------------------------------------------------------------------------- 1 | puts "in #{__FILE__}, line #{__LINE__}" 2 | 3 | MORE_OUTPUT = {:zap => 9} 4 | 5 | def foo(ary) 6 | ary.reverse 7 | end 8 | 9 | @x = "the value of @x defined in #{__FILE__}" 10 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/locals/prog.rb: -------------------------------------------------------------------------------- 1 | require 'script' 2 | 3 | script = Script.load("my-script.rb") 4 | 5 | p eval("x", script.__script_scope) 6 | p script.__local_variable_get(:x) 7 | p script.__local_variables 8 | p script.instance_variables 9 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/program2.rb: -------------------------------------------------------------------------------- 1 | require 'script' 2 | 3 | script = Script.load("scripts/script.rb") { |script| script::INPUT = 3 } 4 | 5 | raise if defined?(INPUT) 6 | 7 | p script::OUTPUT 8 | p script::MORE_OUTPUT 9 | 10 | script.run 11 | 12 | puts "script.x = #{script.x}" 13 | -------------------------------------------------------------------------------- /var/resources: -------------------------------------------------------------------------------- 1 | --- 2 | home: http://rubyworks.github.com/capsule 3 | code: http://github.com/rubyworks/capsule 4 | docs: http://rubydoc.info/gems/capsule 5 | bugs: http://github.com/rubyworks/capsule/issues 6 | mail: http://groups.google.com/group/rubyworks-mailinglist 7 | chat: irc://irc.freenode.net/rubyworks 8 | 9 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/plugin.rb: -------------------------------------------------------------------------------- 1 | require 'script' 2 | 3 | plugins = Dir.glob("plugins/*.rb").map do |file| 4 | Script.load file 5 | end 6 | 7 | p plugins 8 | 9 | plugins.each do |plugin| 10 | puts "plugin #{plugin} defines these constants:" 11 | plugin.constants.each do |const_name| 12 | val = plugin.const_get(const_name) 13 | puts " %20s = %s" % [const_name, val.inspect] 14 | end 15 | puts 16 | end 17 | -------------------------------------------------------------------------------- /work/reference/script-0.3/RELEASE-NOTES: -------------------------------------------------------------------------------- 1 | script 0.3 2 | 3 | - New examples. 4 | 5 | - Changed names of #main_file, #dir, and #loaded_features to be prefixed 6 | with __ to avoid conflicts. 7 | 8 | - Local variables in the top level script file are avaiable through the 9 | __script_scope method. See examples and docs. 10 | 11 | script 0.2 12 | 13 | - Fixed bug with making module function out of top-level method defs. 14 | 15 | script 0.1 16 | 17 | - first release 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/autoscript-example.rb: -------------------------------------------------------------------------------- 1 | require 'autoscript' 2 | 3 | module M 4 | autoscript :SimpleScript, "scripts/simple-script.rb" 5 | end 6 | 7 | autoscript :SimpleScript, "scripts/simple-script.rb" 8 | 9 | 10 | p M::SimpleScript::VALUE 11 | p SimpleScript::VALUE 12 | 13 | # This shows that the two arrays are different, because two scripts were loaded. 14 | M::SimpleScript::VALUE << 4 15 | SimpleScript::VALUE << 5 16 | 17 | p M::SimpleScript::VALUE 18 | p SimpleScript::VALUE 19 | -------------------------------------------------------------------------------- /work/reference/script-0.3/examples/scripts/script.rb: -------------------------------------------------------------------------------- 1 | puts "in #{__FILE__}, line #{__LINE__}" 2 | 3 | load "sub-script.rb" 4 | 5 | OUTPUT = ["input was #{INPUT}"] 6 | 7 | require 'lib/x-accessor' 8 | require 'lib/x-accessor' # only loaded once 9 | 10 | # Falls back to Kernel.load, since "benchmark.rb" isn't in the current dir. 11 | load "benchmark.rb" unless $LOADED_FEATURES.include?("benchmark.rb") 12 | 13 | # Falls back to Kernel.require. 14 | require 'yaml' 15 | 16 | MORE_OUTPUT[:buzz] = 3 17 | 18 | OUTPUT << foo((1..10).to_a) # foo is defined in sub-script.rb 19 | 20 | def self.run 21 | puts "#{self} is running!" 22 | end 23 | -------------------------------------------------------------------------------- /work/faq.rdoc: -------------------------------------------------------------------------------- 1 | = Facets FAQs 2 | 3 | Q. How do I use the core mixins on object-by-object basis? 4 | 5 | A. One way is to use Facets' Capsule library. 6 | 7 | require 'facets/capsule' 8 | 9 | MyStringExts = Capsule.load('facets/string/align')::String 10 | 11 | s = "string" 12 | 13 | s.extend(MyStringExts) 14 | 15 | s.align_right(10) #=> " string" 16 | 17 | Or handle it using in-module load methods. 18 | 19 | require 'facets/module/require' 20 | 21 | module MyStringExts 22 | module_load 'facets/string/align' 23 | include String 24 | end 25 | 26 | s = "string" 27 | 28 | s.extend(MyStringExts) 29 | 30 | s.align_right(10) #=> " string" 31 | 32 | 33 | -------------------------------------------------------------------------------- /Assembly: -------------------------------------------------------------------------------- 1 | --- 2 | gem: 3 | active: true 4 | 5 | github: 6 | folder: web 7 | 8 | yard: 9 | priority: 2 10 | active: false 11 | 12 | qed: 13 | files: demo/*.rdoc 14 | 15 | qedoc: 16 | title: Capsule 17 | files: demo/*.rdoc 18 | output: 19 | - DEMO.rdoc 20 | - site/qed.html 21 | 22 | syntax: 23 | files: lib/ 24 | 25 | dnote: 26 | title: Developer's Notes 27 | labels: ~ 28 | output: log/notes.html 29 | 30 | vclog: 31 | output: 32 | - log/changes.html 33 | - log/history.html 34 | 35 | locat: 36 | output: log/locat.html 37 | active: false 38 | 39 | email: 40 | mailto: 41 | - ruby-talk@ruby-lang.org 42 | - rubyworks-mailinglist@googlegroups.com 43 | 44 | custom: 45 | document: | 46 | cmd = "shomen yard > web/docs/<%= name %>-<%= version %>.json" 47 | puts cmd; sh cmd 48 | 49 | -------------------------------------------------------------------------------- /DEMO.rdoc: -------------------------------------------------------------------------------- 1 | = Capsule Example 2 | 3 | Let's say we have a Ruby script called 'hello.rb': 4 | 5 | def hello 6 | "HELLO!" 7 | end 8 | 9 | We can load that script into a Capsule and have direct access 10 | to the #hello method. 11 | 12 | Hello = Capsule.new('hello.rb') 13 | 14 | Hello.hello #=> "HELLO!" 15 | 16 | By default, capsules are self extendind (`extend self`), which is 17 | why the above method become accessible. This behavior can be tured 18 | off by passing a `extend=>false` option to the #new method. 19 | 20 | Hello2 = Capsule.new('hello.rb', :extend=>false) 21 | 22 | expect NoMethodError do 23 | Hello2.hello 24 | end 25 | 26 | Without automatic self extension, the capsule still has access to all the 27 | classes, modules, constants and explicitly defined class methods of the 28 | loaded script. 29 | 30 | 31 | -------------------------------------------------------------------------------- /demo/01_example.rdoc: -------------------------------------------------------------------------------- 1 | = Capsule Example 2 | 3 | Let's say we have a Ruby script called 'hello.rb': 4 | 5 | def hello 6 | "HELLO!" 7 | end 8 | 9 | We can load that script into a Capsule and have direct access 10 | to the #hello method. 11 | 12 | Hello = Capsule.new('hello.rb') 13 | 14 | Hello.hello #=> "HELLO!" 15 | 16 | By default, capsules are self extendind (`extend self`), which is 17 | why the above method become accessible. This behavior can be tured 18 | off by passing a `extend=>false` option to the #new method. 19 | 20 | Hello2 = Capsule.new('hello.rb', :extend=>false) 21 | 22 | expect NoMethodError do 23 | Hello2.hello 24 | end 25 | 26 | Without automatic self extension, the capsule still has access to all the 27 | classes, modules, constants and explicitly defined class methods of the 28 | loaded script. 29 | 30 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 13 | Script 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Script.src/M000006.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | to_s (Script) 9 | 10 | 11 | 12 | 13 |
# File lib/script.rb, line 102
14 |   def to_s
15 |     "#<#{self.class}:#{File.join(dir, File.basename(main_file))}>"
16 |   end
17 | 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Script.src/M000005.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | method_added (Script) 9 | 10 | 11 | 12 | 13 |
# File lib/script.rb, line 99
14 |   def method_added(name)
15 |     module_function(name)
16 |   end
17 | 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/fr_class_index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 12 | 13 | 14 | Classes 15 | 16 | 17 | 18 | 19 | 20 |
21 |

Classes

22 |
23 | Kernel
24 | Module
25 | Script
26 | Script::MissingFile
27 |
28 |
29 | 30 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Kernel.src/M000008.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | autoscript (Kernel) 9 | 10 | 11 | 12 | 13 |
# File lib/autoscript.rb, line 26
14 |   def autoscript mod, filename
15 |     Object.autoscript mod, filename
16 |   end
17 | 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Module.src/M000007.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | autoscript (Module) 9 | 10 | 11 | 12 | 13 |
# File lib/autoscript.rb, line 19
14 |   def autoscript mod, filename
15 |     (@autoscript ||= {})[mod] = filename
16 |   end
17 | 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Kernel.src/M000006.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | autoscript (Kernel) 9 | 10 | 11 | 12 | 13 |
# File lib/autoscript.rb, line 31
14 |   def autoscript mod, filename
15 |     Object.autoscript mod, filename
16 |   end
17 | 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Kernel.src/M000007.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | autoscript (Kernel) 9 | 10 | 11 | 12 | 13 |
# File lib/autoscript.rb, line 31
14 |   def autoscript mod, filename
15 |     Object.autoscript mod, filename
16 |   end
17 | 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Module.src/M000005.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | autoscript (Module) 9 | 10 | 11 | 12 | 13 |
# File lib/autoscript.rb, line 24
14 |   def autoscript mod, filename
15 |     (@autoscript ||= {})[mod] = filename
16 |   end
17 | 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Module.src/M000006.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | autoscript (Module) 9 | 10 | 11 | 12 | 13 |
# File lib/autoscript.rb, line 24
14 |   def autoscript mod, filename
15 |     (@autoscript ||= {})[mod] = filename
16 |   end
17 | 18 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/fr_file_index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 12 | 13 | 14 | Files 15 | 16 | 17 | 18 | 19 | 20 |
21 |

Files

22 | 29 |
30 | 31 | -------------------------------------------------------------------------------- /work/consider/autoscript.rb: -------------------------------------------------------------------------------- 1 | require 'script' 2 | 3 | class Module 4 | alias const_missing_before_autoscript const_missing 5 | 6 | def const_missing(sym) # :nodoc: 7 | filename = @autoscript && @autoscript[sym] 8 | if filename 9 | mod = Script.load(filename) 10 | const_set sym, mod 11 | else 12 | const_missing_before_autoscript(sym) 13 | end 14 | end 15 | 16 | # When the constant named by symbol +mod+ is referenced, loads the script 17 | # in filename using Script.load and defines the constant to be equal to the 18 | # resulting Script module. 19 | # 20 | # Use like Module#autoload--however, the underlying opertation is #load rather 21 | # than #require, because scripts, unlike libraries, can be loaded more than 22 | # once. See examples/autoscript-example.rb 23 | 24 | def autoscript mod, filename 25 | (@autoscript ||= {})[mod] = filename 26 | end 27 | end 28 | 29 | module Kernel 30 | # Calls Object.autoscript. 31 | def autoscript mod, filename 32 | Object.autoscript mod, filename 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /work/reference/script-0.3/lib/autoscript.rb: -------------------------------------------------------------------------------- 1 | require 'script' 2 | 3 | class Module 4 | alias const_missing_before_autoscript const_missing 5 | 6 | def const_missing(sym) # :nodoc: 7 | filename = @autoscript && @autoscript[sym] 8 | if filename 9 | mod = Script.load(filename) 10 | const_set sym, mod 11 | else 12 | const_missing_before_autoscript(sym) 13 | end 14 | end 15 | 16 | # When the constant named by symbol +mod+ is referenced, loads the script 17 | # in filename using Script.load and defines the constant to be equal to the 18 | # resulting Script module. 19 | # 20 | # Use like Module#autoload--however, the underlying opertation is #load rather 21 | # than #require, because scripts, unlike libraries, can be loaded more than 22 | # once. See examples/autoscript-example.rb 23 | 24 | def autoscript mod, filename 25 | (@autoscript ||= {})[mod] = filename 26 | end 27 | end 28 | 29 | module Kernel 30 | # Calls Object.autoscript. 31 | def autoscript mod, filename 32 | Object.autoscript mod, filename 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /HISTORY.rdoc: -------------------------------------------------------------------------------- 1 | = RELEASE HISTORY 2 | 3 | == 1.1.1 / 2011-10-22 4 | 5 | Capsule is now released under the BSD-2-Clause license. Other than that, 6 | this release is just an administrative update, bringing the project 7 | uptodate with the latest build tools. The functionality of the library 8 | itself has not changed. 9 | 10 | Changes: 11 | 12 | * Switch to BSD-2-Clause license. 13 | * Modernize project for latest build tools. 14 | 15 | 16 | == 1.1.0 / 2011-05-12 17 | 18 | This release makes two small enhancements to the Capsule class. 19 | It will automatically try `.rb` extension of script names that 20 | lack an extension and it now provides an `:extend` option which 21 | can be set to `false` to deactive a capsules sef extension 22 | (`extend self`). 23 | 24 | Changes: 25 | 26 | * Search for scripts with common ruby extensions. 27 | * Add :extend option to control self extension. 28 | 29 | 30 | == 1.0.0 / 2009-07-01 31 | 32 | This is the initial stand-alone release of Capsule, 33 | spun-off from Ruby Facets. 34 | 35 | Changes: 36 | 37 | * 1 Major Enhancement 38 | 39 | * Happy Birthday! 40 | 41 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/fr_method_index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 12 | 13 | 14 | Methods 15 | 16 | 17 | 18 | 19 | 20 | 31 | 32 | -------------------------------------------------------------------------------- /.ruby: -------------------------------------------------------------------------------- 1 | --- 2 | source: 3 | - var 4 | authors: 5 | - name: Thomas Sawyer 6 | email: transfire@gmail.com 7 | - name: Joel VanderWerf 8 | copyrights: 9 | - holder: Thomas Sawyer 10 | year: '2007' 11 | license: BSD-2 12 | - holder: Joel VanderWerf 13 | year: '2004' 14 | license: Ruby 15 | replacements: [] 16 | alternatives: [] 17 | requirements: 18 | - name: detroit 19 | groups: 20 | - build 21 | development: true 22 | - name: qed 23 | groups: 24 | - test 25 | development: true 26 | dependencies: [] 27 | conflicts: [] 28 | repositories: 29 | - uri: git://github.com/rubyworks/capsule.git 30 | scm: git 31 | name: upstream 32 | resources: 33 | home: http://rubyworks.github.com/capsule 34 | code: http://github.com/rubyworks/capsule 35 | docs: http://rubydoc.info/gems/capsule 36 | bugs: http://github.com/rubyworks/capsule/issues 37 | mail: http://groups.google.com/group/rubyworks-mailinglist 38 | chat: irc://irc.freenode.net/rubyworks 39 | extra: {} 40 | load_path: 41 | - lib 42 | revision: 0 43 | created: '2005-01-01' 44 | summary: Encapsulate Scripts in Modules 45 | title: Capsule 46 | version: 1.1.1 47 | name: capsule 48 | description: Capsule is subclass of Module. It encapsulates an extenal script as a 49 | functions module. 50 | organization: Rubyworks 51 | date: '2011-11-11' 52 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Script.src/M000002.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | load (Script) 9 | 10 | 11 | 12 | 13 |
# File lib/script.rb, line 53
14 |   def load(file, wrap = false)
15 |     load_in_module(File.join(@dir, file))
16 |     true
17 |   rescue MissingFile
18 |     super
19 |   end
20 | 21 | -------------------------------------------------------------------------------- /lib/capsule/autoimport.rb: -------------------------------------------------------------------------------- 1 | class Module 2 | 3 | const_missing_definition_for_autoimport = lambda do 4 | #$autoimport_activated = true 5 | alias const_missing_before_autoimport const_missing 6 | 7 | def const_missing(sym) # :nodoc: 8 | filename = @autoimport && @autoimport[sym] 9 | if filename 10 | mod = Capsule.load(filename) 11 | const_set sym, mod 12 | else 13 | const_missing_before_autoimport(sym) 14 | end 15 | end 16 | end 17 | 18 | # See Kernel#autoimport. 19 | # 20 | # TODO: Document this method using meta-programming markup. 21 | define_method(:autoimport) do |mod, file| 22 | if @autoimport.empty? #unless $autoimport_activated 23 | const_missing_definition_for_autoimport.call 24 | end 25 | (@autoimport ||= {})[mod] = file 26 | end 27 | end 28 | 29 | 30 | module Kernel 31 | # When the constant named by symbol +mod+ is referenced, loads the script 32 | # in filename using Capsule.load and defines the constant to be equal to the 33 | # resulting Capsule module. 34 | # 35 | # Use like Module#autoload. However, the underlying opertation is #load rather 36 | # than #require, because scripts, unlike libraries, can be loaded more than 37 | # once. See examples/autoscript-example.rb. 38 | # 39 | # This method call `Object.autoimport` as defined in Module. 40 | def autoimport(mod, file) 41 | Object.autoimport(mod, file) 42 | end 43 | end 44 | 45 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Script.src/M000001.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | new (Script) 9 | 10 | 11 | 12 | 13 |
# File lib/script.rb, line 30
14 |   def initialize(main_file)   # :yields: self
15 |     @main_file = File.expand_path(main_file)
16 |     @dir = File.dirname(@main_file)
17 |     @loaded_features = {}
18 |     
19 |     yield self if block_given?
20 |     load_in_module(main_file)
21 |   end
22 | 23 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Script.src/M000004.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | load_in_module (Script) 9 | 10 | 11 | 12 | 13 |
# File lib/script.rb, line 88
14 |   def load_in_module(file)
15 |     module_eval(IO.read(file), File.expand_path(file))
16 |   rescue Errno::ENOENT => e
17 |     if /#{file}$/ =~ e.message
18 |       raise MissingFile, e.message
19 |     else
20 |       raise
21 |     end
22 |   end
23 | 24 | -------------------------------------------------------------------------------- /COPYING.rdoc: -------------------------------------------------------------------------------- 1 | = COPYRIGHT NOTICES 2 | 3 | == Capsule - Copyright (c) 2007 Thomas Sawyer 4 | 5 | PROJECT:: Capsule 6 | WEBSITE:: http://rubyworks.github.com/capsule 7 | LICENSE:: BSD 2 Clause License 8 | 9 | Copyright (c) 2007 Thomas Sawyer 10 | 11 | Redistribution and use in source and binary forms, with or without modification, are 12 | permitted provided that the following conditions are met: 13 | 14 | 1. Redistributions of source code must retain the above copyright notice, this list of 15 | conditions and the following disclaimer. 16 | 17 | 2. Redistributions in binary form must reproduce the above copyright notice, this list 18 | of conditions and the following disclaimer in the documentation and/or other materials 19 | provided with the distribution. 20 | 21 | THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 23 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OR 24 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 27 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | 32 | == Script - Copyright (c) 2004 Joel VanderWerf 33 | 34 | PROJECT:: Script 35 | WEBSITE:: http://redshift.sourceforge.net/script/ 36 | LICENSE:: Ruby License 37 | 38 | Capsule is a derivation of Joel VanderWerf's Script library. 39 | 40 | Usable under the Ruby license. 41 | 42 | Questions to mailto:vjoel@users.sourceforge.net. 43 | 44 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Script.src/M000003.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | require (Script) 9 | 10 | 11 | 12 | 13 |
# File lib/script.rb, line 69
14 |   def require(feature)
15 |     unless @loaded_features[feature]
16 |       @loaded_features[feature] = true
17 |       file = File.join(@dir, feature)
18 |       file += ".rb" unless /\.rb$/ =~ file
19 |       load_in_module(file)
20 |     end
21 |   rescue MissingFile
22 |     @loaded_features[feature] = false
23 |     super
24 |   end
25 | 26 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/files/RELEASE-NOTES.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | File: RELEASE-NOTES 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 |

RELEASE-NOTES

51 | 52 | 53 | 54 | 56 | 57 | 58 | 59 | 60 | 61 |
Path:RELEASE-NOTES 55 |
Last Update:Sat May 15 23:00:46 PDT 2004
62 |
63 | 64 | 65 |
66 | 67 | 68 | 69 |
70 | 71 |
72 |

73 | script 0.1 74 |

75 |
    76 |
  • first release 77 | 78 |
  • 79 |
80 | 81 |
82 | 83 | 84 |
85 | 86 | 87 |
88 | 89 | 90 | 91 | 92 |
93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 |
105 | 106 | 107 |
108 |

[Validate]

109 |
110 | 111 | 112 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/files/README.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | File: README 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 |

README

51 | 52 | 53 | 54 | 56 | 57 | 58 | 59 | 60 | 61 |
Path:README 55 |
Last Update:Sat May 15 23:00:11 PDT 2004
62 |
63 | 64 | 65 |
66 | 67 | 68 | 69 |
70 | 71 |
72 |

73 | To install: 74 |

75 |
 76 |   ruby install.rb config
 77 |   ruby install.rb setup
 78 |   ruby install.rb install
 79 | 
80 | 81 |
82 | 83 | 84 |
85 | 86 | 87 |
88 | 89 | 90 | 91 | 92 |
93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 |
105 | 106 | 107 |
108 |

[Validate]

109 |
110 | 111 | 112 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/files/lib/autoscript_rb.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | File: autoscript.rb 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 |

autoscript.rb

51 | 52 | 53 | 54 | 56 | 57 | 58 | 59 | 60 | 61 |
Path:lib/autoscript.rb 55 |
Last Update:Sun Jun 20 23:01:54 PDT 2004
62 |
63 | 64 | 65 |
66 | 67 | 68 | 69 |
70 | 71 | 72 |
73 |

Required files

74 | 75 |
76 | script   77 |
78 |
79 | 80 |
81 | 82 | 83 |
84 | 85 | 86 | 87 | 88 |
89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 |
101 | 102 | 103 |
104 |

[Validate]

105 |
106 | 107 | 108 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Script/MissingFile.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | Class: Script::MissingFile 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | 66 | 67 | 70 | 71 |
ClassScript::MissingFile
In: 58 | 59 | lib/script.rb 60 | 61 |
62 |
Parent: 68 | LoadError 69 |
72 |
73 | 74 | 75 |
76 | 77 | 78 | 79 |
80 | 81 |
82 |

83 | Raised by load_in_module, caught by load and require. 84 |

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

[Validate]

114 |
115 | 116 | 117 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/files/lib/script_rb.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | File: script.rb 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 |

script.rb

51 | 52 | 53 | 54 | 56 | 57 | 58 | 59 | 60 | 61 |
Path:lib/script.rb 55 |
Last Update:Sun Dec 05 18:50:14 PST 2004
62 |
63 | 64 | 65 |
66 | 67 | 68 | 69 |
70 | 71 |
72 |

73 | A module which is an instance of the Script class encapsulates in its scope 75 | the top-level methods, top-level constants, and instance variables defined 76 | in a ruby script file (and its subfiles) loaded by a ruby program. This 77 | allows use of script files to define objects that can be loaded into a 78 | program in much the same way that objects can be loaded from YAML or 79 | Marshal files. 80 |

81 |

82 | See intro.txt for an overview. 83 |

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

[Validate]

113 |
114 | 115 | 116 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Kernel.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | Module: Kernel 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 |
ModuleKernel
In: 58 | 59 | lib/autoscript.rb 60 | 61 |
62 |
66 |
67 | 68 | 69 |
70 | 71 | 72 | 73 |
74 | 75 | 76 | 77 |
78 | 79 |
80 |

Methods

81 | 82 |
83 | autoscript   84 |
85 |
86 | 87 |
88 | 89 | 90 | 91 | 92 |
93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 |
103 |

Public Instance methods

104 | 105 |
106 | 107 | 108 | 114 | 115 |
116 |

117 | Calls Object.autoscript. 118 |

119 |
120 |
121 | 122 | 123 |
124 | 125 | 126 |
127 | 128 | 129 |
130 |

[Validate]

131 |
132 | 133 | 134 | -------------------------------------------------------------------------------- /work/reference/script-0.3/lib/script-0.rb: -------------------------------------------------------------------------------- 1 | # A module which is an instance of the Script class encapsulates in its scope 2 | # the top-level methods, top-level constants, and instance variables defined in 3 | # a ruby script file (and its subfiles) loaded by a ruby program. This allows 4 | # use of script files to define objects that can be loaded into a program in 5 | # much the same way that objects can be loaded from YAML or Marshal files. 6 | # 7 | # See intro.txt[link:files/intro_txt.html] for an overview. 8 | 9 | class Script < Module 10 | # The file with which the Script was instantiated. 11 | attr_reader :main_file 12 | 13 | # The directory in which main_file is located, and relative to which 14 | # #load searches for files before falling back to Kernel#load. 15 | attr_reader :dir 16 | 17 | # A hash that maps filename=>true for each file that has been 18 | # required locally by the script. This has the same semantics as $", 19 | # alias $LOADED_FEATURES, except that it is local to this script. 20 | attr_reader :loaded_features 21 | 22 | class << self 23 | alias load new 24 | end 25 | 26 | # Creates new Script, and loads _main_file_ in the scope of the Script. If a 27 | # block is given, the script is passed to it before loading from the file, and 28 | # constants can be defined as inputs to the script. 29 | 30 | def initialize(main_file) # :yields: self 31 | extend ScriptModuleMethods 32 | @main_file = File.expand_path(main_file) 33 | @dir = File.dirname(@main_file) 34 | @loaded_features = {} 35 | 36 | yield self if block_given? 37 | load_in_module(main_file) 38 | end 39 | 40 | # Loads _file_ into this Script. Searches relative to the local dir, that is, 41 | # the dir of the file given in the original call to 42 | # Script.load(file), loads the file, if found, into this Script's 43 | # scope, and returns true. If the file is not found, falls back to 44 | # Kernel.load, which searches on $LOAD_PATH, loads the file, 45 | # if found, into global scope, and returns true. Otherwise, raises 46 | # LoadError. 47 | # 48 | # The _wrap_ argument is passed to Kernel.load in the fallback case, 49 | # when the file is not found locally. 50 | # 51 | # Typically called from within the main file to load additional sub files, or 52 | # from those sub files. 53 | 54 | def load(file, wrap = false) 55 | load_in_module(File.join(@dir, file)) 56 | true 57 | rescue MissingFile 58 | super 59 | end 60 | 61 | # Analogous to Kernel#require. First tries the local dir, then falls 62 | # back to Kernel#require. Will load a given _feature_ only once. 63 | # 64 | # Note that extensions (*.so, *.dll) can be required in the global scope, as 65 | # usual, but not in the local scope. (This is not much of a limitation in 66 | # practice--you wouldn't want to load an extension more than once.) This 67 | # implementation falls back to Kernel#require when the argument is an 68 | # extension or is not found locally. 69 | 70 | def require(feature) 71 | unless @loaded_features[feature] 72 | @loaded_features[feature] = true 73 | file = File.join(@dir, feature) 74 | file += ".rb" unless /\.rb$/ =~ file 75 | load_in_module(file) 76 | end 77 | rescue MissingFile 78 | @loaded_features[feature] = false 79 | super 80 | end 81 | 82 | # Raised by #load_in_module, caught by #load and #require. 83 | class MissingFile < LoadError; end 84 | 85 | # Loads _file_ in this module's context. Note that \_\_FILE\_\_ and 86 | # \_\_LINE\_\_ work correctly in _file_. 87 | # Called by #load and #require; not normally called directly. 88 | 89 | def load_in_module(file) 90 | module_eval(IO.read(file), File.expand_path(file)) 91 | rescue Errno::ENOENT => e 92 | if /#{file}$/ =~ e.message 93 | raise MissingFile, e.message 94 | else 95 | raise 96 | end 97 | end 98 | 99 | def to_s # :nodoc: 100 | "#<#{self.class}:#{File.join(dir, File.basename(main_file))}>" 101 | end 102 | 103 | module ScriptModuleMethods 104 | # This is so that def meth... behaves like in Ruby's top-level 105 | # context. The implementation simply calls 106 | # Module#module_function(name). 107 | def method_added(name) # :nodoc: 108 | module_function(name) 109 | end 110 | end 111 | end 112 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Module.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | Class: Module 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | 66 | 67 | 70 | 71 |
ClassModule
In: 58 | 59 | lib/autoscript.rb 60 | 61 |
62 |
Parent: 68 | Object 69 |
72 |
73 | 74 | 75 |
76 | 77 | 78 | 79 |
80 | 81 | 82 | 83 |
84 | 85 |
86 |

Methods

87 | 88 |
89 | autoscript   90 |
91 |
92 | 93 |
94 | 95 | 96 | 97 | 98 |
99 | 100 | 101 | 102 |
103 |

External Aliases

104 | 105 |
106 | 107 | 108 | 109 | 110 | 111 | 112 |
const_missing->const_missing_before_autoscript
113 |
114 |
115 | 116 | 117 | 118 | 119 | 120 | 121 |
122 |

Public Instance methods

123 | 124 |
125 | 126 | 127 | 133 | 134 |
135 |

136 | When the constant named by symbol mod is referenced, loads the 137 | script in filename using Script.load and 138 | defines the constant to be equal to the resulting Script module. 140 |

141 |

142 | Use like Module#autoload—however, the underlying opertation is load 143 | rather than require, because scripts, unlike libraries, can be loaded more 144 | than once. See examples/autoscript-example.rb 145 |

146 |
147 |
148 | 149 | 150 |
151 | 152 | 153 |
154 | 155 | 156 |
157 |

[Validate]

158 |
159 | 160 | 161 | -------------------------------------------------------------------------------- /.gemspec: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | require 'yaml' 4 | 5 | Gem::Specification.new do |gemspec| 6 | 7 | manifest = Dir.glob('manifest{,.txt}', File::FNM_CASEFOLD).first 8 | 9 | scm = case 10 | when File.directory?('.git') 11 | :git 12 | end 13 | 14 | files = case 15 | when manifest 16 | File.readlines(manifest). 17 | map{ |line| line.strip }. 18 | reject{ |line| line.empty? || line[0,1] == '#' } 19 | when scm == :git 20 | `git ls-files -z`.split("\0") 21 | else 22 | Dir.glob('{**/}{.*,*}') # TODO: be more specific using standard locations ? 23 | end.select{ |path| File.file?(path) } 24 | 25 | patterns = { 26 | :bin_files => 'bin/*', 27 | :lib_files => 'lib/{**/}*.rb', 28 | :ext_files => 'ext/{**/}extconf.rb', 29 | :doc_files => '*.{txt,rdoc,md,markdown,tt,textile}', 30 | :test_files => '{test/{**/}*_test.rb,spec/{**/}*_spec.rb}' 31 | } 32 | 33 | glob_files = lambda { |pattern| 34 | Dir.glob(pattern).select { |path| 35 | File.file?(path) && files.include?(path) 36 | } 37 | } 38 | 39 | #files = glob_files[patterns[:files]] 40 | 41 | executables = glob_files[patterns[:bin_files]].map do |path| 42 | File.basename(path) 43 | end 44 | 45 | extensions = glob_files[patterns[:ext_files]].map do |path| 46 | File.basename(path) 47 | end 48 | 49 | metadata = YAML.load_file('.ruby') 50 | 51 | # build-out the gemspec 52 | 53 | case metadata['revision'] 54 | when 0 55 | gemspec.name = metadata['name'] 56 | gemspec.version = metadata['version'] 57 | gemspec.summary = metadata['summary'] 58 | gemspec.description = metadata['description'] 59 | 60 | metadata['authors'].each do |author| 61 | gemspec.authors << author['name'] 62 | 63 | if author.has_key?('email') 64 | if gemspec.email 65 | gemspec.email << author['email'] 66 | else 67 | gemspec.email = [author['email']] 68 | end 69 | end 70 | end 71 | 72 | gemspec.licenses = metadata['licenses'] 73 | 74 | metadata['requirements'].each do |req| 75 | name = req['name'] 76 | version = req['version'] 77 | groups = req['groups'] || [] 78 | 79 | #development = req['development'] 80 | #if development 81 | # # populate development dependencies 82 | # if gemspec.respond_to?(:add_development_dependency) 83 | # gemspec.add_development_dependency(name,*version) 84 | # else 85 | # gemspec.add_dependency(name,*version) 86 | # end 87 | #else 88 | # # populate runtime dependencies 89 | # if gemspec.respond_to?(:add_runtime_dependency) 90 | # gemspec.add_runtime_dependency(name,*version) 91 | # else 92 | # gemspec.add_dependency(name,*version) 93 | # end 94 | #end 95 | 96 | if groups.empty? or groups.include?('runtime') 97 | # populate runtime dependencies 98 | if gemspec.respond_to?(:add_runtime_dependency) 99 | gemspec.add_runtime_dependency(name,*version) 100 | else 101 | gemspec.add_dependency(name,*version) 102 | end 103 | else 104 | # populate development dependencies 105 | if gemspec.respond_to?(:add_development_dependency) 106 | gemspec.add_development_dependency(name,*version) 107 | else 108 | gemspec.add_dependency(name,*version) 109 | end 110 | end 111 | end 112 | 113 | # convert external dependencies into a requirements 114 | if metadata['external_dependencies'] 115 | ##gemspec.requirements = [] unless metadata['external_dependencies'].empty? 116 | metadata['external_dependencies'].each do |req| 117 | gemspec.requirements << req.to_s 118 | end 119 | end 120 | 121 | # determine homepage from resources 122 | homepage = metadata['resources'].find{ |key, url| key =~ /^home/ } 123 | gemspec.homepage = homepage.last if homepage 124 | 125 | gemspec.require_paths = metadata['load_path'] || ['lib'] 126 | gemspec.post_install_message = metadata['install_message'] 127 | 128 | # RubyGems specific metadata 129 | gemspec.files = files 130 | gemspec.extensions = extensions 131 | gemspec.executables = executables 132 | 133 | if Gem::VERSION < '1.7.' 134 | gemspec.default_executable = gemspec.executables.first 135 | end 136 | 137 | gemspec.test_files = glob_files[patterns[:test_files]] 138 | 139 | unless gemspec.files.include?('.document') 140 | gemspec.extra_rdoc_files = glob_files[patterns[:doc_files]] 141 | end 142 | end 143 | end 144 | -------------------------------------------------------------------------------- /work/reference/script-0.3/intro.txt: -------------------------------------------------------------------------------- 1 | == Script 2 | 3 | Script is a subclass of Module. A module which is an instance of the Script 4 | class encapsulates in its scope the top-level methods, top-level constants, and 5 | instance variables defined in a ruby script file (and its dependent files) 6 | loaded by a ruby program. This allows use of script files to define objects that 7 | can be loaded into a program in much the same way that objects can be loaded 8 | from YAML or Marshal files. There is also an autoscript method which functions 9 | like ruby's autoload but based on is Script.load (see 10 | examples/autoload-example.rb). 11 | 12 | The implementation of Script is very simple, consisting of about 60 lines of 13 | code. 14 | 15 | 16 | === Synopsis 17 | 18 | - program.rb: 19 | 20 | require 'script' 21 | my_script = Script.load("my-script.rb") 22 | p my_script::VALUE 23 | my_script.run 24 | 25 | --- 26 | 27 | - my-script.rb: 28 | 29 | VALUE = [1,2,3] 30 | def run 31 | puts "#{self} running." 32 | end 33 | 34 | --- 35 | 36 | - output: 37 | 38 | $ ruby program.rb 39 | [1, 2, 3] 40 | # running. 41 | 42 | 43 | === Usage 44 | 45 | Script modules are instantiated with Script.new(main_file) or the alias 46 | Script.load(main_file). All the top-level constants and top-level 47 | methods that are defined in the +main_file+ and its dependent local files (see 48 | below) are scoped in the same Script module, and are thereby available to the 49 | calling program. 50 | 51 | The +main_file+ can load or require other files with +load+ and +require+, as 52 | usual. These methods, in the Script context, add some behavior to the +Kernel+ 53 | +load+ and +require+ methods: Script#load and Script#require 54 | first search for files relative to the +main_file+'s dir. Files loaded in this 55 | way ("dependent local files") are treated like the script file itself: top-level 56 | definitions are added to the script module that is returned by +load+ or 57 | +require+. 58 | 59 | Both Script#load and Script#require fall back to the Kernel 60 | versions if the file is not found locally. Hence, other ruby libraries can be 61 | loaded and required as usual, assuming their names do not conflict with local 62 | file names. Definitions from those files go into the usual scope (typically 63 | global). The normal ruby +load+ and +require+ behavior can be forced by calling 64 | Kernel.load and Kernel.require. 65 | 66 | A Script immitates the way the top-level ruby context works, so a ruby file that 67 | was originally intended to be run from the top level, defining top-level 68 | constants and top-level methods, can also be run as a Script, and its top-level 69 | constants and top-level methods are wrapped in the script's scope. The 70 | difference between this behavior and simply wrapping the loaded definitions in 71 | an _anonymous_ module using Kernel.load(main_file, true) is that the 72 | top-level methods and top-level constants defined in the script are accessible 73 | using the Script instance. 74 | 75 | The top-level definitions of a Script can be accessed after it has been 76 | loaded, as follows: 77 | 78 | script.meth 79 | 80 | - Call a method defined using def meth or def self.meth in 81 | the script file. 82 | 83 | script::K 84 | 85 | - Access a class, module, or constant defined using K = val in the 86 | script file. 87 | 88 | An "input" can be passed to the script before loading. Simply call Script.new 89 | (or Script.load) with a block. The block is passed a single argument, the 90 | Script module, and executed before the files are loaded into the Script's 91 | scope. Setting a constant in this block makes the constant available to the 92 | script during loading. For example: 93 | 94 | script = Script.load("my-script.rb") { |script| script::INPUT = 3 } 95 | 96 | Note that all methods defined in the script file are both instance methods of 97 | the module and methods of the module instance (the effect of 98 | Module#module_function). So include-ing a Script module in a 99 | class will give instances of the class all the methods and constants defined in 100 | the script, and they will reference the instance's instance variables, 101 | rather than the Script module's instance variables. 102 | 103 | The Script class was inspired by Nobu Nokada's suggestion in 104 | http://ruby-talk.org/62727, in a thread (started in http://ruby-talk.org/62660) 105 | about how to use ruby script files as specifications of objects. 106 | 107 | === Legal and Contact Information 108 | 109 | Usable under the Ruby license. Copyright (C)2004 Joel VanderWerf. Questions to 110 | mailto:vjoel@users.sourceforge.net. 111 | -------------------------------------------------------------------------------- /work/reference/script-0.3/lib/script.rb: -------------------------------------------------------------------------------- 1 | # A module which is an instance of the Script class encapsulates in its scope 2 | # the top-level methods, top-level constants, and instance variables defined in 3 | # a ruby script file (and its subfiles) loaded by a ruby program. This allows 4 | # use of script files to define objects that can be loaded into a program in 5 | # much the same way that objects can be loaded from YAML or Marshal files. 6 | # 7 | # See intro.txt[link:files/intro_txt.html] for an overview. 8 | 9 | class Script < Module 10 | # The file with which the Script was instantiated. 11 | attr_reader :__main_file 12 | 13 | # The directory in which main_file is located, and relative to which 14 | # #load searches for files before falling back to Kernel#load. 15 | attr_reader :__dir 16 | 17 | # A hash that maps filename=>true for each file that has been 18 | # required locally by the script. This has the same semantics as $", 19 | # alias $LOADED_FEATURES, except that it is local to this script. 20 | attr_reader :__loaded_features 21 | 22 | class << self 23 | alias load new 24 | end 25 | 26 | # Creates new Script, and loads _main_file_ in the scope of the Script. If a 27 | # block is given, the script is passed to it before loading from the file, and 28 | # constants can be defined as inputs to the script. 29 | 30 | def initialize(main_file) # :yields: self 31 | extend ScriptModuleMethods 32 | @__main_file = File.expand_path(main_file) 33 | @__dir = File.dirname(@__main_file) 34 | @__loaded_features = {} 35 | 36 | yield self if block_given? 37 | load_in_module(main_file) 38 | end 39 | 40 | # Loads _file_ into this Script. Searches relative to the local dir, that is, 41 | # the dir of the file given in the original call to 42 | # Script.load(file), loads the file, if found, into this Script's 43 | # scope, and returns true. If the file is not found, falls back to 44 | # Kernel.load, which searches on $LOAD_PATH, loads the file, 45 | # if found, into global scope, and returns true. Otherwise, raises 46 | # LoadError. 47 | # 48 | # The _wrap_ argument is passed to Kernel.load in the fallback case, 49 | # when the file is not found locally. 50 | # 51 | # Typically called from within the main file to load additional sub files, or 52 | # from those sub files. 53 | 54 | def load(file, wrap = false) 55 | load_in_module(File.join(@__dir, file)) 56 | true 57 | rescue MissingFile 58 | super 59 | end 60 | 61 | # Analogous to Kernel#require. First tries the local dir, then falls 62 | # back to Kernel#require. Will load a given _feature_ only once. 63 | # 64 | # Note that extensions (*.so, *.dll) can be required in the global scope, as 65 | # usual, but not in the local scope. (This is not much of a limitation in 66 | # practice--you wouldn't want to load an extension more than once.) This 67 | # implementation falls back to Kernel#require when the argument is an 68 | # extension or is not found locally. 69 | 70 | def require(feature) 71 | unless @__loaded_features[feature] 72 | @__loaded_features[feature] = true 73 | file = File.join(@__dir, feature) 74 | file += ".rb" unless /\.rb$/ =~ file 75 | load_in_module(file) 76 | end 77 | rescue MissingFile 78 | @__loaded_features[feature] = false 79 | super 80 | end 81 | 82 | # Raised by #load_in_module, caught by #load and #require. 83 | class MissingFile < LoadError; end 84 | 85 | # Loads _file_ in this module's context. Note that \_\_FILE\_\_ and 86 | # \_\_LINE\_\_ work correctly in _file_. 87 | # Called by #load and #require; not normally called directly. 88 | 89 | def load_in_module(__file__) 90 | module_eval("@__script_scope ||= binding\n" + IO.read(__file__), 91 | File.expand_path(__file__), 0) 92 | # start numbering at 0 because of the extra line. 93 | # The extra line does nothing in sub-script files. 94 | rescue Errno::ENOENT 95 | if /#{__file__}$/ =~ $!.message # No extra locals in this scope. 96 | raise MissingFile, $!.message 97 | else 98 | raise 99 | end 100 | end 101 | 102 | def to_s # :nodoc: 103 | "#<#{self.class}:#{File.join(__dir, File.basename(__main_file))}>" 104 | end 105 | 106 | module ScriptModuleMethods 107 | # This is so that def meth... behaves like in Ruby's top-level 108 | # context. The implementation simply calls 109 | # Module#module_function(name). 110 | def method_added(name) # :nodoc: 111 | module_function(name) 112 | end 113 | 114 | attr_reader :__script_scope 115 | 116 | # Gets list of local vars in the script. Does not see local vars in files 117 | # loaded or required by that script. 118 | def __local_variables 119 | eval("local_variables", __script_scope) 120 | end 121 | 122 | # Gets value of local var in the script. Does not see local vars in files 123 | # loaded or required by that script. 124 | def __local_variable_get(name) 125 | eval(name.to_s, __script_scope) 126 | end 127 | end 128 | end 129 | -------------------------------------------------------------------------------- /work/reference/script-0.3/lib/~script.rb: -------------------------------------------------------------------------------- 1 | # A module which is an instance of the Script class encapsulates in its scope 2 | # the top-level methods, top-level constants, and instance variables defined in 3 | # a ruby script file (and its subfiles) loaded by a ruby program. This allows 4 | # use of script files to define objects that can be loaded into a program in 5 | # much the same way that objects can be loaded from YAML or Marshal files. 6 | # 7 | # See intro.txt[link:files/intro_txt.html] for an overview. 8 | 9 | class Script < Module 10 | # The file with which the Script was instantiated. 11 | attr_reader :__main_file 12 | 13 | # The directory in which main_file is located, and relative to which 14 | # #load searches for files before falling back to Kernel#load. 15 | attr_reader :__dir 16 | 17 | # A hash that maps filename=>true for each file that has been 18 | # required locally by the script. This has the same semantics as $", 19 | # alias $LOADED_FEATURES, except that it is local to this script. 20 | attr_reader :__loaded_features 21 | 22 | class << self 23 | alias load new 24 | end 25 | 26 | # Creates new Script, and loads _main_file_ in the scope of the Script. If a 27 | # block is given, the script is passed to it before loading from the file, and 28 | # constants can be defined as inputs to the script. 29 | 30 | def initialize(main_file) # :yields: self 31 | extend ScriptModuleMethods 32 | @__main_file = File.expand_path(main_file) 33 | @__dir = File.dirname(@__main_file) 34 | @__loaded_features = {} 35 | 36 | yield self if block_given? 37 | load_in_module(main_file) 38 | end 39 | 40 | # Loads _file_ into this Script. Searches relative to the local dir, that is, 41 | # the dir of the file given in the original call to 42 | # Script.load(file), loads the file, if found, into this Script's 43 | # scope, and returns true. If the file is not found, falls back to 44 | # Kernel.load, which searches on $LOAD_PATH, loads the file, 45 | # if found, into global scope, and returns true. Otherwise, raises 46 | # LoadError. 47 | # 48 | # The _wrap_ argument is passed to Kernel.load in the fallback case, 49 | # when the file is not found locally. 50 | # 51 | # Typically called from within the main file to load additional sub files, or 52 | # from those sub files. 53 | 54 | def load(file, wrap = false) 55 | load_in_module(File.join(@__dir, file)) 56 | true 57 | rescue MissingFile 58 | super 59 | end 60 | 61 | # Analogous to Kernel#require. First tries the local dir, then falls 62 | # back to Kernel#require. Will load a given _feature_ only once. 63 | # 64 | # Note that extensions (*.so, *.dll) can be required in the global scope, as 65 | # usual, but not in the local scope. (This is not much of a limitation in 66 | # practice--you wouldn't want to load an extension more than once.) This 67 | # implementation falls back to Kernel#require when the argument is an 68 | # extension or is not found locally. 69 | 70 | def require(feature) 71 | unless @__loaded_features[feature] 72 | @__loaded_features[feature] = true 73 | file = File.join(@__dir, feature) 74 | file += ".rb" unless /\.rb$/ =~ file 75 | load_in_module(file) 76 | end 77 | rescue MissingFile 78 | @__loaded_features[feature] = false 79 | super 80 | end 81 | 82 | # Raised by #load_in_module, caught by #load and #require. 83 | class MissingFile < LoadError; end 84 | 85 | # Loads _file_ in this module's context. Note that \_\_FILE\_\_ and 86 | # \_\_LINE\_\_ work correctly in _file_. 87 | # Called by #load and #require; not normally called directly. 88 | 89 | def load_in_module(__file__) 90 | module_eval("@__script_scope ||= proc{}\n" + IO.read(__file__), 91 | File.expand_path(__file__), 0) 92 | # start numbering at 0 because of the extra line. 93 | # The extra line does nothing in sub-script files. 94 | rescue Errno::ENOENT 95 | if /#{__file__}$/ =~ $!.message # No extra locals in this scope. 96 | raise MissingFile, $!.message 97 | else 98 | raise 99 | end 100 | end 101 | 102 | def to_s # :nodoc: 103 | "#<#{self.class}:#{File.join(__dir, File.basename(__main_file))}>" 104 | end 105 | 106 | module ScriptModuleMethods 107 | # This is so that def meth... behaves like in Ruby's top-level 108 | # context. The implementation simply calls 109 | # Module#module_function(name). 110 | def method_added(name) # :nodoc: 111 | module_function(name) 112 | end 113 | 114 | attr_reader :__script_scope 115 | 116 | # Gets list of local vars in the script. Does not see local vars in files 117 | # loaded or required by that script. 118 | def __local_variables 119 | eval("local_variables", __script_scope) 120 | end 121 | 122 | # Gets value of local var in the script. Does not see local vars in files 123 | # loaded or required by that script. 124 | def __local_variable_get(name) 125 | eval(name.to_s, __script_scope) 126 | end 127 | end 128 | end 129 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/rdoc-style.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | font-family: Verdana,Arial,Helvetica,sans-serif; 4 | font-size: 90%; 5 | margin: 0; 6 | margin-left: 40px; 7 | padding: 0; 8 | background: white; 9 | } 10 | 11 | h1,h2,h3,h4 { margin: 0; color: #efefef; background: transparent; } 12 | h1 { font-size: 150%; } 13 | h2,h3,h4 { margin-top: 1em; } 14 | 15 | a { background: #eef; color: #039; text-decoration: none; } 16 | a:hover { background: #039; color: #eef; } 17 | 18 | /* Override the base stylesheet's Anchor inside a table cell */ 19 | td > a { 20 | background: transparent; 21 | color: #039; 22 | text-decoration: none; 23 | } 24 | 25 | /* and inside a section title */ 26 | .section-title > a { 27 | background: transparent; 28 | color: #eee; 29 | text-decoration: none; 30 | } 31 | 32 | /* === Structural elements =================================== */ 33 | 34 | div#index { 35 | margin: 0; 36 | margin-left: -40px; 37 | padding: 0; 38 | font-size: 90%; 39 | } 40 | 41 | 42 | div#index a { 43 | margin-left: 0.7em; 44 | } 45 | 46 | div#index .section-bar { 47 | margin-left: 0px; 48 | padding-left: 0.7em; 49 | background: #ccc; 50 | font-size: small; 51 | } 52 | 53 | 54 | div#classHeader, div#fileHeader { 55 | width: auto; 56 | color: white; 57 | padding: 0.5em 1.5em 0.5em 1.5em; 58 | margin: 0; 59 | margin-left: -40px; 60 | border-bottom: 3px solid #006; 61 | } 62 | 63 | div#classHeader a, div#fileHeader a { 64 | background: inherit; 65 | color: white; 66 | } 67 | 68 | div#classHeader td, div#fileHeader td { 69 | background: inherit; 70 | color: white; 71 | } 72 | 73 | 74 | div#fileHeader { 75 | background: #057; 76 | } 77 | 78 | div#classHeader { 79 | background: #048; 80 | } 81 | 82 | 83 | .class-name-in-header { 84 | font-size: 180%; 85 | font-weight: bold; 86 | } 87 | 88 | 89 | div#bodyContent { 90 | padding: 0 1.5em 0 1.5em; 91 | } 92 | 93 | div#description { 94 | padding: 0.5em 1.5em; 95 | background: #efefef; 96 | border: 1px dotted #999; 97 | } 98 | 99 | div#description h1,h3,h3,h4,h5,h6 { 100 | color: black; 101 | background: transparent; 102 | } 103 | 104 | div#validator-badges { 105 | text-align: center; 106 | } 107 | div#validator-badges img { border: 0; } 108 | 109 | div#copyright { 110 | color: #333; 111 | background: #efefef; 112 | font: 0.75em sans-serif; 113 | margin-top: 5em; 114 | margin-bottom: 0; 115 | padding: 0.5em 2em; 116 | } 117 | 118 | 119 | /* === Classes =================================== */ 120 | 121 | table.header-table { 122 | color: white; 123 | font-size: small; 124 | } 125 | 126 | .type-note { 127 | font-size: small; 128 | color: #DEDEDE; 129 | } 130 | 131 | .xxsection-bar { 132 | background: #eee; 133 | color: #333; 134 | padding: 3px; 135 | } 136 | 137 | .section-bar { 138 | color: #333; 139 | border-bottom: 1px solid #999; 140 | margin-left: -20px; 141 | } 142 | 143 | 144 | .section-title { 145 | background: #79a; 146 | color: #eee; 147 | padding: 3px; 148 | margin-top: 2em; 149 | margin-left: -30px; 150 | border: 1px solid #999; 151 | } 152 | 153 | .top-aligned-row { vertical-align: top } 154 | .bottom-aligned-row { vertical-align: bottom } 155 | 156 | /* --- Context section classes ----------------------- */ 157 | 158 | .context-row { } 159 | .context-item-name { font-family: monospace; font-weight: bold; color: black; } 160 | .context-item-value { font-size: small; color: #448; } 161 | .context-item-desc { color: #333; padding-left: 2em; } 162 | 163 | /* --- Method classes -------------------------- */ 164 | .method-detail { 165 | background: #efefef; 166 | padding: 0; 167 | margin-top: 0.5em; 168 | margin-bottom: 1em; 169 | border: 1px dotted #ccc; 170 | } 171 | .method-heading { 172 | color: black; 173 | background: #ccc; 174 | border-bottom: 1px solid #666; 175 | padding: 0.2em 0.5em 0 0.5em; 176 | } 177 | .method-signature { color: black; background: inherit; } 178 | .method-name { font-weight: bold; } 179 | .method-args { font-style: italic; } 180 | .method-description { padding: 0 0.5em 0 0.5em; } 181 | 182 | /* --- Source code sections -------------------- */ 183 | 184 | a.source-toggle { font-size: 90%; } 185 | div.method-source-code { 186 | background: #262626; 187 | color: #ffdead; 188 | margin: 1em; 189 | padding: 0.5em; 190 | border: 1px dashed #999; 191 | overflow: hidden; 192 | } 193 | 194 | div.method-source-code pre { color: #ffdead; overflow: hidden; } 195 | 196 | /* --- Ruby keyword styles --------------------- */ 197 | 198 | .standalone-code { background: #221111; color: #ffdead; overflow: hidden; } 199 | 200 | .ruby-constant { color: #7fffd4; background: transparent; } 201 | .ruby-keyword { color: #00ffff; background: transparent; } 202 | .ruby-ivar { color: #eedd82; background: transparent; } 203 | .ruby-operator { color: #00ffee; background: transparent; } 204 | .ruby-identifier { color: #ffdead; background: transparent; } 205 | .ruby-node { color: #ffa07a; background: transparent; } 206 | .ruby-comment { color: #b22222; font-weight: bold; background: transparent; } 207 | .ruby-regexp { color: #ffa07a; background: transparent; } 208 | .ruby-value { color: #7fffd4; background: transparent; } -------------------------------------------------------------------------------- /work/reference/script-0.3/lib/script-1.rb: -------------------------------------------------------------------------------- 1 | # A module which is an instance of the Script class encapsulates in its scope 2 | # the top-level methods, top-level constants, and instance variables defined in 3 | # a ruby script file (and its subfiles) loaded by a ruby program. This allows 4 | # use of script files to define objects that can be loaded into a program in 5 | # much the same way that objects can be loaded from YAML or Marshal files. 6 | # 7 | # See intro.txt[link:files/intro_txt.html] for an overview. 8 | 9 | class Script < Module 10 | # The file with which the Script was instantiated. 11 | attr_reader :__main_file 12 | 13 | # The directory in which main_file is located, and relative to which 14 | # #load searches for files before falling back to Kernel#load. 15 | attr_reader :__dir 16 | 17 | # A hash that maps filename=>true for each file that has been 18 | # required locally by the script. This has the same semantics as $", 19 | # alias $LOADED_FEATURES, except that it is local to this script. 20 | attr_reader :__loaded_features 21 | 22 | class << self 23 | alias load new 24 | end 25 | 26 | # Creates new Script, and loads _main_file_ in the scope of the Script. If a 27 | # block is given, the script is passed to it before loading from the file, and 28 | # constants can be defined as inputs to the script. 29 | 30 | def initialize(main_file) # :yields: self 31 | extend ScriptModuleMethods 32 | @__main_file = File.expand_path(main_file) 33 | @__dir = File.dirname(@__main_file) 34 | @__loaded_features = {} 35 | 36 | yield self if block_given? 37 | load_in_module(main_file) 38 | end 39 | 40 | # Loads _file_ into this Script. Searches relative to the local dir, that is, 41 | # the dir of the file given in the original call to 42 | # Script.load(file), loads the file, if found, into this Script's 43 | # scope, and returns true. If the file is not found, falls back to 44 | # Kernel.load, which searches on $LOAD_PATH, loads the file, 45 | # if found, into global scope, and returns true. Otherwise, raises 46 | # LoadError. 47 | # 48 | # The _wrap_ argument is passed to Kernel.load in the fallback case, 49 | # when the file is not found locally. 50 | # 51 | # Typically called from within the main file to load additional sub files, or 52 | # from those sub files. 53 | 54 | def load(file, wrap = false) 55 | load_in_module(File.join(@__dir, file)) 56 | true 57 | rescue MissingFile 58 | super 59 | end 60 | 61 | # Analogous to Kernel#require. First tries the local dir, then falls 62 | # back to Kernel#require. Will load a given _feature_ only once. 63 | # 64 | # Note that extensions (*.so, *.dll) can be required in the global scope, as 65 | # usual, but not in the local scope. (This is not much of a limitation in 66 | # practice--you wouldn't want to load an extension more than once.) This 67 | # implementation falls back to Kernel#require when the argument is an 68 | # extension or is not found locally. 69 | 70 | def require(feature) 71 | unless @__loaded_features[feature] 72 | @__loaded_features[feature] = true 73 | file = File.join(@__dir, feature) 74 | file += ".rb" unless /\.rb$/ =~ file 75 | load_in_module(file) 76 | end 77 | rescue MissingFile 78 | @__loaded_features[feature] = false 79 | super 80 | end 81 | 82 | # Raised by #load_in_module, caught by #load and #require. 83 | class MissingFile < LoadError; end 84 | 85 | # Loads _file_ in this module's context. Note that \_\_FILE\_\_ and 86 | # \_\_LINE\_\_ work correctly in _file_. 87 | # Called by #load and #require; not normally called directly. 88 | 89 | def load_in_module(file) 90 | @__script_file = File.expand_path(file) 91 | _load_in_module 92 | rescue Errno::ENOENT => e 93 | if /#{file}$/ =~ e.message 94 | raise MissingFile, e.message 95 | else 96 | raise 97 | end 98 | end 99 | 100 | def _load_in_module # :nodoc: 101 | # No extra locals in this scope. 102 | module_eval("@__script_scope = proc{}\n" + IO.read(@__script_file), 103 | @__script_file, 0) # start numbering at 0 because of the extra line. 104 | end 105 | 106 | def to_s # :nodoc: 107 | "#<#{self.class}:#{File.join(__dir, File.basename(__main_file))}>" 108 | end 109 | 110 | module ScriptModuleMethods 111 | # This is so that def meth... behaves like in Ruby's top-level 112 | # context. The implementation simply calls 113 | # Module#module_function(name). 114 | def method_added(name) # :nodoc: 115 | module_function(name) 116 | end 117 | 118 | # Call this method on your script to make local scope, including local vars, 119 | # accessible via the script object. See scope example. This can be 120 | # called within the script itself or it can be called on the script by 121 | # the loading program. 122 | def accessible_scope! 123 | extend AccessibleScope 124 | end 125 | end 126 | 127 | module AccessibleScope 128 | attr_reader :__script_scope 129 | 130 | def locals 131 | eval("local_variables", __script_scope) 132 | end 133 | 134 | def local(name) 135 | unless name.is_a?(Symbol) 136 | raise ArgumentError, "Variable name must be symbol, not #{name.class}" 137 | end 138 | eval(name.to_s, __script_scope) 139 | end 140 | end 141 | end 142 | -------------------------------------------------------------------------------- /work/consider/script.rb: -------------------------------------------------------------------------------- 1 | # A module which is an instance of the Script class encapsulates in its scope 2 | # the top-level methods, top-level constants, and instance variables defined in 3 | # a ruby script file (and its subfiles) loaded by a ruby program. This allows 4 | # use of script files to define objects that can be loaded into a program in 5 | # much the same way that objects can be loaded from YAML or Marshal files. 6 | # 7 | # See intro.txt[link:files/intro_txt.html] for an overview. 8 | 9 | class Script < Module 10 | # The file with which the Script was instantiated. 11 | attr_reader :main_file 12 | 13 | # The directory in which main_file is located, and relative to which 14 | # #load searches for files before falling back to Kernel#load. 15 | #attr_reader :dir 16 | 17 | # An array of paths to search for scripts. This has the same 18 | # semantics as $:, alias $LOAD_PATH, except 19 | # that it is local to this script. The path of the current 20 | # script is added automatically (equivalent to '.') 21 | attr_reader :load_path 22 | 23 | # A hash that maps filename=>true for each file that has been 24 | # required locally by the script. This has the same semantics as $", 25 | # alias $LOADED_FEATURES, except that it is local to this script. 26 | attr_reader :loaded_features 27 | 28 | class << self 29 | alias load new 30 | end 31 | 32 | # Creates new Script, and loads _main_file_ in the scope of the Script. If a 33 | # block is given, the script is passed to it before loading from the file, and 34 | # constants can be defined as inputs to the script. 35 | 36 | def initialize(main_file, options=nil, &block) 37 | extend self 38 | 39 | options ||= {} 40 | 41 | @main_file = File.expand_path(main_file) 42 | @load_path = options[:load_path] || [] 43 | #@load_path |= [File.dirname(@main_file)] # before or after? 44 | @loaded_features = options[:loaded_features] || {} 45 | 46 | # TODO In order to load/require at the instance level. 47 | # This needs to be in a separate namespace however 48 | # b/c it can interfere with what is expected. 49 | #[ :require, :load ].each{ |meth| 50 | # m = method(meth) 51 | # define_method(meth) do |*args| m.call(*args) end 52 | #} 53 | 54 | module_eval(&block) if block 55 | extend self 56 | 57 | load_in_module(main_file) 58 | end 59 | 60 | # Lookup feature in load path. 61 | 62 | def load_path_lookup(feature) 63 | paths = File.join('{' + @load_path.join(',') + '}', feature + '{,.rb,.rbs}') 64 | files = Dir.glob(paths) 65 | match = files.find{ |f| ! @loaded_features.include?(f) } 66 | return match 67 | end 68 | 69 | # Loads _file_ into this Script. Searches relative to the local dir, that is, 70 | # the dir of the file given in the original call to 71 | # Script.load(file), loads the file, if found, into this Script's 72 | # scope, and returns true. If the file is not found, falls back to 73 | # Kernel.load, which searches on $LOAD_PATH, loads the file, 74 | # if found, into global scope, and returns true. Otherwise, raises 75 | # LoadError. 76 | # 77 | # The _wrap_ argument is passed to Kernel.load in the fallback case, 78 | # when the file is not found locally. 79 | # 80 | # Typically called from within the main file to load additional sub files, or 81 | # from those sub files. 82 | # 83 | #-- 84 | # TODO Need to add load_path lookup. 85 | #++ 86 | 87 | def load(file, wrap = false) 88 | load_in_module(File.join(@dir, file)) 89 | true 90 | rescue MissingFile 91 | super 92 | end 93 | 94 | # Analogous to Kernel#require. First tries the local dir, then falls 95 | # back to Kernel#require. Will load a given _feature_ only once. 96 | # 97 | # Note that extensions (*.so, *.dll) can be required in the global scope, as 98 | # usual, but not in the local scope. (This is not much of a limitation in 99 | # practice--you wouldn't want to load an extension more than once.) This 100 | # implementation falls back to Kernel#require when the argument is an 101 | # extension or is not found locally. 102 | # 103 | #-- 104 | # This was using load_in_module rather than include_script. Maybe is still should 105 | # and one should have to call include_script instead? Think about this. 106 | #++ 107 | 108 | def require(feature) 109 | file = load_path_lookup(feature) 110 | return super unless file 111 | begin 112 | @loaded_features[file] = true 113 | load_in_module(file) 114 | rescue MissingFile 115 | @loaded_features[file] = false 116 | super 117 | end 118 | end 119 | 120 | # Raised by #load_in_module, caught by #load and #require. 121 | class MissingFile < LoadError; end 122 | 123 | # Loads _file_ in this module's context. Note that \_\_FILE\_\_ and 124 | # \_\_LINE\_\_ work correctly in _file_. 125 | # Called by #load and #require; not normally called directly. 126 | 127 | def load_in_module(file) 128 | module_eval(IO.read(file), File.expand_path(file)) 129 | rescue Errno::ENOENT => e 130 | if /#{file}$/ =~ e.message 131 | raise MissingFile, e.message 132 | else 133 | raise 134 | end 135 | end 136 | 137 | def include_script(file) 138 | include self.class.new(file, :load_path=>load_path, :loaded_features=>loaded_features) 139 | rescue Errno::ENOENT => e 140 | if /#{file}$/ =~ e.message 141 | raise MissingFile, e.message 142 | else 143 | raise 144 | end 145 | end 146 | 147 | # 148 | def include(*mods) 149 | super 150 | extend self 151 | end 152 | 153 | def to_s # :nodoc: 154 | "#<#{self.class}:#{main_file}>" 155 | end 156 | 157 | end #class Script 158 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = Capsule 2 | 3 | {Homepage}[home: http://rubyworks.github.com/capsule] | 4 | {Documentation}[http://rubydoc.info/gems/capsule] | 5 | {Development}[http://github.com/rubyworks/capsule] | 6 | {Report Issue}[http://github.com/rubyworks/capsule/issues] | 7 | {Mailing List}[http://groups.google.com/group/rubyworks-mailinglist] | 8 | {IRC Channel}[irc://irc.freenode.net/rubyworks] 9 | 10 | {}[http://travis-ci.org/rubyworks/capsule] 11 | 12 | 13 | == Description 14 | 15 | Capsule is a subclass of Module. A module which is an instance of the Capsule 16 | class encapsulates in its scope the top-level methods, top-level constants, and 17 | instance variables defined in a Ruby script (and its dependent files) 18 | loaded by a Ruby program. This allows use of script files to define objects that 19 | can be loaded into a program in much the same way that objects can be loaded 20 | from YAML or Marshaled files. There is also an autoimport method which functions 21 | like Ruby's autoload but based on is Capsule.load. 22 | 23 | 24 | == Synopsis 25 | 26 | To encapsulate a script in a Capsule: 27 | 28 | myscript = Capsule.new('myscript.rb') 29 | 30 | If the script is in Ruby's $LOAD_PATH, then you can use +Capsule.load+. 31 | 32 | myscript = Capsule.load('myscript.rb') 33 | 34 | Here is an example: 35 | 36 | # myscript.rb 37 | 38 | VALUE = [1,2,3] 39 | 40 | def run 41 | puts "#{self} is running." 42 | end 43 | 44 | And the encapsulating program: 45 | 46 | # program.rb: 47 | 48 | require 'capsule' 49 | 50 | myscript = Capsule.load("myscript.rb") 51 | 52 | p myscript::VALUE 53 | 54 | myscript.run 55 | 56 | Running `program.rb` will result in: 57 | 58 | $ ruby program.rb 59 | [1, 2, 3] 60 | # is running. 61 | 62 | 63 | == Usage 64 | 65 | Capsule modules are instantiated with Capsule.new(main_file) or the alias 66 | Capsule.load(main_file). All the top-level constants and top-level 67 | methods that are defined in the +main_file+ and its dependent local files (see 68 | below) are scoped in the same Capsule module, and are thereby available to the 69 | calling program. 70 | 71 | The +main_file+ can load or require other files with +load+ and +require+, as 72 | usual. These methods, in the Capsule context, add some behavior to the +Kernel+ 73 | +load+ and +require+ methods: Capsule#load and Capsule#require 74 | first search for files relative to the +main_file+'s dir. Files loaded in this 75 | way ("dependent local files") are treated like the script file itself: top-level 76 | definitions are added to the script module that is returned by +load+ or 77 | +require+. 78 | 79 | Both Capsule#load and Capsule#require fall back to the Kernel 80 | versions if the file is not found locally. Hence, other ruby libraries can be 81 | loaded and required as usual, assuming their names do not conflict with local 82 | file names. Definitions from those files go into the usual scope (typically 83 | global). The normal ruby +load+ and +require+ behavior can be forced by calling 84 | Kernel.load and Kernel.require. 85 | 86 | A Capsule immitates the way the top-level ruby context works, so a ruby file that 87 | was originally intended to be run from the top level, defining top-level 88 | constants and top-level methods, can also be run as a Capsule, and its top-level 89 | constants and top-level methods are wrapped in the script's scope. The 90 | difference between this behavior and simply wrapping the loaded definitions in 91 | an _anonymous_ module using Kernel.load(main_file, true) is that the 92 | top-level methods and top-level constants defined in the script are accessible 93 | using the Capsule instance. 94 | 95 | The top-level definitions of a Capsule can be accessed after it has been 96 | loaded, as follows: 97 | 98 | capsule.meth 99 | 100 | - Call a method defined using def meth or def self.meth in 101 | the script file. 102 | 103 | capsule::K 104 | 105 | - Access a class, module, or constant defined using K = val in the 106 | script file. 107 | 108 | An "input" can be passed to the script before loading. Simply call Capsule.new 109 | (or Capsule.load) with a block. The block is passed a single argument, the 110 | Capsule module, and executed before the files are loaded into the Capsule's 111 | scope. Setting a constant in this block makes the constant available to the 112 | script during loading. For example: 113 | 114 | script = Capsule.load("my-script.rb") { |capsule| capsule::INPUT = 3 } 115 | 116 | Note that all methods defined in the script file are both instance methods of 117 | the module and methods of the module instance (the effect of 118 | Module#module_function). So include-ing a Capsule module in a 119 | class will give instances of the class all the methods and constants defined in 120 | the script, and they will reference the instance's instance variables, 121 | rather than the Capsule module's instance variables. 122 | 123 | The Capsule class was inspired by Nobu Nokada's suggestion in 124 | http://ruby-talk.org/62727, in a thread (started in http://ruby-talk.org/62660) 125 | about how to use ruby script files as specifications of objects. 126 | 127 | 128 | == Installation 129 | 130 | To install with RubyGems simply open a console and type: 131 | 132 | gem install capsule 133 | 134 | Local installation requires Setup.rb (gem install setup), 135 | then download the tarball package and type: 136 | 137 | tar -xvzf capsule-1.0.0.tgz 138 | cd capsule-1.0.0 139 | sudo setup.rb all 140 | 141 | Windows users use 'ruby setup.rb all'. 142 | 143 | 144 | == Legal 145 | 146 | (FreeBSD License) 147 | 148 | Copyright (c) 2007 Thomas Sawyer 149 | 150 | Unless otherwise agreed upon by the copyright holder, this program is 151 | ditributed under the terms of the BSD-2-Clause license. 152 | 153 | Capsule is based on Joel VanderWerf's Script library. 154 | 155 | Copyright (c) 2004 Joel VanderWerf 156 | 157 | See COPYING.rdoc file for details. 158 | -------------------------------------------------------------------------------- /lib/capsule.rb: -------------------------------------------------------------------------------- 1 | #require 'rbconfig' 2 | require 'capsule/autoimport' 3 | 4 | # A module which is an instance of the Capsule class encapsulates in its scope 5 | # the top-level methods, top-level constants, and instance variables defined in 6 | # a ruby script file (and its subfiles) loaded by a ruby program. This allows 7 | # use of script files to define objects that can be loaded into a program in 8 | # much the same way that objects can be loaded from YAML or Marshal files. 9 | # 10 | # See intro.txt[link:files/intro_txt.html] for an overview. 11 | # 12 | class Capsule < Module 13 | 14 | #DLEXT = Config::CONFIG['DLEXT'] 15 | 16 | # Ruby script extensions to automatically try when searching for 17 | # a script on the load_path. 18 | 19 | SUFFIXES = ['.rb', '.rbs'] #, '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar'] 20 | 21 | # The script file with which the import was instantiated. 22 | 23 | attr_reader :main_file 24 | 25 | # The directory in which main_file is located, and relative to which 26 | # #load searches for files before falling back to Kernel#load. 27 | #attr_reader :dir 28 | 29 | # An array of paths to search for scripts. This has the same 30 | # semantics as $:, alias $LOAD_PATH, except 31 | # that it is local to this script. The path of the current 32 | # script is added automatically. 33 | 34 | attr_reader :load_path 35 | 36 | # A hash that maps filename=>true for each file that has been 37 | # required locally by the script. This has the same semantics as $", 38 | # alias $LOADED_FEATURES, except that it is local to this script. 39 | 40 | attr_reader :loaded_features 41 | 42 | # As with #new but will search Ruby's $LOAD_PATH first. 43 | # This will also try `.rb` extensions, like require does. 44 | 45 | def self.load(main_file, options=nil, &block) 46 | file = nil 47 | $LOAD_PATH.each do |path| 48 | file = File.join(path, main_file) 49 | break if file = File.file?(file) 50 | break if file = Dir.glob(file + '{' + SUFFIXES.join(',') + '}').first 51 | end 52 | new(file || main_file, options=nil, &block) 53 | end 54 | 55 | # Creates new Capsule, and loads _main_file_ in the scope of the script. If a 56 | # block is given, the script is passed to it before loading from the file, and 57 | # constants can be defined as inputs to the script. 58 | 59 | def initialize(main_file, options=nil, &block) 60 | options ||= {} 61 | 62 | @main_file = File.expand_path(main_file) 63 | 64 | @load_path = options[:load_path] || [] 65 | @loaded_features = options[:loaded_features] || {} 66 | 67 | @extend = true # default 68 | @extend = options[:extend] if options.key?(:extend) 69 | 70 | ## add script's path to load_path 71 | ## TODO: should we be doing this? 72 | @load_path |= [File.dirname(@main_file)] 73 | 74 | ## if @extend (the default) module extends itself 75 | extend self if @extend 76 | 77 | module_eval(&block) if block 78 | 79 | load_in_module(main_file) 80 | end 81 | 82 | # Lookup feature in load path. 83 | 84 | def load_path_lookup(feature) 85 | paths = File.join('{' + @load_path.join(',') + '}', feature + '{' + SUFFIXES + '}') 86 | files = Dir.glob(paths) 87 | match = files.find{ |f| ! @loaded_features.include?(f) } 88 | return match 89 | end 90 | 91 | # Loads _file_ into the capsule. Searches relative to the local dir, that is, 92 | # the dir of the file given in the original call to 93 | # Capsule.load(file), loads the file, if found, into this Capsule's 94 | # scope, and returns true. If the file is not found, falls back to 95 | # Kernel.load, which searches on $LOAD_PATH, loads the file, 96 | # if found, into global scope, and returns true. Otherwise, raises 97 | # LoadError. 98 | # 99 | # The _wrap_ argument is passed to Kernel.load in the fallback case, 100 | # when the file is not found locally. 101 | # 102 | # Typically called from within the main file to load additional sub files, or 103 | # from those sub files. 104 | 105 | def load(file, wrap = false) 106 | file = load_path_lookup(feature) 107 | return super unless file 108 | load_in_module(file) #File.join(@dir, file)) 109 | true 110 | rescue MissingFile 111 | super 112 | end 113 | 114 | # Analogous to Kernel#require. First tries the local dir, then falls 115 | # back to Kernel#require. Will load a given _feature_ only once. 116 | # 117 | # Note that extensions (*.so, *.dll) can be required in the global scope, as 118 | # usual, but not in the local scope. (This is not much of a limitation in 119 | # practice--you wouldn't want to load an extension more than once.) This 120 | # implementation falls back to Kernel#require when the argument is an 121 | # extension or is not found locally. 122 | # 123 | #-- 124 | # TODO: Should this be using #include_script instead? 125 | #++ 126 | 127 | def require(feature) 128 | file = load_path_lookup(feature) 129 | return super unless file 130 | begin 131 | @loaded_features[file] = true 132 | load_in_module(file) 133 | rescue MissingFile 134 | @loaded_features[file] = false 135 | super 136 | end 137 | end 138 | 139 | # Checks the class of each +mods+. If a String, then calls #include_script, 140 | # otherwise behaves like normal #include. 141 | 142 | def include(*mods) 143 | mods.reverse_each do |mod| 144 | case mod 145 | when String 146 | include_script(mod) 147 | else 148 | super(mod) 149 | extend self if @extend 150 | end 151 | end 152 | end 153 | 154 | # Create a new Capsule for a script and include it into the current capsule. 155 | 156 | def include_script(file) 157 | include self.class.new(file, :load_path=>load_path, :loaded_features=>loaded_features, :extend=>false) 158 | rescue Errno::ENOENT => e 159 | if /#{file}$/ =~ e.message 160 | raise MissingFile, e.message 161 | else 162 | raise 163 | end 164 | extend self if @extend 165 | end 166 | 167 | # Loads _file_ in this module's context. Note that \_\_FILE\_\_ and 168 | # \_\_LINE\_\_ work correctly in _file_. 169 | # Called by #load and #require; not normally called directly. 170 | 171 | def load_in_module(file) 172 | module_eval(IO.read(file), File.expand_path(file)) 173 | rescue Errno::ENOENT => e 174 | if /#{file}$/ =~ e.message 175 | raise MissingFile, e.message 176 | else 177 | raise 178 | end 179 | end 180 | 181 | # Give inspection of Capsule with script file name. 182 | 183 | def inspect # :nodoc: 184 | "#<#{self.class}:#{main_file}>" 185 | end 186 | 187 | # Raised by #load_in_module, caught by #load and #require. 188 | 189 | class MissingFile < ::LoadError; end 190 | 191 | end 192 | 193 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/files/sim_rb.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | File: sim.rb 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 |

sim.rb

51 | 52 | 53 | 54 | 56 | 57 | 58 | 59 | 60 | 61 |
Path:sim.rb 55 |
Last Update:Sat May 15 19:19:13 PDT 2004
62 |
63 | 64 | 65 |
66 | 67 | 68 |
69 | 70 |
71 |

72 | A module which is an instance of the Script class encapsulates in its scope 74 | the top-level methods, top-level constants, and instance variables defined 75 | in a ruby script file (and its subfiles) loaded by a ruby program. This 76 | allows use of script files to define objects that can be loaded into a 77 | program in much the same way that objects can be loaded from YAML or 78 | Marshal files. 79 |

80 |

81 | Script modules are instantiated with 82 | Script.new(main_file) 83 | or the alias Script.load(main_file). The 85 | main_file can load or require other dependent files with Script#load or Script#require, using path 88 | names relative to main_file’s dir. All the top-level 89 | constants and top-level methods that are defined in the main_file 90 | and dependent local files are scoped in the same Script module. 92 |

93 |

94 | Both Script#load and 95 | Script#require fall 96 | back to the Kernel versions if the file is not found locally. Hence, other 97 | ruby libraries can be loaded and required as usual, assuming their names do 98 | not conflict with local file names. Definitions from those files go into 99 | the usual scope (typically global). 100 |

101 |

102 | A Script immitates the way the 103 | top-level ruby context works, so a ruby file that was originally intended 104 | to be run from the top level, defining top-level constants and top-level 105 | methods, can also be run as a Script, 106 | and its top-level constants and top-level methods are wrapped in the 107 | script’s scope. The difference between this behavior and simply 108 | wrapping in an anonymous module using Kernel.load(main_file, true) 109 | is that the top-level methods and top-level constants defined in the script 110 | are accessible using the Script 111 | instance. 112 |

113 |

114 | The top-level definitions of a Script 115 | can be accessed after it has been loaded, as follows: 116 |

117 |

118 | script.meth 119 |

120 |
    121 |
  • Call a method defined using def meth in the script file. 122 | 123 |
  • 124 |
125 |

126 | script::K 127 |

128 |
    129 |
  • Access a constant defined using K = val in the script file. 130 | 131 |
  • 132 |
133 |

134 | An "input" can be passed to the script before loading. Simply 135 | call Script.new (or Script.load) with a block. The 137 | block is passed a single argument, the Script module, and executed before the 139 | files are loaded into the Script’s scope. Setting a constant 141 | in this block makes the constant available to the script during loading. 142 | For example: 143 |

144 |

145 | script = Script.load("my-script.rb") 147 | { |script| script::INPUT = 3 } 148 |

149 |

150 | Note that all methods defined in the script file are both instance methods 151 | of the module and methods of the module instance (the effect of 152 | Module#module_function). So include-ing a Script module in a class will give 154 | instances of the class all the methods and constants defined in the script, 155 | except that they will reference the instance’s instance variables, 156 | rather than the Script module’s 157 | instance variables. 158 |

159 |

160 | The Script class was inspired by Nobu 161 | Nokada’s suggestion in ruby-talk:62727, in a thread (started in 162 | ruby-talk:62660) about how to use ruby script files as specifications of 163 | objects. 164 |

165 | 166 |
167 | 168 | 169 | 170 |
171 |

Constants

172 | 173 |
174 | 175 | 176 | 177 | 178 | 179 | 180 |
INPUT=3 }
181 |
182 |
183 | 184 | 185 | 186 | 187 |
188 |

Classes and Modules

189 | 190 | Class Script
191 |   ::Class Script::MissingFile
192 | 193 |
194 | 195 |
196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 |
206 | 207 | 208 |
209 |

[Validate]

210 |
211 | 212 | 213 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/files/intro_txt.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | File: intro.txt 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 |

intro.txt

51 | 52 | 53 | 54 | 56 | 57 | 58 | 59 | 60 | 61 |
Path:intro.txt 55 |
Last Update:Sun Dec 05 18:40:10 PST 2004
62 |
63 | 64 | 65 |
66 | 67 | 68 | 69 |
70 | 71 |
72 |

Script

73 |

74 | Script is a subclass of Module. A module which is an instance of 76 | the Script class encapsulates in its 77 | scope the top-level methods, top-level constants, and instance variables 78 | defined in a ruby script file (and its dependent files) loaded by a ruby 79 | program. This allows use of script files to define objects that can be 80 | loaded into a program in much the same way that objects can be loaded from 81 | YAML or Marshal files. There is also an autoscript method which functions 82 | like ruby’s autoload but based on is Script.load (see 84 | examples/autoload-example.rb). 85 |

86 |

87 | The implementation of Script is very 88 | simple, consisting of about 60 lines of code. 89 |

90 |

Synopsis

91 |
    92 |
  • program.rb: 93 | 94 |
     95 |   require 'script'
     96 |   my_script = Script.load("my-script.rb")
     97 |   p my_script::VALUE
     98 |   my_script.run
     99 | 
    100 |
  • 101 |
102 |
    103 |
  • my-script.rb: 104 | 105 |
    106 |   VALUE = [1,2,3]
    107 |   def run
    108 |     puts "#{self} running."
    109 |   end
    110 | 
    111 |
  • 112 |
113 |
    114 |
  • output: 115 | 116 |
    117 |   $ ruby program.rb
    118 |   [1, 2, 3]
    119 |   #<Script:/tmp/my-script.rb> running.
    120 | 
    121 |
  • 122 |
123 |

Usage

124 |

125 | Script modules are instantiated with 126 | Script.new(main_file) 127 | or the alias Script.load(main_file). All 129 | the top-level constants and top-level methods that are defined in the 130 | main_file and its dependent local files (see below) are scoped in 131 | the same Script module, and are 132 | thereby available to the calling program. 133 |

134 |

135 | The main_file can load or require other files with load 136 | and require, as usual. These methods, in the Script context, add some behavior to the 138 | Kernel load and 139 | require methods: Script#load and Script#require first search 142 | for files relative to the main_file’s dir. Files loaded in 143 | this way ("dependent local files") are treated like the script 144 | file itself: top-level definitions are added to the script module that is 145 | returned by load or require. 146 |

147 |

148 | Both Script#load and 149 | Script#require fall 150 | back to the Kernel versions if the 151 | file is not found locally. Hence, other ruby libraries can be loaded and 152 | required as usual, assuming their names do not conflict with local file 153 | names. Definitions from those files go into the usual scope (typically 154 | global). The normal ruby load and require behavior can be 155 | forced by calling Kernel.load and Kernel.require. 156 |

157 |

158 | A Script immitates the way the 159 | top-level ruby context works, so a ruby file that was originally intended 160 | to be run from the top level, defining top-level constants and top-level 161 | methods, can also be run as a Script, 162 | and its top-level constants and top-level methods are wrapped in the 163 | script’s scope. The difference between this behavior and simply 164 | wrapping the loaded definitions in an anonymous module using 165 | Kernel.load(main_file, true) is that the top-level methods and 166 | top-level constants defined in the script are accessible using the Script instance. 168 |

169 |

170 | The top-level definitions of a Script 171 | can be accessed after it has been loaded, as follows: 172 |

173 |

174 | script.meth 175 |

176 |
    177 |
  • Call a method defined using def meth or def self.meth in 178 | the script file. 179 | 180 |
  • 181 |
182 |

183 | script::K 184 |

185 |
    186 |
  • Access a class, module, or constant defined using K = val in the 187 | script file. 188 | 189 |
  • 190 |
191 |

192 | An "input" can be passed to the script before loading. Simply 193 | call Script.new (or Script.load) with a block. The 195 | block is passed a single argument, the Script module, and executed before the 197 | files are loaded into the Script’s scope. Setting a constant 199 | in this block makes the constant available to the script during loading. 200 | For example: 201 |

202 |
203 |   script = Script.load("my-script.rb") { |script| script::INPUT = 3 }
204 | 
205 |

206 | Note that all methods defined in the script file are both instance methods 207 | of the module and methods of the module instance (the effect of 208 | Module#module_function). So include-ing a Script module in a class will give 210 | instances of the class all the methods and constants defined in the script, 211 | and they will reference the instance’s instance variables, rather 212 | than the Script module’s 213 | instance variables. 214 |

215 |

216 | The Script class was inspired by Nobu 217 | Nokada’s suggestion in ruby-talk.org/62727, in a thread 219 | (started in ruby-talk.org/62660) 220 | about how to use ruby script files as specifications of objects. 221 |

222 |

Legal and Contact Information

223 |

224 | Usable under the Ruby license. Copyright (C)2004 Joel VanderWerf. Questions 225 | to vjoel@users.sourceforge.net. 227 |

228 | 229 |
230 | 231 | 232 |
233 | 234 | 235 |
236 | 237 | 238 | 239 | 240 |
241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 |
253 | 254 | 255 |
256 |

[Validate]

257 |
258 | 259 | 260 | -------------------------------------------------------------------------------- /work/reference/script-0.3/doc/classes/Script.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | Class: Script 9 | 10 | 11 | 12 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | 66 | 67 | 72 | 73 |
ClassScript
In: 58 | 59 | lib/script.rb 60 | 61 |
62 |
Parent: 68 | 69 | Module 70 | 71 |
74 |
75 | 76 | 77 |
78 | 79 | 80 | 81 |
82 | 83 |
84 |

85 | A module which is an instance of the Script class 86 | encapsulates in its scope the top-level methods, top-level constants, and 87 | instance variables defined in a ruby script file (and its subfiles) loaded 88 | by a ruby program. This allows use of script files to define objects that 89 | can be loaded into a program in much the same way that objects can be 90 | loaded from YAML or Marshal files. 91 |

92 |

93 | See intro.txt for an overview. 94 |

95 | 96 |
97 | 98 | 99 |
100 | 101 |
102 |

Methods

103 | 104 |
105 | load   106 | load_in_module   107 | new   108 | require   109 |
110 |
111 | 112 |
113 | 114 | 115 | 116 | 117 |
118 | 119 |
120 |

Classes and Modules

121 | 122 | Class Script::MissingFile
123 | 124 |
125 | 126 | 127 |
128 |

External Aliases

129 | 130 |
131 | 132 | 133 | 134 | 135 | 136 | 137 |
new->load
138 |
139 |
140 | 141 | 142 |
143 |

Attributes

144 | 145 |
146 | 147 | 148 | 149 | 150 | 156 | 157 | 158 | 159 | 160 | 167 | 168 | 169 | 170 | 171 | 175 | 176 |
dir [R]  151 | The directory in which main_file is located, and relative to which load searches for files before falling back 153 | to Kernel#load. 154 | 155 |
loaded_features [R]  161 | A hash that maps filename=>true for each file that has been 162 | required locally by the script. This has the same semantics as 163 | $", alias $LOADED_FEATURES, except that it is local 164 | to this script. 165 | 166 |
main_file [R]  172 | The file with which the Script was instantiated. 173 | 174 |
177 |
178 |
179 | 180 | 181 | 182 | 183 |
184 |

Public Class methods

185 | 186 |
187 | 188 | 189 | 195 | 196 |
197 |

198 | Creates new Script, and loads main_file 199 | in the scope of the Script. If a block is given, 200 | the script is passed to it before loading from the file, and constants can 201 | be defined as inputs to the script. 202 |

203 |
204 |
205 | 206 |

Public Instance methods

207 | 208 |
209 | 210 | 211 | 217 | 218 |
219 |

220 | Loads file into this Script. Searches 221 | relative to the local dir, that is, the dir of the file given in the 222 | original call to Script.load(file), loads the file, if 224 | found, into this Script’s scope, and 225 | returns true. If the file is not found, falls back to Kernel.load, 226 | which searches on $LOAD_PATH, loads the file, if found, into 227 | global scope, and returns true. Otherwise, raises LoadError. 228 |

229 |

230 | The wrap argument is passed to Kernel.load in the 231 | fallback case, when the file is not found locally. 232 |

233 |

234 | Typically called from within the main file to load additional sub files, or 235 | from those sub files. 236 |

237 |
238 |
239 | 240 |
241 | 242 | 243 | 249 | 250 |
251 |

252 | Loads file in this module’s context. Note that 253 | __FILE__ and __LINE__ work correctly in file. 254 | Called by load and require; not normally called directly. 256 |

257 |
258 |
259 | 260 |
261 | 262 | 263 | 269 | 270 |
271 |

272 | Analogous to Kernel#require. First tries the local dir, then falls 273 | back to Kernel#require. Will load a given feature only 274 | once. 275 |

276 |

277 | Note that extensions (*.so, *.dll) can be required in the global scope, as 278 | usual, but not in the local scope. (This is not much of a limitation in 279 | practice—you wouldn’t want to load an extension more than 280 | once.) This implementation falls back to Kernel#require when the 281 | argument is an extension or is not found locally. 282 |

283 |
284 |
285 | 286 | 287 |
288 | 289 | 290 |
291 | 292 | 293 |
294 |

[Validate]

295 |
296 | 297 | 298 | -------------------------------------------------------------------------------- /work/reference/script-0.3/install.rb: -------------------------------------------------------------------------------- 1 | # 2 | # This file is automatically generated. DO NOT MODIFY! 3 | # 4 | # install.rb 5 | # 6 | # Copyright (c) 2000-2002 Minero Aoki 7 | # 8 | # This program is free software. 9 | # You can distribute/modify this program under the terms of 10 | # the GNU Lesser General Public License version 2. 11 | # 12 | 13 | ### begin compat.rb 14 | 15 | unless Enumerable.instance_methods.include? 'inject' then 16 | module Enumerable 17 | def inject( result ) 18 | each do |i| 19 | result = yield(result, i) 20 | end 21 | result 22 | end 23 | end 24 | end 25 | 26 | def File.read_all( fname ) 27 | File.open(fname, 'rb') {|f| return f.read } 28 | end 29 | 30 | def File.write( fname, str ) 31 | File.open(fname, 'wb') {|f| f.write str } 32 | end 33 | 34 | ### end compat.rb 35 | ### begin config.rb 36 | 37 | if i = ARGV.index(/\A--rbconfig=/) then 38 | file = $' 39 | ARGV.delete_at(i) 40 | require file 41 | else 42 | require 'rbconfig' 43 | end 44 | 45 | 46 | class ConfigTable 47 | 48 | c = ::Config::CONFIG 49 | 50 | rubypath = c['bindir'] + '/' + c['ruby_install_name'] 51 | 52 | major = c['MAJOR'].to_i 53 | minor = c['MINOR'].to_i 54 | teeny = c['TEENY'].to_i 55 | version = "#{major}.#{minor}" 56 | 57 | # ruby ver. >= 1.4.4? 58 | newpath_p = ((major >= 2) or 59 | ((major == 1) and 60 | ((minor >= 5) or 61 | ((minor == 4) and (teeny >= 4))))) 62 | 63 | re = Regexp.new('\A' + Regexp.quote(c['prefix'])) 64 | subprefix = lambda {|path| 65 | re === path and path.sub(re, '$prefix') 66 | } 67 | 68 | if c['rubylibdir'] then 69 | # 1.6.3 < V 70 | stdruby = subprefix.call(c['rubylibdir']) 71 | siteruby = subprefix.call(c['sitedir']) 72 | versite = subprefix.call(c['sitelibdir']) 73 | sodir = subprefix.call(c['sitearchdir']) 74 | elsif newpath_p then 75 | # 1.4.4 <= V <= 1.6.3 76 | stdruby = "$prefix/lib/ruby/#{version}" 77 | siteruby = subprefix.call(c['sitedir']) 78 | versite = siteruby + '/' + version 79 | sodir = "$site-ruby/#{c['arch']}" 80 | else 81 | # V < 1.4.4 82 | stdruby = "$prefix/lib/ruby/#{version}" 83 | siteruby = "$prefix/lib/ruby/#{version}/site_ruby" 84 | versite = siteruby 85 | sodir = "$site-ruby/#{c['arch']}" 86 | end 87 | 88 | DESCRIPTER = [ 89 | [ 'prefix', [ c['prefix'], 90 | 'path', 91 | 'path prefix of target environment' ] ], 92 | [ 'std-ruby', [ stdruby, 93 | 'path', 94 | 'the directory for standard ruby libraries' ] ], 95 | [ 'site-ruby-common', [ siteruby, 96 | 'path', 97 | 'the directory for version-independent non-standard ruby libraries' ] ], 98 | [ 'site-ruby', [ versite, 99 | 'path', 100 | 'the directory for non-standard ruby libraries' ] ], 101 | [ 'bin-dir', [ '$prefix/bin', 102 | 'path', 103 | 'the directory for commands' ] ], 104 | [ 'rb-dir', [ '$site-ruby', 105 | 'path', 106 | 'the directory for ruby scripts' ] ], 107 | [ 'so-dir', [ sodir, 108 | 'path', 109 | 'the directory for ruby extentions' ] ], 110 | [ 'data-dir', [ '$prefix/share', 111 | 'path', 112 | 'the directory for shared data' ] ], 113 | [ 'ruby-path', [ rubypath, 114 | 'path', 115 | 'path to set to #! line' ] ], 116 | [ 'ruby-prog', [ rubypath, 117 | 'name', 118 | 'the ruby program using for installation' ] ], 119 | [ 'make-prog', [ 'make', 120 | 'name', 121 | 'the make program to compile ruby extentions' ] ], 122 | [ 'without-ext', [ 'no', 123 | 'yes/no', 124 | 'does not compile/install ruby extentions' ] ] 125 | ] 126 | 127 | SAVE_FILE = 'config.save' 128 | 129 | def ConfigTable.each_name( &block ) 130 | keys().each( &block ) 131 | end 132 | 133 | def ConfigTable.keys 134 | DESCRIPTER.collect {|k,*dummy| k } 135 | end 136 | 137 | def ConfigTable.each_definition( &block ) 138 | DESCRIPTER.each( &block ) 139 | end 140 | 141 | def ConfigTable.get_entry( name ) 142 | name, ent = DESCRIPTER.assoc(name) 143 | ent 144 | end 145 | 146 | def ConfigTable.get_entry!( name ) 147 | get_entry(name) or raise ArgumentError, "no such config: #{name}" 148 | end 149 | 150 | def ConfigTable.add_entry( name, vals ) 151 | ConfigTable::DESCRIPTER.push [name,vals] 152 | end 153 | 154 | def ConfigTable.remove_entry( name ) 155 | get_entry name or raise ArgumentError, "no such config: #{name}" 156 | DESCRIPTER.delete_if {|n,arr| n == name } 157 | end 158 | 159 | def ConfigTable.config_key?( name ) 160 | get_entry(name) ? true : false 161 | end 162 | 163 | def ConfigTable.bool_config?( name ) 164 | ent = get_entry(name) or return false 165 | ent[1] == 'yes/no' 166 | end 167 | 168 | def ConfigTable.value_config?( name ) 169 | ent = get_entry(name) or return false 170 | ent[1] != 'yes/no' 171 | end 172 | 173 | def ConfigTable.path_config?( name ) 174 | ent = get_entry(name) or return false 175 | ent[1] == 'path' 176 | end 177 | 178 | 179 | class << self 180 | 181 | alias newobj new 182 | 183 | def new 184 | c = newobj() 185 | c.__send__ :init 186 | c 187 | end 188 | 189 | def load 190 | c = newobj() 191 | File.file? SAVE_FILE or 192 | raise InstallError, "#{File.basename $0} config first" 193 | File.foreach( SAVE_FILE ) do |line| 194 | k, v = line.split( '=', 2 ) 195 | c.instance_eval { 196 | @table[k] = v.strip 197 | } 198 | end 199 | c 200 | end 201 | 202 | end 203 | 204 | def initialize 205 | @table = {} 206 | end 207 | 208 | def init 209 | DESCRIPTER.each do |k, (default, vname, desc, default2)| 210 | @table[k] = default 211 | end 212 | end 213 | private :init 214 | 215 | def save 216 | File.open( SAVE_FILE, 'w' ) {|f| 217 | @table.each do |k, v| 218 | f.printf "%s=%s\n", k, v if v 219 | end 220 | } 221 | end 222 | 223 | def []=( k, v ) 224 | ConfigTable.config_key? k or raise InstallError, "unknown config option #{k}" 225 | if ConfigTable.path_config? k then 226 | @table[k] = (v[0,1] != '$') ? File.expand_path(v) : v 227 | else 228 | @table[k] = v 229 | end 230 | end 231 | 232 | def []( key ) 233 | @table[key] or return nil 234 | @table[key].gsub( %r<\$([^/]+)> ) { self[$1] } 235 | end 236 | 237 | def set_raw( key, val ) 238 | @table[key] = val 239 | end 240 | 241 | def get_raw( key ) 242 | @table[key] 243 | end 244 | 245 | end 246 | 247 | 248 | class MetaConfigEnvironment 249 | 250 | def self.eval_file( file ) 251 | return unless File.file? file 252 | new.instance_eval File.read_all(file), file, 1 253 | end 254 | 255 | private 256 | 257 | def config_names 258 | ConfigTable.keys 259 | end 260 | 261 | def config?( name ) 262 | ConfigTable.config_key? name 263 | end 264 | 265 | def bool_config?( name ) 266 | ConfigTable.bool_config? name 267 | end 268 | 269 | def value_config?( name ) 270 | ConfigTable.value_config? name 271 | end 272 | 273 | def path_config?( name ) 274 | ConfigTable.path_config? name 275 | end 276 | 277 | def add_config( name, argname, default, desc ) 278 | ConfigTable.add_entry name,[default,argname,desc] 279 | end 280 | 281 | def add_path_config( name, default, desc ) 282 | add_config name, 'path', default, desc 283 | end 284 | 285 | def add_bool_config( name, default, desc ) 286 | add_config name, 'yes/no', default ? 'yes' : 'no', desc 287 | end 288 | 289 | def set_config_default( name, default ) 290 | if bool_config? name then 291 | ConfigTable.get_entry!(name)[0] = default ? 'yes' : 'no' 292 | else 293 | ConfigTable.get_entry!(name)[0] = default 294 | end 295 | end 296 | 297 | def remove_config( name ) 298 | ent = ConfigTable.get_entry(name) 299 | ConfigTable.remove_entry name 300 | ent 301 | end 302 | 303 | end 304 | 305 | ### end config.rb 306 | ### begin fileop.rb 307 | 308 | module FileOperations 309 | 310 | def mkdir_p( dname, prefix = nil ) 311 | dname = prefix + dname if prefix 312 | $stderr.puts "mkdir -p #{dname}" if verbose? 313 | return if no_harm? 314 | 315 | # does not check '/'... it's too abnormal case 316 | dirs = dname.split(%r_(?=/)_) 317 | if /\A[a-z]:\z/i === dirs[0] then 318 | disk = dirs.shift 319 | dirs[0] = disk + dirs[0] 320 | end 321 | dirs.each_index do |idx| 322 | path = dirs[0..idx].join('') 323 | Dir.mkdir path unless dir? path 324 | end 325 | end 326 | 327 | def rm_f( fname ) 328 | $stderr.puts "rm -f #{fname}" if verbose? 329 | return if no_harm? 330 | 331 | if File.exist? fname or File.symlink? fname then 332 | File.chmod 0777, fname 333 | File.unlink fname 334 | end 335 | end 336 | 337 | def rm_rf( dn ) 338 | $stderr.puts "rm -rf #{dn}" if verbose? 339 | return if no_harm? 340 | 341 | Dir.chdir dn 342 | Dir.foreach('.') do |fn| 343 | next if fn == '.' 344 | next if fn == '..' 345 | if dir? fn then 346 | verbose_off { 347 | rm_rf fn 348 | } 349 | else 350 | verbose_off { 351 | rm_f fn 352 | } 353 | end 354 | end 355 | Dir.chdir '..' 356 | Dir.rmdir dn 357 | end 358 | 359 | def mv( src, dest ) 360 | rm_f dest 361 | begin 362 | File.link src, dest 363 | rescue 364 | File.write dest, File.read_all(src) 365 | File.chmod File.stat(src).mode, dest 366 | end 367 | rm_f src 368 | end 369 | 370 | def install( from, dest, mode, prefix = nil ) 371 | $stderr.puts "install #{from} #{dest}" if verbose? 372 | return if no_harm? 373 | 374 | realdest = prefix + dest if prefix 375 | if dir? realdest then 376 | realdest += '/' + File.basename(from) 377 | end 378 | str = File.read_all(from) 379 | if diff? str, realdest then 380 | verbose_off { 381 | rm_f realdest if File.exist? realdest 382 | } 383 | File.write realdest, str 384 | File.chmod mode, realdest 385 | 386 | File.open( objdir + '/InstalledFiles', 'a' ) {|f| f.puts realdest } 387 | end 388 | end 389 | 390 | def diff?( orig, targ ) 391 | return true unless File.exist? targ 392 | orig != File.read_all(targ) 393 | end 394 | 395 | def command( str ) 396 | $stderr.puts str if verbose? 397 | system str or raise RuntimeError, "'system #{str}' failed" 398 | end 399 | 400 | def ruby( str ) 401 | command config('ruby-prog') + ' ' + str 402 | end 403 | 404 | def dir?( dname ) 405 | # for corrupted windows stat() 406 | File.directory?( (dname[-1,1] == '/') ? dname : dname + '/' ) 407 | end 408 | 409 | def all_files( dname ) 410 | Dir.open( dname ) {|d| 411 | return d.find_all {|n| File.file? "#{dname}/#{n}" } 412 | } 413 | end 414 | 415 | def all_dirs( dname ) 416 | Dir.open( dname ) {|d| 417 | return d.find_all {|n| dir? "#{dname}/#{n}" } - %w(. ..) 418 | } 419 | end 420 | 421 | end 422 | 423 | ### end fileop.rb 424 | ### begin base.rb 425 | 426 | class InstallError < StandardError; end 427 | 428 | 429 | class Installer 430 | 431 | Version = '3.1.2' 432 | Copyright = 'Copyright (c) 2000-2002 Minero Aoki' 433 | 434 | 435 | @toplevel = nil 436 | 437 | def self.declear_toplevel_installer( inst ) 438 | @toplevel and 439 | raise ArgumentError, 'more than one toplevel installer decleared' 440 | @toplevel = inst 441 | end 442 | 443 | def self.toplevel_installer 444 | @toplevel 445 | end 446 | 447 | 448 | FILETYPES = %w( bin lib ext data ) 449 | 450 | include FileOperations 451 | 452 | def initialize( config, opt, srcroot, objroot ) 453 | @config = config 454 | @options = opt 455 | @srcdir = File.expand_path(srcroot) 456 | @objdir = File.expand_path(objroot) 457 | @currdir = '.' 458 | end 459 | 460 | def inspect 461 | "#<#{type} #{__id__}>" 462 | end 463 | 464 | # 465 | # configs/options 466 | # 467 | 468 | def get_config( key ) 469 | @config[key] 470 | end 471 | 472 | alias config get_config 473 | 474 | def set_config( key, val ) 475 | @config[key] = val 476 | end 477 | 478 | def no_harm? 479 | @options['no-harm'] 480 | end 481 | 482 | def verbose? 483 | @options['verbose'] 484 | end 485 | 486 | def verbose_off 487 | save, @options['verbose'] = @options['verbose'], false 488 | yield 489 | @options['verbose'] = save 490 | end 491 | 492 | # 493 | # srcdir/objdir 494 | # 495 | 496 | attr_reader :srcdir 497 | alias srcdir_root srcdir 498 | alias package_root srcdir 499 | 500 | def curr_srcdir 501 | "#{@srcdir}/#{@currdir}" 502 | end 503 | 504 | attr_reader :objdir 505 | alias objdir_root objdir 506 | 507 | def curr_objdir 508 | "#{@objdir}/#{@currdir}" 509 | end 510 | 511 | def srcfile( path ) 512 | curr_srcdir + '/' + path 513 | end 514 | 515 | def srcexist?( path ) 516 | File.exist? srcfile(path) 517 | end 518 | 519 | def srcdirectory?( path ) 520 | dir? srcfile(path) 521 | end 522 | 523 | def srcfile?( path ) 524 | File.file? srcfile(path) 525 | end 526 | 527 | def srcentries( path = '.' ) 528 | Dir.open( curr_srcdir + '/' + path ) {|d| 529 | return d.to_a - %w(. ..) - hookfilenames 530 | } 531 | end 532 | 533 | def srcfiles( path = '.' ) 534 | srcentries(path).find_all {|fname| 535 | File.file? File.join(curr_srcdir, path, fname) 536 | } 537 | end 538 | 539 | def srcdirectories( path = '.' ) 540 | srcentries(path).find_all {|fname| 541 | dir? File.join(curr_srcdir, path, fname) 542 | } 543 | end 544 | 545 | def dive_into( rel ) 546 | return unless dir? "#{@srcdir}/#{rel}" 547 | 548 | dir = File.basename(rel) 549 | Dir.mkdir dir unless dir? dir 550 | save = Dir.pwd 551 | Dir.chdir dir 552 | $stderr.puts '---> ' + rel if verbose? 553 | @currdir = rel 554 | yield 555 | Dir.chdir save 556 | $stderr.puts '<--- ' + rel if verbose? 557 | @currdir = File.dirname(rel) 558 | end 559 | 560 | # 561 | # config 562 | # 563 | 564 | def exec_config 565 | exec_task_traverse 'config' 566 | end 567 | 568 | def config_dir_bin( rel ) 569 | end 570 | 571 | def config_dir_lib( rel ) 572 | end 573 | 574 | def config_dir_ext( rel ) 575 | extconf if extdir? curr_srcdir 576 | end 577 | 578 | def extconf 579 | opt = @options['config-opt'].join(' ') 580 | command "#{config('ruby-prog')} #{curr_srcdir}/extconf.rb #{opt}" 581 | end 582 | 583 | def config_dir_data( rel ) 584 | end 585 | 586 | # 587 | # setup 588 | # 589 | 590 | def exec_setup 591 | exec_task_traverse 'setup' 592 | end 593 | 594 | def setup_dir_bin( relpath ) 595 | all_files( curr_srcdir ).each do |fname| 596 | add_rubypath "#{curr_srcdir}/#{fname}" 597 | end 598 | end 599 | 600 | SHEBANG_RE = /\A\#!\s*\S*ruby\S*/ 601 | 602 | def add_rubypath( path ) 603 | $stderr.puts %Q if verbose? 604 | return if no_harm? 605 | 606 | tmpfile = File.basename(path) + '.tmp' 607 | begin 608 | File.open( path ) {|r| 609 | File.open( tmpfile, 'w' ) {|w| 610 | first = r.gets 611 | return unless SHEBANG_RE === first # reject '/usr/bin/env ruby' 612 | 613 | w.print first.sub( SHEBANG_RE, '#!' + config('ruby-path') ) 614 | w.write r.read 615 | } } 616 | mv tmpfile, File.basename(path) 617 | ensure 618 | rm_f tmpfile if File.exist? tmpfile 619 | end 620 | end 621 | 622 | def setup_dir_lib( relpath ) 623 | end 624 | 625 | def setup_dir_ext( relpath ) 626 | if extdir? curr_srcdir then 627 | make 628 | end 629 | end 630 | 631 | def make 632 | command config('make-prog') 633 | end 634 | 635 | def setup_dir_data( relpath ) 636 | end 637 | 638 | # 639 | # install 640 | # 641 | 642 | def exec_install 643 | exec_task_traverse 'install' 644 | end 645 | 646 | def install_dir_bin( rel ) 647 | install_files targfiles, config('bin-dir') + '/' + rel, 0755 648 | end 649 | 650 | def install_dir_lib( rel ) 651 | install_files targfiles, config('rb-dir') + '/' + rel, 0644 652 | end 653 | 654 | def install_dir_ext( rel ) 655 | if extdir? curr_srcdir then 656 | install_dir_ext_main File.dirname(rel) 657 | end 658 | end 659 | 660 | def install_dir_ext_main( rel ) 661 | install_files allext('.'), config('so-dir') + '/' + rel, 0555 662 | end 663 | 664 | def install_dir_data( rel ) 665 | install_files targfiles, config('data-dir') + '/' + rel, 0644 666 | end 667 | 668 | def install_files( list, dest, mode ) 669 | mkdir_p dest, @options['install-prefix'] 670 | list.each do |fname| 671 | install fname, dest, mode, @options['install-prefix'] 672 | end 673 | end 674 | 675 | def targfiles 676 | (targfilenames() - hookfilenames()).collect {|fname| 677 | File.exist?(fname) ? fname : File.join(curr_srcdir(), fname) 678 | } 679 | end 680 | 681 | def targfilenames 682 | [ curr_srcdir(), '.' ].inject([]) {|ret, dir| 683 | ret | all_files(dir) 684 | } 685 | end 686 | 687 | def hookfilenames 688 | %w( pre-%s post-%s pre-%s.rb post-%s.rb ).collect {|fmt| 689 | %w( config setup install clean ).collect {|t| sprintf fmt, t } 690 | }.flatten 691 | end 692 | 693 | def allext( dir ) 694 | _allext(dir) or raise InstallError, 695 | "no extention exists: Have you done 'ruby #{$0} setup' ?" 696 | end 697 | 698 | DLEXT = /\.#{ ::Config::CONFIG['DLEXT'] }\z/ 699 | 700 | def _allext( dir ) 701 | Dir.open( dir ) {|d| 702 | return d.find_all {|fname| DLEXT === fname } 703 | } 704 | end 705 | 706 | # 707 | # clean 708 | # 709 | 710 | def exec_clean 711 | exec_task_traverse 'clean' 712 | rm_f 'config.save' 713 | rm_f 'InstalledFiles' 714 | end 715 | 716 | def clean_dir_bin( rel ) 717 | end 718 | 719 | def clean_dir_lib( rel ) 720 | end 721 | 722 | def clean_dir_ext( rel ) 723 | clean 724 | end 725 | 726 | def clean 727 | command config('make-prog') + ' clean' if File.file? 'Makefile' 728 | end 729 | 730 | def clean_dir_data( rel ) 731 | end 732 | 733 | # 734 | # lib 735 | # 736 | 737 | def exec_task_traverse( task ) 738 | run_hook 'pre-' + task 739 | FILETYPES.each do |type| 740 | if config('without-ext') == 'yes' and type == 'ext' then 741 | $stderr.puts 'skipping ext/* by user option' if verbose? 742 | next 743 | end 744 | traverse task, type, task + '_dir_' + type 745 | end 746 | run_hook 'post-' + task 747 | end 748 | 749 | def traverse( task, rel, mid ) 750 | dive_into( rel ) { 751 | run_hook 'pre-' + task 752 | __send__ mid, rel.sub( %r_\A.*?(?:/|\z)_, '' ) 753 | all_dirs( curr_srcdir ).each do |d| 754 | traverse task, rel + '/' + d, mid 755 | end 756 | run_hook 'post-' + task 757 | } 758 | end 759 | 760 | def run_hook( name ) 761 | try_run_hook curr_srcdir + '/' + name or 762 | try_run_hook curr_srcdir + '/' + name + '.rb' 763 | end 764 | 765 | def try_run_hook( fname ) 766 | return false unless File.file? fname 767 | 768 | env = self.dup 769 | begin 770 | env.instance_eval File.read_all(fname), fname, 1 771 | rescue 772 | raise InstallError, "hook #{fname} failed:\n" + $!.message 773 | end 774 | true 775 | end 776 | 777 | def extdir?( dir ) 778 | File.exist? dir + '/MANIFEST' 779 | end 780 | 781 | end 782 | 783 | ### end base.rb 784 | ### begin toplevel.rb 785 | 786 | class ToplevelInstaller < Installer 787 | 788 | TASKS = [ 789 | [ 'config', 'saves your configurations' ], 790 | [ 'show', 'shows current configuration' ], 791 | [ 'setup', 'compiles extention or else' ], 792 | [ 'install', 'installs files' ], 793 | [ 'clean', "does `make clean' for each extention" ] 794 | ] 795 | 796 | 797 | def initialize( root ) 798 | super nil, {'verbose' => true}, root, '.' 799 | Installer.declear_toplevel_installer self 800 | end 801 | 802 | 803 | def execute 804 | run_metaconfigs 805 | 806 | case task = parsearg_global() 807 | when 'config' 808 | @config = ConfigTable.new 809 | else 810 | @config = ConfigTable.load 811 | end 812 | parsearg_TASK task 813 | 814 | exectask task 815 | end 816 | 817 | 818 | def run_metaconfigs 819 | MetaConfigEnvironment.eval_file "#{srcdir_root}/#{metaconfig}" 820 | end 821 | 822 | def metaconfig 823 | 'metaconfig' 824 | end 825 | 826 | 827 | def exectask( task ) 828 | if task == 'show' then 829 | exec_show 830 | else 831 | try task 832 | end 833 | end 834 | 835 | def try( task ) 836 | $stderr.printf "#{File.basename $0}: entering %s phase...\n", task if verbose? 837 | begin 838 | __send__ 'exec_' + task 839 | rescue 840 | $stderr.printf "%s failed\n", task 841 | raise 842 | end 843 | $stderr.printf "#{File.basename $0}: %s done.\n", task if verbose? 844 | end 845 | 846 | # 847 | # processing arguments 848 | # 849 | 850 | def parsearg_global 851 | task_re = /\A(?:#{TASKS.collect {|i| i[0] }.join '|'})\z/ 852 | 853 | while arg = ARGV.shift do 854 | case arg 855 | when /\A\w+\z/ 856 | task_re === arg or raise InstallError, "wrong task: #{arg}" 857 | return arg 858 | 859 | when '-q', '--quiet' 860 | @options['verbose'] = false 861 | 862 | when '--verbose' 863 | @options['verbose'] = true 864 | 865 | when '-h', '--help' 866 | print_usage $stdout 867 | exit 0 868 | 869 | when '-v', '--version' 870 | puts "#{File.basename $0} version #{Version}" 871 | exit 0 872 | 873 | when '--copyright' 874 | puts Copyright 875 | exit 0 876 | 877 | else 878 | raise InstallError, "unknown global option '#{arg}'" 879 | end 880 | end 881 | 882 | raise InstallError, 'no task or global option given' 883 | end 884 | 885 | 886 | def parsearg_TASK( task ) 887 | mid = "parsearg_#{task}" 888 | if respond_to? mid, true then 889 | __send__ mid 890 | else 891 | ARGV.empty? or 892 | raise InstallError, "#{task}: unknown options: #{ARGV.join ' '}" 893 | end 894 | end 895 | 896 | def parsearg_config 897 | re = /\A--(#{ConfigTable.keys.join '|'})(?:=(.*))?\z/ 898 | @options['config-opt'] = [] 899 | 900 | while i = ARGV.shift do 901 | if /\A--?\z/ === i then 902 | @options['config-opt'] = ARGV.dup 903 | break 904 | end 905 | m = re.match(i) or raise InstallError, "config: unknown option #{i}" 906 | name, value = m.to_a[1,2] 907 | if value then 908 | if ConfigTable.bool_config?(name) then 909 | /\A(y(es)?|n(o)?|t(rue)?|f(alse))\z/i === value or raise InstallError, "config: --#{name} allows only yes/no for argument" 910 | value = (/\Ay(es)?|\At(rue)/i === value) ? 'yes' : 'no' 911 | end 912 | else 913 | ConfigTable.bool_config?(name) or raise InstallError, "config: --#{name} requires argument" 914 | value = 'yes' 915 | end 916 | @config[name] = value 917 | end 918 | end 919 | 920 | def parsearg_install 921 | @options['no-harm'] = false 922 | @options['install-prefix'] = '' 923 | while a = ARGV.shift do 924 | case a 925 | when /\A--no-harm\z/ 926 | @options['no-harm'] = true 927 | when /\A--prefix=(.*)\z/ 928 | path = $1 929 | path = File.expand_path(path) unless path[0,1] == '/' 930 | @options['install-prefix'] = path 931 | else 932 | raise InstallError, "install: unknown option #{a}" 933 | end 934 | end 935 | end 936 | 937 | 938 | def print_usage( out ) 939 | out.puts 940 | out.puts 'Usage:' 941 | out.puts " ruby #{File.basename $0} " 942 | out.puts " ruby #{File.basename $0} [] []" 943 | 944 | fmt = " %-20s %s\n" 945 | out.puts 946 | out.puts 'Global options:' 947 | out.printf fmt, '-q,--quiet', 'suppress message outputs' 948 | out.printf fmt, ' --verbose', 'output messages verbosely' 949 | out.printf fmt, '-h,--help', 'print this message' 950 | out.printf fmt, '-v,--version', 'print version and quit' 951 | out.printf fmt, '--copyright', 'print copyright and quit' 952 | 953 | out.puts 954 | out.puts 'Tasks:' 955 | TASKS.each do |name, desc| 956 | out.printf " %-10s %s\n", name, desc 957 | end 958 | 959 | out.puts 960 | out.puts 'Options for config:' 961 | ConfigTable.each_definition do |name, (default, arg, desc, default2)| 962 | out.printf " %-20s %s [%s]\n", 963 | '--'+ name + (ConfigTable.bool_config?(name) ? '' : '='+arg), 964 | desc, 965 | default2 || default 966 | end 967 | out.printf " %-20s %s [%s]\n", 968 | '--rbconfig=path', 'your rbconfig.rb to load', "running ruby's" 969 | 970 | out.puts 971 | out.puts 'Options for install:' 972 | out.printf " %-20s %s [%s]\n", 973 | '--no-harm', 'only display what to do if given', 'off' 974 | 975 | out.puts 976 | end 977 | 978 | # 979 | # config 980 | # 981 | 982 | def exec_config 983 | super 984 | @config.save 985 | end 986 | 987 | # 988 | # show 989 | # 990 | 991 | def exec_show 992 | ConfigTable.each_name do |k| 993 | v = @config.get_raw(k) 994 | if not v or v.empty? then 995 | v = '(not specified)' 996 | end 997 | printf "%-10s %s\n", k, v 998 | end 999 | end 1000 | 1001 | end 1002 | 1003 | ### end toplevel.rb 1004 | 1005 | if $0 == __FILE__ then 1006 | begin 1007 | installer = ToplevelInstaller.new( File.dirname($0) ) 1008 | installer.execute 1009 | rescue 1010 | raise if $DEBUG 1011 | $stderr.puts $!.message 1012 | $stderr.puts "try 'ruby #{$0} --help' for usage" 1013 | exit 1 1014 | end 1015 | end 1016 | --------------------------------------------------------------------------------