#encoding: utf-8 $:.push File.expand_path("../lib", __FILE__) # Maintain your gem's version: require "patchfile/version" bundle_update_flag = ARGV[0]=='update' || ARGV[0]=='install' if bundle_update_flag app_path = File.expand_path(__dir__) dir_pwd = ENV['PWD'] template_path = dir_pwd + '/app/templates' all_template = Dir.glob(template_path+'/*/') #if !Dir.exist?("#{ENV['PWD']}"+"/app/assets/stylesheets/lib/jquery-ui-1.12.1/") # puts "copying jquery-ui" # begin # `cp -rf "#{app_path}/updatefiles/javascripts/." "#{dir_pwd}/app/assets/javascripts/"` # `cp -r "#{app_path}/updatefiles/accessibility/jquery-ui-1.13.2/." "#{dir_pwd}/app/assets/stylesheets/lib/jquery-ui-1.13.2/"` # rescue # puts "There is an error in coppying jquery-ui" # end #end @original_dir = Dir.pwd `cd "#{dir_pwd}" && git add app/templates/ app/assets built_in_extensions.rb config/mongoid.yml bin/ && cd "#{@original_dir}"` #using git to add modified files and to avoid git pull not working #依規則重建站台的 .gitmodules(核心不追蹤此檔;放在上一行 git add 之後,避免帶 .git 的複製版型又被加回索引) unless ENV['ORBIT_GITMODULES_REBUILT'] ENV['ORBIT_GITMODULES_REBUILT'] = '1' begin require 'open3' require 'fileutils' if Dir.exist?(File.join(dir_pwd, '.git')) && Dir.exist?(template_path) Dir.chdir(dir_pwd) do candidates = {} if File.exist?('.gitmodules') entries = {} Open3.capture3('git', 'config', '-f', '.gitmodules', '--get-regexp', '^submodule\.')[0].each_line do |line| k, v = line.strip.split(' ', 2) m = k.to_s.match(/\Asubmodule\.(.+)\.(path|url|branch)\z/) (entries[m[1]] ||= {})[m[2]] = v.to_s if m end entries.each_value { |e| candidates[e['path']] = e if e['path'].to_s.start_with?('app/templates/') } end gitlinks = Open3.capture3('git', 'ls-files', '-s', '--', 'app/templates')[0].lines.map { |l| l.strip.split(/\s+/, 4) }.select { |l| l[0] == '160000' }.map { |l| l[3] } gitlinks.each { |p| candidates[p] ||= {'path' => p} } keep = {} candidates.each do |path, e| next unless gitlinks.include?(path) && File.exist?(File.join(path, 'template.json')) url = e['url'].to_s url = Open3.capture3('git', '-C', path, 'remote', 'get-url', 'origin')[0].strip if url.empty? next if url.empty? || File.basename(path) != File.basename(url).sub(/\.git\z/, '') remote_head, _, st = Open3.capture3({'GIT_TERMINAL_PROMPT' => '0'}, 'timeout', '20', 'git', 'ls-remote', '--symref', url, 'HEAD') next unless st.success? branch = Open3.capture3('git', '-C', path, 'rev-parse', '--abbrev-ref', 'HEAD')[0].strip branch = e['branch'].to_s if branch.empty? || branch == 'HEAD' branch = remote_head[%r{ref: refs/heads/(\S+)\s+HEAD}, 1].to_s if branch.empty? keep[path] = {'url' => url, 'branch' => branch} end File.open('.gitmodules', 'w') do |f| keep.each do |path, e| f.puts "[submodule \"#{path}\"]" f.puts "\tpath = #{path}" f.puts "\turl = #{e['url']}" f.puts "\tbranch = #{e['branch']}" unless e['branch'].empty? end end system('git', 'rm', '-q', '--cached', '--ignore-unmatch', '--', '.gitmodules') (gitlinks - keep.keys).each { |p| system('git', 'rm', '-q', '--cached', '--', p) } Dir.glob('app/templates/*/.git').each do |g| next unless File.directory?(g) next if keep.key?(File.dirname(g)) FileUtils.rm_rf(g) puts "removed copied .git in #{File.dirname(g)}" end end puts "finish rebuilding .gitmodules in #{dir_pwd}" end rescue => e puts "rebuild .gitmodules failed: #{e.inspect}" end end #Add patchfile to Admin area @patchfile_li = File.read(app_path+"/patchfile_li.txt").force_encoding('UTF-8') @patchfile_li_choices = File.read(app_path+"/patchfile_li_choices.txt").force_encoding('UTF-8') @file_text = File.read(dir_pwd+'/app/views/shared/_side_bar.html.erb').force_encoding('UTF-8') if !@file_text.include?('