class Motion::Project::Config

Public Instance Methods

concat_files(opts={}) click to toggle source
# File lib/compressor.rb, line 10
def concat_files(opts={})
  opts[:exclude] ||= [ "/app/" ]
  opts[:parallel] ||= 4
  concatenate_files!(extract_concatenated_files(Array(opts[:exclude])), opts[:parallel])
end

Private Instance Methods

concatenate_files!(concat_files, parallel=4) click to toggle source
# File lib/compressor.rb, line 40
def concatenate_files!(concat_files, parallel=4)
  group_number = 1
  previous_concat_path = nil

  concat_files.in_groups(parallel, false) do |group|
    concat_path = File.join(File.expand_path(@project_dir), "build", "app-concatenated-#{group_number}.rb")
    temp_concat_path = File.join(File.expand_path(@project_dir), "build", "app-concatenated-#{group_number}-temp.rb")
    Motion::Project::App.info "Concat", concat_path

    # Prep the concat path for writing
    Dir.mkdir(File.dirname(concat_path)) unless File.exist?(File.dirname(concat_path))
    File.new(concat_path, 'w') unless File.exist?(concat_path)

    # Concatenate this group of files
    File.open(temp_concat_path, 'a') do |concat|
      concat << "# File generated by 'Compressor' by Jamon Holmgren\n\n"
      concat << "# "
      group.each do |filename|
        concat << "# #{"=" * filename.length}\n"
        concat << "# #{filename}\n"
        concat << "# #{"=" * filename.length}\n"
        concat << File.read(filename)
        concat << "\n"
      end
    end

    if File.exist?(concat_path) && !FileUtils.cmp(temp_concat_path, concat_path)
      # Changed file, replace the original
      File.delete(concat_path)
      FileUtils.mv(temp_concat_path, concat_path)
    else
      # No change, throw it away
      File.delete(temp_concat_path)
    end

    # Add this concatenated file to the build files
    @files.unshift concat_path

    # Ensure that each file gets loaded in the right order
    if previous_concat_path
      files_dependencies concat_path => previous_concat_path
    end
    previous_concat_path = concat_path

    # Next group
    group_number += 1
  end
end
extract_concatenated_files(excluded=[]) click to toggle source
# File lib/compressor.rb, line 18
def extract_concatenated_files(excluded=[])
  @files.flatten!
  concatenated_files = @files.select { |f| excluded.none? { |excluded_match| !!f.match(excluded_match) } }
  @files = @files - concatenated_files
  @dependencies = Dependency.new(@files - @exclude_from_detect_dependencies, @dependencies).run

  # TODO: Remove all concatenated files from the dependency hash
  # @dependencies.each do |target, dependencies|
  #   if @files.include?(target)
  #     @dependencies[target] = dependencies - concatenated_files
  #   else
  #     @dependencies.delete(target)
  #   end
  # end

  order_concatenated_files(concatenated_files)
end
order_concatenated_files(concatenated_files) click to toggle source
# File lib/compressor.rb, line 36
def order_concatenated_files(concatenated_files)
  concatenated_files.map { |file| file_dependencies(file) }.flatten.uniq
end