fake_gem 0.1.12 → 0.1.13
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.
- data/Rakefile +4 -0
- data/bin/fake_gem +47 -0
- data/lib/fake_gem.rb +49 -19
- data/readme.md +27 -53
- data/spec/fake_gem_spec/common/project_a/fake_gem +2 -1
- data/spec/fake_gem_spec/twice/kit/fake_gem +1 -1
- data/spec/fake_gem_spec/twice/saas/fake_gem +1 -1
- data/spec/fake_gem_spec.rb +9 -0
- metadata +5 -4
data/Rakefile
CHANGED
data/bin/fake_gem
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
if ARGV.empty? or %w(-h --help help).any?{|k| ARGV.include? k}
|
4
|
+
puts <<-TEXT
|
5
|
+
fake_gem makes any directory looks like ruby gem.
|
6
|
+
|
7
|
+
Usage:
|
8
|
+
$ fake_gem ~/projects ~/other_projects
|
9
|
+
|
10
|
+
Details:
|
11
|
+
Let's suppose Your projects are located
|
12
|
+
in the /my_projects dir, and there is 3 folders:
|
13
|
+
~/my_projects
|
14
|
+
/app
|
15
|
+
/lib1
|
16
|
+
/lib2
|
17
|
+
|
18
|
+
You want lib1 and lib2 to be available as a gems
|
19
|
+
while You are developing Your app, do following steps:
|
20
|
+
1. mark lib1 as fake_gem:
|
21
|
+
create /projects/lib/fake_gem file
|
22
|
+
and add there following lines:
|
23
|
+
name: lib1
|
24
|
+
libs: lib
|
25
|
+
2. do the same for lib2
|
26
|
+
3. enable fake_gem in current bash session, type:
|
27
|
+
$ fake_gem ~/projects
|
28
|
+
|
29
|
+
All done, now lib1 and lib2 will be
|
30
|
+
availiable as real gems in ruby scripts.
|
31
|
+
TEXT
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
|
35
|
+
dir = File.expand_path("#{__FILE__}/../..")
|
36
|
+
|
37
|
+
rubyopt = ENV['RUBYOPT'] || ""
|
38
|
+
rubyopt = "#{rubyopt} -r#{dir}/lib/fake_gem.rb" unless rubyopt =~ /fake_gem/
|
39
|
+
|
40
|
+
fake_gem_paths = (ENV['FAKE_GEM_PATHS'] || "").split(':')
|
41
|
+
fake_gem_paths = (fake_gem_paths + ARGV).uniq.join(':')
|
42
|
+
|
43
|
+
puts <<-TEXT
|
44
|
+
Please execute following commands or add it to Your bash profile:
|
45
|
+
RUBYOPT=#{rubyopt}
|
46
|
+
FAKE_GEM_PATHS=#{fake_gem_paths}
|
47
|
+
TEXT
|
data/lib/fake_gem.rb
CHANGED
@@ -7,6 +7,21 @@ unless defined?(FakeGem) or defined?(JRuby)
|
|
7
7
|
|
8
8
|
$indent = -1
|
9
9
|
Kernel.class_eval do
|
10
|
+
alias :gem_without_fgem :gem
|
11
|
+
def gem name, *version_requirements
|
12
|
+
gem_without_fgem name, *version_requirements
|
13
|
+
rescue Gem::LoadError => e
|
14
|
+
|
15
|
+
# need this check to prevent very tricky bug caused by recursive retry and resulting in
|
16
|
+
# files will be required multiple times and strange error messages will be produced
|
17
|
+
raise e if e.raised_in_fake_gem
|
18
|
+
|
19
|
+
unless FakeGem.activate_gem(name)
|
20
|
+
e.raised_in_fake_gem = true
|
21
|
+
raise e
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
10
25
|
alias :require_without_fgem :require
|
11
26
|
def require path
|
12
27
|
require_without_fgem path
|
@@ -44,28 +59,15 @@ unless defined?(FakeGem) or defined?(JRuby)
|
|
44
59
|
|
45
60
|
module FakeGem
|
46
61
|
class FakeGemSpec
|
47
|
-
|
48
|
-
def initialize file
|
62
|
+
attr_reader :dir, :name, :libs
|
63
|
+
def initialize file
|
49
64
|
should_exist file
|
50
65
|
@dir, @libs = File.expand_path(File.dirname(file)), []
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
if args.empty?
|
56
|
-
@libs
|
57
|
-
else
|
58
|
-
args = args.first if args.first.is_a? Array
|
59
|
-
args = args.collect do |d|
|
60
|
-
d = d.to_s
|
61
|
-
d = (d =~ /^\//) ? d : "#{dir}/#{d}"
|
62
|
-
should_exist d, "file '%{file}' not exist (folder '#{dir}' is false_gem but the folder declared as :lib not exist)!"
|
63
|
-
d
|
64
|
-
end
|
65
|
-
@libs.push(*args)
|
66
|
-
end
|
66
|
+
|
67
|
+
require 'yaml'
|
68
|
+
spec = YAML.load_file file
|
69
|
+
self.name, self.libs = spec['name'], Array(spec['libs'])
|
67
70
|
end
|
68
|
-
attr_writer :libs
|
69
71
|
|
70
72
|
def inspect
|
71
73
|
relative_libs = @libs.collect{|l| l.sub("#{@dir}/", '')}
|
@@ -79,6 +81,18 @@ unless defined?(FakeGem) or defined?(JRuby)
|
|
79
81
|
raise msg.sub('%{file}', file) unless File.exist? file
|
80
82
|
file
|
81
83
|
end
|
84
|
+
|
85
|
+
def libs= libs
|
86
|
+
libs = libs.collect do |d|
|
87
|
+
d = d.to_s
|
88
|
+
d = (d =~ /^\//) ? d : "#{dir}/#{d}"
|
89
|
+
should_exist d, "file '%{file}' not exist (folder '#{dir}' is false_gem but the folder declared as :lib not exist)!"
|
90
|
+
d
|
91
|
+
end
|
92
|
+
@libs = libs
|
93
|
+
end
|
94
|
+
|
95
|
+
attr_writer :name
|
82
96
|
end
|
83
97
|
|
84
98
|
class << self
|
@@ -128,6 +142,22 @@ unless defined?(FakeGem) or defined?(JRuby)
|
|
128
142
|
false
|
129
143
|
end
|
130
144
|
end
|
145
|
+
|
146
|
+
# searches for that path in all libs inside of registered fake_gems and
|
147
|
+
# updates $LOAD_PATH if found.
|
148
|
+
def activate_gem name
|
149
|
+
found = gems.find{|gem_spec| gem_spec.name && (gem_spec.name == name)}
|
150
|
+
|
151
|
+
if found
|
152
|
+
gems.delete found
|
153
|
+
found.libs.each do |lib_path|
|
154
|
+
$LOAD_PATH << lib_path unless $LOAD_PATH.include? lib_path
|
155
|
+
end
|
156
|
+
true
|
157
|
+
else
|
158
|
+
false
|
159
|
+
end
|
160
|
+
end
|
131
161
|
|
132
162
|
# list of found false-gems
|
133
163
|
def gems
|
data/readme.md
CHANGED
@@ -1,61 +1,35 @@
|
|
1
|
-
|
1
|
+
FakeGem - very handy hack that makes any directory looks like gem and greatly simplifies gem development. Forget about compiling & pushing Your gems or changing the Bundler's Gemfile file.
|
2
|
+
Yes, there's the :path opion in Bundler, but it doesn't solve the problem - if You have multiple projects depending on multiple gems - then You have to change multiple paths in multiple Gemfiles, that's boing. And the FakeGem solves this problem very easily.
|
2
3
|
|
3
|
-
##
|
4
|
+
## Usage
|
4
5
|
|
5
|
-
|
6
|
-
|
6
|
+
``` bash
|
7
|
+
fake_gem ~/my_projects
|
8
|
+
```
|
7
9
|
|
8
|
-
|
9
|
-
can add standard gem's specification and it become real gem, without any change for other code that uses it.
|
10
|
+
Let's suppose Your projects are located in the /my_projects dir, and because You respect the 'divide and rule' principle Your app divided into 3 modules - the app itself and 2 gems:
|
10
11
|
|
11
|
-
|
12
|
+
```
|
13
|
+
~/my_projects
|
14
|
+
/app
|
15
|
+
/lib1
|
16
|
+
/lib2
|
17
|
+
```
|
18
|
+
|
19
|
+
You want lib1 and lib2 to be available as a gems while You are developing Your app, do following steps:
|
12
20
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
user_management.rb
|
23
|
-
fake_gem # libs :lib
|
24
|
-
|
25
|
-
/billing
|
26
|
-
/lib
|
27
|
-
billing.rb
|
28
|
-
fake_gem # libs :lib
|
29
|
-
|
30
|
-
/some-other-app
|
31
|
-
/lib
|
32
|
-
/some_other_app.rb
|
33
|
-
|
34
|
-
You should do the following steps:
|
35
|
-
|
36
|
-
1. create 2 files called 'fake_gem' with the "libs :lib" content inside each of 'user-management' and 'billing' libraries.
|
37
|
-
|
38
|
-
2. add the following lines to your 'app.rb' file:
|
39
|
-
|
40
|
-
require 'fake_gem'
|
41
|
-
FakeGem.paths '~/projects'
|
42
|
-
|
43
|
-
Now both of those libraries are available inside of your application, like that:
|
44
|
-
|
45
|
-
require 'user_management' # => true
|
46
|
-
require 'billing' # => true
|
47
|
-
|
48
|
-
FakeGem ignores and don't mess with your other projects don't marked with the 'fake_gem' label.
|
49
|
-
|
50
|
-
require 'some_other_app' # => Error, no such file!
|
51
|
-
|
52
|
-
**Note**: there's a better way, without affecting the 'app.rb' file. Add following lines to your ~/.profile
|
53
|
-
|
54
|
-
export FAKE_GEM_PATH="~/projects"
|
55
|
-
export RUBYOPT="-rrubygems -r<your path to the 'fake_gem' gem dir>/lib/fake_gem.rb"
|
56
|
-
|
57
|
-
now you don't need the step 2 (don't forget to reload your .profile changes by executing $source ~/.profile).
|
21
|
+
1. mark lib1 as fake_gem: create /projects/lib/fake_gem file and add there following lines:
|
22
|
+
name: lib1
|
23
|
+
libs: lib
|
24
|
+
2. do the same for lib2
|
25
|
+
3. enable fake_gem in current bash session, type:
|
26
|
+
$ fake_gem ~/projects
|
27
|
+
|
28
|
+
All done, now lib1 and lib2 will be availiable as real gems in ruby scripts.
|
29
|
+
No changes needed to source code, You can use real gems or fake gems - the application knows nothing about it.
|
58
30
|
|
59
31
|
## Installation
|
60
32
|
|
61
|
-
|
33
|
+
```
|
34
|
+
gem install fake_gem
|
35
|
+
```
|
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
name: project_a
|
2
|
+
libs: lib
|
@@ -1 +1 @@
|
|
1
|
-
libs
|
1
|
+
libs: lib
|
@@ -1 +1 @@
|
|
1
|
-
libs
|
1
|
+
libs: lib
|
data/spec/fake_gem_spec.rb
CHANGED
@@ -41,6 +41,15 @@ describe FakeGem do
|
|
41
41
|
$LOAD_PATH.should_not include("#{spec_dir}/common/project_b/lib")
|
42
42
|
end
|
43
43
|
|
44
|
+
it "hould load directories with fake_gem as gems" do
|
45
|
+
FakeGem.paths "#{spec_dir}/common"
|
46
|
+
FakeGem.gems.size.should == 1
|
47
|
+
|
48
|
+
gem 'project_a'
|
49
|
+
|
50
|
+
$LOAD_PATH.should include("#{spec_dir}/common/project_a/lib")
|
51
|
+
end
|
52
|
+
|
44
53
|
it "should load directories with fake_gem as gems" do
|
45
54
|
FakeGem.paths "#{spec_dir}/common"
|
46
55
|
FakeGem.gems.size.should == 1
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fake_gem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.13
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,14 +10,14 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-08-06 00:00:00 +04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
17
17
|
description:
|
18
18
|
email:
|
19
|
-
executables:
|
20
|
-
|
19
|
+
executables:
|
20
|
+
- fake_gem
|
21
21
|
extensions: []
|
22
22
|
|
23
23
|
extra_rdoc_files: []
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- spec/fake_gem_spec/twice/saas/fake_gem
|
36
36
|
- spec/fake_gem_spec/twice/saas/lib/the_saas.rb
|
37
37
|
- spec/fake_gem_spec.rb
|
38
|
+
- bin/fake_gem
|
38
39
|
has_rdoc: true
|
39
40
|
homepage: http://github.com/alexeypetrushin/fake_gem
|
40
41
|
licenses: []
|