Class: Fusion::Basic

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

Direct Known Subclasses

Optimized, Quick

Instance Method Summary collapse

Constructor Details

#initialize(bundle_config = nil, project_path = nil) ⇒ Basic

Returns a new instance of Basic.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/basic.rb', line 13

def initialize(bundle_config=nil, project_path=nil)
  
  if bundle_config && project_path

    @bundle_configs = [bundle_config]
    @bundle_options = { :project_path => project_path }
  else

    @bundle_options = Fusion.instance_variable_get('@options')

    if @bundle_options[:bundle_configs]
      @bundle_configs = @bundle_options[:bundle_configs]
    else
      @bundle_configs = YAML::load(File.open(@bundle_options[:bundle_file_path]))
    end

  end

  @log = @bundle_options[:logger] || Logger.new(STDOUT)

end

Instance Method Details

#gather_files(config) ⇒ Object



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
78
79
80
81
82
83
84
85
# File 'lib/basic.rb', line 47

def gather_files(config)
  input_files = []

  if(config[:input_files])
    config[:input_files].each do |input_file|
      if (input_file =~ URI::regexp).nil?
        # Not a URL
        file_path = input_file

        unless input_file == File.absolute_path(input_file)
          file_path = File.join(@bundle_options[:project_path], input_file)
        end

        input_files << file_path
      else
        # This is a remote file, if we don't have it, get it
        input_files << get_remote_file(input_file)
      end          
    end
  end

  if (config[:input_directory])
    directory = File.join(@bundle_options[:project_path],config[:input_directory])

    unless File.exists?(directory)
      @log.debug "Path #{directory} does not exist"
      FileUtils.mkpath(directory)
      @log.debug "Created path: #{directory}"
    end

    file_names = Dir.open(directory).entries.sort.find_all {|filename| filename.end_with?(".js") }

    input_files += file_names.collect do |file_name|
      File.join(directory, file_name)
    end
  end

  input_files.uniq
end

#get_output_file(config) ⇒ Object

Raises:

  • (Exception)


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/basic.rb', line 87

def get_output_file(config)
  raise Exception.new("Undefined js bundler output file") if config[:output_file].nil?
  output_file = File.join(@bundle_options[:project_path], config[:output_file])
  path_directories = output_file.split("/")

  if path_directories.size > 1
    path = File.join(File.expand_path("."), path_directories[0..-2].join("/"))
    FileUtils::mkpath(File.join(path,"/"))
  end

  output_file
end

#get_remote_file(url) ⇒ Object

Raises:

  • (Exception)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/basic.rb', line 100

def get_remote_file(url)
  filename = CGI.escape(url)
  local_directory = File.join(@bundle_options[:project_path], ".remote")
  local_file_path = File.join(local_directory, filename)
  
  return local_file_path if File.exists?(local_file_path)
  
  @log.debug "Fetching remote file (#{url})"

  m = Mechanize.new
  response = m.get(url)

  raise Exception.new("Error downloading file (#{url}) -- returned code #{repsonse.code}") unless response.code == "200"
  
  @log.debug "Got file (#{url})"

  unless Dir.exists?(local_directory)
    Dir.mkdir(local_directory)
  end
              
  File.open(local_file_path,"w") {|f| f << response.body}
  
  local_file_path
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/basic.rb', line 35

def run
  start = Time.now

  bundles = @bundle_configs.collect do |config|
    bundle(config)
  end

  @log.debug "Javascript Reloaded #{@bundle_configs.size} bundle(s) (#{Time.now - start}s)"

  bundles
end