guard-js-static-require 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in guard-js-static-require.gemspec
4
+ gemspec
@@ -0,0 +1,5 @@
1
+ guard 'minitest' do
2
+ watch(%r|^test/(.*)\/?test_(.*)\.rb|)
3
+ watch(%r|^lib/(.*?)([^/]+)\.rb$|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
4
+ watch(%r|^test/test_helper\.rb|) { "test" }
5
+ end
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "guard/js-static-require/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "guard-js-static-require"
7
+ s.version = Guard::JsStaticRequireVersion::VERSION
8
+ s.authors = ["Wilker Lucio"]
9
+ s.email = ["wilkerlucio@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Automatic replace script tags for loading files}
12
+ s.description = %q{This guard watches for new/removed javascript files and automatic inject the script tags for loading them on an html page.}
13
+
14
+ s.rubyforge_project = "guard-js-static-require"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "guard-minitest"
22
+ s.add_development_dependency "growl_notify"
23
+ end
@@ -0,0 +1,160 @@
1
+ require 'guard'
2
+ require 'guard/guard'
3
+
4
+ module Guard
5
+ class Jsstaticrequire < Guard
6
+ DEFAULT_OPTIONS = {
7
+ :build_on_start => false,
8
+ :libs => [],
9
+ :start_delim => /<!-- START JS_STATIC_REQUIRE -->/,
10
+ :end_delim => /<!-- END JS_STATIC_REQUIRE -->/
11
+ }
12
+
13
+ attr_accessor :files
14
+
15
+ # Initialize a Guard.
16
+ # @param [Array<Guard::Watcher>] watchers the Guard file watchers
17
+ # @param [Hash] options the custom Guard options
18
+ def initialize(watchers = [], options = {})
19
+ defaults = DEFAULT_OPTIONS.clone
20
+
21
+ @files = []
22
+
23
+ options[:libs].each do |lib|
24
+ watchers << ::Guard::Watcher.new(%r{^#{ lib }/(.+\.js)$})
25
+ end
26
+
27
+ super(watchers, defaults.merge(options))
28
+ end
29
+
30
+ # Call once when Guard starts. Please override initialize method to init stuff.
31
+ # @raise [:task_has_failed] when start has failed
32
+ def start
33
+ run_all if options[:build_on_start]
34
+ end
35
+
36
+ # Called when `stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
37
+ # @raise [:task_has_failed] when stop has failed
38
+ def stop
39
+ end
40
+
41
+ # Called when `reload|r|z + enter` is pressed.
42
+ # This method should be mainly used for "reload" (really!) actions like reloading passenger/spork/bundler/...
43
+ # @raise [:task_has_failed] when reload has failed
44
+ def reload
45
+ end
46
+
47
+ # Called when just `enter` is pressed
48
+ # This method should be principally used for long action like running all specs/tests/...
49
+ # @raise [:task_has_failed] when run_all has failed
50
+ def run_all
51
+ UI.info "Injecting scripts on #{options[:updates]}"
52
+
53
+ reset_files
54
+ inject_script_load
55
+
56
+ Notifier.notify("Success injected scripts on #{options[:updates]}")
57
+ end
58
+
59
+ # Called on file(s) modifications that the Guard watches.
60
+ # @param [Array<String>] paths the changes files or paths
61
+ # @raise [:task_has_failed] when run_on_change has failed
62
+ def run_on_change(paths)
63
+ return unless contains_new_path?(paths)
64
+ run_all
65
+ end
66
+
67
+ # Called on file(s) deletions that the Guard watches.
68
+ # @param [Array<String>] paths the deleted files or paths
69
+ # @raise [:task_has_failed] when run_on_change has failed
70
+ def run_on_deletion(paths)
71
+ run_all
72
+ end
73
+
74
+ def reset_files
75
+ @files = scan_libs(options[:libs])
76
+ end
77
+
78
+ def scan_libs(libs)
79
+ libs.map { |path| scan_path(path) }.flatten.uniq
80
+ end
81
+
82
+ def scan_path(path)
83
+ if File.directory? path
84
+ Dir.glob(path + "/**/*.js").sort do |a, b|
85
+ da = a.split(File::SEPARATOR)
86
+ db = b.split(File::SEPARATOR)
87
+
88
+ comp = da.length <=> db.length
89
+
90
+ if comp == 0
91
+ a <=> b
92
+ else
93
+ comp
94
+ end
95
+ end
96
+ else
97
+ [path]
98
+ end
99
+ end
100
+
101
+ def contains_new_path?(paths)
102
+ (paths - @files).length > 0
103
+ end
104
+
105
+ def inject(value, source)
106
+ if os = source.match(options[:start_delim]) and oe = source.match(options[:end_delim])
107
+ os = os.end(0)
108
+ oe = oe.begin(0)
109
+ source[os...oe] = value
110
+ end
111
+
112
+ source
113
+ end
114
+
115
+ def tabulation(source)
116
+ if os = source.match(options[:start_delim])
117
+ line = source[0...os.begin(0)].split(/\r\n|\r|\n/).last
118
+
119
+ spaces = line.match(/^([\s\t]*)/)
120
+ spaces[1]
121
+ else
122
+ nil
123
+ end
124
+ end
125
+
126
+ def build_load_string(source)
127
+ tab = tabulation(source)
128
+ scripts = "\n"
129
+
130
+ @files.each do |file|
131
+ scripts += %Q{#{tab}<script type="text/javascript" src="#{relative_path(options[:updates], file)}"></script>\n}
132
+ end
133
+
134
+ scripts += tab
135
+ scripts
136
+ end
137
+
138
+ def relative_path(target, path)
139
+ target_dir = File.expand_path(File.dirname(target)).split(File::SEPARATOR)
140
+ path_dir = File.expand_path(File.dirname(path)).split(File::SEPARATOR)
141
+
142
+ while target_dir.length > 0 and target_dir.first == path_dir.first
143
+ target_dir.shift
144
+ path_dir.shift
145
+ end
146
+
147
+ (".." + File::Separator) * (target_dir.length) + path_dir.push(File.basename(path)).join(File::SEPARATOR)
148
+ end
149
+
150
+ def inject_script_load
151
+ source = File.read(options[:updates])
152
+ loader = build_load_string(source)
153
+ result = inject(loader, source)
154
+
155
+ File.open(options[:updates], "wb") do |file|
156
+ file << result
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1 @@
1
+ guard "js-static-require", :libs => ["vendor", "lib"], :updates => "examples/index.html"
@@ -0,0 +1,5 @@
1
+ module Guard
2
+ module JsStaticRequireVersion
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
File without changes
File without changes
@@ -0,0 +1,68 @@
1
+ require "test_helper"
2
+
3
+ class TestJsStaticRequire < MiniTest::Unit::TestCase
4
+ def setup
5
+ @guard = Guard::Jsstaticrequire.new([], {
6
+ :start_delim => /<!-- s -->/,
7
+ :end_delim => /<!-- e -->/
8
+ })
9
+ end
10
+
11
+ def test_scan_libs
12
+ assert_equal ["test/fixtures/js-tree/internal/file1.js",
13
+ "test/fixtures/js-tree/file.js",
14
+ "test/fixtures/js-tree/a/before.js",
15
+ "test/fixtures/js-tree/internal/file2.js"], @guard.scan_libs(["test/fixtures/js-tree/internal/file1.js", "test/fixtures/js-tree"])
16
+ end
17
+
18
+ def test_scan_path_sending_a_file
19
+ assert_equal ["lib/guard/js_static_require.rb"], @guard.scan_path("lib/guard/js_static_require.rb")
20
+ end
21
+
22
+ def test_scan_path_with_directory
23
+ assert_equal ["test/fixtures/js-tree/file.js",
24
+ "test/fixtures/js-tree/a/before.js",
25
+ "test/fixtures/js-tree/internal/file1.js",
26
+ "test/fixtures/js-tree/internal/file2.js"], @guard.scan_path("test/fixtures/js-tree")
27
+ end
28
+
29
+ def test_code_injection
30
+ string = "hello <!-- s --><!-- e --> dad"
31
+ assert_equal "hello <!-- s -->inside<!-- e --> dad", @guard.inject("inside", string)
32
+ end
33
+
34
+ def test_code_injection_invalid
35
+ string = "hello <!-- u --><!-- e --> dad"
36
+ assert_equal "hello <!-- u --><!-- e --> dad", @guard.inject("inside", string)
37
+ end
38
+
39
+ def test_tabulation
40
+ string = "hello\n <!-- s -->"
41
+ assert_equal(" ", @guard.tabulation(string))
42
+ end
43
+
44
+ def test_tabulation_with_string_on_beginning_of_line
45
+ string = "hello\n a <!-- s -->"
46
+ assert_equal(" ", @guard.tabulation(string))
47
+ end
48
+
49
+ def test_tabulation_on_first_line
50
+ string = " <!-- s -->"
51
+ assert_equal(" ", @guard.tabulation(string))
52
+ end
53
+
54
+ def test_build_load_string
55
+ @guard.files = ["vendor/jquery.js", "vendor/underscore.js"]
56
+ @guard.options[:updates] = "examples/index.html"
57
+
58
+ expected = %Q{\n <script type="text/javascript" src="../vendor/jquery.js"></script>\n <script type="text/javascript" src="../vendor/underscore.js"></script>\n }
59
+
60
+ assert_equal(expected, @guard.build_load_string(" <!-- s -->"))
61
+ end
62
+
63
+ def test_relative_path
64
+ assert_equal("../lib/library.js", @guard.relative_path("examples/index.html", "lib/library.js"))
65
+ assert_equal("lib/library.js", @guard.relative_path("index.html", "lib/library.js"))
66
+ assert_equal("../library.js", @guard.relative_path("examples/index.html", "library.js"))
67
+ end
68
+ end
@@ -0,0 +1,2 @@
1
+ require "minitest/unit"
2
+ require "guard/js-static-require"
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-js-static-require
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Wilker Lucio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-10-21 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: guard-minitest
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: growl_notify
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: This guard watches for new/removed javascript files and automatic inject the script tags for loading them on an html page.
38
+ email:
39
+ - wilkerlucio@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - Guardfile
50
+ - Rakefile
51
+ - guard_js_static_require.gemspec
52
+ - lib/guard/js-static-require.rb
53
+ - lib/guard/js-static-require/templates/Guardfile
54
+ - lib/guard/js-static-require/version.rb
55
+ - test/fixtures/js-tree/a/before.js
56
+ - test/fixtures/js-tree/file.js
57
+ - test/fixtures/js-tree/internal/file1.js
58
+ - test/fixtures/js-tree/internal/file2.js
59
+ - test/guard/test_js-static-require.rb
60
+ - test/test_helper.rb
61
+ homepage: ""
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: guard-js-static-require
84
+ rubygems_version: 1.8.10
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Automatic replace script tags for loading files
88
+ test_files:
89
+ - test/fixtures/js-tree/a/before.js
90
+ - test/fixtures/js-tree/file.js
91
+ - test/fixtures/js-tree/internal/file1.js
92
+ - test/fixtures/js-tree/internal/file2.js
93
+ - test/guard/test_js-static-require.rb
94
+ - test/test_helper.rb