assets.rake 5.58 KB
Newer Older
1 2 3 4 5 6 7 8
# Theming constants
THEME_NAME = ENV_TOKENS['THEME_NAME']
USE_CUSTOM_THEME = !(THEME_NAME.nil? || THEME_NAME.empty?)
if USE_CUSTOM_THEME
    THEME_ROOT = File.join(ENV_ROOT, "themes", THEME_NAME)
    THEME_SASS = File.join(THEME_ROOT, "static", "sass")
end

9 10
MINIMAL_DARWIN_NOFILE_LIMIT = 8000

11 12 13 14
def xmodule_cmd(watch=false, debug=false)
    xmodule_cmd = 'xmodule_assets common/static/xmodule'
    if watch
        "watchmedo shell-command " +
15 16 17 18 19
                  "--patterns='*.js;*.coffee;*.sass;*.scss;*.css' " +
                  "--recursive " +
                  "--command='#{xmodule_cmd}' " +
                  "--wait " +
                  "common/lib/xmodule"
20 21 22 23 24 25
    else
        xmodule_cmd
    end
end

def coffee_cmd(watch=false, debug=false)
26 27 28 29 30 31
    if watch && Launchy::Application.new.host_os_family.darwin?
        available_files = Process::getrlimit(:NOFILE)[0]
        if available_files < MINIMAL_DARWIN_NOFILE_LIMIT
            Process.setrlimit(:NOFILE, MINIMAL_DARWIN_NOFILE_LIMIT)

        end
32
    end
33 34 35 36 37
    if watch
        "node_modules/.bin/coffee --compile --watch . "
    else
        "node_modules/.bin/coffee --compile `find . -name *.coffee` "
    end
38 39 40
end

def sass_cmd(watch=false, debug=false)
41 42 43 44 45 46 47
    sass_load_paths = ["./common/static/sass"]
    sass_watch_paths = ["*/static"]
    if USE_CUSTOM_THEME
      sass_load_paths << THEME_SASS
      sass_watch_paths << THEME_SASS
    end

48
    "sass #{debug ? '--debug-info' : '--style compressed'} " +
49
          "--load-path #{sass_load_paths.join(' ')} " +
50
          "#{watch ? '--watch' : '--update'} -E utf-8 #{sass_watch_paths.join(' ')}"
51 52
end

53
# This task takes arguments purely to pass them via dependencies to the preprocess task
54
desc "Compile all assets"
55
task :assets, [:system, :env] => 'assets:all'
56

57
namespace :assets do
58

59 60 61
    desc "Compile all assets in debug mode"
    multitask :debug

62 63 64 65 66 67
    desc "Preprocess all templatized static asset files"
    task :preprocess, [:system, :env] do |t, args|
      args.with_defaults(:system => "lms", :env => "dev")
      sh(django_admin(args.system, args.env, "preprocess_assets")) do |ok, status|
        abort "asset preprocessing failed!" if !ok
      end
68 69
    end

70 71 72 73 74
    desc "Watch all assets for changes and automatically recompile"
    task :watch => 'assets:_watch' do
        puts "Press ENTER to terminate".red
        $stdin.gets
    end
75

76
    {:xmodule => [:install_python_prereqs],
77
     :coffee => [:install_node_prereqs, :'assets:coffee:clobber'],
78
     :sass => [:install_ruby_prereqs, :preprocess]}.each_pair do |asset_type, prereq_tasks|
79
        # This task takes arguments purely to pass them via dependencies to the preprocess task
80
        desc "Compile all #{asset_type} assets"
81
        task asset_type, [:system, :env] => prereq_tasks do |t, args|
82
            cmd = send(asset_type.to_s + "_cmd", watch=false, debug=false)
83 84 85 86 87
            if cmd.kind_of?(Array)
                cmd.each {|c| sh(c)}
            else
                sh(cmd)
            end
88 89
        end

90 91
        # This task takes arguments purely to pass them via dependencies to the preprocess task
        multitask :all, [:system, :env] => asset_type
92
        multitask :debug => "assets:#{asset_type}:debug"
93
        multitask :_watch => "assets:#{asset_type}:_watch"
94 95 96

        namespace asset_type do
            desc "Compile all #{asset_type} assets in debug mode"
97
            task :debug => prereq_tasks do
98 99 100 101 102
                cmd = send(asset_type.to_s + "_cmd", watch=false, debug=true)
                sh(cmd)
            end

            desc "Watch all #{asset_type} assets and compile on change"
103 104 105 106 107
            task :watch => "assets:#{asset_type}:_watch" do
                puts "Press ENTER to terminate".red
                $stdin.gets
            end

108 109
            # Fully compile before watching for changes
            task :_watch => (prereq_tasks + ["assets:#{asset_type}:debug"]) do
110
                cmd = send(asset_type.to_s + "_cmd", watch=true, debug=true)
111
                if cmd.kind_of?(Array)
112
                    cmd.each {|c| singleton_process(c)}
113
                else
114
                    singleton_process(cmd)
115
                end
116 117 118 119 120
            end
        end
    end

    multitask :sass => 'assets:xmodule'
121 122 123 124
    namespace :sass do
        multitask :debug => 'assets:xmodule:debug'
    end

125
    multitask :coffee => 'assets:xmodule'
126 127
    namespace :coffee do
        multitask :debug => 'assets:xmodule:debug'
128 129 130 131 132

        desc "Remove compiled coffeescript files"
        task :clobber do
            FileList['*/static/coffee/**/*.js'].each {|f| File.delete(f)}
        end
133
    end
134 135 136 137 138

    namespace :xmodule do
        # Only start the xmodule watcher after the coffee and sass watchers have already started
        task :_watch => ['assets:coffee:_watch', 'assets:sass:_watch']
    end
139 140
end

141 142 143 144 145 146 147 148 149 150 151
# This task does the real heavy lifting to gather all of the static
# assets. We want people to call it via the wrapper below, so we
# don't provide a description so that it won't show up in rake -T.
task :gather_assets, [:system, :env] => :assets do |t, args|
    sh("#{django_admin(args.system, args.env, 'collectstatic', '--noinput')} > /dev/null") do |ok, status|
        if !ok
            abort "collectstatic failed!"
        end
    end
end

152 153 154
[:lms, :cms].each do |system|
    # Per environment tasks
    environments(system).each do |env|
155 156
        # This task wraps the one above, since we need the system and
        # env arguments to be passed to all dependent tasks.
157
        desc "Compile coffeescript and sass, and then run collectstatic in the specified environment"
158
        task "#{system}:gather_assets:#{env}" do
159
          Rake::Task[:gather_assets].invoke(system, env)
160 161 162
        end
    end
end