Class: Jscompiler::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/jscompiler/commands/base.rb

Direct Known Subclasses

Closure, Uglifier, Yahoo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



31
32
33
# File 'lib/jscompiler/commands/base.rb', line 31

def initialize(opts = {})
  @options = opts
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/jscompiler/commands/base.rb', line 29

def options
  @options
end

Class Method Details

.compile_group(group, opts = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jscompiler/commands/base.rb', line 106

def self.compile_group(group, opts = {})
  puts("\r\nCompiling #{group} group...")
  
  t0 = Time.now
  Jscompiler::Config.compiler_command(group, opts).new(opts.merge({
    :group => group
  })).run
  t1 = Time.now

  puts("Done. Compilation took #{t1-t0} seconds\r\n")
end

Instance Method Details

#comments_regexpObject



43
44
45
# File 'lib/jscompiler/commands/base.rb', line 43

def comments_regexp 
  /\/\*(!)*[^*]*\*+(?:[^*\/][^*]*\*+)*\//  
end

#debug_file_pathObject



71
72
73
# File 'lib/jscompiler/commands/base.rb', line 71

def debug_file_path
  Jscompiler::Config.output_destination(group, :suffix => ".debug")
end

#execute(cmd, opts = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jscompiler/commands/base.rb', line 91

def execute(cmd, opts = {})
  puts("\r\n$ " + cmd)
  return if opts[:cold]

  # Kernel.spawn(command)    

  result = system(cmd)
  return if opts[:ignore_result]

  unless result
    puts("Build failed.")
    exit 1
  end      
end

#generate_temp_fileObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/jscompiler/commands/base.rb', line 75

def generate_temp_file
  File.open(temp_file_path, 'w') do |file| 
    Jscompiler::Config.files(group).each do |fl|
      puts("Processing #{fl}...")
      content = File.read(fl)
      content = sanitize(content)
      file.write(content)
    end
  end
end

#groupObject



35
36
37
# File 'lib/jscompiler/commands/base.rb', line 35

def group 
  options[:group]
end

#output_file_pathObject



86
87
88
89
# File 'lib/jscompiler/commands/base.rb', line 86

def output_file_path
  return options["output"] if options["output"]
  Jscompiler::Config.output_destination(group, options)
end

#prepare_arguments(args) ⇒ Object



51
52
53
# File 'lib/jscompiler/commands/base.rb', line 51

def prepare_arguments(args)
  args.collect{|arg| arg.join(' ')}.join(' ')
end

#prepare_command(cmd, args) ⇒ Object



55
56
57
# File 'lib/jscompiler/commands/base.rb', line 55

def prepare_command(cmd, args)
  "#{cmd} #{prepare_arguments(args)}"
end

#runObject



39
40
41
# File 'lib/jscompiler/commands/base.rb', line 39

def run
  raise("Must be implemented by the extending class")
end

#sanitize(content) ⇒ Object



47
48
49
# File 'lib/jscompiler/commands/base.rb', line 47

def sanitize(content)
  content.gsub(comments_regexp, '')
end

#save_or_delete_temp_fileObject



59
60
61
62
63
64
65
# File 'lib/jscompiler/commands/base.rb', line 59

def save_or_delete_temp_file
  if Jscompiler::Config.debug?(group)
    FileUtils.mv(temp_file_path, debug_file_path)
  else
    FileUtils.rm(temp_file_path)
  end
end

#temp_file_pathObject



67
68
69
# File 'lib/jscompiler/commands/base.rb', line 67

def temp_file_path
  Jscompiler::Config.output_destination(group, :suffix => ".tmp")
end