Class: Capucine::CoffeeScript

Inherits:
Object
  • Object
show all
Defined in:
lib/coffeescript.rb

Instance Method Summary collapse

Constructor Details

#initialize(capucine) ⇒ CoffeeScript

Returns a new instance of CoffeeScript.



6
7
8
# File 'lib/coffeescript.rb', line 6

def initialize(capucine)
  @cap = capucine
end

Instance Method Details

#compile_dir(input, output) ⇒ Object



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
# File 'lib/coffeescript.rb', line 23

def compile_dir(input, output)

  if File.extname(input) == '.coffee'
    coffee_files = input
  else
    coffee_files = File.join @cap.settings.working_dir, input, "**/**.coffee"
  end

  Dir.glob(coffee_files).each do |file_coffee_o|

    file_coffee = File.expand_path file_coffee_o
    base_in_dir = File.join @cap.settings.working_dir, input

    relative_path = file_coffee.gsub(File.join(@cap.settings.working_dir, @cap.settings.conf['coffeescript_files_dir'] ),'')

    relative_path = relative_path.gsub(/\.coffee$/, '.js')
    relative_path_min = relative_path.gsub(/\.js$/, '.min.js')

    file_out = File.join @cap.settings.working_dir, output, relative_path
    file_out_min = File.join @cap.settings.working_dir, output, relative_path_min

    relative_coffee_file = file_coffee.gsub(base_in_dir, '')

    opts = {:bare => @cap.settings.conf['coffeescript_bare']}
    coffee_output_min = ""

    error = false
    begin
      coffee_output = ::CoffeeScript.compile(File.read(file_coffee), opts)
    rescue Exception => e
      error = true
      message = "#{e.message}"
      coffee_output = "var message = \"CoffeeScript Error (#{relative_coffee_file.gsub(/^\//, '')}) => \";"
      coffee_output += "message += \"#{message}\";"
      coffee_output += "throw message;"
    end

    coffee_output_min << Packr.pack(coffee_output)
    FileUtils.mkdir_p File.dirname(file_out)

    puts "[coffee] - #{relative_path} => #{message}" if error

    f1 = File.open(file_out, 'w+')
    f1.write(coffee_output)
    f1.close

    f2 = File.open(file_out_min, 'w+')
    f2.write(coffee_output_min)
    f2.close
  end

end

#run_once(file = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/coffeescript.rb', line 10

def run_once(file = nil)

  unless file
    files = Dir.glob(File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_output_dir'], '*.js')
    FileUtils.rm_r files
    self.compile_dir @cap.settings.conf['coffeescript_files_dir'], @cap.settings.conf['coffeescript_output_dir']
  else
    self.compile_dir file, @cap.settings.conf['coffeescript_output_dir']
  end

  puts "[coffee] - Compiled"
end

#run_watchObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/coffeescript.rb', line 77

def run_watch
  require 'fssm'

  files_to_lookat = File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_files_dir']
  js_generated_dir = File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_output_dir']

  coffee = @cap.coffee

  coffee_thread = Thread.new {

    FSSM.monitor(files_to_lookat, :directories => true) do
      update do |b, r|
        file = File.join b, r
        coffee.run_once file if File.extname(r) == '.coffee' # COFFEE
      end

      create do |b, r|
        file = File.join b, r
        coffee.run_once file if File.extname(r) == '.coffee' # COFFEE
      end

      delete do |b, r|

        file_name = File.expand_path File.join(b, r)
        folder_name = File.dirname file_name
        js_files = [
          File.join(js_generated_dir, r.gsub('.coffee', '.js')),
          File.join(js_generated_dir, r.gsub('.coffee', '.min.js')),
        ]

        if Dir["#{folder_name}/*"].empty? and folder_name != js_generated_dir
          # delete full dir
          to_delete = File.expand_path(File.dirname(js_files[0]))
          FileUtils.rm_r to_delete
        else
          js_files.each do |file|
            File.delete(file) if File.exist?(file)
          end
        end

      end
    end

  }

  return coffee_thread


end