rubinjam 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12b4693f0228c9ef025562f3de9ca18dac47fc11
4
- data.tar.gz: 41987578a65685f0e4f88f125d0eff2a25ae0f10
3
+ metadata.gz: 352528474106c74c5ec74fd6b7fc8785d4ea73d1
4
+ data.tar.gz: 32437116794027bd101b3039949f25e28bef582d
5
5
  SHA512:
6
- metadata.gz: b749725f4df230e2f657915216b6b7aa7eb68885b468a49743c1b6316f630c0f51110ee4198e13fd4fd792271a38e339cf50343ec0c12c9595e187afc5b56f5b
7
- data.tar.gz: 59a85c541e2cecd673d4d2868f9667c415cbfb0db3e6f29a91dd0c116bc47d34c08a4d09770770a4c7b8f21c7ddc52cab856a8894d525c5cda193aa69032e9a6
6
+ metadata.gz: 3adaf88e521ce70e5b169abbba87f351a845c378691c0129248865e70a2f85616543a5e66dd314999b896f1d4b99848473c3face09cd7cd937bcb3e914a9c88d
7
+ data.tar.gz: 7eda2b3a6765e7314d2f5a0b958f1f7f59e0e1e3efc2f2f5bfc7729b39f732ea8f3759d10de11660a827fd76b67bf68822e4cbdbdcae601df0d9656780e8d846
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2013 Michael Grosser <michael@grosser.it>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,108 @@
1
+ module Rubinjam
2
+ ROOT = File.expand_path("../", __FILE__) << "/rubinjam/"
3
+
4
+ class << self
5
+ def normalize_file(file)
6
+ return file unless file.start_with?("/")
7
+ if file.start_with?(ROOT)
8
+ file.sub(ROOT, "")
9
+ else
10
+ file.split('/lib/').last
11
+ end
12
+ end
13
+
14
+ def file_from_nesting(mod, const)
15
+ if file = mod.rubinjam_autload[const]
16
+ return [mod, file]
17
+ end
18
+
19
+ nesting(mod.name)[1..-1].detect do |mod|
20
+ file = mod.rubinjam_autload[const]
21
+ break [mod, file] if file
22
+ end
23
+ end
24
+
25
+ # this does not reflect the actual Module.nesting of the caller,
26
+ # but it should be close enough
27
+ def nesting(name)
28
+ nesting = []
29
+ namespace = name.split("::")
30
+ namespace.inject(Object) do |base, n|
31
+ klass = base.const_get(n)
32
+ nesting << klass
33
+ klass
34
+ end
35
+ nesting.reverse
36
+ end
37
+ end
38
+
39
+ module ModuleAutoloadFix
40
+ def self.included(base)
41
+ base.class_eval do
42
+ def rubinjam_autload
43
+ @rubinjam_autload ||= {}
44
+ end
45
+
46
+ alias autoload_without_rubinjam autoload
47
+ def autoload(const, file)
48
+ normalized_file = Rubinjam.normalize_file(file)
49
+ if Rubinjam::LIBRARIES[normalized_file]
50
+ rubinjam_autload[const] = normalized_file
51
+ else
52
+ autoload_without_rubinjam(const, file)
53
+ end
54
+ end
55
+
56
+ alias const_missing_without_rubinjam const_missing
57
+ def const_missing(const)
58
+ # do not load twice / go into infitire loops
59
+ @rubinjam_tried_const_missing ||= {}
60
+ if @rubinjam_tried_const_missing[const]
61
+ return const_missing_without_rubinjam(const)
62
+ end
63
+ @rubinjam_tried_const_missing[const] = true
64
+
65
+ # try to find autoload in current module or nesting
66
+ nesting, file = Rubinjam.file_from_nesting(self, const)
67
+ if file
68
+ require file
69
+ nesting.const_get(const)
70
+ else
71
+ const_missing_without_rubinjam(const)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ module BaseAutoloadFix
79
+ def self.included(base)
80
+ base.class_eval do
81
+ alias autoload_without_rubinjam autoload
82
+
83
+ def autoload(const, file)
84
+ normalized_file = Rubinjam.normalize_file(file)
85
+ if Rubinjam::LIBRARIES[normalized_file]
86
+ require normalized_file
87
+ else
88
+ autoload_without_rubinjam(const, file)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ Module.send(:include, Rubinjam::ModuleAutoloadFix)
97
+ include Rubinjam::BaseAutoloadFix
98
+
99
+ def require(file)
100
+ normalized_file = Rubinjam.normalize_file(file)
101
+ if code = Rubinjam::LIBRARIES[normalized_file]
102
+ return if code == :loaded
103
+ eval(code, TOPLEVEL_BINDING, "rubinjam/#{normalized_file}.rb")
104
+ Rubinjam::LIBRARIES[normalized_file] = :loaded
105
+ else
106
+ super
107
+ end
108
+ end
@@ -0,0 +1,3 @@
1
+ module Rubinjam
2
+ VERSION = "0.5.1"
3
+ end
data/lib/rubinjam.rb ADDED
@@ -0,0 +1,112 @@
1
+ require "tmpdir"
2
+ require "bundler"
3
+ require "rubinjam/version"
4
+
5
+ module Rubinjam
6
+ HEADER = '#!/usr/bin/env ruby'.freeze
7
+
8
+ class << self
9
+ def pack(dir)
10
+ Dir.chdir(dir) do
11
+ binaries = Dir["bin/*"]
12
+ raise "No binary found in ./bin" if binaries.size == 0
13
+ raise "Can only pack exactly 1 binary, found #{binaries.join(",")} in ./bin" unless binaries.size == 1
14
+ content = environment + File.read(binaries.first)
15
+ [File.basename(binaries.first), content]
16
+ end
17
+ end
18
+
19
+ def pack_gem(gem, version=nil)
20
+ require "shellwords"
21
+ require "rubygems/package"
22
+
23
+ Dir.mktmpdir do |dir|
24
+ Dir.chdir(dir) do
25
+ # fetch
26
+ command = "gem fetch #{Shellwords.escape(gem)}"
27
+ command << " -v" << version if version
28
+ sh(command)
29
+
30
+ # load spec
31
+ gem_ball = Dir["*.gem"].first
32
+ spec = Gem::Package.new(gem_ball).spec.to_ruby
33
+ sh("gem unpack #{gem_ball}")
34
+
35
+ # bundle
36
+ Dir.chdir(gem_ball.sub(".gem", "")) do
37
+ File.open("#{gem}.gemspec", "w") { |f| f.write spec }
38
+ Rubinjam.pack(Dir.pwd)
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def libraries
47
+ libs_from_paths(["lib"]).merge!(gem_libraries).merge!("rubinjam/internal" => internal_code)
48
+ end
49
+
50
+ def internal_code
51
+ if defined?(LIBRARIES)
52
+ LIBRARIES["rubinjam/internal"] # dogfooding ourself -> cannot reed files
53
+ else
54
+ File.read(File.expand_path("../rubinjam/internal.rb", __FILE__))
55
+ end
56
+ end
57
+
58
+ def gem_libraries
59
+ return {} unless gemspec = Dir["*.gemspec"].first
60
+ return {} unless File.read(gemspec) =~ /add_(runtime_)?dependency/
61
+
62
+ Dir.mktmpdir do |dir|
63
+ sh "cp -R . #{dir}/"
64
+ Dir.chdir(dir) do
65
+ write gemspec, File.read(gemspec).gsub(/.*add_development_dependency.*/, "")
66
+ write "Gemfile", <<-RUBY.gsub(/^ /, "")
67
+ source "https://rubygems.org"
68
+ gemspec
69
+ RUBY
70
+
71
+ sh("rm -f Gemfile.lock")
72
+ sh("BUNDLE_IGNORE_CONFIG=1 bundle install --quiet --path bundle") # heroku has a --deployment config -> ignore it
73
+ paths = sh("bundle exec ruby -e 'puts $LOAD_PATH'").split("\n")
74
+ paths = paths.grep(%r{/gems/}).reject { |r| r =~ %r{/gems/bundler-\d} }
75
+ libs_from_paths(paths)
76
+ end
77
+ end
78
+ end
79
+
80
+ def libs_from_paths(paths)
81
+ paths.select { |p| File.directory?(p) }.inject({}) do |all, path|
82
+ Dir.chdir path do
83
+ all.merge!(Hash[Dir["**/*.rb"].map { |f| [f.sub(/\.rb$/, ""), File.read(f)] }])
84
+ end
85
+ end
86
+ end
87
+
88
+ def sh(command, options={})
89
+ result = Bundler.with_clean_env { `#{command} 2>/dev/null` }
90
+ raise "#{options[:fail] ? "SUCCESS" : "FAIL"} #{command}\n#{result}" if $?.success? == !!options[:fail]
91
+ result
92
+ end
93
+
94
+ def write(file, content)
95
+ FileUtils.mkdir_p(File.dirname(file))
96
+ File.open(file, "w") { |f| f.write content }
97
+ end
98
+
99
+ def environment
100
+ <<-RUBY.gsub(/^ /, "")
101
+ #{HEADER}
102
+ # generated by rubinjam v#{VERSION} -- https://github.com/grosser/rubinjam
103
+ module Rubinjam
104
+ LIBRARIES = {
105
+ #{libraries.map { |name,content| "#{name.inspect} => #{content.inspect}" }.join(",\n ")}
106
+ }
107
+ end
108
+ eval(Rubinjam::LIBRARIES.fetch("rubinjam/internal"), TOPLEVEL_BINDING, "rubinjam")
109
+ RUBY
110
+ end
111
+ end
112
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubinjam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -31,7 +31,11 @@ executables:
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - MIT-LICENSE
34
35
  - bin/rubinjam
36
+ - lib/rubinjam.rb
37
+ - lib/rubinjam/internal.rb
38
+ - lib/rubinjam/version.rb
35
39
  homepage: https://github.com/grosser/rubinjam
36
40
  licenses:
37
41
  - MIT