forked from jruby/warbler
/
traits.rb
106 lines (86 loc) · 2.64 KB
/
traits.rb
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
#--
# Copyright (c) 2010-2011 Engine Yard, Inc.
# Copyright (c) 2007-2009 Sun Microsystems, Inc.
# This source code is available under the MIT license.
# See the file LICENSE.txt for details.
#++
require 'stringio'
module Warbler
# Traits are project configuration characteristics that correspond
# to the framework or project layout. Each trait corresponds to a
# class in Warbler::Traits that contains baked-in knowledge about
# the kind of project and how it should be packed into the jar or
# war file.
module Traits
attr_accessor :traits
def initialize
@traits = auto_detect_traits
end
def auto_detect_traits
Traits.constants.map {|t| Traits.const_get(t)}.select {|tc| tc.detect? }.sort
end
def before_configure
trait_objects.each {|t| t.before_configure }
end
def after_configure
trait_objects.each {|t| t.after_configure }
end
def trait_objects
@trait_objects ||= @traits.map {|klass| klass.new(self) }
end
def update_archive(jar)
trait_objects.each {|t| t.update_archive(jar) }
end
def dump_traits
@trait_objects = nil
@traits.collect! {|t| t.name }
end
end
# Each trait class includes this module to receive shared functionality.
module Trait
module ClassMethods
def <=>(o)
requires?(o) ? 1 : (o.requires?(self) ? -1 : 0)
end
def requires?(t)
false
end
end
def self.included(base)
base.extend ClassMethods
end
attr_reader :config
def initialize(config)
@config = config
end
def before_configure
end
def after_configure
end
def update_archive(jar)
end
def add_init_load_path(path)
config.init_contents << StringIO.new("$LOAD_PATH.unshift __FILE__.sub(/!.*/, '!/#{path}')\n")
end
def add_main_rb(jar, bin_path)
jar.files['META-INF/main.rb'] = StringIO.new("load '#{bin_path}'")
end
def update_gem_path(default_gem_path)
if config.gem_path != default_gem_path
config.gem_path = "/#{config.gem_path}" unless config.gem_path =~ %r{^/}
sub_gem_path = config.gem_path[1..-1]
config.pathmaps.gemspecs.each {|p| p.sub!(default_gem_path[1..-1], sub_gem_path)}
config.pathmaps.gems.each {|p| p.sub!(default_gem_path[1..-1], sub_gem_path)}
config.webxml["gem"]["path"] = config.gem_path if config.webxml
end
end
end
end
require 'warbler/traits/jar'
require 'warbler/traits/war'
require 'warbler/traits/rails'
require 'warbler/traits/merb'
require 'warbler/traits/rack'
require 'warbler/traits/bundler'
require 'warbler/traits/gemspec'
require 'warbler/traits/nogemspec'