Class: Cyborg::Assets::AssetType

Inherits:
Object
  • Object
show all
Defined in:
lib/cyborg/plugin/assets/asset.rb

Direct Known Subclasses

Javascripts, Stylesheets, Svgs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, base) ⇒ AssetType



6
7
8
9
# File 'lib/cyborg/plugin/assets/asset.rb', line 6

def initialize(plugin, base)
  @base = base
  @plugin = plugin
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



4
5
6
# File 'lib/cyborg/plugin/assets/asset.rb', line 4

def base
  @base
end

#pluginObject (readonly)

Returns the value of attribute plugin.



4
5
6
# File 'lib/cyborg/plugin/assets/asset.rb', line 4

def plugin
  @plugin
end

Instance Method Details

#build_msg(file) ⇒ Object



43
44
45
# File 'lib/cyborg/plugin/assets/asset.rb', line 43

def build_msg(file)
  "Built: #{destination(file).sub(plugin.root+'/','')}"
end

#change(modified, added, removed) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/cyborg/plugin/assets/asset.rb', line 96

def change(modified, added, removed)
  puts "Added: #{file_event(added)}"       unless added.empty?
  puts "Removed: #{file_event(removed)}"   unless removed.empty?
  puts "Modified: #{file_event(modified)}" unless modified.empty?

  build
end

#compress(file) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cyborg/plugin/assets/asset.rb', line 111

def compress(file)
  return unless Cyborg.production?
  mtime = File.mtime(file)
  gz_file = "#{file}.gz"
  return if File.exist?(gz_file) && File.mtime(gz_file) >= mtime

  File.open(gz_file, "wb") do |dest|
    gz = Zlib::GzipWriter.new(dest, Zlib::BEST_COMPRESSION)
    gz.mtime = mtime.to_i
    IO.copy_stream(open(file), gz)
    gz.close
  end

  File.utime(mtime, mtime, gz_file)
end

#destination(path) ⇒ Object



71
72
73
# File 'lib/cyborg/plugin/assets/asset.rb', line 71

def destination(path)
  plugin.asset_path(versioned(path))
end

#file_event(files) ⇒ Object



104
105
106
107
108
109
# File 'lib/cyborg/plugin/assets/asset.rb', line 104

def file_event(files)
  list = files.map { |f| f.sub(base+'/', '') }.join("  \n")
  list = "  \n#{files}" if 1 < files.size

  list 
end

#filter_files(names = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cyborg/plugin/assets/asset.rb', line 25

def filter_files(names=nil)
  names = [names].flatten.compact.map do |n|
    File.basename(n).sub(/(\..+)$/,'')
  end

  if !names.empty?
    find_files.select do |f|
      names.include? File.basename(f).sub(/(\..+)$/,'')
    end
  else
    find_files
  end
end

#find_filesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cyborg/plugin/assets/asset.rb', line 11

def find_files
  if @files
    @files
  else
    files = Dir[File.join(base, "*.#{ext}")].reject do |f|
      # Filter out partials
      File.basename(f).start_with?('_')
    end

    @files = files if Cyborg.production?
    files
  end
end

#find_node_module(cmd) ⇒ Object

Determine if an NPM module is installed by checking paths with ‘npm ls` Returns path to binary if installed



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cyborg/plugin/assets/asset.rb', line 49

def find_node_module(cmd)
  response = Open3.capture3("npm ls #{cmd}")

  # Look in local `./node_modules` path.
  # Be sure stderr is empty (the second argument returned by capture3)
  if response[1].empty?
    "$(npm bin)/#{cmd}"

  # Check global module path
  elsif Open3.capture3("npm ls -g #{cmd}")[1].empty?
    cmd
  end
end

#npm_command(cmd) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/cyborg/plugin/assets/asset.rb', line 63

def npm_command(cmd)
  cmd = cmd.split(' ')
  path = find_node_module(cmd.shift)
  if path
    system "#{path} #{cmd.join(' ')}"
  end
end

#url(path) ⇒ Object



75
76
77
# File 'lib/cyborg/plugin/assets/asset.rb', line 75

def url(path)
  plugin.asset_url(versioned(path))
end

#urls(names = nil) ⇒ Object



79
80
81
# File 'lib/cyborg/plugin/assets/asset.rb', line 79

def urls(names=nil)
  filter_files(names).map{ |file| url(file) }
end

#versioned(path) ⇒ Object



39
40
41
# File 'lib/cyborg/plugin/assets/asset.rb', line 39

def versioned(path)
  File.basename(path).sub(/(\.\w+)$/, '-'+plugin.version+'\1')
end

#watchObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cyborg/plugin/assets/asset.rb', line 83

def watch
  puts "Watching for changes to #{base.sub(plugin.root+'/', '')}..."

  Thread.new {
    listener = Listen.to(base) do |modified, added, removed|
      change(modified, added, removed)
    end

    listener.start # not blocking
    sleep
  }
end