assets.rake 5.69 KB
Newer Older
1
# Theming constants
2
USE_CUSTOM_THEME = ENV_TOKENS.has_key?('FEATURES') && ENV_TOKENS['FEATURES']['USE_CUSTOM_THEME']
3
if USE_CUSTOM_THEME
4
    THEME_NAME = ENV_TOKENS['THEME_NAME']
5 6 7 8
    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
    if watch
e0d committed
34
        "node_modules/.bin/coffee --compile --watch lms/ cms/ common/"
35
    else
e0d committed
36
        "node_modules/.bin/coffee --compile `find lms/ cms/ common/ -type f -name *.coffee` "
37
    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 ? '' : '--style compressed'} " +
49
          "--cache-location /tmp/sass-cache " +
50
          "--load-path #{sass_load_paths.join(' ')} " +
51
          "#{watch ? '--watch' : '--update'} -E utf-8 #{sass_watch_paths.join(' ')}"
52 53
end

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

58
namespace :assets do
59

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

63 64 65 66 67 68
    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
69 70
    end

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

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

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

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

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

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

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

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

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

    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
140 141
end

142 143 144 145 146 147 148 149 150 151 152
# 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

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