1
+ # @note needs to use explicit nesting. so this file can be loaded directly without loading 'metasploit/framework', this
2
+ # file can be used prior to Bundler.require.
3
+ module Metasploit
4
+ module Framework
5
+ # Extension to `Kernel#require` behavior.
6
+ module Require
7
+ #
8
+ # Module Methods
9
+ #
10
+
11
+ # Tries to require `name`. If a `LoadError` occurs, then `without_warning` is printed to standard error using
12
+ # `Kernel#warn`, along with instructions for reinstalling the bundle. If a `LoadError` does not occur, then
13
+ # `with_block` is called.
14
+ #
15
+ # @param name [String] the name of the library to `Kernel#require`.
16
+ # @param without_warning [String] warning to print if `name` cannot be required.
17
+ # @yield block to run when `name` requires successfully
18
+ # @yieldreturn [void]
19
+ # @return [void]
20
+ def self . optionally ( name , without_warning )
21
+ begin
22
+ require name
23
+ rescue LoadError
24
+ warn without_warning
25
+ warn "Bundle installed '--without #{ Bundler . settings . without . join ( ' ' ) } '"
26
+ warn "To clear the without option do `bundle install --without ''` " \
27
+ "(the --without flag with an empty string) or " \
28
+ "`rm -rf .bundle` to remove the .bundle/config manually and " \
29
+ "then `bundle install`"
30
+ else
31
+ if block_given?
32
+ yield
33
+ end
34
+ end
35
+ end
36
+
37
+ # Tries to `require 'active_record/railtie'` to define the activerecord Rails initializers and rake tasks.
38
+ #
39
+ # @example Optionally requiring 'active_record/railtie'
40
+ # require 'metasploit/framework/require'
41
+ #
42
+ # class MyClass
43
+ # def setup
44
+ # if database_enabled
45
+ # Metasploit::Framework::Require.optionally_active_record_railtie
46
+ # end
47
+ # end
48
+ # end
49
+ #
50
+ # @return [void]
51
+ def self . optionally_active_record_railtie
52
+ optionally (
53
+ 'active_record/railtie' ,
54
+ 'activerecord not in the bundle, so database support will be disabled.'
55
+ )
56
+ end
57
+
58
+ # Tries to `require 'metasploit/credential/creation'` and include it in the `including_module`.
59
+ #
60
+ # @param including_module [Module] `Class` or `Module` that wants to `include Metasploit::Credential::Creation`.
61
+ # @return [void]
62
+ def self . optionally_include_metasploit_credential_creation ( including_module )
63
+ optionally (
64
+ 'metasploit/credential/creation' ,
65
+ "metasploit-credential not in the bundle, so Metasploit::Credential creation will fail for #{ including_module . name } " ,
66
+ ) do
67
+ including_module . send ( :include , Metasploit ::Credential ::Creation )
68
+ end
69
+ end
70
+
71
+ #
72
+ # Instance Methods
73
+ #
74
+
75
+ # Tries to `require 'metasploit/credential/creation'` and include it in this `Class` or `Module`.
76
+ #
77
+ # @example Using in a `Module`
78
+ # require 'metasploit/framework/require'
79
+ #
80
+ # module MyModule
81
+ # extend Metasploit::Framework::Require
82
+ #
83
+ # optionally_include_metasploit_credential_creation
84
+ # end
85
+ #
86
+ # @return [void]
87
+ def optionally_include_metasploit_credential_creation
88
+ Metasploit ::Framework ::Require . optionally_include_metasploit_credential_creation ( self )
89
+ end
90
+ end
91
+ end
92
+ end
0 commit comments