File: extensions.rb

package info (click to toggle)
passenger 2.2.11debian-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,576 kB
  • ctags: 28,138
  • sloc: cpp: 66,323; ruby: 9,646; ansic: 2,425; python: 141; sh: 56; makefile: 29
file content (182 lines) | stat: -rw-r--r-- 4,488 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#  Phusion Passenger - http://www.modrails.com/
#  Copyright (c) 2008, 2009 Phusion
#
#  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

require 'pathname'

# Provides useful extensions for Rake.
module RakeExtensions

# Allows one to define Rake rules in the context of the given
# subdirectory. For example,
#
#  subdir 'foo' do
#     file 'libfoo.so' => ['foo.c'] do
#        sh 'gcc foo.c -shared -fPIC -o libfoo.so'
#     end
#  end
#  
#  subdir 'bar' do
#     file 'bar' => ['bar.c', '../foo/libfoo.so'] do
#         sh 'gcc bar.c -o bar -L../foo -lfoo'
#     end
#  end
#
# is equivalent to:
#
#  file 'foo/libfoo.so' => ['foo/foo.c'] do
#     Dir.chdir('foo') do
#        sh 'gcc foo.c -shared -fPIC -o libfoo.so'
#     end
#  end
#  
#  file 'bar/bar' => ['bar/bar.c', 'foo/libfoo.so'] do
#      Dir.chdir('bar') do
#         sh 'gcc bar.c -o bar -L../foo -lfoo'
#      end
#  end
#
# === String dependencies are assumed to be filenames
#
# But be careful with string dependencies. They are assumed to be filenames,
# and will be automatically converted. For example:
#
#  subdir 'foo' do
#     task 'super_app' => ['super_app:compile', 'super_app:unit_test']
#  
#     task 'super_app:compile' do
#        ...
#     end
#  
#     task 'super_app:unit_test' do
#        ...
#     end
#  end
#
# will be treated like:
#
#  subdir 'foo' do
#     # !!!!!!!
#     task 'super_app' => ['foo/super_app:compile', 'foo/super_app:unit_test']
#  
#     task 'super_app:compile' do
#        ...
#     end
#  
#     task 'super_app:unit_test' do
#        ...
#     end
#  end
#
# To solve this, declare your dependencies as symbols:
#
#  task 'super_app' => [:'super_app:compile', :'super_app:unit_test']
#
# (note the leading ':' character)
#
# === Supported Rake commands
#
# Only the <tt>file</tt> and <tt>target</tt> Rake commands are supported.
def subdir(dir, &block)
	subdir = Subdir.new(dir)
	Dir.chdir(dir) do
		subdir.instance_eval(&block)
	end
end

class Subdir # :nodoc:
	def initialize(dir)
		@dir = dir
		@toplevel_dir = Pathname.getwd
	end
	
	def file(args, &block)
		case args
		when String
			args = mangle_path(args)
		when Hash
			target = mangle_path(args.keys[0])
			sources = mangle_path_or_path_array(args.values[0])
			args = { target => sources }
		end
		Rake::FileTask.define_task(args) do
			puts "### In #{@dir}:"
			Dir.chdir(@dir) do
				Object.class_eval(&block)
			end
			puts ""
		end
	end
	
	def task(*args, &block)
		if !args.empty? && args[0].is_a?(Hash)
			target = args[0].keys[0]
			sources = mangle_path_or_path_array(args[0].values[0])
			args[0] = { target => sources }
		end
		if block_given?
			Rake::Task.define_task(*args) do
				puts "### In #{@dir}:"
				Dir.chdir(@dir) do
					Object.class_eval(&block)
				end
				puts ""
			end
		else
			Rake::Task.define_task(*args)
		end
	end

private
	def mangle_path(path)
		path = File.expand_path(path)
		return Pathname.new(path).relative_path_from(@toplevel_dir).to_s
	end
	
	def mangle_path_array(array)
		array = array.dup
		array.each_with_index do |item, i|
			if item.is_a?(String)
				array[i] = mangle_path(item)
			end
		end
		return array
	end
	
	def mangle_path_or_path_array(item)
		case item
		when String
			return mangle_path(item)
		when Array
			return mangle_path_array(item)
		else
			return item
		end
	end
end

end # module RakeExtensions

Object.class_eval do
	include RakeExtensions
end