adva-static 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +0,0 @@
1
- module Adva
2
- class Static
3
- module Watch
4
- autoload :Handler, 'adva/static/watch/handler'
5
- end
6
- end
7
- end
@@ -1,57 +0,0 @@
1
- require 'observer'
2
- require 'watchr'
3
- require 'watchr/event_handlers/portable'
4
-
5
- module Adva
6
- class Static
7
- module Watch
8
- class Handler
9
- include Observable
10
-
11
- def initialize(observable, pattern)
12
- add_observer(observable)
13
- @pattern = pattern
14
- @current = Dir[pattern]
15
- @mtime = Time.now
16
- end
17
-
18
- def listen
19
- loop { trigger; sleep(0.5) }
20
- end
21
-
22
- def trigger
23
- events.each do |path, event|
24
- changed(true)
25
- notify_observers(path, event)
26
- end
27
- end
28
-
29
- protected
30
-
31
- def events
32
- @last = @current.dup
33
- @current = Dir[@pattern]
34
- deleted + created + modified
35
- end
36
-
37
- def modified
38
- (@current & @last).each do |path|
39
- mtime = File.mtime(path)
40
- if mtime > @mtime
41
- @mtime = mtime
42
- return [[path, :modified]]
43
- end
44
- end && []
45
- end
46
-
47
- def created
48
- (@current - @last).map { |path| [path, :created] }
49
- end
50
-
51
- def deleted
52
- (@last - @current).map { |path| [path, :deleted] }
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,73 +0,0 @@
1
- require 'thor'
2
- require 'thor/group'
3
- require 'patches/thor/core_ext/hash'
4
- require 'patches/thor/group/symbolized_options'
5
-
6
- module Adva
7
- module Tasks
8
- class Static
9
- class Setup < Thor::Group
10
- namespace 'adva:static:setup'
11
- desc 'Setup a static version of your site'
12
- class_option :source, :required => false, :banner => 'source directory (defaults to import)'
13
- class_option :target, :required => false, :banner => 'source directory (defaults to export)'
14
- class_option :host, :required => false, :banner => 'hostname of your site (defaults to example.org)'
15
- class_option :title, :required => false, :banner => 'title of your site (defaults to the hostname)'
16
- class_option :remote, :required => false, :banner => 'github repository url (defaults to none)'
17
-
18
- def export
19
- require 'config/environment'
20
- Adva::Static::Setup.new(symbolized_options).run
21
- end
22
- end
23
-
24
- class Import < Thor::Group
25
- namespace 'adva:static:import'
26
- desc 'Import a site from a directory'
27
- class_option :source, :required => false
28
-
29
- def import
30
- require 'config/environment'
31
- Adva::Static::Import.new(symbolized_options).run
32
- end
33
- end
34
-
35
- class Export < Thor::Group
36
- namespace 'adva:static:export'
37
- desc 'Export a static version of a site'
38
- class_option :target, :required => false
39
-
40
- def export
41
- require 'config/environment'
42
- Adva::Static::Export.new(Rails.application, symbolized_options).run
43
- end
44
- end
45
-
46
- class Update < Thor::Group
47
- namespace 'adva:static:update'
48
- desc 'Import and export a static version of a site'
49
- class_option :source, :required => false
50
- class_option :target, :required => false
51
-
52
- def export
53
- require 'config/environment'
54
- Adva::Static::Import.new(symbolized_options).run
55
- Adva::Static::Export.new(Rails.application, symbolized_options).run
56
- end
57
- end
58
-
59
- class Server < Thor::Group
60
- namespace 'adva:static:server'
61
- desc 'Start the adva:static server and watcher'
62
- class_option :root, :required => false, :default => 'export'
63
-
64
- def server
65
- ARGV.shift
66
- Dir.chdir(symbolized_options[:root])
67
- require "rack"
68
- ::Rack::Server.start
69
- end
70
- end
71
- end
72
- end
73
- end
@@ -1,3 +0,0 @@
1
- module AdvaStatic
2
- VERSION = "0.0.2"
3
- end
@@ -1,85 +0,0 @@
1
- Given /^an empty import directory "([^"]+)"$/ do |name|
2
- @import_dir = Pathname.new("/tmp/adva-static-test/import/#{name}")
3
- import_dir.mkpath
4
- end
5
-
6
- Given /^an empty export directory$/ do
7
- @export_dir = Pathname.new("/tmp/adva-static-test/export")
8
- export_dir.mkpath
9
- end
10
-
11
- Given /^a source file "([^\"]*)"$/ do |filename|
12
- setup_files([filename, '']) unless filename.blank?
13
- end
14
-
15
- Given /^a source file "([^\"]*)" with the following values:$/ do |filename, table|
16
- setup_files([filename, YAML.dump(table.rows_hash)]) unless filename.blank?
17
- end
18
-
19
- Given /^the following source files:$/ do |table|
20
- table.hashes.each do |hash|
21
- setup_files([hash.delete('file'), YAML.dump(hash)])
22
- end
23
- end
24
-
25
- Given /^a watcher has started$/ do
26
- @watch ||= begin
27
- Adva::Static::Rack::Watch.any_instance.stubs(:run!)
28
- Adva::Static::Rack::Watch.new(Adva::Static::Rack::Export.new(Rails.application, :target => export_dir), :dir => import_dir)
29
- end
30
- end
31
-
32
- When /^I run the import task$/ do
33
- Adva::Static::Import.new(:source => import_dir).run
34
- end
35
-
36
- When /^I create the file "([^"]*)" with the following values triggering the watcher:$/ do |filename, table|
37
- setup_files([filename, YAML.dump(table.rows_hash)]) unless filename.blank?
38
- @watch.send(:handler).trigger
39
- end
40
-
41
- When /^I update the file "([^"]*)" with the following values triggering the watcher:$/ do |filename, table|
42
- setup_files([filename, YAML.dump(table.rows_hash)])
43
- file = import_dir.join(filename)
44
- file.utime(file.atime, future)
45
- @watch.send(:handler).trigger
46
- end
47
-
48
- When /^I delete the file "([^"]*)" triggering the watcher$/ do |filename|
49
- import_dir.join(filename).unlink
50
- @watch.send(:handler).trigger
51
- end
52
-
53
- Then /^the watcher should "([^\"]*)" the following "([^\"]*)" params for the file "([^\"]*)":$/ do |method, key, file, table|
54
- import = Adva::Static::Import.new(:source => import_dir)
55
- request = import.request_for(file)
56
- params = request.params
57
- method.downcase!
58
-
59
- # model = import.send(:recognize, file).first
60
- # debugger
61
-
62
- case method
63
- when 'put', 'delete'
64
- assert_equal method, params['_method'].try(:downcase)
65
- when 'post'
66
- assert !params.key?('_method')
67
- end
68
-
69
- expected = table.rows_hash.symbolize_keys
70
- actual = request.params[key.to_sym].slice(*expected.keys)
71
- assert_equal expected, actual
72
- end
73
-
74
- Then /^there should be an export file "([^"]*)" containing "([^"]*)"$/ do |filename, text|
75
- file = export_dir.join(filename)
76
- assert file.exist?, "expected #{file.to_s.inspect} to exist"
77
- file = File.read(file)
78
- assert file.include?(text), "expected #{file.inspect} to include #{text.inspect} "
79
- end
80
-
81
- Then /^there should not be an export file "([^"]*)"$/ do |filename|
82
- file = export_dir.join(filename)
83
- assert !file.exist?, "expected #{file.to_s.inspect} not to exist"
84
- end
85
-
@@ -1,133 +0,0 @@
1
- module TestHelper
2
- module Static
3
- def setup
4
- setup_import_directory
5
- super
6
- end
7
-
8
- def teardown_import_directory
9
- import_dir.rmtree rescue nil
10
- end
11
-
12
- def teardown_export_directory
13
- export_dir.rmtree rescue nil
14
- end
15
-
16
- def import_dir
17
- @import_dir ||= Pathname.new('/tmp/adva-static-test/import/ruby-i18n.org')
18
- end
19
-
20
- def export_dir
21
- @export_dir ||= Pathname.new('/tmp/adva-static-test/export')
22
- end
23
-
24
- def teardown
25
- teardown_import_directory
26
- super
27
- end
28
-
29
- def request(path)
30
- Adva::Static::Import.new(:source => import_dir).request_for(path)
31
- end
32
-
33
- def source(path)
34
- Adva::Static::Import::Source.new(path, import_dir)
35
- end
36
-
37
- def setup_site_record
38
- Factory(:site, :host => 'ruby-i18n.org')
39
- end
40
-
41
- def setup_root_page_record
42
- setup_site_record
43
- end
44
-
45
- def setup_non_root_page_record
46
- site = setup_site_record
47
- site.pages.create!(:name => 'Contact')
48
- end
49
-
50
- def setup_root_blog_record
51
- site = setup_site_record
52
- site.pages.first.destroy
53
-
54
- site.blogs.create!(:name => 'Home', :posts_attributes => [
55
- { :title => 'Welcome to the future of I18n in Ruby on Rails', :body => 'Welcome to the future!', :created_at => '2008-07-31' }
56
- ])
57
- end
58
-
59
- def setup_non_root_blog_record
60
- site = setup_site_record
61
- site.blogs.create!(:name => 'Blog', :posts_attributes => [
62
- { :title => 'Welcome to the future of I18n in Ruby on Rails', :body => 'Welcome to the future!', :created_at => '2008-07-31' }
63
- ])
64
- end
65
-
66
- def setup_import_directory
67
- import_dir.mkpath
68
- setup_dirs(%w(images javascripts stylesheets))
69
- setup_files(['config.ru', 'foo'], ['site.yml', YAML.dump(:host => 'ruby-i18n.org', :name => 'Ruby I18n', :title => 'Ruby I18n')])
70
- end
71
-
72
- def setup_root_blog
73
- setup_files(
74
- ['2008/07/31/welcome-to-the-future-of-i18n-in-ruby-on-rails.yml', YAML.dump(:body => 'Welcome to the future')],
75
- ['2009/07/12/ruby-i18n-gem-hits-0-2-0.yml', YAML.dump(:body => 'Ruby I18n hits 0.2.0')]
76
- )
77
- end
78
-
79
- def setup_non_root_blog
80
- setup_files(
81
- ['blog/2008/07/31/welcome-to-the-future-of-i18n-in-ruby-on-rails.yml', YAML.dump(:body => 'Welcome to the future')],
82
- ['blog/2009/07/12/ruby-i18n-gem-hits-0-2-0.yml', YAML.dump(:body => 'Ruby I18n hits 0.2.0')]
83
- )
84
- end
85
-
86
- def setup_root_page
87
- setup_files(
88
- ['index.yml', YAML.dump(:name => 'Home', :body => 'home')]
89
- )
90
- end
91
-
92
- def setup_root_nested_page
93
- setup_files(
94
- ['home/nested.yml', YAML.dump(:body => 'nested under home')]
95
- )
96
- end
97
-
98
- def setup_non_root_page
99
- setup_files(
100
- ['contact.yml', YAML.dump(:body => 'contact')]
101
- )
102
- end
103
-
104
- def setup_non_root_nested_page
105
- setup_files(
106
- ['contact/nested.yml', YAML.dump(:body => 'nested under contact')]
107
- )
108
- end
109
-
110
- def setup_nested_page
111
- setup_files(
112
- ['contact/mailer.yml', YAML.dump(:body => 'contact mailer')]
113
- )
114
- end
115
-
116
- def setup_dirs(paths)
117
- paths.each do |path|
118
- FileUtils.mkdir_p(import_dir.join(path))
119
- end
120
- end
121
-
122
- def setup_files(*files)
123
- files.each do |path, content|
124
- import_dir.join(File.dirname(path)).mkpath
125
- File.open(import_dir.join(path), 'w') { |f| f.write(content) }
126
- end
127
- end
128
-
129
- def future
130
- Time.local(Time.now.year + 1, Time.now.month, Time.now.day)
131
- end
132
- end
133
- end