|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -begin |
4 | | - require 'active_support' |
5 | | - require 'active_support/core_ext' |
6 | | -rescue NameError |
7 | | - warn 'active_support is not available. AppMap execution will continue optimistically without it...' |
8 | | -end |
| 3 | +# This is the file that's loaded when you require 'appmap'. |
| 4 | +# If you require this file, we assume that you want to automatically activate all |
| 5 | +# the AppMap functionality that seems suitable for your project. |
| 6 | +# For example, if your project is a Rails project, the Railtie will be loaded. |
| 7 | +# If your bundle includes rspec, the appmap/rspec will be loaded. |
| 8 | +# |
| 9 | +# If you don't want this "all-in" behavior, then you can use the 'require' option |
| 10 | +# in your Gemfile to selectively activate just the AppMap features that you want. Then |
| 11 | +# you can manually configure/require other features, elsewhere in your code. |
| 12 | +# Note that you should always require 'appmap/agent' as early as possible, so that it can |
| 13 | +# observe and hook as much code loading as possible. |
| 14 | +# |
| 15 | +# Modules that you can load independently include: |
| 16 | +# - appmap/agent |
| 17 | +# - appmap/railtie |
| 18 | +# - appmap/rspec |
| 19 | +# - appmap/minitest |
| 20 | +# - appmap/swagger (Rake task) |
9 | 21 |
|
10 | 22 | require 'appmap/version' |
11 | | -require 'appmap/util' |
12 | | -require 'appmap/hook' |
13 | | -require 'appmap/config' |
14 | | -require 'appmap/trace' |
15 | | -require 'appmap/class_map' |
16 | | -require 'appmap/metadata' |
17 | | -require 'appmap/open' |
18 | | - |
19 | | -# load extension |
20 | | -require 'appmap/appmap' |
21 | | - |
22 | | -module AppMap |
23 | | - class << self |
24 | | - @configuration = nil |
25 | | - @configuration_file_path = nil |
26 | | - |
27 | | - # Gets the configuration. If there is no configuration, the default |
28 | | - # configuration is initialized. |
29 | | - def configuration |
30 | | - @configuration ||= initialize_configuration |
31 | | - end |
32 | | - |
33 | | - # Sets the configuration. This is only expected to happen once per |
34 | | - # Ruby process. |
35 | | - def configuration=(config) |
36 | | - warn 'AppMap is already configured' if @configuration && config |
37 | | - |
38 | | - @configuration = config |
39 | | - end |
40 | | - |
41 | | - def default_config_file_path |
42 | | - ENV['APPMAP_CONFIG_FILE'] || 'appmap.yml' |
43 | | - end |
44 | | - |
45 | | - # Configures AppMap for recording. Default behavior is to configure from |
46 | | - # APPMAP_CONFIG_FILE, or 'appmap.yml'. If no config file is available, a |
47 | | - # configuration will be automatically generated and used - and the user is prompted |
48 | | - # to create the config file. |
49 | | - # |
50 | | - # This method also activates the code hooks which record function calls as trace events. |
51 | | - # Call this function before the program code is loaded by the Ruby VM, otherwise |
52 | | - # the load events won't be seen and the hooks won't activate. |
53 | | - def initialize_configuration(config_file_path = default_config_file_path) |
54 | | - Util.startup_message "Configuring AppMap from path #{config_file_path}" |
55 | | - Config.load_from_file(config_file_path).tap do |configuration| |
56 | | - self.configuration = configuration |
57 | | - Hook.new(configuration).enable |
58 | | - end |
59 | | - end |
60 | | - |
61 | | - def info(msg) |
62 | | - if defined?(::Rails) && defined?(::Rails.logger) |
63 | | - ::Rails.logger.info msg |
64 | | - else |
65 | | - warn msg |
66 | | - end |
67 | | - end |
68 | | - |
69 | | - # Used to start tracing, stop tracing, and record events. |
70 | | - def tracing |
71 | | - @tracing ||= Trace::Tracing.new |
72 | | - end |
73 | | - |
74 | | - # Records the events which occur while processing a block, |
75 | | - # and returns an AppMap as a Hash. |
76 | | - def record |
77 | | - tracer = tracing.trace |
78 | | - begin |
79 | | - yield |
80 | | - ensure |
81 | | - tracing.delete(tracer) |
82 | | - end |
83 | | - |
84 | | - events = [].tap do |event_list| |
85 | | - event_list << tracer.next_event.to_h while tracer.event? |
86 | | - end |
87 | | - { |
88 | | - 'version' => AppMap::APPMAP_FORMAT_VERSION, |
89 | | - 'metadata' => detect_metadata, |
90 | | - 'classMap' => class_map(tracer.event_methods), |
91 | | - 'events' => events |
92 | | - } |
93 | | - end |
94 | | - |
95 | | - # Uploads an AppMap to the AppLand website and displays it. |
96 | | - def open(appmap = nil, &block) |
97 | | - appmap ||= AppMap.record(&block) |
98 | | - AppMap::Open.new(appmap).perform |
99 | | - end |
100 | | - |
101 | | - # Builds a class map from a config and a list of Ruby methods. |
102 | | - def class_map(methods) |
103 | | - ClassMap.build_from_methods(methods) |
104 | | - end |
105 | | - |
106 | | - # Returns default metadata detected from the Ruby system and from the |
107 | | - # filesystem. |
108 | | - def detect_metadata |
109 | | - @metadata ||= Metadata.detect.freeze |
110 | | - @metadata.deep_dup |
111 | | - end |
112 | | - end |
113 | | -end |
| 23 | +require 'appmap/agent' |
114 | 24 |
|
115 | 25 | lambda do |
116 | 26 | Initializer = Struct.new(:class_name, :module_name, :gem_module_name) |
117 | 27 |
|
118 | 28 | INITIALIZERS = { |
119 | 29 | 'Rails::Railtie' => Initializer.new('AppMap::Railtie', 'appmap/railtie', 'railtie'), |
120 | 30 | 'RSpec' => Initializer.new('AppMap::RSpec', 'appmap/rspec', 'rspec-core'), |
121 | | - 'Minitest::Unit::TestCase' => Initializer.new('AppMap::Minitest', 'appmap/minitest', 'minitest') |
| 31 | + 'Minitest::Unit::TestCase' => Initializer.new('AppMap::Minitest', 'appmap/minitest', 'minitest'), |
| 32 | + 'Rake' => [ |
| 33 | + Initializer.new('AppMap::Swagger', 'appmap/swagger', 'rake') |
| 34 | + ] |
122 | 35 | } |
123 | 36 |
|
124 | 37 | TracePoint.new(:class) do |tp| |
@@ -155,6 +68,11 @@ def detect_metadata |
155 | 68 | if defined?(::Minitest) |
156 | 69 | require 'appmap/minitest' |
157 | 70 | end |
| 71 | + |
| 72 | + if defined?(::Rake) |
| 73 | + require 'appmap/swagger' |
| 74 | + end |
| 75 | + |
158 | 76 | end.call |
159 | 77 |
|
160 | 78 | AppMap.initialize_configuration if ENV['APPMAP'] == 'true' |
0 commit comments