shaf 1.6.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/iana_link_relations.csv.gz +0 -0
  5. data/lib/shaf.rb +6 -0
  6. data/lib/shaf/alps/attribute_serializer.rb +41 -0
  7. data/lib/shaf/alps/json_serializer.rb +50 -0
  8. data/lib/shaf/alps/relation_serializer.rb +70 -0
  9. data/lib/shaf/api_doc/link_relations.rb +77 -0
  10. data/lib/shaf/authenticator.rb +56 -0
  11. data/lib/shaf/authenticator/base.rb +161 -0
  12. data/lib/shaf/authenticator/basic_auth.rb +25 -0
  13. data/lib/shaf/authenticator/challenge.rb +32 -0
  14. data/lib/shaf/authenticator/parameter.rb +31 -0
  15. data/lib/shaf/authenticator/request.rb +17 -0
  16. data/lib/shaf/command/console.rb +1 -1
  17. data/lib/shaf/command/generate.rb +5 -2
  18. data/lib/shaf/command/new.rb +20 -7
  19. data/lib/shaf/command/templates/Gemfile.erb +1 -0
  20. data/{templates/config/settings.yml → lib/shaf/command/templates/config/settings.yml.erb} +1 -5
  21. data/lib/shaf/errors.rb +11 -0
  22. data/lib/shaf/extensions.rb +3 -3
  23. data/lib/shaf/extensions/api_routes.rb +60 -0
  24. data/lib/shaf/extensions/authorize.rb +11 -9
  25. data/lib/shaf/extensions/log.rb +1 -1
  26. data/lib/shaf/extensions/resource_uris.rb +139 -63
  27. data/lib/shaf/extensions/symbolic_routes.rb +22 -19
  28. data/lib/shaf/formable.rb +1 -2
  29. data/lib/shaf/formable/form.rb +1 -1
  30. data/lib/shaf/generator.rb +2 -0
  31. data/lib/shaf/generator/base.rb +2 -3
  32. data/lib/shaf/generator/controller.rb +11 -7
  33. data/lib/shaf/generator/doc.rb +17 -0
  34. data/lib/shaf/generator/forms.rb +1 -0
  35. data/lib/shaf/generator/helper.rb +2 -1
  36. data/lib/shaf/generator/migration/base.rb +7 -3
  37. data/lib/shaf/generator/migration/type.rb +4 -26
  38. data/lib/shaf/generator/migration/types.rb +45 -16
  39. data/lib/shaf/generator/model.rb +1 -2
  40. data/lib/shaf/generator/profile.rb +52 -0
  41. data/lib/shaf/generator/serializer.rb +38 -73
  42. data/lib/shaf/generator/templates/api/policy.rb.erb +2 -2
  43. data/lib/shaf/generator/templates/api/profile.rb.erb +16 -0
  44. data/lib/shaf/generator/templates/api/serializer.rb.erb +2 -2
  45. data/lib/shaf/generator/templates/spec/integration_spec.rb.erb +1 -2
  46. data/lib/shaf/generator/templates/spec/serializer_spec.rb.erb +5 -5
  47. data/lib/shaf/helpers.rb +4 -0
  48. data/lib/shaf/helpers/authentication.rb +79 -0
  49. data/lib/shaf/helpers/payload.rb +14 -34
  50. data/lib/shaf/helpers/vary.rb +8 -0
  51. data/lib/shaf/logger.rb +12 -0
  52. data/lib/shaf/parser.rb +65 -0
  53. data/lib/shaf/parser/base.rb +44 -0
  54. data/lib/shaf/parser/form_data.rb +15 -0
  55. data/lib/shaf/parser/json.rb +26 -0
  56. data/lib/shaf/profile.rb +110 -0
  57. data/lib/shaf/profile/attribute.rb +29 -0
  58. data/lib/shaf/profile/evaluator.rb +46 -0
  59. data/lib/shaf/profile/relation.rb +29 -0
  60. data/lib/shaf/profile/unique_id.rb +58 -0
  61. data/lib/shaf/profiles.rb +42 -0
  62. data/lib/shaf/profiles/shaf_basic.rb +20 -0
  63. data/lib/shaf/profiles/shaf_error.rb +48 -0
  64. data/lib/shaf/profiles/shaf_form.rb +109 -0
  65. data/lib/shaf/responder.rb +41 -2
  66. data/lib/shaf/responder/alps_json.rb +25 -0
  67. data/lib/shaf/responder/base.rb +20 -17
  68. data/lib/shaf/responder/hal.rb +65 -4
  69. data/lib/shaf/responder/html.rb +43 -16
  70. data/lib/shaf/responder/problem_json.rb +1 -1
  71. data/lib/shaf/serializer.rb +31 -0
  72. data/lib/shaf/settings.rb +25 -12
  73. data/lib/shaf/spec.rb +1 -0
  74. data/lib/shaf/spec/authenticator.rb +13 -0
  75. data/lib/shaf/spec/base.rb +1 -1
  76. data/lib/shaf/spec/http_method_utils.rb +1 -1
  77. data/lib/shaf/spec/integration_spec.rb +25 -13
  78. data/lib/shaf/spec/payload_utils.rb +2 -2
  79. data/lib/shaf/supported_http_methods.rb +15 -0
  80. data/lib/shaf/tasks/api_doc_task.rb +24 -3
  81. data/lib/shaf/tasks/routes_task.rb +14 -17
  82. data/lib/shaf/upgrade/manifest.rb +11 -2
  83. data/lib/shaf/upgrade/package.rb +73 -43
  84. data/lib/shaf/upgrade/version.rb +11 -10
  85. data/lib/shaf/utils.rb +19 -5
  86. data/lib/shaf/version.rb +3 -1
  87. data/lib/shaf/yard.rb +34 -0
  88. data/lib/shaf/yard/attribute_method_handler.rb +19 -0
  89. data/lib/shaf/yard/attribute_object.rb +30 -0
  90. data/lib/shaf/yard/base_method_handler.rb +30 -0
  91. data/lib/shaf/yard/link_method_handler.rb +39 -0
  92. data/lib/shaf/yard/link_object.rb +60 -0
  93. data/lib/shaf/yard/nested_attributes.rb +37 -0
  94. data/lib/shaf/yard/parser.rb +64 -0
  95. data/lib/shaf/yard/profile_method_handler.rb +51 -0
  96. data/lib/shaf/yard/profile_object.rb +21 -0
  97. data/lib/shaf/yard/resource_object.rb +55 -0
  98. data/lib/shaf/yard/serializer_handler.rb +27 -0
  99. data/templates/api/controllers/base_controller.rb +0 -10
  100. data/templates/api/controllers/docs_controller.rb +5 -3
  101. data/templates/api/controllers/root_controller.rb +7 -1
  102. data/templates/api/policies/base_policy.rb +2 -0
  103. data/templates/api/serializers/base_serializer.rb +1 -3
  104. data/templates/api/serializers/error_serializer.rb +1 -5
  105. data/templates/api/serializers/form_serializer.rb +1 -5
  106. data/templates/api/serializers/validation_error_serializer.rb +1 -5
  107. data/templates/config/bootstrap.rb +1 -2
  108. data/templates/config/directories.rb +52 -44
  109. data/templates/config/helpers.rb +1 -1
  110. data/templates/config/initializers.rb +52 -8
  111. data/templates/config/initializers/authentication.rb +18 -0
  112. data/templates/config/initializers/db_migrations.rb +2 -2
  113. data/templates/config/initializers/logging.rb +2 -2
  114. data/templates/spec/spec_helper.rb +2 -0
  115. data/upgrades/0.5.0.tar.gz +0 -0
  116. data/upgrades/1.0.4.tar.gz +0 -0
  117. data/upgrades/1.1.0.tar.gz +0 -0
  118. data/upgrades/2.0.0.tar.gz +0 -0
  119. data/yard_templates/api_doc/doc_index/html/body.erb +3 -0
  120. data/yard_templates/api_doc/doc_index/setup.rb +8 -0
  121. data/yard_templates/api_doc/html/css/api-doc.css +222 -0
  122. data/yard_templates/api_doc/html/favicon.ico +0 -0
  123. data/yard_templates/api_doc/html/js/switch_tab.js +17 -0
  124. data/yard_templates/api_doc/html/setup.rb +59 -0
  125. data/yard_templates/api_doc/layout/html/footer.erb +3 -0
  126. data/yard_templates/api_doc/layout/html/header.erb +7 -0
  127. data/yard_templates/api_doc/layout/html/layout.erb +13 -0
  128. data/yard_templates/api_doc/layout/setup.rb +24 -0
  129. data/yard_templates/api_doc/profile/html/attributes.erb +10 -0
  130. data/yard_templates/api_doc/profile/html/profile.erb +6 -0
  131. data/yard_templates/api_doc/profile/html/relations.erb +10 -0
  132. data/yard_templates/api_doc/profile/setup.rb +38 -0
  133. data/yard_templates/api_doc/profile_attribute/html/attribute.erb +23 -0
  134. data/yard_templates/api_doc/profile_attribute/setup.rb +21 -0
  135. data/yard_templates/api_doc/profile_relation/html/relation.erb +37 -0
  136. data/yard_templates/api_doc/profile_relation/setup.rb +41 -0
  137. data/yard_templates/api_doc/resource/html/attributes.erb +10 -0
  138. data/yard_templates/api_doc/resource/html/profile.erb +14 -0
  139. data/yard_templates/api_doc/resource/html/relations.erb +10 -0
  140. data/yard_templates/api_doc/resource/html/resource.erb +5 -0
  141. data/yard_templates/api_doc/resource/setup.rb +56 -0
  142. data/yard_templates/api_doc/resource_attribute/html/attribute.erb +23 -0
  143. data/yard_templates/api_doc/resource_attribute/setup.rb +20 -0
  144. data/yard_templates/api_doc/resource_relation/html/relation.erb +47 -0
  145. data/yard_templates/api_doc/resource_relation/setup.rb +80 -0
  146. data/yard_templates/api_doc/setup.rb +31 -0
  147. data/yard_templates/api_doc/sidebar/html/profile_list.erb +8 -0
  148. data/yard_templates/api_doc/sidebar/html/search.erb +7 -0
  149. data/yard_templates/api_doc/sidebar/html/serializer_list.erb +8 -0
  150. data/yard_templates/api_doc/sidebar/html/sidebar.erb +13 -0
  151. data/yard_templates/api_doc/sidebar/setup.rb +56 -0
  152. metadata +137 -30
  153. metadata.gz.sig +1 -3
  154. data/lib/shaf/extensions/current_user.rb +0 -48
  155. data/lib/shaf/responder/hal_serializable.rb +0 -64
data/lib/shaf/spec.rb CHANGED
@@ -6,3 +6,4 @@ require 'shaf/spec/base'
6
6
  require 'shaf/spec/integration_spec'
7
7
  require 'shaf/spec/serializer_spec'
8
8
  require 'shaf/spec/system_spec'
9
+ require 'shaf/spec/authenticator'
@@ -0,0 +1,13 @@
1
+ module Shaf
2
+ module Spec
3
+ class Authenticator < Shaf::Authenticator::Base
4
+ scheme 'Test'
5
+
6
+ param :realm, required: false
7
+
8
+ def self.credentials(authorization, _request)
9
+ { id: authorization&.to_i }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -24,7 +24,7 @@ module Shaf
24
24
  end
25
25
 
26
26
  before do
27
- $logger&.info <<~LOG
27
+ Shaf.log.info <<~LOG
28
28
  \n
29
29
  ##########################################################################
30
30
  # #{self.class.superclass.name} - #{name}
@@ -7,7 +7,7 @@ module Shaf
7
7
 
8
8
  %i[get put patch post delete options head link unlink].each do |m|
9
9
  define_method m do |uri, payload = nil, options = {}|
10
- set_headers
10
+ set_authentication
11
11
 
12
12
  if payload
13
13
  payload = JSON.generate(payload) if payload.respond_to? :to_h
@@ -8,13 +8,18 @@ module Shaf
8
8
 
9
9
  register_spec_type self do |_desc, args|
10
10
  next unless args&.is_a?(Hash)
11
- args[:type]&.to_s == 'integration'
11
+ args[:type].to_s == 'integration'
12
12
  end
13
13
 
14
- def set_headers
15
- if defined?(@__integration_test_auth_token) && @__integration_test_auth_token
16
- header Settings.auth_token_header, @__integration_test_auth_token
17
- end
14
+ private
15
+
16
+ attr_accessor :__authenticated_user_id
17
+
18
+ def set_authentication
19
+ id = __authenticated_user_id
20
+ authorization = "#{Authenticator.scheme} #{id}" if id
21
+
22
+ header 'Authorization', authorization
18
23
  end
19
24
 
20
25
  def parse_response(body)
@@ -28,6 +33,21 @@ module Shaf
28
33
  App.app
29
34
  end
30
35
 
36
+ def authenticate(user)
37
+ self.__authenticated_user_id = user&.id
38
+ end
39
+
40
+ def unauthenticate
41
+ self.__authenticated_user_id = nil
42
+ end
43
+
44
+ def with_authenticated(user, &block)
45
+ authenticate(user)
46
+ yield
47
+ ensure
48
+ unauthenticate
49
+ end
50
+
31
51
  def follow_rel(rel, method: nil)
32
52
  assert_has_link(rel)
33
53
  link = links[rel.to_sym]
@@ -37,14 +57,6 @@ module Shaf
37
57
  get link[:href]
38
58
  end
39
59
  end
40
-
41
- def auth_token(token)
42
- @__integration_test_auth_token = token
43
- end
44
-
45
- def clear_auth_token
46
- @__integration_test_auth_token = nil
47
- end
48
60
  end
49
61
  end
50
62
  end
@@ -29,11 +29,11 @@ module Shaf
29
29
  last_payload[:_embedded]&.keys || []
30
30
  end
31
31
 
32
- def embedded(name = nil)
32
+ def embedded(name = nil, &block)
33
33
  assert_has_embedded name unless name.nil?
34
34
  keys = [:_embedded, name&.to_sym].compact
35
35
  return last_payload.dig(*keys) unless block_given?
36
- exec_embed_block(last_payload.dig(*keys), Proc.new)
36
+ exec_embed_block(last_payload.dig(*keys), block)
37
37
  end
38
38
 
39
39
  def each_embedded(name, &block)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shaf
4
+ SUPPORTED_HTTP_METHODS = [
5
+ :get,
6
+ :put,
7
+ :post,
8
+ :patch,
9
+ :delete,
10
+ :head,
11
+ :options,
12
+ :link,
13
+ :unlink
14
+ ]
15
+ end
@@ -1,6 +1,3 @@
1
- require 'shaf/api_doc/document'
2
- require 'shaf/api_doc/comment'
3
-
4
1
  module Shaf
5
2
  module Tasks
6
3
  class ApiDocTask
@@ -9,6 +6,11 @@ module Shaf
9
6
  attr_accessor :document_class, :source_dir, :html_output_dir, :yaml_output_dir
10
7
 
11
8
  def initialize
9
+ return show_deprecation_message if RUBY_VERSION >= '3.0.0'
10
+
11
+ require 'shaf/api_doc/document'
12
+ require 'shaf/api_doc/comment'
13
+
12
14
  yield self if block_given?
13
15
  validate_attributes!
14
16
  @document_class ||= ApiDoc::Document
@@ -25,6 +27,8 @@ module Shaf
25
27
  namespace :doc do
26
28
  desc "Generate API documentation"
27
29
  task :generate do
30
+ show_deprecation_message
31
+
28
32
  files = Dir.glob(File.join(source_dir, "*.rb"))
29
33
  files.each do |file|
30
34
  read_file file do |doc|
@@ -47,6 +51,23 @@ module Shaf
47
51
  end
48
52
  end
49
53
 
54
+ def show_deprecation_message
55
+ ruby3_msg = <<~RUBY3 if RUBY_VERSION >= '3.0.0'
56
+
57
+ Due to errors with the Redcarpet gem it's not possible to use this deprecated rake task with Ruby >= 3.0.0
58
+ If you need to continue with this task please use an older version of Ruby.
59
+ RUBY3
60
+
61
+ puts <<~MSG
62
+ This way of generating documentation is DEPRECATED.#{ruby3_msg}
63
+
64
+ Please move the documentation comments into profiles instead and run:
65
+ shaf generate doc
66
+ See https://github.com/sammyhenningsson/shaf/blob/main/doc/DOCUMENTATION.md for more info
67
+
68
+ MSG
69
+ end
70
+
50
71
  def read_file(file)
51
72
  doc = document_class.new
52
73
  comment = ApiDoc::Comment.new
@@ -1,29 +1,26 @@
1
1
  module Shaf
2
2
  module Tasks
3
3
  class RoutesTask
4
- include Rake::DSL
4
+ extend Rake::DSL
5
5
 
6
- def initialize(*)
7
- desc "List path helpers"
8
- task :routes do
9
- require 'shaf/utils'
10
- require 'config/database'
6
+ desc 'List path helpers'
7
+ task :routes do
8
+ require 'shaf/utils'
9
+ require 'config/database'
11
10
 
12
- extend Shaf::Utils
13
- bootstrap
11
+ extend Shaf::Utils
12
+ bootstrap
14
13
 
15
- helpers = UriHelperMethods.path_helpers_for.sort { |a, b| a[0].to_s <=> b[0].to_s }
16
- helpers.each do |controller, methods|
17
- puts "\n#{controller}:"
18
- methods.each do |method|
19
- template_method = "#{method}_template".to_sym
20
- puts sprintf( "%-60s%s" , method, controller.send(template_method))
21
- end
14
+ Shaf::ApiRoutes::Registry.controllers.each do |controller|
15
+ puts "\n#{controller}:"
16
+ Shaf::ApiRoutes::Registry.routes_for(controller) do |methods, template, symbol|
17
+ puts format(
18
+ ' %-50<symbol>s%-30<methods>s%<template>s',
19
+ {symbol: symbol, methods: methods.join(' | '), template: template}
20
+ )
22
21
  end
23
22
  end
24
23
  end
25
24
  end
26
-
27
- RoutesTask.new
28
25
  end
29
26
  end
@@ -12,6 +12,7 @@ module Shaf
12
12
  @files[:add] = params[:add] || {}
13
13
  @files[:drop] = (params[:drop] || []).map { |d| Regexp.new(d) }
14
14
  @files[:regexp] = build_patterns(params[:substitutes])
15
+ @files[:messages] = params[:messages] || []
15
16
  end
16
17
 
17
18
  def patches
@@ -30,6 +31,10 @@ module Shaf
30
31
  files[:regexp]
31
32
  end
32
33
 
34
+ def messages
35
+ files[:messages]
36
+ end
37
+
33
38
  def patches_for(file)
34
39
  patches.select { |_, pattern| file =~ pattern }.keys
35
40
  end
@@ -47,7 +52,8 @@ module Shaf
47
52
  additions: additions.size,
48
53
  removals: removals.size,
49
54
  patches: patches.size,
50
- regexps: regexps.size
55
+ regexps: regexps.size,
56
+ messages: messages.size
51
57
  }
52
58
  end
53
59
 
@@ -55,7 +61,8 @@ module Shaf
55
61
  "Add: #{additions.size}, " \
56
62
  "Del: #{removals.size}, " \
57
63
  "Patch: #{patches.size}, " \
58
- "Regexp: #{regexps.size}"
64
+ "Regexp: #{regexps.size}, " \
65
+ "Messages: #{messages.size}"
59
66
  end
60
67
 
61
68
  private
@@ -82,3 +89,5 @@ end
82
89
  # - api/policies/base_policy.rb
83
90
  # substitutes:
84
91
  # d3b07384d113edec49eaa6238ad5ff00: api/models/.*.rb
92
+ # messages:
93
+ # - b1946ac92492d2347c6235b4d2611184
@@ -6,6 +6,9 @@ require 'open3'
6
6
  require 'fileutils'
7
7
  require 'tempfile'
8
8
  require 'yaml'
9
+ require 'file_transactions'
10
+
11
+ FT = FileTransactions
9
12
 
10
13
  module Shaf
11
14
  module Upgrade
@@ -23,8 +26,6 @@ module Shaf
23
26
  MANIFEST_FILENAME = 'manifest'.freeze
24
27
  IGNORE_FILE_PATTERNS = [/.rej\z/, /.orig\z/]
25
28
 
26
- attr_reader :version
27
-
28
29
  class << self
29
30
  def all
30
31
  target_versions.map(&method(:new))
@@ -53,6 +54,8 @@ module Shaf
53
54
  end
54
55
  end
55
56
 
57
+ attr_reader :version, :manifest
58
+
56
59
  def initialize(version, manifest = nil, files = {})
57
60
  @version = Version.new(version)
58
61
  @manifest = manifest
@@ -77,25 +80,32 @@ module Shaf
77
80
  end
78
81
 
79
82
  def apply(dir = nil)
80
- puts "Applying changes for version #{version}"
81
- [
82
- apply_patches(dir),
83
- apply_drops(dir),
84
- apply_additions,
85
- apply_substitutes(dir),
86
- ].all?
83
+ puts "\n ## Applying changes for Shaf version: #{version} ##"
84
+
85
+ FT.transaction do
86
+ result = [
87
+ apply_patches(dir),
88
+ apply_drops(dir),
89
+ apply_additions,
90
+ apply_substitutes(dir),
91
+ print_messages,
92
+ ]
93
+ raise UpgradeError, 'Upgrade failed' unless result.all?
94
+ end
95
+
96
+ true
87
97
  end
88
98
 
89
99
  def to_s
90
- str = "Shaf::Upgrade::Package for version #{@version}"
91
- return str if @manifest.nil?
92
- "#{str} (#{@manifest.stats_str})"
100
+ str = "Shaf::Upgrade::Package for version #{version}"
101
+ return str if manifest.nil?
102
+ "#{str} (#{manifest.stats_str})"
93
103
  end
94
104
 
95
105
  private
96
106
 
97
107
  def tarball
98
- file = File.join(UPGRADE_FILES_PATH, "#{@version}.tar.gz")
108
+ file = File.join(UPGRADE_FILES_PATH, "#{version}.tar.gz")
99
109
  return file if File.exist? file
100
110
  raise VersionNotFoundError
101
111
  end
@@ -114,24 +124,25 @@ module Shaf
114
124
  def parse_manifest(content)
115
125
  hash = YAML.safe_load(content)
116
126
  @manifest = Manifest.new(
117
- target_version: hash["target_version"],
118
- patches: hash["patches"],
119
- add: hash["add"],
120
- drop: hash["drop"],
121
- substitutes: hash["substitutes"]
127
+ target_version: hash['target_version'],
128
+ patches: hash['patches'],
129
+ add: hash['add'],
130
+ drop: hash['drop'],
131
+ substitutes: hash['substitutes'],
132
+ messages: hash['messages']
122
133
  )
123
134
  end
124
135
 
125
136
  def validate
126
- raise ManifestNotFoundError unless @manifest
127
- raise VersionConflictError unless @version == @manifest.target_version
137
+ raise ManifestNotFoundError unless manifest
138
+ raise VersionConflictError unless version == manifest.target_version
128
139
 
129
140
  from_file = @files.keys.to_set
130
141
 
131
- manifest_patches = @manifest.files[:patch].keys.to_set
142
+ manifest_patches = manifest.patches.keys.to_set
132
143
  raise MissingFileError if from_file < manifest_patches
133
144
 
134
- to_add = @manifest.files[:add].keys.to_set
145
+ to_add = manifest.additions.keys.to_set
135
146
  raise MissingFileError if from_file < to_add
136
147
 
137
148
  @files.each do |md5, content|
@@ -156,7 +167,7 @@ module Shaf
156
167
 
157
168
  def apply_patches(dir = nil)
158
169
  files_in(dir).map do |file|
159
- @manifest.patches_for(file).map do |name|
170
+ manifest.patches_for(file).map do |name|
160
171
  patch = @files[name]
161
172
  apply_patch(file, patch)
162
173
  end.all?
@@ -164,25 +175,29 @@ module Shaf
164
175
  end
165
176
 
166
177
  def apply_patch(file, patch)
167
- Open3.popen3('patch', file) do |i, o, e, t|
168
- i.write patch
169
- i.close
170
- puts o.read
171
- err = e.read
172
- puts err unless err.empty?
173
- result = t.value.success?
174
- STDERR.puts "Failed to apply patch for: #{file}\n" unless result
175
- result
178
+ result = nil
179
+
180
+ FT::ChangeFileCommand.execute(file) do
181
+ Open3.popen3('patch', file) do |i, o, e, t|
182
+ i.write patch
183
+ i.close
184
+ puts o.read
185
+ err = e.read
186
+ puts err unless err.empty?
187
+ result = t.value.success?
188
+ end
176
189
  end
190
+
191
+ STDERR.puts "Failed to apply patch for: #{file}\n" unless result
192
+ result
177
193
  end
178
194
 
179
195
  def apply_additions
180
- puts '' unless @manifest.additions.empty?
181
- @manifest.additions.all? do |chksum, filename|
196
+ puts '' unless manifest.additions.empty?
197
+ manifest.additions.all? do |chksum, filename|
182
198
  content = @files[chksum]
183
- FileUtils.mkdir_p File.dirname(filename)
184
199
  puts "adding file: #{filename}"
185
- File.open(filename, 'w') { |file| file.write(content) }
200
+ FT::CreateFileCommand.execute(filename) { content }
186
201
  true
187
202
  rescue StandardError => e
188
203
  STDERR.puts "Failed to add '#{filename}': #{e.message}\n"
@@ -191,11 +206,11 @@ module Shaf
191
206
  end
192
207
 
193
208
  def apply_drops(dir = nil)
194
- puts '' unless @manifest.removals.empty?
209
+ puts '' unless manifest.removals.empty?
195
210
  files_in(dir).all? do |file|
196
- next true unless @manifest.drop?(file)
211
+ next true unless manifest.drop?(file)
197
212
  puts "removing file: #{file}"
198
- File.unlink(file)
213
+ FT::DeleteFileCommand.execute file
199
214
  true
200
215
  rescue StandardError => e
201
216
  STDERR.puts "Failed to unlink '#{file}': #{e.message}\n"
@@ -204,9 +219,9 @@ module Shaf
204
219
  end
205
220
 
206
221
  def apply_substitutes(dir = nil)
207
- puts '' unless @manifest.regexps.empty?
222
+ puts '' unless manifest.regexps.empty?
208
223
  files_in(dir).map do |file|
209
- @manifest.regexps_for(file).map do |name|
224
+ manifest.regexps_for(file).map do |name|
210
225
  params = YAML.safe_load(@files[name])
211
226
  params.transform_keys!(&:to_sym)
212
227
  apply_substitute(file, params)
@@ -220,14 +235,29 @@ module Shaf
220
235
  pattern = Regexp.new(params[:pattern])
221
236
  replacement = params[:replace]
222
237
 
238
+ changed = false
223
239
  tmp = Tempfile.open do |new_file|
224
240
  File.readlines(file).each do |line|
225
- new_file << line.gsub(pattern, replacement)
241
+ changed = line.gsub!(pattern, replacement) || changed
242
+ new_file << line
226
243
  end
227
244
  new_file
228
245
  end
229
246
 
230
- FileUtils.mv(tmp.path, file)
247
+ return true unless changed
248
+
249
+ puts "modifying file: #{file}"
250
+ FT::MoveFileCommand.new(from: tmp.path, to: file).execute
251
+ true
252
+ end
253
+
254
+ def print_messages
255
+ manifest.messages.each do |chksum|
256
+ message = @files[chksum]
257
+ puts "\n#{message}"
258
+ end
259
+
260
+ true
231
261
  end
232
262
  end
233
263
  end