buildpack-support 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/LICENSE +202 -0
  2. data/NOTICE +2 -0
  3. data/docs/cache.md +77 -0
  4. data/docs/component.md +1 -0
  5. data/docs/configuration.md +27 -0
  6. data/docs/logging.md +54 -0
  7. data/docs/other.md +1 -0
  8. data/docs/rake.md +1 -0
  9. data/docs/repository.md +116 -0
  10. data/docs/test.md +1 -0
  11. data/lib/buildpack_support.rb +18 -0
  12. data/lib/buildpack_support/base_buildpack.rb +166 -0
  13. data/lib/buildpack_support/buildpack_version.rb +124 -0
  14. data/lib/buildpack_support/cache.rb +24 -0
  15. data/lib/buildpack_support/cache/application_cache.rb +41 -0
  16. data/lib/buildpack_support/cache/cached_file.rb +103 -0
  17. data/lib/buildpack_support/cache/download_cache.rb +280 -0
  18. data/lib/buildpack_support/cache/inferred_network_failure.rb +26 -0
  19. data/lib/buildpack_support/cache/internet_availability.rb +64 -0
  20. data/lib/buildpack_support/component.rb +24 -0
  21. data/lib/buildpack_support/component/application.rb +76 -0
  22. data/lib/buildpack_support/component/base_component.rb +78 -0
  23. data/lib/buildpack_support/component/base_droplet.rb +96 -0
  24. data/lib/buildpack_support/component/downloads.rb +88 -0
  25. data/lib/buildpack_support/component/services.rb +84 -0
  26. data/lib/buildpack_support/component/versioned_dependency_component.rb +71 -0
  27. data/lib/buildpack_support/component/versioned_downloads.rb +57 -0
  28. data/lib/buildpack_support/component/with_timing.rb +40 -0
  29. data/lib/buildpack_support/configuration_utils.rb +58 -0
  30. data/lib/buildpack_support/constantize.rb +46 -0
  31. data/lib/buildpack_support/dash_case.rb +29 -0
  32. data/lib/buildpack_support/directory_finder.rb +45 -0
  33. data/lib/buildpack_support/filtering_pathname.rb +227 -0
  34. data/lib/buildpack_support/format_duration.rb +57 -0
  35. data/lib/buildpack_support/logging.rb +22 -0
  36. data/lib/buildpack_support/logging/delegating_logger.rb +48 -0
  37. data/lib/buildpack_support/logging/logger_factory.rb +148 -0
  38. data/lib/buildpack_support/qualify_path.rb +36 -0
  39. data/lib/buildpack_support/rake.rb +22 -0
  40. data/lib/buildpack_support/rake/buildpack_stage_task.rb +86 -0
  41. data/lib/buildpack_support/rake/cached_artifact_finder.rb +99 -0
  42. data/lib/buildpack_support/rake/check_api_doc_task.rb +70 -0
  43. data/lib/buildpack_support/rake/dependency_cache_task.rb +87 -0
  44. data/lib/buildpack_support/rake/disable_remote_downloads_task.rb +80 -0
  45. data/lib/buildpack_support/rake/package_task.rb +133 -0
  46. data/lib/buildpack_support/rake/package_zip_task.rb +80 -0
  47. data/lib/buildpack_support/rake/repository_configuration_finder.rb +66 -0
  48. data/lib/buildpack_support/rake/write_version_file_task.rb +82 -0
  49. data/lib/buildpack_support/repository.rb +24 -0
  50. data/lib/buildpack_support/repository/configured_item.rb +81 -0
  51. data/lib/buildpack_support/repository/repository_index.rb +98 -0
  52. data/lib/buildpack_support/repository/wildcard_version_resolver.rb +75 -0
  53. data/lib/buildpack_support/shell.rb +41 -0
  54. data/lib/buildpack_support/snake_case.rb +30 -0
  55. data/lib/buildpack_support/space_case.rb +29 -0
  56. data/lib/buildpack_support/test/application_helper.rb +41 -0
  57. data/lib/buildpack_support/test/base_component_helper.rb +59 -0
  58. data/lib/buildpack_support/test/base_droplet_helper.rb +36 -0
  59. data/lib/buildpack_support/test/console_helper.rb +57 -0
  60. data/lib/buildpack_support/test/environment_helper.rb +32 -0
  61. data/lib/buildpack_support/test/internet_availability_helper.rb +29 -0
  62. data/lib/buildpack_support/test/logging_helper.rb +50 -0
  63. data/lib/buildpack_support/test/scratch_helper.rb +32 -0
  64. data/lib/buildpack_support/test/versioned_dependency_component_helper.rb +32 -0
  65. data/lib/buildpack_support/test/with_load_path_helper.rb +27 -0
  66. data/lib/buildpack_support/to_b.rb +38 -0
  67. data/lib/buildpack_support/tokenized_version.rb +157 -0
  68. data/lib/buildpack_support/version.rb +23 -0
  69. data/spec/buildpack_support/base_buildpack_spec.rb +112 -0
  70. data/spec/buildpack_support/buildpack_version_spec.rb +122 -0
  71. data/spec/buildpack_support/cache/application_cache_spec.rb +56 -0
  72. data/spec/buildpack_support/cache/cached_file_spec.rb +94 -0
  73. data/spec/buildpack_support/cache/download_cache_spec.rb +293 -0
  74. data/spec/buildpack_support/cache/internet_availability_spec.rb +57 -0
  75. data/spec/buildpack_support/cache/yield_file_with_content.rb +30 -0
  76. data/spec/buildpack_support/component/application_spec.rb +81 -0
  77. data/spec/buildpack_support/component/base_component_spec.rb +81 -0
  78. data/spec/buildpack_support/component/base_droplet_spec.rb +72 -0
  79. data/spec/buildpack_support/component/downloads_spec.rb +63 -0
  80. data/spec/buildpack_support/component/services_spec.rb +80 -0
  81. data/spec/buildpack_support/component/versioned_dependency_component_spec.rb +58 -0
  82. data/spec/buildpack_support/component/versioned_downloads_spec.rb +58 -0
  83. data/spec/buildpack_support/component/with_timing_spec.rb +30 -0
  84. data/spec/buildpack_support/configuration_utils_spec.rb +39 -0
  85. data/spec/buildpack_support/constantize_spec.rb +34 -0
  86. data/spec/buildpack_support/dash_case_spec.rb +41 -0
  87. data/spec/buildpack_support/directory_finder_spec.rb +41 -0
  88. data/spec/buildpack_support/filtering_pathname_spec.rb +443 -0
  89. data/spec/buildpack_support/format_duration_spec.rb +60 -0
  90. data/spec/buildpack_support/logging/delegating_logger_spec.rb +62 -0
  91. data/spec/buildpack_support/logging/logger_factory_spec.rb +262 -0
  92. data/spec/buildpack_support/qualify_path_spec.rb +42 -0
  93. data/spec/buildpack_support/rake/buildpack_stage_task_spec.rb +88 -0
  94. data/spec/buildpack_support/rake/cached_artifact_finder_spec.rb +73 -0
  95. data/spec/buildpack_support/rake/check_api_doc_task_spec.rb +69 -0
  96. data/spec/buildpack_support/rake/dependency_cache_task_spec.rb +133 -0
  97. data/spec/buildpack_support/rake/disable_remote_downloads_task_spec.rb +91 -0
  98. data/spec/buildpack_support/rake/package_task_spec.rb +335 -0
  99. data/spec/buildpack_support/rake/package_zip_task_spec.rb +91 -0
  100. data/spec/buildpack_support/rake/repository_configuration_finder_spec.rb +61 -0
  101. data/spec/buildpack_support/rake/write_version_file_task_spec.rb +96 -0
  102. data/spec/buildpack_support/repository/configured_item_spec.rb +78 -0
  103. data/spec/buildpack_support/repository/repository_index_spec.rb +118 -0
  104. data/spec/buildpack_support/repository/wildcard_version_resolver_spec.rb +73 -0
  105. data/spec/buildpack_support/shell_spec.rb +32 -0
  106. data/spec/buildpack_support/snake_case_spec.rb +45 -0
  107. data/spec/buildpack_support/space_case_spec.rb +41 -0
  108. data/spec/buildpack_support/to_b_spec.rb +41 -0
  109. data/spec/buildpack_support/tokenized_version_spec.rb +132 -0
  110. data/spec/fixtures/application/test-file +0 -0
  111. data/spec/fixtures/config/found-config.yml +2 -0
  112. data/spec/fixtures/droplet-resources/droplet-resource +0 -0
  113. data/spec/fixtures/stub-download-with-top-level.zip +0 -0
  114. data/spec/fixtures/stub-download.tar.gz +0 -0
  115. data/spec/fixtures/stub-download.zip +0 -0
  116. data/spec/fixtures/test-cache.yml +18 -0
  117. data/spec/fixtures/test-index.yml +2 -0
  118. data/spec/fixtures/test_component.rb +0 -0
  119. data/spec/fixtures/zip-contents/test-directory/test-deep-file +0 -0
  120. data/spec/fixtures/zip-contents/test-file +0 -0
  121. data/spec/spec_helper.rb +30 -0
  122. metadata +416 -0
@@ -0,0 +1,57 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # A mixin that adds the ability to format a +Numeric+ as a user-readable duration
17
+ class Numeric
18
+
19
+ # Formats a number as a user-readable duration
20
+ #
21
+ # @return [String] a user-readable duration. Follows the following algorithm
22
+ # 1. If more than an hour, print hours and minutes
23
+ # 2. If less than an hour and more than a minute, print minutes and seconds
24
+ # 3. If less than a minute and more than a second, print seconds.tenths
25
+ def duration
26
+ remainder = self
27
+
28
+ hours = (remainder / HOUR).to_int
29
+ remainder -= HOUR * hours
30
+
31
+ minutes = (remainder / MINUTE).to_int
32
+ remainder -= MINUTE * minutes
33
+
34
+ return "#{hours}h #{minutes}m" if hours > 0
35
+
36
+ seconds = (remainder / SECOND).to_int
37
+ remainder -= SECOND * seconds
38
+
39
+ return "#{minutes}m #{seconds}s" if minutes > 0
40
+
41
+ tenths = (remainder / TENTH).to_int
42
+ "#{seconds}.#{tenths}s"
43
+ end
44
+
45
+ MILLISECOND = 0.001.freeze
46
+
47
+ TENTH = (100 * MILLISECOND).freeze
48
+
49
+ SECOND = (10 * TENTH).freeze
50
+
51
+ MINUTE = (60 * SECOND).freeze
52
+
53
+ HOUR = (60 * MINUTE).freeze
54
+
55
+ private_constant :MILLISECOND, :TENTH, :SECOND, :MINUTE, :HOUR
56
+
57
+ end
@@ -0,0 +1,22 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ module BuildpackSupport
17
+
18
+ # A module encapsulating all of the code for the logging utilities
19
+ module Logging
20
+ end
21
+
22
+ end
@@ -0,0 +1,48 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'buildpack_support/logging'
17
+ require 'logger'
18
+
19
+ module BuildpackSupport
20
+ module Logging
21
+
22
+ # A +Logger+ subclass that forwards all messages to a collection of delegates
23
+ class DelegatingLogger < ::Logger
24
+
25
+ # Creates an instance
26
+ #
27
+ # @param [Class] klass the class to use as the +progname+ for log messages
28
+ # @param [Array<Logger>] delegates the +Logger+ instances to delegate to
29
+ def initialize(klass, delegates)
30
+ @klass = klass
31
+ @delegates = delegates
32
+ end
33
+
34
+ # Adds a message to the delegate +Logger+ instances
35
+ #
36
+ # @param [Logger::Severity] severity the severity of the message
37
+ # @param [String] message the message
38
+ # @param [String] progname the message when passed in as a parameter
39
+ # @yield evaluated for the message
40
+ # @return [Void]
41
+ def add(severity, message = nil, progname = nil, &block)
42
+ @delegates.each { |delegate| delegate.add severity, message || progname, @klass, &block }
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,148 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'buildpack_support/logging'
17
+ require 'buildpack_support/logging/delegating_logger'
18
+ require 'buildpack_support/configuration_utils'
19
+ require 'buildpack_support/constantize'
20
+ require 'fileutils'
21
+ require 'logger'
22
+ require 'monitor'
23
+ require 'singleton'
24
+
25
+ module BuildpackSupport
26
+ module Logging
27
+
28
+ # Responsible for configuring and creating all +Logger+ instances. +Logger+s created by the factory log all messages
29
+ # to a file located at +app_dir/.buildpack.log+. They also log all messages, filtered by the configured
30
+ # severity, to +$stderr+. Severity can be configured (in decreasing priority) by using the +JBP_LOG_LEVEL+
31
+ # environment variable, the Ruby +$DEBUG+ and +$VERBOSE+ flags, and the +config/logging.yml+ file. If none of these
32
+ # is set, then the severity defaults to +INFO+.
33
+ class LoggerFactory
34
+ include ::Singleton
35
+
36
+ attr_reader :initialized
37
+
38
+ def initialize
39
+ @configuration_utils = BuildpackSupport::ConfigurationUtils.new false
40
+ @monitor = Monitor.new
41
+ end
42
+
43
+ # Sets up the logger factory
44
+ #
45
+ # @param [Pathname] app_dir the application directory
46
+ # @return [Void]
47
+ def setup(app_dir)
48
+ @monitor.synchronize do
49
+ @log_file = app_dir + '.buildpack.log'
50
+ @delegates = [file_logger, console_logger]
51
+ @initialized = true
52
+ end
53
+ end
54
+
55
+ # Returns a configured logger for a given +Class+. The +Class+ that is passed in is used as the +progname+, for all
56
+ # messages logged by the logger. If this is called before the +setup()+ method, a failure will be generated.
57
+ #
58
+ # @param [Class] klass the class that the logger is created for
59
+ # @return [Logger] the logger that was requested
60
+ def get_logger(klass)
61
+ @monitor.synchronize do
62
+ fail "Attempted to get Logger for #{short_class(klass)} before initialization" unless @initialized
63
+ DelegatingLogger.new wrapped_short_class(klass), @delegates
64
+ end
65
+ end
66
+
67
+ # Returns the location of the log file. If this is called before the +setup()+ method, a failure will be generated.
68
+ #
69
+ # @return [Pathname] the location of the log file
70
+ def log_file
71
+ @monitor.synchronize do
72
+ fail 'Attempted to get log file before initialization' unless @initialized
73
+ @log_file
74
+ end
75
+ end
76
+
77
+ # Resets the configuration of the factory
78
+ #
79
+ # @return [Void]
80
+ def reset
81
+ @monitor.synchronize do
82
+ @initialized = false
83
+ end
84
+ end
85
+
86
+ class << self
87
+
88
+ # Returns a configured logger for a given +Class+. The +Class+ that is passed in is used as the +progname+, for all
89
+ # messages logged by the logger. If this is called before the +setup()+ method, a failure will be generated. Note this
90
+ #
91
+ # @param [Class] klass the class that the logger is created for
92
+ # @return [Logger] the logger that was requested
93
+ # @deprecated use +LoggerFactory.instance.get_logger(klass)+ instead
94
+ def get_logger(klass)
95
+ LoggerFactory.instance.get_logger(klass)
96
+ end
97
+
98
+ end
99
+
100
+ private
101
+
102
+ def console_logger
103
+ logger = Logger.new($stderr)
104
+ logger.level = severity
105
+ logger.formatter = lambda do |severity, _datetime, klass, message|
106
+ "#{klass.ljust(32)} #{severity.ljust(5)} #{message}\n"
107
+ end
108
+
109
+ logger
110
+ end
111
+
112
+ def file_logger
113
+ FileUtils.mkdir_p File.dirname(@log_file)
114
+
115
+ logger = Logger.new(@log_file)
116
+ logger.level = ::Logger::DEBUG
117
+ logger.formatter = lambda do |severity, datetime, klass, message|
118
+ "#{datetime.strftime('%FT%T.%2N%z')} #{klass.ljust(32)} #{severity.ljust(5)} #{message}\n"
119
+ end
120
+
121
+ logger
122
+ end
123
+
124
+ def ruby_mode
125
+ $VERBOSE || $DEBUG ? 'DEBUG' : nil
126
+ end
127
+
128
+ def severity
129
+ severity = ENV['BP_LOG_LEVEL']
130
+ severity = ruby_mode unless severity
131
+ severity = @configuration_utils.load('logging')['default_log_level'] unless severity
132
+ severity = 'INFO' unless severity
133
+
134
+ "::Logger::Severity::#{severity.upcase}".constantize
135
+ end
136
+
137
+ def short_class(klass)
138
+ klass.to_s.split('::').last
139
+ end
140
+
141
+ def wrapped_short_class(klass)
142
+ "[#{short_class(klass)}]"
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+ end
@@ -0,0 +1,36 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License. language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'buildpack_support'
18
+
19
+ module BuildpackSupport
20
+
21
+ # Mixin with a method for qualifying a given path relative to a root
22
+ module QualifyPath
23
+
24
+ # Qualifies the path such that is is formatted as +$PWD/<path>+. Also ensures that the path is relative to a root,
25
+ # which defaults to the +@droplet_root+ of the class.
26
+ #
27
+ # @param [Pathname] path the path to qualify
28
+ # @param [Pathname] root the root to make relative to
29
+ # @return [String] the qualified path
30
+ def qualify_path(path, root = @droplet_root)
31
+ "$PWD/#{path.relative_path_from(root)}"
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,22 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ module BuildpackSupport
17
+
18
+ # A module encapsulating all of the code for the Rake utilities
19
+ module Rake
20
+ end
21
+
22
+ end
@@ -0,0 +1,86 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'buildpack_support/rake'
17
+ require 'rake'
18
+ require 'rake/tasklib'
19
+ require 'yaml'
20
+
21
+ module BuildpackSupport
22
+ module Rake
23
+
24
+ # A task generator for the tasks that copy files to the staging directory
25
+ class BuildpackStageTask < ::Rake::TaskLib
26
+
27
+ # @!attribute [rw] files
28
+ # @return [Array<String>] the files to copy to the +staging_dir+
29
+ attr_accessor :files
30
+
31
+ # @!attribute [rw] package_name
32
+ # @return [String] the name of the package
33
+ attr_accessor :package_name
34
+
35
+ # @!attribute [rw] staging_dir
36
+ # @return [String] the directory to zip the contents of
37
+ attr_accessor :staging_dir
38
+
39
+ # @!attribute [rw] verbose
40
+ # @return [Boolean] the verbosity of the task. Defaults to +false+.
41
+ attr_accessor :verbose
42
+
43
+ # Creates an instance
44
+ def initialize(&task_block)
45
+ @verbose = false
46
+
47
+ task_block.call(*[self].slice(0, task_block.arity)) if task_block
48
+ abort 'files must be configured' unless files
49
+ abort 'package_name must be configured' unless package_name
50
+ abort 'staging_dir must be configured' unless staging_dir
51
+
52
+ create_copy_tasks files, package_name, staging_dir, verbose
53
+ end
54
+
55
+ # Runs the task
56
+ #
57
+ # @param [String] source the file to copy
58
+ # @param [String] target where to copy the file to
59
+ # @param [Boolean] verbose whether to print messages to the console
60
+ # @return [void]
61
+ def run_task(source, target, verbose)
62
+ RakeFileUtils.verbose(verbose) do
63
+ mkdir_p File.dirname(target)
64
+ cp source, target
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def create_copy_tasks(files, package_name, staging_dir, verbose)
71
+ files.reject { |f| File.directory? f }.each do |source|
72
+ target = target(source, staging_dir)
73
+
74
+ task package_name => target
75
+ file(target => source) { |t| run_task t.source, t.name, verbose }
76
+ end
77
+ end
78
+
79
+ def target(file, staging_dir)
80
+ "#{staging_dir}/#{file}"
81
+ end
82
+
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,99 @@
1
+ # Encoding: utf-8
2
+ # # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'buildpack_support/cache/download_cache'
17
+ require 'buildpack_support/configuration_utils'
18
+ require 'buildpack_support/rake'
19
+ require 'buildpack_support/repository/wildcard_version_resolver'
20
+ require 'buildpack_support/tokenized_version'
21
+ require 'yaml'
22
+
23
+ module BuildpackSupport
24
+ module Rake
25
+
26
+ # A class that finds all artifacts eligible to be cached
27
+ class CachedArtifactFinder
28
+
29
+ # Creates a new instance
30
+ def initialize
31
+ @cache = BuildpackSupport::Cache::DownloadCache.new
32
+ @configuration_utils = BuildpackSupport::ConfigurationUtils.new
33
+ @default_repository_root = @configuration_utils.load('repository')['default_repository_root'].chomp('/')
34
+ @version_resolver = BuildpackSupport::Repository::WildcardVersionResolver.new
35
+ end
36
+
37
+ # Returns a collection of URIs to be cached
38
+ #
39
+ # @param [Array<Hash>] configurations a collection of hashes that contain repository configurations
40
+ # @return [Array<String>] a collection of uris to be cached
41
+ def find(configurations)
42
+ configurations.map do |configuration|
43
+ index_uris(configuration).map do |index_uri|
44
+ [index_uri, cached_artifact(configuration, index_uri)]
45
+ end
46
+ end.flatten
47
+ end
48
+
49
+ private
50
+
51
+ ARCHITECTURES = %w(x86_64).freeze
52
+
53
+ PLATFORMS = %w(centos6 lucid mountainlion precise trusty).freeze
54
+
55
+ private_constant :ARCHITECTURES, :PLATFORMS
56
+
57
+ def augment(raw, pattern, candidates)
58
+ raw =~ pattern ? candidates.map { |p| raw.gsub pattern, p } : raw
59
+ end
60
+
61
+ def augment_architecture(raw)
62
+ augment(raw, /\{architecture\}/, ARCHITECTURES)
63
+ end
64
+
65
+ def augment_path(raw)
66
+ "#{raw.chomp('/')}/index.yml"
67
+ end
68
+
69
+ def augment_platform(raw)
70
+ augment(raw, /\{platform\}/, PLATFORMS)
71
+ end
72
+
73
+ def augment_repository_root(raw)
74
+ raw.gsub(/\{default.repository.root\}/, @default_repository_root)
75
+ end
76
+
77
+ def cached_artifact(configuration, index_uri)
78
+ @cache.get(index_uri) do |f|
79
+ index = YAML.load f
80
+ index[resolved_version(configuration, index).to_s]
81
+ end
82
+ end
83
+
84
+ def index_uris(configuration)
85
+ [configuration['repository_root']]
86
+ .map { |r| augment_repository_root r }
87
+ .map { |r| augment_platform r }.flatten
88
+ .map { |r| augment_architecture r }.flatten
89
+ .map { |r| augment_path r }
90
+ end
91
+
92
+ def resolved_version(configuration, index)
93
+ @version_resolver.resolve(BuildpackSupport::TokenizedVersion.new(configuration['version']), index.keys)
94
+ end
95
+
96
+ end
97
+
98
+ end
99
+ end