Created
June 18, 2017 07:30
-
-
Save windwiny/9fb6f56a2f1bdd5ed69a9d502124242b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'pp' | |
require 'optparse' | |
options = {:can_skip_newerbundle=>true} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: gem-ext-compile-v2.rb [options]" | |
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| | |
options[:verbose] = v | |
end | |
opts.on("--[no-]skip-new-ruby", "skip if bundle newer than bin/ruby file") do |v| | |
options[:can_skip_newerbundle] = v | |
end | |
end.parse! | |
p options | |
RUBY_HOME="/usr/local/ruby_trunk" | |
RUBY_BIN="#{RUBY_HOME}/bin/ruby" | |
GEM_PATH = "#{RUBY_HOME}/lib/ruby/gems/2.4.0" | |
BUNDLE_FILES = Dir.glob "#{GEM_PATH}/**/*.bundle" | |
kvs = BUNDLE_FILES.group_by do |e| | |
%r{/gems/(?<name>.+?)/|/extensions/.+?/.+?/(?<name>.+?)/} =~ e[GEM_PATH.size..-1] | |
name ? name+'/'+File.basename(e) : nil | |
end | |
# pp kvs | |
p [BUNDLE_FILES.size, kvs.size] | |
File.open('/tmp/gem-ext-compile-v2.sh', 'w') do |f| | |
f.puts '#!/bin/bash' | |
f.puts | |
kvs.each do |gem_name_ver, bundle_paths| | |
if options[:can_skip_newerbundle] && bundle_paths.select {|f| File.stat(f).mtime < File.stat(RUBY_BIN).mtime }.empty? | |
f.puts "# --- skip #{gem_name_ver}, #{bundle_paths.size} files newer than RUBY_BIN" | |
f.puts | |
next | |
end | |
pre_path = "#{GEM_PATH}/gems/#{gem_name_ver.split('/')[0]}" | |
bundle_name = File.basename(bundle_paths[0]).chomp('.bundle') | |
guest_path_1 = "#{pre_path}/**/#{bundle_name}" | |
guest_path_2 = "#{pre_path}/**" | |
source_dest = Dir.glob(["#{guest_path_1}/extconf.rb", "#{guest_path_1}/Makefile", | |
"#{guest_path_2}/extconf.rb", "#{guest_path_2}/Makefile"],0)[0] | |
p [guest_path_1, guest_path_2, source_dest] if options[:verbose] | |
unless source_dest | |
f.puts "# ---- ERROR not find extconf.rb or Makefile #{gem_name_ver} ----" | |
f.puts | |
next | |
end | |
cmd = "cd #{File.dirname(source_dest)}" | |
f.puts %{echo -e "\${GREEN}#{cmd} \${WHITE}"} | |
f.puts cmd | |
if File.basename(source_dest) != 'Makefile' | |
cmd = "ruby extconf.rb" | |
f.puts %{echo -e "\${RED}#{cmd} \${WHITE}"} | |
f.puts cmd | |
end | |
cmd = "make" | |
f.puts %{echo -e "\${RED}#{cmd} \${WHITE}"} | |
f.puts cmd | |
bundle_paths.map do |e| | |
unless e==source_dest | |
cmd = "ln -f #{File.dirname(source_dest)+'/'+File.basename(bundle_paths[0])} #{e}" | |
f.puts %{echo -e "\${GREEN}#{cmd} \${WHITE}"} | |
f.puts cmd | |
end | |
end | |
cmd = "make clean" | |
f.puts %{echo -e "\${RED}#{cmd} \${WHITE}"} | |
f.puts cmd | |
cmd = "cd -" | |
f.puts %{echo -e "\${GREEN}#{cmd} \${WHITE}"} | |
f.puts cmd | |
f.puts %{echo -e "\\n\\n"} | |
f.puts | |
f.puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment