<commit_before>require 'pathname'

module MotionRequire
  class DependencyBuilder
    def self.build(app, load_paths = ['app', 'vendor'])
      builder = new(app, load_paths)
      app.files = [File.join(File.dirname(__FILE__), 'require.rb')] + builder.file_list
      # app.files_dependencies(builder.dependencies)
    end

    def initialize(app, load_paths)
      @app = app
      @load_paths = load_paths.map { |path| File.join(@app.project_dir, path) }
      @dependencies = {}
      @file_list = []
      @resolved = false
    end

    def dependencies
      resolve
      @dependencies
    end

    def file_list
      resolve
      @file_list
    end

    private

    def resolve
      return if @resolved
      add_dependencies_for_file(find_file('app_delegate'))
      @resolved = true
    end

    def add_dependencies_for_file(file_name)
      return if @file_list.member?(file_name)

      deps = dependencies_for_file(file_name)

      @dependencies[file_name] = deps unless deps.empty?

      @file_list.unshift(file_name)

      deps.each do |file|
        add_dependencies_for_file(file)
      end
    end

    def find_file(file)
      (@load_paths + $:).each do |path|
        file_name = File.join(path, file + '.rb')
        return file_name if File.exist?(file_name)
      end

      raise LoadError.new("cannot load such file -- #{file}")
    end

    def dependencies_for_file(file_name)
      deps = []

      file = File.new(file_name, 'r')

      file.each_line do |line|
        if match = line.match(/^\s*require '([^']+)'\s*$/)
          deps << match[1]
        end
      end

      deps.map! {|file| find_file(file)}

      deps
    end
  end<commit_msg>better regexp for finding `require` statements<commit_after>require 'pathname'

module MotionRequire
  class DependencyBuilder
    def self.build(app, load_paths = ['app', 'vendor'])
      builder = new(app, load_paths)
      app.files = [File.join(File.dirname(__FILE__), 'require.rb')] + builder.file_list
      # app.files_dependencies(builder.dependencies)
    end

    def initialize(app, load_paths)
      @app = app
      @load_paths = load_paths.map { |path| File.join(@app.project_dir, path) }
      @dependencies = {}
      @file_list = []
      @resolved = false
    end

    def dependencies
      resolve
      @dependencies
    end

    def file_list
      resolve
      @file_list
    end

    private

    def resolve
      return if @resolved
      add_dependencies_for_file(find_file('app_delegate'))
      @resolved = true
    end

    def add_dependencies_for_file(file_name)
      return if @file_list.member?(file_name)

      deps = dependencies_for_file(file_name)

      @dependencies[file_name] = deps unless deps.empty?

      @file_list.unshift(file_name)

      deps.each do |file|
        add_dependencies_for_file(file)
      end
    end

    def find_file(file)
      (@load_paths + $:).each do |path|
        file_name = File.join(path, file + '.rb')
        return file_name if File.exist?(file_name)
      end

      raise LoadError.new("cannot load such file -- #{file}")
    end

    def dependencies_for_file(file_name)
      deps = []

      file = File.new(file_name, 'r')

      file.each_line do |line|
        if match = line.match(/^\s*require ([\"'])([^']+)\1\s*$/)
          deps << match[2]
        end
      end

      deps.map! {|file| find_file(file)}

      deps
    end
  end
{'fraction_non_alphanumeric': 0.11247803163444639, 'fraction_numerical': 0.0008787346221441124, 'mean_word_length': 2.4356136820925554, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2544475', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1088, 'n_words': 240}