File: build.rake

package info (click to toggle)
ruby-cssbundling-rails 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 192 kB
  • sloc: ruby: 190; javascript: 7; sh: 6; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 2,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
namespace :css do
  desc "Install CSS dependencies"
  task :install do
    command = Cssbundling::Tasks.install_command
    unless system(command)
      raise "cssbundling-rails: Command install failed, ensure #{command.split.first} is installed"
    end
  end

  desc "Build your CSS bundle"
  build_task = task :build do
    command = Cssbundling::Tasks.build_command
    unless system(command)
      raise "cssbundling-rails: Command build failed, ensure `#{command}` runs without errors"
    end
  end
  build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"] || ENV["SKIP_BUN_INSTALL"]
end

module Cssbundling
  module Tasks
    extend self

    LOCK_FILES = {
      bun: %w[bun.lockb bun.lock yarn.lock],
      pnpm: %w[pnpm-lock.yaml],
      npm: %w[package-lock.json]
    }

    def install_command
      case
      when using_tool?(:bun) then "bun install"
      when using_tool?(:yarn) then "yarn install"
      when using_tool?(:yarnpkg) then "yarnpkg install"
      when using_tool?(:pnpm) then "pnpm install"
      when using_tool?(:npm) then "npm install"
      else raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies"
      when :yarnpkg then "yarnpkg build:css"
      end
    end

    def build_command
      case
      when using_tool?(:bun) then "bun run build:css"
      when using_tool?(:yarn) then "yarn build:css"
      when using_tool?(:yarnpkg) then "yarnpkg build:css"
      when using_tool?(:pnpm) then "pnpm build:css"
      when using_tool?(:npm) then "npm run build:css"
      else raise "cssbundling-rails: No suitable tool found for building CSS"
      end
    end

    private

    def tool_exists?(tool)
      system "command -v #{tool} > /dev/null"
    end

    def using_tool?(tool)
      tool_exists?(tool) && LOCK_FILES[tool].any? { |file| File.exist?(file) }
    end
  end
end

unless ENV["SKIP_CSS_BUILD"]
  if Rake::Task.task_defined?("assets:precompile")
    Rake::Task["assets:precompile"].enhance(["css:build"])
  end

  if Rake::Task.task_defined?("test:prepare")
    Rake::Task["test:prepare"].enhance(["css:build"])
  elsif Rake::Task.task_defined?("spec:prepare")
    Rake::Task["spec:prepare"].enhance(["css:build"])
  elsif Rake::Task.task_defined?("db:test:prepare")
    Rake::Task["db:test:prepare"].enhance(["css:build"])
  end
end