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
@@ -0,0 +1,29 @@
1
+ module Trackman
2
+ module Components
3
+ module Shippable
4
+ def ship diff
5
+ to_ship = diff.inject([])do |memo, (k, v)|
6
+ memo + v.map{ |x| {:proc => build_proc(k, x), :value => x} }
7
+ end
8
+
9
+ to_ship.sort_by{ |x| x[:value] }.each do |x|
10
+ x[:proc].call
11
+ end
12
+ end
13
+ private
14
+ def build_proc symbol, instance
15
+ case symbol
16
+ when :update
17
+ proc = Proc.new { instance.update }
18
+ when :create
19
+ proc = Proc.new { instance.insert }
20
+ when :delete
21
+ proc = Proc.new { instance.delete }
22
+ else
23
+ raise "something is wrong."
24
+ end
25
+ proc
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ module Trackman
2
+ module Errors
3
+ @@classes = [:AssetNotFoundError, :ConfigNotFoundError, :ConfigSetupError]
4
+
5
+ Trackman.autoloads 'trackman/errors', @@classes do |s,p|
6
+ autoload s, p
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ module Trackman
2
+ module Errors
3
+ class AssetNotFoundError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Trackman
2
+ module Errors
3
+ class ConfigNotFoundError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module Trackman
3
+ module Errors
4
+ class ConfigSetupError < StandardError
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Trackman
2
+ module Path
3
+ @@modules = [:Resolver, :Rails32Resolver, :RailsResolver]
4
+
5
+ Trackman.autoloads 'trackman/path', @@modules do |s,p|
6
+ autoload s, p
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ module Trackman
2
+ module Path
3
+ module Rails32Resolver
4
+ include Resolver
5
+
6
+ def translate url, parent_url
7
+ root = working_dir.realpath
8
+
9
+ path = url.dup
10
+ path.slice! /^(\/assets|assets\/)/
11
+ path = Pathname.new path
12
+
13
+ path = prepare_for_sprocket(path, parent_url, root) if path.relative?
14
+ begin
15
+ path = sprockets.resolve path
16
+ rescue Sprockets::FileNotFound => e
17
+ Trackman::Utility::Debugger.trace "Could not find path: #{path}\n#{e.message}"
18
+ return nil
19
+ end
20
+ path.relative_path_from(root).to_s
21
+ end
22
+
23
+ def prepare_for_sprocket path, parent_url, root
24
+ folder = (root + Pathname.new(parent_url)).parent.realpath
25
+ path = (folder + path).to_s
26
+ path.slice! sprockets.paths.select{|p| path.include? p }.first
27
+ path
28
+ end
29
+
30
+ def sprockets
31
+ ::Rails.application.assets
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ module Trackman
2
+ module Path
3
+ module RailsResolver
4
+ include Resolver
5
+
6
+ alias old_translate translate
7
+ alias old_parent_of parent_of
8
+
9
+ def parent_of(url)
10
+ if url.to_s.include?('assets')
11
+ old_parent_of(url).ascend do |p|
12
+ return p if p.basename.to_s == 'assets'
13
+ end
14
+ else
15
+ return old_parent_of(url)
16
+ end
17
+ end
18
+
19
+ def translate url, parent_url
20
+ path = old_translate(url, parent_url)
21
+
22
+ parts = path.split('/')
23
+ parts.insert(0, 'public') if parts.first != 'public'
24
+
25
+ parts.join('/')
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ module Trackman
2
+ module Path
3
+ module Resolver
4
+ def translate url, parent_url
5
+ raise "parent_url: #{parent_url} is required to be relative" if Pathname.new(parent_url).absolute?
6
+
7
+ url = Pathname.new(url) unless url.is_a? Pathname
8
+ parent_url = Pathname.new(parent_url) unless parent_url.is_a? Pathname
9
+
10
+ if url.relative?
11
+ parent = parent_of(parent_url)
12
+ child = url
13
+ else
14
+ parent = working_dir
15
+ s = url.to_s
16
+ child = Pathname.new(s[1...s.length])
17
+ end
18
+
19
+ (parent + child).relative_path_from(working_dir).to_s
20
+ end
21
+
22
+ def working_dir
23
+ Pathname.new Dir.pwd
24
+ end
25
+
26
+ def parent_of(url)
27
+ (working_dir + url).parent
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -1,19 +1,7 @@
1
1
  module Trackman
2
2
  module Scaffold
3
-
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
3
+ Trackman.autoloads 'trackman/scaffold', [:ContentSaver] do |s,p|
4
+ autoload s, p
13
5
  end
14
-
15
- @@modules = [:ContentSaver]
16
-
17
- autoloads 'trackman/scaffold', @@modules
18
6
  end
19
7
  end
@@ -0,0 +1,9 @@
1
+ module Trackman
2
+ module Utility
3
+ @@modules = [:Configuration, :Debugger]
4
+
5
+ ::Trackman.autoloads 'trackman/utility', @@modules do |s,p|
6
+ autoload s, p
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,98 @@
1
+ require 'versionomy'
2
+
3
+ module Trackman
4
+ module Utility
5
+ class Configuration
6
+ @@ERROR = 'ERROR_PAGE_URL'
7
+ @@MAINTENANCE = 'MAINTENANCE_PAGE_URL'
8
+ @@TRACKMAN_ERROR = 'TRACKMAN_ERROR_PAGE_URL'
9
+ @@TRACKMAN_MAINTENANCE = 'TRACKMAN_MAINTENANCE_PAGE_URL'
10
+
11
+ attr_accessor :configs, :heroku_version, :options
12
+
13
+ def initialize(heroku_version, options = {})
14
+ self.options = options
15
+ self.heroku_version = heroku_version
16
+ self.configs = get_configs
17
+ end
18
+
19
+ def setup
20
+ raise Trackman::Errors::ConfigSetupError, "Your heroku version is too low, trackman requires '~> 2.26'." unless is_heroku_valid
21
+ rename_configs
22
+ add_configs
23
+ puts "Done!"
24
+ end
25
+
26
+ def get_configs
27
+ result = run "heroku config -s" do |option|
28
+ "heroku config -s #{option}"
29
+ end
30
+ Trackman::Configuration.s_to_h(result)
31
+ end
32
+
33
+
34
+ def add_configs
35
+ if configs.include?(@@TRACKMAN_ERROR) && configs.include?(@@TRACKMAN_MAINTENANCE)
36
+ trackman_configs = {}
37
+ [[@@TRACKMAN_ERROR, @@ERROR], [@@TRACKMAN_MAINTENANCE, @@MAINTENANCE]].each do |old_c, new_c|
38
+ trackman_configs[new_c] = configs[old_c]
39
+ end
40
+
41
+ add = trackman_configs.map{|k,v| "#{k}=#{v}" }.join(' ')
42
+ add_config add
43
+ else
44
+ raise Trackman::Errors::ConfigSetupError, "cannot find trackman configuration, make sure trackman addon is installed"
45
+ end
46
+ end
47
+
48
+ def rename_configs
49
+ bkp = {}
50
+ [@@ERROR, @@MAINTENANCE].each do |c|
51
+ bkp[c] = configs[c] if configs.include? c
52
+ end
53
+
54
+ add = Hash[bkp.map {|k, v| [k + "_bkp", v] }].map{|k,v| "#{k}=#{v}" }.select{|c| !configs.include? c }.join(' ')
55
+
56
+ unless add.empty?
57
+ add_config add
58
+ end
59
+ end
60
+
61
+ def self.s_to_h configs
62
+ new_configs = {}
63
+ configs.split(" ").each do |a|
64
+ key_val = a.split("=")
65
+ new_configs[key_val[0]] = key_val[1]
66
+ end
67
+ new_configs
68
+ end
69
+
70
+ def self.h_to_s configs
71
+ out = []
72
+ configs.each do |k,v|
73
+ out << k + "=" + v
74
+ end
75
+ out.join ' '
76
+ end
77
+
78
+ private
79
+ def is_heroku_valid
80
+ Versionomy.parse(heroku_version) >= Versionomy.parse("2.26.2")
81
+ end
82
+
83
+ def add_config add
84
+ run "heroku config:add #{add}" do |option|
85
+ "heroku config:add #{option} #{add}"
86
+ end
87
+ end
88
+
89
+ def run command
90
+ command = yield("--app #{options[:app]}") unless self.options[:app].nil?
91
+ puts "exec: #{command}"
92
+ result = `#{command}`
93
+ raise "An error occured running the last command." if $? != 0
94
+ result
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,24 @@
1
+ require 'logger'
2
+ require 'rest-client'
3
+
4
+ module Trackman
5
+ module Utility
6
+ class Debugger
7
+ @@server_url = ENV['TRACKMAN_URL']
8
+
9
+ def self.debug_mode?
10
+ !ENV['TRACKMAN_DEBUG_MODE'].nil? && ENV['TRACKMAN_DEBUG_MODE'] == 'true'
11
+ end
12
+
13
+ def self.trace data
14
+ puts data if debug_mode?
15
+ end
16
+
17
+ def self.log_exception ex
18
+ RestClient.post "#{@@server_url}/exceptions", :exception => { :message => ex.message, :backtrace => ex.backtrace }, :ssl_version => 'SSLv3'
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ RestClient.log = Logger.new(STDOUT) if Trackman::Utility::Debugger.debug_mode?
@@ -19,7 +19,8 @@ if defined?(Rails)
19
19
  module Trackman
20
20
  class Railtie < Rails::Railtie
21
21
  rake_tasks do
22
- Dir[File.join(File.dirname(__FILE__),'../rails_generators/trackman_tasks/templates/*.rake')].each { |f| load f }
22
+ path = '../rails_generators/trackman_tasks/templates/*.rake'
23
+ Dir[File.join(File.dirname(__FILE__), path)].each { |f| load f }
23
24
  end
24
25
 
25
26
  initializer "trackman.hook" do |app|
@@ -1,3 +1,3 @@
1
1
  module Trackman
2
- VERSION = "0.5.4"
2
+ VERSION = "0.5.5"
3
3
  end
@@ -12,6 +12,6 @@ namespace :trackman do
12
12
  desc "Sets up the heroku configs required by Trackman"
13
13
  task :setup, :app do |t, args|
14
14
  heroku_version = Gem.loaded_specs["heroku"].version.to_s
15
- Trackman::ConfigurationHandler.new(heroku_version, :app => args[:app]).setup
15
+ Trackman::Configuration.new(heroku_version, :app => args[:app]).setup
16
16
  end
17
17
  end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Trackman::Assets::Components::AssetFactory do
3
+ describe Trackman::Assets::AssetFactory do
4
4
  class TestFactory
5
- extend Trackman::Assets::Components::AssetFactory
5
+ extend Trackman::Assets::AssetFactory
6
6
  end
7
7
 
8
8
  before :all do
@@ -68,6 +68,6 @@ describe Trackman::Assets::Components::AssetFactory do
68
68
 
69
69
  TestFactory.asset_pipeline_enabled?.should be_false
70
70
  asset = TestFactory.create(:path => 'x.css')
71
- asset.should_not be_a_kind_of Rails32PathResolver
71
+ asset.should_not be_a_kind_of Rails32Resolver
72
72
  end
73
73
  end
data/spec/asset_spec.rb CHANGED
@@ -4,7 +4,7 @@ require 'digest/md5'
4
4
  describe Trackman::Assets::Asset do
5
5
  it "should raise an exception if the path is not good" do
6
6
  wrong_path = "./wrong_path.html"
7
- lambda { Asset.new(:path => wrong_path) }.should raise_error Trackman::Assets::Errors::AssetNotFoundError
7
+ lambda { Asset.new(:path => wrong_path) }.should raise_error Trackman::Errors::AssetNotFoundError
8
8
  end
9
9
 
10
10
  it "is equal if the path is the same" do
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class TestComposite
4
- include Trackman::Assets::Components::CompositeAsset
4
+ include Trackman::Assets::CompositeAsset
5
5
 
6
6
  def path
7
7
  'parent'
@@ -18,7 +18,7 @@ class TestAsset < Trackman::Assets::Asset
18
18
  end
19
19
  end
20
20
 
21
- describe Trackman::Assets::Components::CompositeAsset do
21
+ describe Trackman::Assets::CompositeAsset do
22
22
  before :each do
23
23
  @composite = TestComposite.new
24
24
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- class TestConfigurationHandler < Trackman::ConfigurationHandler
3
+ class TestConfiguration < Trackman::Utility::Configuration
4
4
  def get_configs
5
5
  {
6
6
  "TRACKMAN_MAINTENANCE_PAGE_URL" => "\"en_US.UTF-8\"",
@@ -12,7 +12,7 @@ class TestConfigurationHandler < Trackman::ConfigurationHandler
12
12
  attr_accessor :heroku_configs
13
13
 
14
14
  def add_config add
15
- self.heroku_configs = (heroku_configs || {}).merge(ConfigurationHandler.s_to_h(add))
15
+ self.heroku_configs = (heroku_configs || {}).merge(Configuration.s_to_h(add))
16
16
  end
17
17
 
18
18
  def run command
@@ -20,9 +20,9 @@ class TestConfigurationHandler < Trackman::ConfigurationHandler
20
20
  end
21
21
  end
22
22
 
23
- describe ConfigurationHandler do
23
+ describe Configuration do
24
24
  before :each do
25
- @config = TestConfigurationHandler.new "2.26.2"
25
+ @config = TestConfiguration.new "2.26.2"
26
26
  end
27
27
 
28
28
  it "creates heroku configs" do
@@ -44,13 +44,13 @@ describe ConfigurationHandler do
44
44
  "TRACKMANw_ERROR_PAGE_URL" => "\"error_page_url\""
45
45
  }
46
46
 
47
- lambda{ @config.setup }.should raise_error(Trackman::SetupException)
47
+ lambda{ @config.setup }.should raise_error(Trackman::Errors::ConfigSetupError)
48
48
  end
49
49
 
50
50
  it "raises an error if trackman version is bad" do
51
51
  @config.heroku_version = "2.22.2"
52
52
 
53
- lambda{ @config.setup }.should raise_error(Trackman::SetupException)
53
+ lambda{ @config.setup }.should raise_error(Trackman::Errors::ConfigSetupError)
54
54
  end
55
55
 
56
56
  it "backups configs when they already exist and add the new ones" do