<commit_before>require 'guard'
require 'guard/plugin'
require 'less'

module Guard
  class Less < Plugin

    require 'guard/less/version'

    # ================
    # = Guard method =
    # ================
    def initialize(options = {})
      defaults = {
        all_after_change: true,
        all_on_start: true,
        output: nil,
        import_paths: [],
        compress: false,
        yuicompress: false,
      }

      super(defaults.merge(options))
    end

    def start
      UI.info "Guard::Less #{LessVersion::VERSION} is on the job!"
      run_all if options[:all_on_start]
    end

    # Call with Ctrl-/ signal
    # This method should be principally used for long action like running all specs/tests/...
    def run_all
      UI.info "Guard::Less: compiling all files"
      files = Dir.glob('**/*.*')
      paths = Watcher.match_files(self, files).uniq
      run(paths)
    end

    # Call on file(s) modifications
    def run_on_changes(paths)
      options[:all_after_change] ? run_all : run(paths)
    end

    def run(paths)
      directories = nested_directory_map(paths)

      directories.each do |destination, stylesheets|
        stylesheets.each do |lessfile|
          # Skip partials
          basename = File.basename(lessfile)
          next if basename[0,1] == "_"

          cssfile = File.join(destination, basename.gsub(/\.less$/, '.css'))

          # Just in case
          if cssfile == lessfile
            UI.info "Guard::Less: Skipping #{lessfile} since the output would overwrite the original file"
          elsif mtime(cssfile) >= mtime_including_imports(lessfile)
            UI.info "Guard::Less: Skipping #{lessfile} because #{cssfile} is already up-to-date"
          else
            UI.info "Guard::Less: #{lessfile} -> #{cssfile}\n"
            FileUtils.mkdir_p(File.expand_path(destination))
            compile(lessfile, cssfile)
          end
        end
      end
    end

    private

    # Parse the source lessfile and write to target cssfile
    def compile(lessfile, cssfile)
      import_paths = options[:import_paths].unshift(File.dirname(lessfile))
      parser = ::Less::Parser.new paths: import_paths, filename: lessfile
      File.open(lessfile,'r') do |infile|
        File.open(cssfile,'w') do |outfile|
          tree = parser.parse(infile.read)
          if options[:yuicompress]
          	outfile << tree.to_css(:yuicompress => options[:yuicompress])
          elsif options[:compress]
          	outfile << tree.to_css(:compress => options[:compress])
          end
        end
      end
      true
    rescue Exception => e
      UI.info "Guard::Less: Compiling #{lessfile} failed with message: #{e.message}"
      false
    end
<commit_msg>Fix logic issue from last pull-request<commit_after>require 'guard'
require 'guard/plugin'
require 'less'

module Guard
  class Less < Plugin

    require 'guard/less/version'

    # ================
    # = Guard method =
    # ================
    def initialize(options = {})
      defaults = {
        all_after_change: true,
        all_on_start: true,
        output: nil,
        import_paths: [],
        compress: false,
        yuicompress: false,
      }

      super(defaults.merge(options))
    end

    def start
      UI.info "Guard::Less #{LessVersion::VERSION} is on the job!"
      run_all if options[:all_on_start]
    end

    # Call with Ctrl-/ signal
    # This method should be principally used for long action like running all specs/tests/...
    def run_all
      UI.info "Guard::Less: compiling all files"
      files = Dir.glob('**/*.*')
      paths = Watcher.match_files(self, files).uniq
      run(paths)
    end

    # Call on file(s) modifications
    def run_on_changes(paths)
      options[:all_after_change] ? run_all : run(paths)
    end

    def run(paths)
      directories = nested_directory_map(paths)

      directories.each do |destination, stylesheets|
        stylesheets.each do |lessfile|
          # Skip partials
          basename = File.basename(lessfile)
          next if basename[0,1] == "_"

          cssfile = File.join(destination, basename.gsub(/\.less$/, '.css'))

          # Just in case
          if cssfile == lessfile
            UI.info "Guard::Less: Skipping #{lessfile} since the output would overwrite the original file"
          elsif mtime(cssfile) >= mtime_including_imports(lessfile)
            UI.info "Guard::Less: Skipping #{lessfile} because #{cssfile} is already up-to-date"
          else
            UI.info "Guard::Less: #{lessfile} -> #{cssfile}\n"
            FileUtils.mkdir_p(File.expand_path(destination))
            compile(lessfile, cssfile)
          end
        end
      end
    end

    private

    # Parse the source lessfile and write to target cssfile
    def compile(lessfile, cssfile)
      import_paths = options[:import_paths].unshift(File.dirname(lessfile))
      parser = ::Less::Parser.new paths: import_paths, filename: lessfile
      File.open(lessfile,'r') do |infile|
        File.open(cssfile,'w') do |outfile|
          tree = parser.parse(infile.read)
         	outfile << tree.to_css(compress: options[:compress], yuicompress: options[:yuicompress])
        end
      end
      true
    rescue Exception => e
      UI.info "Guard::Less: Compiling #{lessfile} failed with message: #{e.message}"
      false
    end

{'fraction_non_alphanumeric': 0.11902092675635277, 'fraction_numerical': 0.0007473841554559044, 'mean_word_length': 2.4073838319541694, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3466433', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1557, 'n_words': 409}