<commit_before>require "tinymce/rails/asset_manifest"

module TinyMCE
  module Rails
    class AssetInstaller
      attr_accessor :logger
      
      def initialize(assets, target, manifest_path)
        @assets = assets
        @target = target
        @manifest_path = manifest_path || target
        
        @logger       = Logger.new($stderr)
        @logger.level = Logger::INFO
      end
      
      def install
        cleanup_assets
        copy_assets
        append_to_manifest
        
        manifest.write
      end
      
      def symlink
        manifest.each(/^tinymce\//) do |asset|
          manifest.remove_digest(asset) do |src, dest|
            symlink_asset(src, dest)
          end
        end
      end
      
      def log_level
        @logger.level
      end

      def log_level=(level)
        if level.is_a?(Integer)
          @logger.level = level
        else
          @logger.level = Logger.const_get(level.to_s.upcase)
        end
      end
    
    private
      def manifest
        @manifest ||= AssetManifest.load(@manifest_path)
      end
      
      def cleanup_assets
        manifest.each(/^tinymce\//) do |asset|
          manifest.remove(asset) if index_asset?(asset)
          
          manifest.remove_digest(asset) do |src, dest|
            move_asset(src, dest)
          end
        end
      end
      
      def copy_assets
        FileUtils.cp_r(@assets, @target, :preserve => true)
      end
      
      def append_to_manifest
        asset_files.each do |file|
          manifest.append(logical_path(file), file)
        end
      end
      
      def asset_files
        Pathname.glob("#{@assets}/**/*").select(&:file?)
      end
      
      def logical_path(file)
        file.relative_path_from(@assets.parent).to_s
      end
      
      def move_asset(src, dest)
        with_asset(src, dest) do |src, dest|
          logger.info "Removing digest from #{src}"
          FileUtils.mv(src, dest, :force => true)
        end
      end
      
      def symlink_asset(src, dest)
        with_asset(src, dest) do |src, dest|
          target = File.basename(src)
          
          unless File.exists?(dest) && File.symlink?(dest) && File.readlink(dest) == target
            logger.info "Creating symlink #{dest}"
            FileUtils.ln_s(target, dest, :force => true)
          else
            logger.debug "Skipping symlink #{dest}, already exists"
          end
        end
      end
      
      def with_asset(src, dest)
        if src != dest
          src = File.join(@target, src)
          dest = File.join(@target, dest)
          
          yield src, dest if File.exists?(src)
        end
      end
      
      def index_asset?(asset)
        asset =~ /\/index\.js$/
      end
    end
  end
end
<commit_msg>Symlink .gz files if they exist<commit_after>require "tinymce/rails/asset_manifest"

module TinyMCE
  module Rails
    class AssetInstaller
      attr_accessor :logger
      
      def initialize(assets, target, manifest_path)
        @assets = assets
        @target = target
        @manifest_path = manifest_path || target
        
        @logger       = Logger.new($stderr)
        @logger.level = Logger::INFO
      end
      
      def install
        cleanup_assets
        copy_assets
        append_to_manifest
        
        manifest.write
      end
      
      def symlink
        manifest.each(/^tinymce\//) do |asset|
          manifest.remove_digest(asset) do |src, dest|
            symlink_asset(src, dest)
          end
        end
      end
      
      def log_level
        @logger.level
      end

      def log_level=(level)
        if level.is_a?(Integer)
          @logger.level = level
        else
          @logger.level = Logger.const_get(level.to_s.upcase)
        end
      end
    
    private
      def manifest
        @manifest ||= AssetManifest.load(@manifest_path)
      end
      
      def cleanup_assets
        manifest.each(/^tinymce\//) do |asset|
          manifest.remove(asset) if index_asset?(asset)
          
          manifest.remove_digest(asset) do |src, dest|
            move_asset(src, dest)
          end
        end
      end
      
      def copy_assets
        FileUtils.cp_r(@assets, @target, :preserve => true)
      end
      
      def append_to_manifest
        asset_files.each do |file|
          manifest.append(logical_path(file), file)
        end
      end
      
      def asset_files
        Pathname.glob("#{@assets}/**/*").select(&:file?)
      end
      
      def logical_path(file)
        file.relative_path_from(@assets.parent).to_s
      end
      
      def move_asset(src, dest)
        with_asset(src, dest) do |src, dest|
          logger.info "Removing digest from #{src}"
          FileUtils.mv(src, dest, :force => true)
        end
      end
      
      def symlink_asset(src, dest)
        with_asset(src, dest) do |src, dest|
          create_symlink(src, dest)
          create_symlink("#{src}.gz", "#{dest}.gz") if File.exists?("#{src}.gz")
        end
      end
      
      def with_asset(src, dest)
        if src != dest
          src = File.join(@target, src)
          dest = File.join(@target, dest)
          
          yield src, dest if File.exists?(src)
        end
      end
      
      def create_symlink(src, dest)
        target = File.basename(src)
        
        unless File.exists?(dest) && File.symlink?(dest) && File.readlink(dest) == target
          logger.info "Creating symlink #{dest}"
          FileUtils.ln_s(target, dest, :force => true)
        else
          logger.debug "Skipping symlink #{dest}, already exists"
        end
      end
      
      def index_asset?(asset)
        asset =~ /\/index\.js$/
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09558567952728536, 'fraction_numerical': 0.0, 'mean_word_length': 1.59000900090009, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5883168', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1700, 'n_words': 391}