basepath 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bin/basepath-init +38 -0
- data/lib/basepath.rb +76 -40
- metadata +25 -12
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/bin/basepath-init
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
open('.base', 'w') { |f| f.write DATA.read }
|
5
|
+
|
6
|
+
puts "Wrote #{Dir.pwd}/.base"
|
7
|
+
|
8
|
+
__END__
|
9
|
+
[consts]
|
10
|
+
# Constants defined here are available as Pathname objects to your application
|
11
|
+
# They can be defined as absolute or relative paths. Relative paths are relative
|
12
|
+
# to the directory containing `.base` (this file).
|
13
|
+
# LIB_PATH = lib
|
14
|
+
# DATA_PATH = ../data # you can refer paths outside of the BASE_PATH tree.
|
15
|
+
# IMG_PATH = DATA_PATH/img # when starting with a constant name, the path is
|
16
|
+
# # expanded relative to it.
|
17
|
+
|
18
|
+
[load_paths]
|
19
|
+
# Paths declared here are added to your `$LOAD_PATH`.
|
20
|
+
# The relative path and constant expansion rules are the same as for [consts]
|
21
|
+
# lib #
|
22
|
+
# LIB_PATH #
|
23
|
+
# vendor/*/lib #
|
24
|
+
|
25
|
+
[requires]
|
26
|
+
yaml
|
27
|
+
open-uri
|
28
|
+
fileutils
|
29
|
+
shellwords
|
30
|
+
open4
|
31
|
+
colored
|
32
|
+
unicode_utils
|
33
|
+
active_support
|
34
|
+
cgi
|
35
|
+
lib/model/*
|
36
|
+
|
37
|
+
[includes]
|
38
|
+
Open4
|
data/lib/basepath.rb
CHANGED
@@ -1,52 +1,88 @@
|
|
1
1
|
require 'pathname'
|
2
2
|
|
3
3
|
module Basepath
|
4
|
+
DOT_BASE = '.base'
|
4
5
|
extend self
|
5
6
|
|
6
7
|
def mine(file = false)
|
7
|
-
|
8
|
-
path = Pathname.new(
|
8
|
+
path_to_caller = path_from_caller_line(caller.last)
|
9
|
+
path = Pathname.new(path_to_caller).realpath
|
9
10
|
file ? path : path.dirname
|
10
11
|
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def path_from_caller_line(caller_line)
|
14
|
+
caller_line.sub(/:\d+(?::in `.*?')?$/, '').sub(/^\(\w+\)$/, '')
|
15
|
+
end
|
16
|
+
|
17
|
+
def find_base(start_path)
|
18
|
+
cur_path = start_path
|
19
|
+
got_base = lambda { cur_path.join(DOT_BASE).exist? }
|
20
|
+
cur_path = cur_path.parent until cur_path == cur_path.parent or got_base[]
|
21
|
+
cur_path if got_base[]
|
22
|
+
end
|
23
|
+
|
24
|
+
def find_base!
|
25
|
+
paths_tried = []
|
26
|
+
index_of_require_line = caller.index { |line| line =~ /`require'$/ } \
|
27
|
+
and caller_line_before_require = caller[index_of_require_line.succ]
|
28
|
+
if index_of_require_line && caller_line_before_require
|
29
|
+
path_from_requirer = Pathname.new(path_from_caller_line(caller_line_before_require)).realpath.dirname
|
30
|
+
base_from_requirer = find_base(path_from_requirer)
|
31
|
+
return base_from_requirer if base_from_requirer
|
32
|
+
paths_tried << path_from_requirer
|
33
|
+
end
|
34
|
+
path_from_pwd = Pathname.new(Dir.pwd).realpath
|
35
|
+
pwd_path_parent_of_requirer_path = index_of_require_line && "#{path_from_requirer}/".index("#{path_from_pwd}/") == 0
|
36
|
+
if not pwd_path_parent_of_requirer_path
|
37
|
+
base_from_pwd = find_base(path_from_pwd)
|
38
|
+
return base_from_pwd if base_from_pwd
|
39
|
+
paths_tried << path_from_pwd
|
40
|
+
end
|
41
|
+
err = "Can't find #{DOT_BASE} for BASE_PATH. (started at #{paths_tried.first}"
|
42
|
+
err << ", then tried #{paths_tried[1]}" if paths_tried[1]
|
43
|
+
err << ")"
|
44
|
+
raise err
|
45
|
+
end
|
46
|
+
|
47
|
+
def const_expand!(s, fallback = true)
|
48
|
+
k = s.sub!(RX_CONSTS, '') && Object.const_get($1)
|
49
|
+
return s if not k unless fallback
|
50
|
+
(k || ::BASE_PATH).join(s)
|
15
51
|
end
|
16
|
-
end
|
17
52
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
.
|
32
|
-
.
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
require lib
|
53
|
+
def resolve!
|
54
|
+
return if Object.const_defined?("BASE_PATH")
|
55
|
+
Object.const_set :BASE_PATH, find_base!
|
56
|
+
|
57
|
+
# read dot_base
|
58
|
+
base_conf = IO.read(::BASE_PATH.join(DOT_BASE)).strip.gsub(/[ \t]/, '').gsub(/\n+/, "\n")\
|
59
|
+
.scan(/^\[(\w+)\]((?:\n[^\[].*)*)/)\
|
60
|
+
.inject(Hash.new('')) { |h, (k, s)| h[k.to_sym] = s.strip; h }
|
61
|
+
base_conf.values.each { |s| s.gsub!(/\s*#.*\n/, "\n") }
|
62
|
+
|
63
|
+
# set path consts
|
64
|
+
k_order = [] # ruby 1.8 doesn't retain hash key order
|
65
|
+
consts = base_conf[:consts].scan(/([A-Z][A-Z0-9_]*)=(.+)/).inject({}) { |h, (k, v)| k_order << k; h[k] = v; h }
|
66
|
+
const_set :RX_CONSTS, /^(BASE_PATH|#{consts.keys.map(&Regexp.method(:escape)).join('|')})(?:\/|$)/
|
67
|
+
k_order.each { |k| Object.const_set(k, const_expand!(consts[k])) }
|
68
|
+
|
69
|
+
# set load_paths
|
70
|
+
load_paths = base_conf[:load_paths].split("\n").map { |s|
|
71
|
+
Dir[const_expand!(s).to_s] }.flatten.select { |s|
|
72
|
+
File.directory? s }
|
73
|
+
$LOAD_PATH.unshift(*load_paths)
|
74
|
+
|
75
|
+
# requires
|
76
|
+
loaded = caller(0).map { |s| s[/\A(.+?)(?:\.rb)?:\d+(?::in `.*?')?\z/, 1] }.compact.uniq
|
77
|
+
globs, names = base_conf[:requires].split("\n").partition { |s| s =~ /\*/ }
|
78
|
+
names.map! { |s| const_expand! s, false }.concat \
|
79
|
+
globs.map { |s| Dir[const_expand!(s).to_s + ".rb"] }\
|
80
|
+
.flatten.select { |s| File.file? s }.map { |s| s.sub(/\.rb$/, '') }
|
81
|
+
names.each { |lib| require lib }
|
82
|
+
|
83
|
+
# includes
|
84
|
+
base_conf[:includes].split("\n").each { |k| Object.send :include, Object.const_get(k.strip) }
|
51
85
|
end
|
52
|
-
end
|
86
|
+
end
|
87
|
+
|
88
|
+
Basepath.resolve!
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basepath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 4
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Caio Chassot
|
@@ -9,23 +14,26 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
13
|
-
default_executable:
|
17
|
+
date: 2010-07-28 00:00:00 -03:00
|
18
|
+
default_executable: basepath-init
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: bacon
|
17
|
-
|
18
|
-
|
19
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
20
25
|
requirements:
|
21
26
|
- - ">="
|
22
27
|
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
23
30
|
version: "0"
|
24
|
-
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
25
33
|
description: By adding a .base file to your application base dir, helps you augment $LOAD_PATH, auto-require files, and set constants to important paths.
|
26
34
|
email: dev@caiochassot.com
|
27
|
-
executables:
|
28
|
-
|
35
|
+
executables:
|
36
|
+
- basepath-init
|
29
37
|
extensions: []
|
30
38
|
|
31
39
|
extra_rdoc_files:
|
@@ -41,6 +49,7 @@ files:
|
|
41
49
|
- lib/basepath.rb
|
42
50
|
- spec/basepath_spec.rb
|
43
51
|
- spec/spec_helper.rb
|
52
|
+
- bin/basepath-init
|
44
53
|
has_rdoc: true
|
45
54
|
homepage: http://github.com/kch/basepath
|
46
55
|
licenses: []
|
@@ -51,21 +60,25 @@ rdoc_options:
|
|
51
60
|
require_paths:
|
52
61
|
- lib
|
53
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
54
64
|
requirements:
|
55
65
|
- - ">="
|
56
66
|
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
57
69
|
version: "0"
|
58
|
-
version:
|
59
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
60
72
|
requirements:
|
61
73
|
- - ">="
|
62
74
|
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
63
77
|
version: "0"
|
64
|
-
version:
|
65
78
|
requirements: []
|
66
79
|
|
67
80
|
rubyforge_project:
|
68
|
-
rubygems_version: 1.3.
|
81
|
+
rubygems_version: 1.3.7
|
69
82
|
signing_key:
|
70
83
|
specification_version: 3
|
71
84
|
summary: Define you application base path for easy requires and general access to files.
|