trackman 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/lib/trackman.rb +14 -7
  2. data/lib/trackman/assets.rb +4 -14
  3. data/lib/trackman/assets/asset.rb +3 -3
  4. data/lib/trackman/assets/asset_factory.rb +46 -0
  5. data/lib/trackman/assets/bundled_asset.rb +26 -0
  6. data/lib/trackman/assets/composite_asset.rb +40 -0
  7. data/lib/trackman/assets/css_asset.rb +1 -1
  8. data/lib/trackman/assets/html_asset.rb +1 -1
  9. data/lib/trackman/assets/persistence.rb +9 -0
  10. data/lib/trackman/assets/persistence/remote.rb +54 -0
  11. data/lib/trackman/assets/remote_asset.rb +4 -45
  12. data/lib/trackman/assets/remote_asset_factory.rb +11 -0
  13. data/lib/trackman/components.rb +9 -0
  14. data/lib/trackman/components/conventions.rb +21 -0
  15. data/lib/trackman/components/diffable.rb +28 -0
  16. data/lib/trackman/components/hashable.rb +27 -0
  17. data/lib/trackman/components/shippable.rb +29 -0
  18. data/lib/trackman/errors.rb +9 -0
  19. data/lib/trackman/errors/asset_not_found_error.rb +6 -0
  20. data/lib/trackman/errors/config_not_found_error.rb +6 -0
  21. data/lib/trackman/errors/config_setup_error.rb +7 -0
  22. data/lib/trackman/path.rb +9 -0
  23. data/lib/trackman/path/rails32_resolver.rb +35 -0
  24. data/lib/trackman/path/rails_resolver.rb +29 -0
  25. data/lib/trackman/path/resolver.rb +32 -0
  26. data/lib/trackman/scaffold.rb +2 -14
  27. data/lib/trackman/utility.rb +9 -0
  28. data/lib/trackman/utility/configuration.rb +98 -0
  29. data/lib/trackman/{core_extensions.rb → utility/core_extensions.rb} +0 -0
  30. data/lib/trackman/utility/debugger.rb +24 -0
  31. data/lib/{trackman_railtie.rb → trackman/utility/railtie.rb} +2 -1
  32. data/lib/trackman/version.rb +1 -1
  33. data/rails_generators/trackman_tasks/templates/trackman.rake +1 -1
  34. data/spec/asset_factory_spec.rb +3 -3
  35. data/spec/asset_spec.rb +1 -1
  36. data/spec/composite_asset_spec.rb +2 -2
  37. data/spec/{configuration_handler_spec.rb → configuration_spec.rb} +6 -6
  38. data/spec/diffable_spec.rb +2 -2
  39. data/spec/helpers/act_like_rails2311.rb +2 -2
  40. data/spec/helpers/act_like_rails32.rb +2 -2
  41. data/spec/helpers/app_creator.rb +12 -5
  42. data/spec/helpers/fakable_pathman_tester.rb +5 -5
  43. data/spec/paths/pathman_spec.rb +2 -2
  44. data/spec/paths/rails32_pathman_spec.rb +2 -2
  45. data/spec/{rails32_path_resolver_spec.rb → rails32_resolver_spec.rb} +3 -3
  46. data/spec/remote_asset_spec.rb +13 -17
  47. data/spec/{scaffolding_spec.rb → scaffold_spec.rb} +0 -0
  48. data/spec/shippable_spec.rb +2 -2
  49. data/spec/spec_helper.rb +6 -6
  50. data/spec/sync_spec.rb +1 -1
  51. metadata +32 -27
  52. data/lib/trackman/assets/components.rb +0 -13
  53. data/lib/trackman/assets/components/asset_factory.rb +0 -48
  54. data/lib/trackman/assets/components/bundled_asset.rb +0 -29
  55. data/lib/trackman/assets/components/composite_asset.rb +0 -42
  56. data/lib/trackman/assets/components/conventions.rb +0 -23
  57. data/lib/trackman/assets/components/diffable.rb +0 -30
  58. data/lib/trackman/assets/components/hashable.rb +0 -29
  59. data/lib/trackman/assets/components/path_resolver.rb +0 -34
  60. data/lib/trackman/assets/components/rails32_path_resolver.rb +0 -39
  61. data/lib/trackman/assets/components/rails_path_resolver.rb +0 -31
  62. data/lib/trackman/assets/components/remote_asset_factory.rb +0 -13
  63. data/lib/trackman/assets/components/shippable.rb +0 -31
  64. data/lib/trackman/assets/errors.rb +0 -10
  65. data/lib/trackman/assets/errors/asset_not_found_error.rb +0 -8
  66. data/lib/trackman/assets/errors/config_not_found_error.rb +0 -8
  67. data/lib/trackman/configuration_handler.rb +0 -99
  68. data/lib/trackman/debugger.rb +0 -9
data/lib/trackman.rb CHANGED
@@ -1,13 +1,20 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
 
4
- require File.expand_path('../trackman/core_extensions', __FILE__)
5
- require File.expand_path('../trackman_railtie', __FILE__)
4
+ require File.expand_path('../trackman/utility/core_extensions', __FILE__)
5
+ require File.expand_path('../trackman/utility/railtie', __FILE__)
6
6
 
7
7
  module Trackman
8
- autoload :Assets, 'trackman/assets'
9
- autoload :ConfigurationHandler, 'trackman/configuration_handler'
10
- autoload :Scaffold, 'trackman/scaffold'
11
- end
8
+ #TODO do something better than this to share the scope
9
+ def self.autoloads path, items
10
+ items.each do |s|
11
+ if block_given?
12
+ yield(s, "#{path}/#{s.trackman_underscore}" )
13
+ else
14
+ autoload s, "#{path}/#{s.trackman_underscore}"
15
+ end
16
+ end
17
+ end
12
18
 
13
- autoload :Debugger, 'trackman/debugger'
19
+ autoloads 'trackman', [:Assets, :Configuration, :Scaffold, :Components, :Errors, :Path, :Utility]
20
+ end
@@ -1,20 +1,10 @@
1
1
  module Trackman
2
2
  module Assets
3
+ @@classes = [:Asset, :HtmlAsset, :RemoteAsset, :CssAsset]
4
+ @@modules = [:CompositeAsset, :AssetFactory, :BundledAsset, :RemoteAssetFactory, :Persistence]
3
5
 
4
- #TODO do something better than this to share the scope
5
- def self.autoloads path, items
6
- items.each do |s|
7
- if block_given?
8
- yield(s, "#{path}/#{s.trackman_underscore}" )
9
- else
10
- autoload s, "#{path}/#{s.trackman_underscore}"
11
- end
12
- end
6
+ Trackman.autoloads 'trackman/assets', (@@classes + @@modules) do |s, p|
7
+ autoload s, p
13
8
  end
14
-
15
- @@classes = [:Asset, :HtmlAsset, :RemoteAsset, :CssAsset]
16
- @@modules = [:Components, :Errors]
17
-
18
- autoloads 'trackman/assets', @@classes.concat(@@modules)
19
9
  end
20
10
  end
@@ -1,7 +1,7 @@
1
1
  module Trackman
2
2
  module Assets
3
3
  class Asset
4
- extend Components::AssetFactory, Components::Conventions
4
+ extend AssetFactory, Components::Conventions
5
5
  extend Components::Diffable, Components::Shippable
6
6
  include Comparable
7
7
 
@@ -67,7 +67,7 @@ module Trackman
67
67
 
68
68
  diff_result = diff(local, remote)
69
69
 
70
- Debugger.trace diff_result.inspect
70
+ Trackman::Utility::Debugger.trace diff_result.inspect
71
71
 
72
72
  ship diff_result
73
73
 
@@ -86,7 +86,7 @@ module Trackman
86
86
  return sync if autosync
87
87
  rescue Exception => ex
88
88
  begin
89
- ::Trackman::Assets::RemoteAsset.log_exception ex
89
+ Trackman::Utility::Debugger.log_exception ex
90
90
  ensure
91
91
  return false
92
92
  end
@@ -0,0 +1,46 @@
1
+ module Trackman
2
+ module Assets
3
+ module AssetFactory
4
+ def create attributes = {}
5
+ path = attributes[:path]
6
+ instance = retrieve_parent(path).new attributes
7
+ add_content_behavior instance
8
+ end
9
+
10
+ def retrieve_parent path
11
+ if File.extname(path) == '.html'
12
+ parent = HtmlAsset
13
+ elsif File.extname(path) == '.css'
14
+ parent = CssAsset
15
+ else
16
+ parent = Asset
17
+ end
18
+ parent
19
+ end
20
+
21
+ def add_content_behavior instance
22
+ if asset_pipeline_enabled?
23
+ instance.extend Path::Rails32Resolver, BundledAsset
24
+ return instance
25
+ elsif rails_defined? #fallback to rails without asset pipeline
26
+ instance.extend Path::RailsResolver
27
+ end
28
+ instance.extend Components::Hashable
29
+
30
+ instance
31
+ end
32
+
33
+ def rails_defined?
34
+ Object.const_defined?(:Rails)
35
+ end
36
+
37
+ def asset_pipeline_enabled?
38
+ rails_defined? &&
39
+ Rails.respond_to?(:application) &&
40
+ Rails.application.config.assets.enabled &&
41
+ Rails.application.respond_to?(:assets) &&
42
+ Rails.application.assets
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,26 @@
1
+ module Trackman
2
+ module Assets
3
+ module BundledAsset
4
+ include Components::Hashable
5
+
6
+ def env
7
+ @@env ||= ::Rails.application.assets.index
8
+ end
9
+
10
+ def data
11
+ result = (@bundled ||= init_data)
12
+
13
+ return super if result.nil? || result.length == 0
14
+ result
15
+ end
16
+
17
+ def init_data
18
+ begin
19
+ return env[env.attributes_for(path.realpath).pathname].to_s
20
+ rescue
21
+ return nil
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ module Trackman
2
+ module Assets
3
+ module CompositeAsset
4
+ @@url = /url\(['"]?([^'")]+)['"]?\)/
5
+ @@import = /url\(['"]?[^'"]+['"]?\)/
6
+
7
+ def self.included(mod)
8
+ mod.send(:include, Path::Resolver)
9
+ end
10
+ def self.extended(mod)
11
+ mod.send(:extend, Path::Resolver)
12
+ end
13
+
14
+ def assets
15
+ internals = children_paths.select{|p| p.internal_path? }.map{|p| {:old => p, :new_path => translate(p, path)} }
16
+ internals = internals.select{|p| !p[:new_path].nil? }.map{|p| asset_from(p[:old], p[:new_path])}
17
+ internals.inject([]) do |sum, a|
18
+ (sum << a) + a.assets.select{|child| !sum.include?(child) }
19
+ end
20
+ end
21
+
22
+ def asset_from(virtual, physical)
23
+ Asset.create(:virtual_path => virtual, :path => physical)
24
+ end
25
+
26
+ def inner_css_paths
27
+ data.scan(@@import).collect{|x| @@url.match(x)[1]}.select{|x| !x.embedded? }
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ class String
34
+ def internal_path?
35
+ self !~ /^http/
36
+ end
37
+ def embedded?
38
+ self.include? 'data:'
39
+ end
40
+ end
@@ -1,7 +1,7 @@
1
1
  module Trackman
2
2
  module Assets
3
3
  class CssAsset < Asset
4
- include Components::CompositeAsset
4
+ include CompositeAsset
5
5
 
6
6
  protected
7
7
  alias children_paths inner_css_paths
@@ -2,7 +2,7 @@ require 'nokogiri'
2
2
  module Trackman
3
3
  module Assets
4
4
  class HtmlAsset < Asset
5
- include Components::CompositeAsset
5
+ include CompositeAsset
6
6
 
7
7
  def document
8
8
  @doc ||= Nokogiri::HTML(data)
@@ -0,0 +1,9 @@
1
+ module Trackman
2
+ module Assets
3
+ module Persistence
4
+ Trackman.autoloads 'trackman/assets/persistence', [:Remote] do |s, p|
5
+ autoload s, p
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,54 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ module Trackman
5
+ module Assets
6
+ module Persistence
7
+ module Remote
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def server_url
14
+ @server_url ||= ENV['TRACKMAN_URL']
15
+ end
16
+ def site
17
+ @site ||= "#{server_url}/assets"
18
+ end
19
+
20
+ def find id
21
+ response = RestClient.get "#{site}/#{id}"
22
+
23
+ body = Hash[JSON.parse(response).map{ |k, v| [k.to_sym, v] }]
24
+
25
+ create(body)
26
+ end
27
+
28
+ def all
29
+ get_attributes.map{ |r| create(r) }.sort
30
+ end
31
+
32
+ def get_attributes
33
+ JSON.parse(RestClient.get site).map{|r| Hash[r.map{ |k, v| [k.to_sym, v] }] }
34
+ end
35
+ end
36
+
37
+ def insert
38
+ response = RestClient.post self.class.site, build_params, :content_type => :json, :accept => :json, :ssl_version => 'SSLv3'
39
+ path = response.headers[:location]
40
+ @id = path[/\d+$/].to_i
41
+ end
42
+
43
+ def update
44
+ RestClient.put "#{self.class.site}/#{id}", build_params, :content_type => :json, :accept => :json, :ssl_version => 'SSLv3'
45
+ end
46
+
47
+ def delete
48
+ response = RestClient.delete "#{self.class.site}/#{id}"
49
+ true
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,17 +1,12 @@
1
1
  require 'rest-client'
2
- require 'json'
3
2
  require 'uri'
4
- require 'logger'
5
- RestClient.log = Logger.new(STDOUT) if Debugger.debug_mode?
6
-
3
+
7
4
  module Trackman
8
5
  module Assets
9
6
  class RemoteAsset < Asset
10
- extend Components::RemoteAssetFactory
11
-
12
- @@server_url = ENV['TRACKMAN_URL']
7
+ extend RemoteAssetFactory
8
+ include Persistence::Remote
13
9
 
14
- @@site = "#{@@server_url}/assets"
15
10
  attr_reader :id
16
11
 
17
12
  def initialize attributes = {}
@@ -22,42 +17,10 @@ module Trackman
22
17
  @file_hash = attributes[:file_hash]
23
18
  end
24
19
 
25
- def self.log_exception ex
26
- RestClient.post "#{@@server_url}/exceptions", :exception => { :message => ex.message, :backtrace => ex.backtrace }, :ssl_version => 'SSLv3'
27
- end
28
-
29
20
  def validate_path?
30
21
  false
31
22
  end
32
23
 
33
- def self.find id
34
- response = RestClient.get "#{@@site}/#{id}"
35
-
36
- body = Hash[JSON.parse(response).map{ |k, v| [k.to_sym, v] }]
37
-
38
- RemoteAsset.create(body)
39
- end
40
-
41
- def self.all
42
- get_attributes.map{ |r| RemoteAsset.create(r) }.sort
43
- end
44
-
45
-
46
- def insert
47
- response = RestClient.post @@site, build_params, :content_type => :json, :accept => :json, :ssl_version => 'SSLv3'
48
- path = response.headers[:location]
49
- @id = path[/\d+$/].to_i
50
- end
51
-
52
- def update
53
- RestClient.put "#{@@site}/#{id}", build_params, :content_type => :json, :accept => :json, :ssl_version => 'SSLv3'
54
- end
55
-
56
- def delete
57
- response = RestClient.delete "#{@@site}/#{id}"
58
- true
59
- end
60
-
61
24
  def ==(other)
62
25
  result = super
63
26
  if result
@@ -74,12 +37,8 @@ module Trackman
74
37
  { :asset => { :virtual_path => virtual_path.to_s, :path => path.to_s, :file => AssetIO.new(path.to_s, data) }, :multipart => true }
75
38
  end
76
39
  def ensure_config
77
- raise Errors::ConfigNotFoundError, "The config TRACKMAN_URL is missing." if @@server_url.nil?
40
+ raise Errors::ConfigNotFoundError, "The config TRACKMAN_URL is missing." if self.class.server_url.nil?
78
41
  end
79
- def self.get_attributes
80
- JSON.parse(RestClient.get @@site).map{|r| Hash[r.map{ |k, v| [k.to_sym, v] }] }
81
- end
82
-
83
42
  class AssetIO < StringIO
84
43
  attr_accessor :filepath
85
44
 
@@ -0,0 +1,11 @@
1
+ module Trackman
2
+ module Assets
3
+ module RemoteAssetFactory
4
+ include Assets::AssetFactory
5
+
6
+ def retrieve_parent(path)
7
+ RemoteAsset
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Trackman
2
+ module Components
3
+ @@modules = [:Conventions, :Diffable, :Hashable, :Shippable]
4
+
5
+ Trackman.autoloads 'trackman/components', @@modules do |s,p|
6
+ autoload s, p
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ module Trackman
2
+ module Components
3
+ module Conventions
4
+ Asset = Trackman::Assets::Asset
5
+
6
+ def maintenance_path
7
+ Pathname.new 'public/503.html'
8
+ end
9
+ def error_path
10
+ Pathname.new 'public/503-error.html'
11
+ end
12
+ def maintenance_page
13
+ Asset.create(:path => maintenance_path, :virtual_path => maintenance_path)
14
+ end
15
+ def error_page
16
+ Asset.create(:path => error_path, :virtual_path => error_path)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,28 @@
1
+ module Trackman
2
+ module Components
3
+ module Diffable
4
+ def diff local, remote
5
+ to_create = local.select{|a| remote.all? { |s| a.path != s.path } }.map{|a| a.to_remote }
6
+
7
+ {
8
+ :create => to_create,
9
+ :update => remote.select{|a| local.any?{ |s| a.path == s.path && a.file_hash != s.file_hash }},
10
+ :delete => define_deleted(local, remote) do |a|
11
+ to_create.any?{ |c| c.path.basename == a.path.basename }
12
+ end
13
+ }
14
+ end
15
+
16
+ private
17
+ # will not delete an html for now.
18
+ # this behaviour is to avoid the removal of the default templates.
19
+ def define_deleted local, remote
20
+ to_delete = remote.select do |a|
21
+ local.all? { |s| s.path != a.path }
22
+ end
23
+
24
+ to_delete.reject{|a| a.path.to_s =~ /.html$/ }.to_a
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ require 'digest/md5'
2
+
3
+ module Trackman
4
+ module Components
5
+ module Hashable
6
+ def data
7
+ @data ||= read_file(path)
8
+ end
9
+
10
+ def file_hash
11
+ @file_hash ||= (data.nil? ? "" : Digest::MD5.hexdigest(data))
12
+ end
13
+
14
+ protected
15
+ def read_file(file_path)
16
+ begin
17
+ file = File.open(file_path)
18
+ return file.read
19
+ rescue
20
+ return nil
21
+ ensure
22
+ file.close unless file.nil?
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end