-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathpackage_manager.rb
183 lines (154 loc) · 5.63 KB
/
package_manager.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
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
183
# frozen_string_literal: false
module LicenseFinder
# Super-class for the different package managers
# (Bundler, NPM, Pip, etc.)
#
# For guidance on adding a new package manager use the shared behavior
#
# it_behaves_like "a PackageManager"
#
# Additional guidelines are:
#
# - implement #current_packages, to return a list of `Package`s this package manager is tracking
# - implement #possible_package_paths, an array of `Pathname`s which are the possible locations which contain a configuration file/folder indicating the package manager is in use.
# - implement(Optional) #package_management_command, string for invoking the package manager
# - implement(Optional) #prepare_command, string for fetching dependencies for package manager (runs when the --prepare flag is passed to license_finder)
class PackageManager
include LicenseFinder::SharedHelpers
class << self
def takes_priority_over
nil
end
def id
name.split('::').last.downcase
end
end
def installed?(logger = Core.default_logger)
if package_management_command.nil?
logger.debug self.class, 'no command defined' # TODO: comment me out
true
elsif command_exists?(package_management_command)
logger.debug self.class, 'is installed', color: :green
true
else
logger.info self.class, 'is not installed', color: :red
false
end
end
# see class description
def package_management_command
nil
end
# see class description
def prepare_command
nil
end
def command_exists?(command)
_stdout, _stderr, status =
if LicenseFinder::Platform.windows?
Cmd.run("where #{command}")
else
Cmd.run("which #{command}")
end
status.success?
end
def initialize(options = {})
@prepare_no_fail = options[:prepare_no_fail]
@logger = options[:logger] || Core.default_logger
@project_path = options[:project_path]
@log_directory = options[:log_directory]
@ignored_groups = options[:ignored_groups]
end
def active?
path = detected_package_path
path&.exist?
end
def project_root?
active?
end
def detected_package_path
possible_package_paths.find(&:exist?)
end
def prepare
if prepare_command
stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(prepare_command) }
unless status.success?
log_errors stderr
error_message = "Prepare command '#{prepare_command}' failed\n#{stderr}"
error_message == error_message.concat("\n#{stdout}\n") if !stdout.nil? && !stdout.empty?
raise error_message unless @prepare_no_fail
end
else
logger.debug self.class, 'no prepare step provided', color: :red
end
end
def current_packages_with_relations
begin
packages = current_packages
rescue StandardError => e
raise e unless @prepare_no_fail
packages = []
end
packages.each do |parent|
parent.children.each do |child_name|
child = packages.detect { |child_package| child_package.name == child_name }
child.parents << parent.name if child
end
end
packages
end
private
attr_reader :logger, :project_path
def log_errors(stderr)
log_errors_with_cmd(prepare_command, stderr)
end
def log_errors_with_cmd(prep_cmd, stderr)
logger.info(prep_cmd, 'did not succeed.', color: :red)
logger.info(prep_cmd, stderr, color: :red)
log_to_file(prep_cmd, stderr)
end
def log_to_file(prep_cmd, contents)
FileUtils.mkdir_p @log_directory
# replace whitespace with underscores and remove slashes
log_file_name = package_management_command&.gsub(/\s/, '_')&.delete('/')
log_file = File.join(@log_directory, "prepare_#{log_file_name || 'errors'}.log")
File.open(log_file, 'w') do |f|
f.write("Prepare command \"#{prep_cmd}\" failed with:\n")
f.write("#{contents}\n\n")
end
end
end
end
require 'license_finder/package'
require 'license_finder/package_managers/bower'
require 'license_finder/package_managers/go_workspace'
require 'license_finder/package_managers/go_15vendorexperiment'
require 'license_finder/package_managers/go_dep'
require 'license_finder/package_managers/gvt'
require 'license_finder/package_managers/glide'
require 'license_finder/package_managers/govendor'
require 'license_finder/package_managers/go_modules'
require 'license_finder/package_managers/trash'
require 'license_finder/package_managers/bundler'
require 'license_finder/package_managers/npm'
require 'license_finder/package_managers/pnpm'
require 'license_finder/package_managers/yarn'
require 'license_finder/package_managers/pip'
require 'license_finder/package_managers/pipenv'
require 'license_finder/package_managers/maven'
require 'license_finder/package_managers/mix'
require 'license_finder/package_managers/cocoa_pods'
require 'license_finder/package_managers/carthage'
require 'license_finder/package_managers/spm'
require 'license_finder/package_managers/gradle'
require 'license_finder/package_managers/rebar'
require 'license_finder/package_managers/erlangmk'
require 'license_finder/package_managers/nuget'
require 'license_finder/package_managers/dotnet'
require 'license_finder/package_managers/dep'
require 'license_finder/package_managers/conan'
require 'license_finder/package_managers/sbt'
require 'license_finder/package_managers/cargo'
require 'license_finder/package_managers/composer'
require 'license_finder/package_managers/conda'
require 'license_finder/package_managers/pub'