tipsy 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require 'find'
2
+ require 'tipsy/view'
2
3
 
3
4
  module Tipsy
4
5
  module Runners
@@ -8,12 +9,11 @@ module Tipsy
8
9
  attr_reader :source_path, :dest_path, :scope
9
10
 
10
11
  def initialize
11
- @source_path = config.public_path
12
- @dest_path = config.compile_to
12
+ @source_path = normalize_path(config.public_path)
13
+ @dest_path = normalize_path(config.compile_to)
13
14
  excluded = [excludes, config.compile.preserve].flatten.uniq
14
15
  @_excludes = excluded
15
16
  clean_existing!
16
-
17
17
  [:public, :images, :assets, :templates].each do |m|
18
18
  send(:"compile_#{m}!")
19
19
  end
@@ -26,14 +26,22 @@ module Tipsy
26
26
  Tipsy::Site.config
27
27
  end
28
28
 
29
+ def skip_path?(src)
30
+ return false unless scope == :clean || scope == :public
31
+ end
32
+
29
33
  def skip_file?(src)
30
34
  return false unless scope == :clean || scope == :public
31
- check = case scope
32
- when :clean then config.compile.preserve
33
- when :public then config.compile.skip
34
- else []
35
+ if scope == :public
36
+ checks = config.compile.skip
37
+ relative = ::Pathname.new(src).relative_path_from(::Pathname.new(source_path))
38
+ reldir = relative.dirname.to_s
39
+ return checks.detect{ |path| path == src || path == relative.to_s || reldir.to_s == path }
40
+ end
41
+ config.compile.preserve.detect do |path|
42
+ relative = src.to_s.gsub(Tipsy::Site.public_path, '').sub(/^\//, '')
43
+ File.basename(src) == path || relative == path
35
44
  end
36
- check.detect{ |path| File.basename(src) == path }
37
45
  end
38
46
 
39
47
  alias :skip_path? :skip_file?
@@ -43,33 +51,23 @@ module Tipsy
43
51
  def clean_existing!
44
52
  @scope = :clean
45
53
  return true unless ::File.directory?(dest_path)
46
- ::Find.find(dest_path) do |path|
47
- next if path == config.compile_to
48
- if ::File.directory?(path)
49
- if skip_path?(path)
50
- ::Find.prune
51
- elsif empty_dir?(path)
52
- rm_rf(path)
53
- ::Find.prune
54
- else
55
- next
56
- end
57
- elsif ::File.exists?(path)
58
- unlink(path) unless skip_file?(path)
59
- end
54
+ tree = Tipsy::Utils::Tree.new(dest_path, source_path)
55
+ tree.collect!
56
+ tree.files.each do |file|
57
+ unlink(file)
60
58
  end
61
-
62
- Dir["#{dest_path}/**/**"].select{ |path| File.directory?(path) }.each do |dir|
63
- next if config.compile_to == dir
59
+
60
+ tree.folders.map(&:to_s).sort{ |d1,d2| d2.size<=>d1.size }.each do |dir|
64
61
  rm_rf(dir) if empty_dir?(dir)
65
62
  end
66
-
67
63
  end
68
64
 
69
65
  def compile_public!
70
66
  @scope = :public
71
67
  log_action("copy", "public files")
72
- copy_tree(source_path, dest_path)
68
+ tree = Tipsy::Utils::Tree.new(source_path, dest_path)
69
+ tree.excludes |= config.compile.skip
70
+ tree.copy!
73
71
  end
74
72
 
75
73
  def compile_images!
@@ -89,7 +87,7 @@ module Tipsy
89
87
  config.compile.assets.each do |file|
90
88
  handler.each_logical_path do |path|
91
89
  should_compile = path.is_a?(Regexp) ? file.match(path) : File.fnmatch(file.to_s, path)
92
- next unless should_compile?
90
+ next unless should_compile
93
91
 
94
92
  if asset = handler.find_asset(path)
95
93
  log_action("compile", asset.logical_path)
@@ -106,6 +104,7 @@ module Tipsy
106
104
  dir == '.' || dir == ".." || dir[0] == "." || !::File.directory?(dir)
107
105
  end
108
106
  dirs = dirs.reject do |dir|
107
+
109
108
  # Skip folders that only have partials or all files/dirs excluded
110
109
  ::Dir.entries(dir).reject{ |f| f.to_s[0] == "_" || excluded?(f) }.empty?
111
110
  end.push(File.join(Tipsy.root, "views"))
@@ -121,21 +120,23 @@ module Tipsy
121
120
  route = "/#{route.to_s}" unless route.absolute?
122
121
  route = File.join(route.to_s, tpl.split(".").first) unless tpl.to_s.match(/^index/i)
123
122
  request = MockRequest.new(route.to_s)
124
- view = Tipsy::View::Base.new(request, MockResponse.new)
123
+ view = ::Tipsy::View::Base.new(request, MockResponse.new)
125
124
 
126
125
  compiled = view.render
127
126
  next if view.template.nil?
128
127
  proper_name = proper_template_name(view.template)
128
+ tpl_path = ::Pathname.new(view.template)
129
+ relation = tpl_path.dirname.relative_path_from(::Pathname.new(view_path))
129
130
 
130
131
  if ::File.directory?(File.join(view_path, request.path))
131
- write_to = ::File.join(request.path, proper_name)
132
+ write_to = ::File.join(request.path, proper_name)
132
133
  else
133
134
  write_to = "#{request.path}#{::File.extname(proper_template_name(view.template))}"
134
135
  end
135
136
 
137
+ mkdir_p(::File.join(dest_path, relation.to_s)) unless relation.to_s == "." || relation.to_s == ".."
136
138
  log_action("render", request.path)
137
- mkdir_p ::File.join(config.compile_to, write_to)
138
- make_file(::File.join(config.compile_to, write_to), compiled)
139
+ make_file(::File.join(config.compile_to, write_to), compiled.body)
139
140
  end
140
141
  end
141
142
  end
@@ -26,7 +26,7 @@ module Tipsy
26
26
  #
27
27
  def copy_file(source, destination)
28
28
  return true if skip_file?(source)
29
- log_action("create", destination)
29
+ log_action("copy", destination)
30
30
  ::FileUtils.cp(source, destination)
31
31
  end
32
32
 
@@ -35,8 +35,8 @@ module Tipsy
35
35
  #
36
36
  def copy_folder(dirname)
37
37
  return true if skip_path?(dirname)
38
- log_action("create", dirname)
39
- ::FileUtils.mkdir(dirname)
38
+ log_action("copy", dirname)
39
+ ::FileUtils.mkdir_p(dirname)
40
40
  end
41
41
 
42
42
  ##
@@ -49,7 +49,8 @@ module Tipsy
49
49
  end
50
50
 
51
51
  def make_file(path, content)
52
- ::File.new(path, 'w').puts(content)
52
+ ::FileUtils.touch(path) unless ::File.exists?(path)
53
+ ::File.open(path, 'w').puts(content)
53
54
  end
54
55
 
55
56
  def rm_rf(path)
@@ -107,7 +108,13 @@ module Tipsy
107
108
  end
108
109
 
109
110
  def log_action(name, action)
110
- ::Tipsy.logger.action(name, action)
111
+ ::Tipsy.logger.action(name, action.to_s.gsub(Tipsy.root, ""))
112
+ end
113
+
114
+ def normalize_path(pname)
115
+ pname = ::Pathname.new(pname) if pname.is_a?(String)
116
+ return pname if pname.absolute?
117
+ ::Pathname.new(::File.join(Tipsy.root, pname.to_s))
111
118
  end
112
119
 
113
120
  end
@@ -91,11 +91,11 @@ module Tipsy
91
91
  private
92
92
 
93
93
  def set_created(path)
94
- (@creations ||= []) << path
94
+ (@creations ||= []) << path.to_s
95
95
  end
96
96
 
97
97
  def set_deleted(path)
98
- (@deletions ||= []) << path
98
+ (@deletions ||= []) << path.to_s
99
99
  end
100
100
 
101
101
  end
@@ -0,0 +1,67 @@
1
+ require 'find'
2
+ require 'pathname'
3
+
4
+ module Tipsy
5
+ module Utils
6
+ ##
7
+ # Class for processing file trees. Copies a source tree to a destination tree
8
+ # excluding specific files and directories.
9
+ #
10
+ class Tree
11
+ include Tipsy::Utils::System
12
+
13
+ attr_reader :source_path, :dest_path, :folders, :files
14
+ attr_accessor :excludes
15
+
16
+ def initialize(src, dest, excl = nil)
17
+ @source_path = normalize_path(::Pathname.new(src))
18
+ @dest_path = normalize_path(::Pathname.new(dest))
19
+ @excludes = (excl || ['.svn', '.git', '.gitignore', '.sass-cache', 'config.erb', '.rb', '.DS_Store'])
20
+ end
21
+
22
+ def collect!
23
+ @folders, @files = [], []
24
+ ::Find.find(source_path.to_s).each do |path|
25
+ next if path == source_path.to_s
26
+ if excluded?(path) || excluded_path?(path)
27
+ ::Find.prune if ::File.directory?(path)
28
+ next
29
+ end
30
+ if ::File.directory?(path)
31
+ @folders << normalize_path(::Pathname.new(path))
32
+ else
33
+ @files << normalize_path(::Pathname.new(path))
34
+ end
35
+ end
36
+ end
37
+
38
+ def copy!
39
+ collect! if folders.nil? || files.nil?
40
+ folders.each do |folder|
41
+ mkdir_p(folder.to_s.gsub(source_path.to_s, dest_path.to_s))
42
+ end
43
+ files.each do |file|
44
+ copy_file(file.to_s, file.to_s.gsub(source_path.to_s, dest_path.to_s))
45
+ end
46
+ end
47
+
48
+ def excluded_path?(src)
49
+ path = ::Pathname.new(src)
50
+ rel = path.relative_path_from(source_path).to_s
51
+ excludes.detect{ |route| route == src || route == rel.to_s }
52
+ end
53
+
54
+ def excluded?(file, against = nil)
55
+ against ||= excludes
56
+ return true if file.to_s == '.' || file.to_s == '..' || against.include?(file)
57
+ check = ::Pathname.new(file)
58
+ return true if against.include?(check.basename.to_s)
59
+ !against.detect do |exc|
60
+ (check.basename == exc || check.extname == exc)
61
+ end.nil?
62
+ end
63
+
64
+ end
65
+ end
66
+ end
67
+
data/lib/tipsy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tipsy
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/tipsy.rb CHANGED
@@ -21,6 +21,7 @@ module Tipsy
21
21
  autoload :System, 'tipsy/utils/system'
22
22
  autoload :SystemTest, 'tipsy/utils/system_test'
23
23
  autoload :Logger, 'tipsy/utils/logger'
24
+ autoload :Tree, 'tipsy/utils/tree'
24
25
  end
25
26
 
26
27
  module Compressors
@@ -0,0 +1 @@
1
+ compile.preserve
@@ -5,16 +5,17 @@ class CompilerTest < ActiveSupport::TestCase
5
5
 
6
6
  def setup
7
7
  Tipsy::Runners::Compiler.send(:include, Tipsy::Utils::SystemTest)
8
+ Tipsy::Utils::Tree.send(:include, Tipsy::Utils::SystemTest)
8
9
  @compiler = Tipsy::Runners::Compiler.new
9
10
  Tipsy::Site.configure!
10
11
  end
11
12
 
12
13
  test "Ensure source path is set" do
13
- compiler.source_path.should == Tipsy::Site.config.public_path
14
+ compiler.source_path.should == Pathname.new(Tipsy::Site.config.public_path)
14
15
  end
15
16
 
16
17
  test "Ensure destination path is set" do
17
- compiler.dest_path.should == Tipsy::Site.config.compile_to
18
+ compiler.dest_path.should == Pathname.new(Tipsy::Site.config.compile_to)
18
19
  end
19
20
 
20
21
  test "An option exists to skip specific files and folders on compile" do
@@ -25,19 +26,7 @@ class CompilerTest < ActiveSupport::TestCase
25
26
  test "Ensure skipped directory is not removed" do
26
27
  compiler.was_deleted?(File.join(Tipsy::Site.compile_to, ".svn")).should == false
27
28
  end
28
-
29
- test "Ensure un-skipped files are removed" do
30
- compiler.was_deleted?(File.join(Tipsy::Site.compile_to, "sub-path-with-skip", "fake.txt")).should == true
31
- end
32
-
33
- test "Ensure un-skipped files in folders are removed" do
34
- compiler.was_deleted?(File.join(Tipsy::Site.compile_to, "sub-path", "fake.txt")).should == true
35
- end
36
-
37
- test "Ensure un-skipped directories are removed" do
38
- compiler.was_deleted?(File.join(Tipsy::Site.compile_to, "sub-path")).should == true
39
- end
40
-
29
+
41
30
  test "Ensure templates are created" do
42
31
  compiler.was_created?(File.join(Tipsy::Site.compile_to, "sub", "page.html")).should == true
43
32
  end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class TreeTest < ActiveSupport::TestCase
4
+ attr_reader :tree
5
+
6
+ def setup
7
+ @tree = Tipsy::Utils::Tree.new(File.join(Tipsy.root, 'public'), File.join(Tipsy.root, 'compiled'))
8
+ @tree.excludes << 'skipped.txt'
9
+ @tree.excludes << "normal/sub/skipped"
10
+ @tree.collect!
11
+ end
12
+
13
+ test "Folders are excluded by name" do
14
+ tree.folders.map(&:to_s).include?(File.join(tree.source_path, 'sub','skipped')).should == false
15
+ end
16
+
17
+ test "Folders above excluded folders are preserved" do
18
+ tree.folders.map(&:to_s).include?(File.join(tree.source_path, 'normal', 'sub')).should == true
19
+ end
20
+
21
+ test "Files are excluded by name" do
22
+ tree.files.map(&:to_s).include?(File.join(tree.source_path, 'normal', 'skipped.txt')).should == false
23
+ end
24
+
25
+ test "Folders of excluded files are preserved" do
26
+ tree.folders.map(&:to_s).include?(File.join(tree.source_path, 'normal')).should == true
27
+ end
28
+
29
+
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tipsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &70264932961280 !ruby/object:Gem::Requirement
16
+ requirement: &70117518099000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70264932961280
24
+ version_requirements: *70117518099000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rack-test
27
- requirement: &70264932960720 !ruby/object:Gem::Requirement
27
+ requirement: &70117518098060 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.6'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70264932960720
35
+ version_requirements: *70117518098060
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: tilt
38
- requirement: &70264932949400 !ruby/object:Gem::Requirement
38
+ requirement: &70117518097160 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '1.3'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70264932949400
46
+ version_requirements: *70117518097160
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: i18n
49
- requirement: &70264932948880 !ruby/object:Gem::Requirement
49
+ requirement: &70117518095800 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0.6'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70264932948880
57
+ version_requirements: *70117518095800
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sass
60
- requirement: &70264932948300 !ruby/object:Gem::Requirement
60
+ requirement: &70117518094340 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '3.1'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70264932948300
68
+ version_requirements: *70117518094340
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
- requirement: &70264932947720 !ruby/object:Gem::Requirement
71
+ requirement: &70117518093420 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '3.1'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70264932947720
79
+ version_requirements: *70117518093420
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: sprockets
82
- requirement: &70264932947120 !ruby/object:Gem::Requirement
82
+ requirement: &70117518092460 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '2.0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70264932947120
90
+ version_requirements: *70117518092460
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: hike
93
- requirement: &70264932946540 !ruby/object:Gem::Requirement
93
+ requirement: &70117518091560 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '1.2'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *70264932946540
101
+ version_requirements: *70117518091560
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: erubis
104
- requirement: &70264932945980 !ruby/object:Gem::Requirement
104
+ requirement: &70117518090840 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ~>
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '2.7'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *70264932945980
112
+ version_requirements: *70117518090840
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: minitest
115
- requirement: &70264932945420 !ruby/object:Gem::Requirement
115
+ requirement: &70117518089860 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ~>
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '2.5'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70264932945420
123
+ version_requirements: *70117518089860
124
124
  description: Tipsy is a mini Rack application for working with static websites using
125
125
  Tilt, and Sprockets.
126
126
  email:
@@ -171,6 +171,7 @@ files:
171
171
  - lib/tipsy/utils/logger.rb
172
172
  - lib/tipsy/utils/system.rb
173
173
  - lib/tipsy/utils/system_test.rb
174
+ - lib/tipsy/utils/tree.rb
174
175
  - lib/tipsy/version.rb
175
176
  - lib/tipsy/view.rb
176
177
  - lib/tipsy/view/base.rb
@@ -178,10 +179,10 @@ files:
178
179
  - lib/tipsy/view/path.rb
179
180
  - test/fixtures/capture.html.erb
180
181
  - test/helpers/tag_test.rb
181
- - test/root/compiled/fake.txt
182
- - test/root/compiled/sub-path-with-skip/fake.txt
183
- - test/root/compiled/sub-path/fake.txt
182
+ - test/root/config.rb
184
183
  - test/root/layouts/default.html.erb
184
+ - test/root/public/normal/skipped.txt
185
+ - test/root/public/skipped/file.txt
185
186
  - test/root/views/index.html.erb
186
187
  - test/root/views/page.html.erb
187
188
  - test/root/views/sub/page.html.erb
@@ -191,6 +192,7 @@ files:
191
192
  - test/unit/site_test.rb
192
193
  - test/unit/tipsy_test.rb
193
194
  - test/unit/utils/system_test.rb
195
+ - test/unit/utils/tree_test.rb
194
196
  - test/view/base_test.rb
195
197
  - test/view/path_test.rb
196
198
  - tipsy.gemspec
@@ -221,10 +223,10 @@ summary: A mini Rack application server for developing static sites.
221
223
  test_files:
222
224
  - test/fixtures/capture.html.erb
223
225
  - test/helpers/tag_test.rb
224
- - test/root/compiled/fake.txt
225
- - test/root/compiled/sub-path-with-skip/fake.txt
226
- - test/root/compiled/sub-path/fake.txt
226
+ - test/root/config.rb
227
227
  - test/root/layouts/default.html.erb
228
+ - test/root/public/normal/skipped.txt
229
+ - test/root/public/skipped/file.txt
228
230
  - test/root/views/index.html.erb
229
231
  - test/root/views/page.html.erb
230
232
  - test/root/views/sub/page.html.erb
@@ -234,5 +236,6 @@ test_files:
234
236
  - test/unit/site_test.rb
235
237
  - test/unit/tipsy_test.rb
236
238
  - test/unit/utils/system_test.rb
239
+ - test/unit/utils/tree_test.rb
237
240
  - test/view/base_test.rb
238
241
  - test/view/path_test.rb
File without changes