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

Returns a new instance of 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



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

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

#destination(path) ⇒ Object



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

def destination(path)
  File.join(plugin.destination, plugin.asset_root, versioned(path))
end

#file_event(files) ⇒ Object



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

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
62
# 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



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

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

#url(path) ⇒ Object



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

def url(path)
  File.join(plugin.asset_root, versioned(path))
end

#urls(names = nil) ⇒ Object



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

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



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

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