polyfox/moon

View on GitHub
tasks/shaders.rake

Summary

Maintainability
Test Coverage
require 'fileutils'

namespace :shaders do
  rootdir = File.expand_path('../', File.dirname(__FILE__))
  dir = File.expand_path('resources/shaders', rootdir)

  task :build_glsl do
    Dir.chdir dir do
      sh "bash ./build.sh"
    end
  end

  task inline_shaders: [:build_glsl] do
    contents = {}
    File.open File.join(rootdir, 'modules/graphics/mrblib/shaders.rb'), 'w' do |f|
      f.puts <<-__EOS__
# Autogenerated #{Time.now.strftime('%D %T')}
# DO NOT MODIFY THIS FILE, ALL CHANGES WILL BE LOST.
#
module Moon
  Shader::DEFAULTS = {
__EOS__
      Dir.chdir File.join(dir, 'build') do
        Dir.glob("**/*.{frag,vert}") do |filename|
          puts "\t Adding Shader #{filename}"
          f.puts "    '#{filename}' => (<<-__EOF__"
          f.puts File.read(filename)
          f.puts "__EOF__\n),"
        end
      end
      f.puts <<-__EOS__
  } # Moon::DEFAULT_SHADERS
end
__EOS__
    end
  end

  task build: [:build_glsl, :inline_shaders]

  task :clean do
    FileUtils::Verbose.rm_rf File.join(dir, 'build')
  end
end