Class: Guard::Jsstaticrequire

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/js-static-require.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :build_on_start => false,
  :libs           => [],
  :start_delim    => /<!-- START JS_STATIC_REQUIRE -->/,
  :end_delim      => /<!-- END JS_STATIC_REQUIRE -->/
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Jsstaticrequire

Initialize a Guard.

Parameters:

  • watchers (Array<Guard::Watcher>) (defaults to: [])

    the Guard file watchers

  • options (Hash) (defaults to: {})

    the custom Guard options



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/guard/js-static-require.rb', line 18

def initialize(watchers = [], options = {})
  defaults = DEFAULT_OPTIONS.clone

  @files = []

  options[:libs].each do |lib|
    watchers << ::Guard::Watcher.new(%r{^#{ lib }/(.+\.js)$})
  end

  super(watchers, defaults.merge(options))
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



13
14
15
# File 'lib/guard/js-static-require.rb', line 13

def files
  @files
end

Instance Method Details

#build_load_string(source) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/guard/js-static-require.rb', line 126

def build_load_string(source)
  tab = tabulation(source)
  scripts = "\n"

  @files.each do |file|
    scripts += %Q{#{tab}<script type="text/javascript" src="#{relative_path(options[:updates], file)}"></script>\n}
  end

  scripts += tab
  scripts
end

#contains_new_path?(paths) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/guard/js-static-require.rb', line 101

def contains_new_path?(paths)
  (paths - @files).length > 0
end

#inject(value, source) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/guard/js-static-require.rb', line 105

def inject(value, source)
  if os = source.match(options[:start_delim]) and oe = source.match(options[:end_delim])
    os = os.end(0)
    oe = oe.begin(0)
    source[os...oe] = value
  end

  source
end

#inject_script_loadObject



150
151
152
153
154
155
156
157
158
# File 'lib/guard/js-static-require.rb', line 150

def inject_script_load
  source = File.read(options[:updates])
  loader = build_load_string(source)
  result = inject(loader, source)

  File.open(options[:updates], "wb") do |file|
    file << result
  end
end

#relative_path(target, path) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/guard/js-static-require.rb', line 138

def relative_path(target, path)
  target_dir = File.expand_path(File.dirname(target)).split(File::SEPARATOR)
  path_dir = File.expand_path(File.dirname(path)).split(File::SEPARATOR)

  while target_dir.length > 0 and target_dir.first == path_dir.first
    target_dir.shift
    path_dir.shift
  end

  (".." + File::Separator) * (target_dir.length) + path_dir.push(File.basename(path)).join(File::SEPARATOR)
end

#reloadObject

Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…

Raises:

  • (:task_has_failed)

    when reload has failed



44
45
# File 'lib/guard/js-static-require.rb', line 44

def reload
end

#reset_filesObject



74
75
76
# File 'lib/guard/js-static-require.rb', line 74

def reset_files
  @files = scan_libs(options[:libs])
end

#run_allObject

Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…

Raises:

  • (:task_has_failed)

    when run_all has failed



50
51
52
53
54
55
56
57
# File 'lib/guard/js-static-require.rb', line 50

def run_all
  UI.info "Injecting scripts on #{options[:updates]}"

  reset_files
  inject_script_load

  Notifier.notify("Success injected scripts on #{options[:updates]}")
end

#run_on_change(paths) ⇒ Object

Called on file(s) modifications that the Guard watches.

Parameters:

  • paths (Array<String>)

    the changes files or paths

Raises:

  • (:task_has_failed)

    when run_on_change has failed



62
63
64
65
# File 'lib/guard/js-static-require.rb', line 62

def run_on_change(paths)
  return unless contains_new_path?(paths)
  run_all
end

#run_on_deletion(paths) ⇒ Object

Called on file(s) deletions that the Guard watches.

Parameters:

  • paths (Array<String>)

    the deleted files or paths

Raises:

  • (:task_has_failed)

    when run_on_change has failed



70
71
72
# File 'lib/guard/js-static-require.rb', line 70

def run_on_deletion(paths)
  run_all
end

#scan_libs(libs) ⇒ Object



78
79
80
# File 'lib/guard/js-static-require.rb', line 78

def scan_libs(libs)
  libs.map { |path| scan_path(path) }.flatten.uniq
end

#scan_path(path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/guard/js-static-require.rb', line 82

def scan_path(path)
  if File.directory? path
    Dir.glob(path + "/**/*.js").sort do |a, b|
      da = a.split(File::SEPARATOR)
      db = b.split(File::SEPARATOR)

      comp = da.length <=> db.length

      if comp == 0
        a <=> b
      else
        comp
      end
    end
  else
    [path]
  end
end

#startObject

Call once when Guard starts. Please override initialize method to init stuff.

Raises:

  • (:task_has_failed)

    when start has failed



32
33
34
# File 'lib/guard/js-static-require.rb', line 32

def start
  run_all if options[:build_on_start]
end

#stopObject

Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).

Raises:

  • (:task_has_failed)

    when stop has failed



38
39
# File 'lib/guard/js-static-require.rb', line 38

def stop
end

#tabulation(source) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/guard/js-static-require.rb', line 115

def tabulation(source)
  if os = source.match(options[:start_delim])
    line = source[0...os.begin(0)].split(/\r\n|\r|\n/).last

    spaces = line.match(/^([\s\t]*)/)
    spaces[1]
  else
    nil
  end
end