<commit_before>#require 'resource_squasher/file_mapper'

module ResourceSquasher
  class ResourceSquasher
    attr_accessor :source_dir
    attr_accessor :rez_base
    attr_accessor :file_mapper
    attr_accessor :output_dir

    def self.initialize
      @project_name = nil
    end
    
    def self.project_name=(new_name)
      if new_name
        @project_name = new_name
      end
    end

    def self.project_name
      unless @project_name
        @project_name = ResourceSquasher::DEFAULT_PROJECT_NAME
      end
      return @project_name
    end

    def self.squash_sproutcore_app(opts={})
      squasher = ResourceSquasher.new(opts)
      squasher.load_all
      squasher.rewrite_resources
    end

    def initialize(_opts={})
      defaults = {
        :source_dir   => DEFAULT_SOURCE_DIR,
        :project_name => DEFAULT_PROJECT_NAME,
        :output_dir   => DEFAULT_OUTPUT_DIR,
        :rez_base     => DEFAULT_REZ_BASE
      }
      opts                          = defaults.merge(_opts)
      ResourceSquasher.project_name= opts[:project_name]
      self.output_dir               = opts[:output_dir]
      self.source_dir               = opts[:source_dir]
      self.rez_base                 = opts[:rez_base]
      self.file_mapper              = FileMapper.new(opts)
    end

    # find the build directory for the sproutcore project self.project_name
    def project_dir(lang = "en")
      File.join(self.source_dir,self.rez_base,ResourceSquasher.project_name,lang)
    end

    #  tmp/build/static/my_system/en/67bd8352e47bfe3a4cabf92df08ef2022c7368a7/
    def most_recent_project_html
      matching = /[a-z|0-9]{40}/ #TODO: This signature is only valid against sproutcore builds.
      parent_dir = Dir.new(project_dir)
      builds = parent_dir.entries.collect { |file| File.new(File.join(parent_dir.path,file)) }.sort { |file1,file2| file1.mtime <=> file2.mtime }
      builds.reject! { |file| ((file.path =~ matching) ? false : true) }
      return File.join(builds.last,"index.html")
    end

    # regex for resources which can be replaced
    def resource_regex
      /['|"]\s*(\/#{self.rez_base}\/[^"|^']*)['|"]/
    end

    # load resources, starting with index.html in the 
    # sproutcore application defined by self.project_name
    def load_all
      html_path = self.most_recent_project_html
      html_resource = html_path.gsub(self.source_dir,"")
      self.file_mapper.add_file(html_resource)
      load_resources(html_path)
    end

    # Recursively load resources found in filename
    def load_resources(filename)
      file = File.new(filename)
      content = file.read
      content.scan(resource_regex) do  |mgroup|
        resource = mgroup.first
        #TODO: return value for add_file is true if new file
        # and adding was successful
        if self.file_mapper.add_file(resource)
          added_file = self.file_mapper.old_names[resource]
          if resource =~ /[\.html|\.js|\.css|\.txt|\.json|\.htm]$/i
            self.load_resources(added_file.old_path)
          end
        else<commit_msg>Handle resources named in html-quoted JSON embedded in HTML element<commit_after>#require 'resource_squasher/file_mapper'

module ResourceSquasher
  class ResourceSquasher
    attr_accessor :source_dir
    attr_accessor :rez_base
    attr_accessor :file_mapper
    attr_accessor :output_dir

    def self.initialize
      @project_name = nil
    end
    
    def self.project_name=(new_name)
      if new_name
        @project_name = new_name
      end
    end

    def self.project_name
      unless @project_name
        @project_name = ResourceSquasher::DEFAULT_PROJECT_NAME
      end
      return @project_name
    end

    def self.squash_sproutcore_app(opts={})
      squasher = ResourceSquasher.new(opts)
      squasher.load_all
      squasher.rewrite_resources
    end

    def initialize(_opts={})
      defaults = {
        :source_dir   => DEFAULT_SOURCE_DIR,
        :project_name => DEFAULT_PROJECT_NAME,
        :output_dir   => DEFAULT_OUTPUT_DIR,
        :rez_base     => DEFAULT_REZ_BASE
      }
      opts                          = defaults.merge(_opts)
      ResourceSquasher.project_name= opts[:project_name]
      self.output_dir               = opts[:output_dir]
      self.source_dir               = opts[:source_dir]
      self.rez_base                 = opts[:rez_base]
      self.file_mapper              = FileMapper.new(opts)
    end

    # find the build directory for the sproutcore project self.project_name
    def project_dir(lang = "en")
      File.join(self.source_dir,self.rez_base,ResourceSquasher.project_name,lang)
    end

    #  tmp/build/static/my_system/en/67bd8352e47bfe3a4cabf92df08ef2022c7368a7/
    def most_recent_project_html
      matching = /[a-z|0-9]{40}/ #TODO: This signature is only valid against sproutcore builds.
      parent_dir = Dir.new(project_dir)
      builds = parent_dir.entries.collect { |file| File.new(File.join(parent_dir.path,file)) }.sort { |file1,file2| file1.mtime <=> file2.mtime }
      builds.reject! { |file| ((file.path =~ matching) ? false : true) }
      return File.join(builds.last,"index.html")
    end

    # regex for resources which can be replaced
    def resource_regex
      /(?:['|"]|&quot;)\s*(\/#{self.rez_base}\/.*?)(?:['|"]|&quot;)/
    end

    # load resources, starting with index.html in the 
    # sproutcore application defined by self.project_name
    def load_all
      html_path = self.most_recent_project_html
      html_resource = html_path.gsub(self.source_dir,"")
      self.file_mapper.add_file(html_resource)
      load_resources(html_path)
    end

    # Recursively load resources found in filename
    def load_resources(filename)
      file = File.new(filename)
      content = file.read
      content.scan(resource_regex) do  |mgroup|
        resource = mgroup.first
        #TODO: return value for add_file is true if new file
        # and adding was successful
        if self.file_mapper.add_file(resource)
          added_file = self.file_mapper.old_names[resource]
          if resource =~ /[\.html|\.js|\.css|\.txt|\.json|\.htm]$/i
            self.load_resources(added_file.old_path)
          end
        else
{'fraction_non_alphanumeric': 0.09520735839922544, 'fraction_numerical': 0.010004841052121995, 'mean_word_length': 2.5951276102088165, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3213880', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1962, 'n_words': 384}