fake_gem 0.1.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.
- data/Rakefile +60 -0
- data/lib/fake_gem.rb +119 -0
- data/readme.md +61 -0
- data/spec/fake_gem_spec/project_a/fake_gem.rb +1 -0
- data/spec/fake_gem_spec/project_a/lib/project_a_file.rb +0 -0
- data/spec/fake_gem_spec/project_b/lib/project_b_file.rb +0 -0
- data/spec/fake_gem_spec.rb +57 -0
- metadata +73 -0
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
Dir.chdir File.dirname(__FILE__)
|
5
|
+
|
6
|
+
# Specs
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
10
|
+
t.spec_files = FileList["spec/**/*_spec.rb"].select{|f| f !~ /\/_/}
|
11
|
+
t.libs = ['lib'].collect{|f| "#{File.dirname __FILE__}/#{f}"}
|
12
|
+
# t.spec_opts = ["--backtrace"]
|
13
|
+
end
|
14
|
+
|
15
|
+
# Gem
|
16
|
+
require 'rake/clean'
|
17
|
+
require 'rake/gempackagetask'
|
18
|
+
require 'fileutils'
|
19
|
+
|
20
|
+
spec = Gem::Specification.new do |s|
|
21
|
+
s.name = "fake_gem"
|
22
|
+
s.version = "0.1.1"
|
23
|
+
s.summary = "Makes any directory looks like Ruby Gem"
|
24
|
+
s.description = "Makes any directory looks like Ruby Gem"
|
25
|
+
s.author = "Alexey Petrushin"
|
26
|
+
# s.email = ""
|
27
|
+
s.homepage = "http://github.com/alexeypetrushin/fake_gem"
|
28
|
+
|
29
|
+
s.platform = Gem::Platform::RUBY
|
30
|
+
s.has_rdoc = true
|
31
|
+
|
32
|
+
s.files = (['Rakefile', 'readme.md'] + Dir.glob("{lib,spec}/**/*"))
|
33
|
+
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
end
|
36
|
+
|
37
|
+
PACKAGE_DIR = "#{File.expand_path File.dirname(__FILE__)}/build"
|
38
|
+
|
39
|
+
Rake::GemPackageTask.new(spec) do |p|
|
40
|
+
package_dir = PACKAGE_DIR
|
41
|
+
# FileUtils.mkdir package_dir unless File.exist? package_dir
|
42
|
+
p.need_tar = true if RUBY_PLATFORM !~ /mswin/
|
43
|
+
p.need_zip = true
|
44
|
+
p.package_dir = package_dir
|
45
|
+
end
|
46
|
+
|
47
|
+
# CLEAN.include [ 'pkg', '*.gem']
|
48
|
+
|
49
|
+
task :push do
|
50
|
+
dir = Dir.chdir PACKAGE_DIR do
|
51
|
+
gem_file = Dir.glob("fake_gem*.gem").first
|
52
|
+
system "gem push #{gem_file}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
task :clean do
|
57
|
+
system "rm -r #{PACKAGE_DIR}"
|
58
|
+
end
|
59
|
+
|
60
|
+
task :release => [:gem, :push, :clean]
|
data/lib/fake_gem.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# If you inject this module wia RUBYOPTS it may be already be processed,
|
2
|
+
# and include('fake_gem') will executes it one more time, so we check it manually.
|
3
|
+
unless defined? FakeGem
|
4
|
+
alias :require_without_fgem :require
|
5
|
+
def require path
|
6
|
+
require_without_fgem path
|
7
|
+
rescue LoadError => e
|
8
|
+
FakeGem.activate(path + '.rb') ? retry : raise(e)
|
9
|
+
end
|
10
|
+
|
11
|
+
alias :load_without_fgem :load
|
12
|
+
def load path, wrap = false
|
13
|
+
load_without_fgem path, wrap
|
14
|
+
rescue LoadError => e
|
15
|
+
FakeGem.activate(path) ? retry : raise(e)
|
16
|
+
end
|
17
|
+
|
18
|
+
module FakeGem
|
19
|
+
class FakeGemSpec
|
20
|
+
attr_accessor :dir
|
21
|
+
def initialize file
|
22
|
+
should_exist file
|
23
|
+
@dir, @libs = File.expand_path(File.dirname(file)), []
|
24
|
+
instance_eval File.read(file), __FILE__, __LINE__
|
25
|
+
end
|
26
|
+
|
27
|
+
def libs *args
|
28
|
+
if args.empty?
|
29
|
+
@libs
|
30
|
+
else
|
31
|
+
args = args.first if args.first.is_a? Array
|
32
|
+
args = args.collect do |d|
|
33
|
+
d = d.to_s
|
34
|
+
d = (d =~ /^\//) ? d : "#{dir}/#{d}"
|
35
|
+
should_exist d
|
36
|
+
d
|
37
|
+
end
|
38
|
+
@libs.push *args
|
39
|
+
end
|
40
|
+
end
|
41
|
+
attr_writer :libs
|
42
|
+
|
43
|
+
protected
|
44
|
+
def should_exist file
|
45
|
+
raise "File #{file} not exist!" unless File.exist? file
|
46
|
+
file
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class << self
|
51
|
+
# Use it to set location for your fake_gems
|
52
|
+
def paths *paths
|
53
|
+
paths = paths.first if paths.first.is_a? Array
|
54
|
+
if paths.empty?
|
55
|
+
unless @paths
|
56
|
+
if env_paths = ENV['FALSE_GEM_PATH']
|
57
|
+
self.paths = env_paths.split(':')
|
58
|
+
else
|
59
|
+
self.paths = []
|
60
|
+
end
|
61
|
+
end
|
62
|
+
@paths
|
63
|
+
else
|
64
|
+
self.paths = paths
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def paths= paths
|
69
|
+
@paths = paths.collect{|l| File.expand_path l}
|
70
|
+
end
|
71
|
+
|
72
|
+
# searches for that path in all libs inside of registered fake_gems and
|
73
|
+
# updates $LOAD_PATH if found.
|
74
|
+
def activate path
|
75
|
+
found = nil
|
76
|
+
catch :found do
|
77
|
+
gems.index do |gem_spec|
|
78
|
+
gem_spec.libs.each do |lib_path|
|
79
|
+
if File.exist? "#{lib_path}/#{path}"
|
80
|
+
found = gem_spec
|
81
|
+
throw :found
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
if found
|
88
|
+
gems.delete found
|
89
|
+
found.libs.each do |lib_path|
|
90
|
+
$LOAD_PATH << lib_path unless $LOAD_PATH.include? lib_path
|
91
|
+
end
|
92
|
+
true
|
93
|
+
else
|
94
|
+
false
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# list of found false-gems
|
99
|
+
def gems
|
100
|
+
unless @gems
|
101
|
+
@gems = []
|
102
|
+
paths.each do |location|
|
103
|
+
Dir.glob("#{location}/*/fake_gem.rb").each do |gem_spec_file|
|
104
|
+
@gems << FakeGemSpec.new(gem_spec_file) unless gem_spec_file =~ /\/fake_gem\/fake_gem\.rb/
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
@gems
|
109
|
+
end
|
110
|
+
attr_writer :gems
|
111
|
+
|
112
|
+
def clear
|
113
|
+
@paths, @gems = nil
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Makes any directory looks like Ruby Gem
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
|
5
|
+
Why? Becouse I want my projects be accessible as a gem, but without overhead like providing gem spec, rebuild and pushing it with every update.
|
6
|
+
I need only the most basic functional of gem system - automatic path resolving. And the **fake_gem** does exactly that.
|
7
|
+
|
8
|
+
**Note**: it's not a gem replacement, It's just a handy hack to simplify development in cases when you don't need all the gem power. At any time you
|
9
|
+
can add standard gem's specification and it become real gem, without any change for other code that uses it.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Let's say you have the 'application' project and want to use 'user-management' and 'billing' libraries in it.
|
14
|
+
|
15
|
+
~/projects
|
16
|
+
/application
|
17
|
+
/lib
|
18
|
+
/app.rb
|
19
|
+
|
20
|
+
/user-management
|
21
|
+
/lib
|
22
|
+
user_management.rb
|
23
|
+
fake_gem.rb # libs :lib
|
24
|
+
|
25
|
+
/billing
|
26
|
+
/lib
|
27
|
+
billing.rb
|
28
|
+
fake_gem.rb # 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.rb' 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.rb' 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 FALSE_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).
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
|
61
|
+
$ sudo gem install fake_gem
|
@@ -0,0 +1 @@
|
|
1
|
+
libs :lib
|
File without changes
|
File without changes
|
@@ -0,0 +1,57 @@
|
|
1
|
+
dir = File.expand_path(File.dirname(__FILE__) + "/..")
|
2
|
+
$LOAD_PATH << "#{dir}/lib" unless $LOAD_PATH.include? "#{dir}/lib"
|
3
|
+
|
4
|
+
require 'spec'
|
5
|
+
require "fake_gem"
|
6
|
+
|
7
|
+
describe FakeGem do
|
8
|
+
before :each do
|
9
|
+
@old_load_path, @old_env = $LOAD_PATH.clone, ENV['FALSE_GEM_PATH']
|
10
|
+
ENV['FALSE_GEM_PATH'] = nil
|
11
|
+
FakeGem.clear
|
12
|
+
|
13
|
+
@data_dir = File.expand_path(__FILE__.sub(/\.rb$/, ''))
|
14
|
+
end
|
15
|
+
|
16
|
+
after :each do
|
17
|
+
ENV['FALSE_GEM_PATH'] = @old_env
|
18
|
+
$LOAD_PATH.replace @old_load_path
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should use FALSE_GEM_PATH environment variable or paths assigned to FakeGem::paths attribute" do
|
22
|
+
FakeGem.paths.should == []
|
23
|
+
|
24
|
+
FakeGem.clear
|
25
|
+
ENV['FALSE_GEM_PATH'] = "/first_path:/second_path"
|
26
|
+
FakeGem.paths.should == %w(/first_path /second_path)
|
27
|
+
|
28
|
+
FakeGem.paths '.'
|
29
|
+
FakeGem.paths.should == [File.expand_path('.')]
|
30
|
+
|
31
|
+
FakeGem.clear
|
32
|
+
FakeGem.paths = ['.']
|
33
|
+
FakeGem.paths.should == [File.expand_path('.')]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should require directories with fake_gem.rb as gems" do
|
37
|
+
FakeGem.paths @data_dir
|
38
|
+
FakeGem.gems.size.should == 1
|
39
|
+
|
40
|
+
require 'project_a_file'
|
41
|
+
lambda{require 'project_b_file'}.should raise_error(/no such file to load/)
|
42
|
+
|
43
|
+
$LOAD_PATH.should include("#{@data_dir}/project_a/lib")
|
44
|
+
$LOAD_PATH.should_not include("#{@data_dir}/project_b/lib")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should load directories with fake_gem.rb as gems" do
|
48
|
+
FakeGem.paths @data_dir
|
49
|
+
FakeGem.gems.size.should == 1
|
50
|
+
|
51
|
+
load 'project_a_file.rb'
|
52
|
+
lambda{load 'project_b_file.rb'}.should raise_error(/no such file to load/)
|
53
|
+
|
54
|
+
$LOAD_PATH.should include("#{@data_dir}/project_a/lib")
|
55
|
+
$LOAD_PATH.should_not include("#{@data_dir}/project_b/lib")
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fake_gem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Alexey Petrushin
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-09 00:00:00 +04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Makes any directory looks like Ruby Gem
|
23
|
+
email:
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- Rakefile
|
32
|
+
- readme.md
|
33
|
+
- lib/fake_gem.rb
|
34
|
+
- spec/fake_gem_spec/project_a/fake_gem.rb
|
35
|
+
- spec/fake_gem_spec/project_a/lib/project_a_file.rb
|
36
|
+
- spec/fake_gem_spec/project_b/lib/project_b_file.rb
|
37
|
+
- spec/fake_gem_spec.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/alexeypetrushin/fake_gem
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Makes any directory looks like Ruby Gem
|
72
|
+
test_files: []
|
73
|
+
|