Sha256: 2df238c9399946d2765a3d9f7df2ae04414b3b60a6f0c5a99e20600e78adaefb
Contents?: true
Size: 1.41 KB
Versions: 3
Compression:
Stored size: 1.41 KB
Contents
module Uki INCLUDE_REGEXP = %r{((?:^|\n)[^\n]\W|^|\n)include\s*\(\s*['"]([^"']+)["']\s*\)(?:\s*;)?(.*?\r?\n|$)} # # Preprocesses include() calls in js files def self.include_js path, included = {}, stack = [] raise Exception.new("File #{path} not found.\nStack: #{stack.join(' -> ')}") unless File.exists?(path) path = File.expand_path path code, base = File.read(path), File.dirname(path) included[path] = true code.gsub(INCLUDE_REGEXP) do |match| if $1.include? '//' # include commented out match else include_path = File.expand_path File.join(base, $2) unless included[include_path] $1 + include_js(include_path, included, stack + [include_path]) + $3 else $1 + $3 end end end end def self.extract_includes path result = [] File.read(path).scan(INCLUDE_REGEXP) do |match| result << $2 unless $1.include? '//' end result end def self.append_include path, include_path count = extract_includes(path).size code = File.read(path) line_break = code.match(/\r\n/) ? "\r\n" : "\n" code = code.gsub(INCLUDE_REGEXP) do |match| next if $1.include? '//' if (count -= 1) > 0 match else ($3.length ? '' : line_break) + match + "include('#{include_path}');" + line_break end end File.open(path, 'w') { |f| f.write(code) } end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
uki-1.0.2 | lib/uki/include_js.rb |
uki-1.0.1 | lib/uki/include_js.rb |
uki-1.0.0 | lib/uki/include_js.rb |