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

def coffee_cmd(watch=false, debug=false)
24 25 26 27 28 29 30
    if watch
        # On OSx, coffee fails with EMFILE when
        # trying to watch all of our coffee files at the same
        # time.
        #
        # Ref: https://github.com/joyent/node/issues/2479
        #
31 32 33 34 35 36 37 38
        # So, instead, we use watchmedo, which works around the problem
        "watchmedo shell-command " +
                  "--command 'node_modules/.bin/coffee -c ${watch_src_path}' " +
                  "--recursive " +
                  "--patterns '*.coffee' " +
                  "--ignore-directories " +
                  "--wait " +
                  "."
39
    else
40
        'node_modules/.bin/coffee --compile .'
41
    end
42 43 44
end

def sass_cmd(watch=false, debug=false)
45 46 47 48 49 50 51
    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

52
    "sass #{debug ? '--debug-info' : '--style compressed'} " +
53
          "--load-path #{sass_load_paths.join(' ')} " +
54
          "--require ./common/static/sass/bourbon/lib/bourbon.rb " +
55
          "#{watch ? '--watch' : '--update'} -E utf-8 #{sass_watch_paths.join(' ')}"
56 57 58 59 60
end

desc "Compile all assets"
multitask :assets => 'assets:all'

61
namespace :assets do
62

63 64 65
    desc "Compile all assets in debug mode"
    multitask :debug

66 67 68 69 70 71
    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
72 73
    end

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

80
    {:xmodule => [:install_python_prereqs],
81
     :coffee => [:install_node_prereqs, :'assets:coffee:clobber'],
82
     :sass => [:install_ruby_prereqs, :preprocess]}.each_pair do |asset_type, prereq_tasks|
83
        desc "Compile all #{asset_type} assets"
84
        task asset_type => prereq_tasks do
85
            cmd = send(asset_type.to_s + "_cmd", watch=false, debug=false)
86 87 88 89 90
            if cmd.kind_of?(Array)
                cmd.each {|c| sh(c)}
            else
                sh(cmd)
            end
91 92 93 94
        end

        multitask :all => asset_type
        multitask :debug => "assets:#{asset_type}:debug"
95
        multitask :_watch => "assets:#{asset_type}:_watch"
96 97 98

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

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

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

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

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

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

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

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

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