forked from theforeman/foreman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin_webpack_directories.rb
executable file
·80 lines (67 loc) · 2.31 KB
/
plugin_webpack_directories.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
#! /usr/bin/env ruby
require 'json'
if File.exist?(File.expand_path(File.join(%w[.. .. Gemfile.in]), __FILE__))
require 'bundler_ext'
gemfile_in = File.expand_path(File.join(%w[.. .. Gemfile.in]), __FILE__)
specs = BundlerExt::Gemfile.parse(gemfile_in, :all).map { |spec, value| value[:dep] }
else
require 'bundler'
begin
specs = Bundler.load.specs
rescue Bundler::GemNotFound
raise if File.exist?(File.expand_path(File.join(%w[.. Gemfile.lock]), __dir__))
specs = []
end
end
def log_skip_plugin(plugin_name, reason)
$stderr.puts "Skipping plugin #{plugin_name}: #{reason}"
end
def log_plugin(plugin_name)
$stderr.puts "Processing plugin #{plugin_name}"
end
def entry_name(plugin_name, entry)
prefix = File.basename(entry).gsub(/[_]?index.js$/, '')
if prefix != ''
"#{plugin_name}:#{prefix}"
else
plugin_name
end
end
config = { entries: {}, paths: [], plugins: {} }
entry_paths = []
plugin_name_regexp = /foreman.*|katello.*/
specs.each do |dep|
dep = dep.to_spec if gemfile_in
# skip other rails engines that are not plugins
# TODO: Consider using the plugin registration api?
unless dep.respond_to?("metadata") && dep.metadata.has_key?("is_foreman_plugin") && dep.metadata["is_foreman_plugin"] == "true"
next unless dep.name =~ plugin_name_regexp
next if dep.name =~ /.*[_-]core$/
end
path = "#{dep.to_spec.full_gem_path}/webpack"
unless Dir.exist?(path)
log_skip_plugin(dep.name, './webpack directory missing')
next
end
unless File.exist?("#{dep.to_spec.full_gem_path}/package.json")
log_skip_plugin(dep.name, './package.json missing')
next
end
log_plugin(dep.name)
package_json = JSON.parse(File.read("#{dep.to_spec.full_gem_path}/package.json"))
entries = Dir["#{path}/*index.js"]
entries << package_json['main'] if package_json['main']
entries.uniq.each do |entry|
# some plugins share the same base directory (tasks-core and tasks, REX etc)
# skip the plugin if its path is already included
next if entry_paths.include?(entry)
if File.exist?(entry)
config[:entries][entry_name(dep.name, entry)] = entry
config[:paths] << path
config[:plugins][entry_name(dep.name, entry)] = {root: dep.to_spec.full_gem_path, entry: entry}
entry_paths << entry
end
end
end
config[:paths].uniq!
puts config.to_json