Class: FluidCLI::Theme::File
- Inherits:
-
Struct
- Object
- Struct
- FluidCLI::Theme::File
- Defined in:
- lib/fluid_cli/theme/file.rb
Constant Summary collapse
- TEMPLATE_TYPES =
%w[ product medium enrollment_pack shop_page library category_page collection_page cart_page home_page join_page page collection post category post_page navbar footer ].freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#remote_checksum ⇒ Object
Returns the value of attribute remote_checksum.
- #warnings ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
Make it possible to check whether a given File is within a list of Files with
include?, some of which may be relative paths while others are absolute paths. - #absolute_path ⇒ Object
- #checksum ⇒ Object
- #delete ⇒ Object
- #exist? ⇒ Boolean
-
#initialize(path, root) ⇒ File
constructor
A new instance of File.
- #json? ⇒ Boolean
- #liquid? ⇒ Boolean
- #liquid_css? ⇒ Boolean
- #mime_type ⇒ Object
- #name(*args) ⇒ Object
- #read ⇒ Object
- #relative_path ⇒ Object
- #template? ⇒ Boolean
- #text? ⇒ Boolean
- #validate_schema ⇒ Object
- #validate_schema_and_raise ⇒ Object
- #write(content) ⇒ Object
Constructor Details
#initialize(path, root) ⇒ File
Returns a new instance of File.
16 17 18 19 20 21 22 23 |
# File 'lib/fluid_cli/theme/file.rb', line 16 def initialize(path, root) super(Pathname.new(path)) # Path may be relative or absolute depending on the source. # By converting both the path and the root to absolute paths, we # can safely fetch a relative path. @relative_path = self.path..relative_path_from(root.) end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path
7 8 9 |
# File 'lib/fluid_cli/theme/file.rb', line 7 def path @path end |
#remote_checksum ⇒ Object
Returns the value of attribute remote_checksum.
8 9 10 |
# File 'lib/fluid_cli/theme/file.rb', line 8 def remote_checksum @remote_checksum end |
#warnings ⇒ Object
106 107 108 |
# File 'lib/fluid_cli/theme/file.rb', line 106 def warnings @warnings || [] end |
Instance Method Details
#==(other) ⇒ Object
Make it possible to check whether a given File is within a list of Files with include?,
some of which may be relative paths while others are absolute paths.
90 91 92 |
# File 'lib/fluid_cli/theme/file.rb', line 90 def ==(other) relative_path == other.relative_path end |
#absolute_path ⇒ Object
98 99 100 |
# File 'lib/fluid_cli/theme/file.rb', line 98 def absolute_path path.realpath.to_s end |
#checksum ⇒ Object
83 84 85 86 |
# File 'lib/fluid_cli/theme/file.rb', line 83 def checksum content = read Digest::SHA256.hexdigest(content) end |
#delete ⇒ Object
51 52 53 |
# File 'lib/fluid_cli/theme/file.rb', line 51 def delete path.delete end |
#exist? ⇒ Boolean
55 56 57 |
# File 'lib/fluid_cli/theme/file.rb', line 55 def exist? path.exist? end |
#json? ⇒ Boolean
75 76 77 |
# File 'lib/fluid_cli/theme/file.rb', line 75 def json? path.extname == ".json" end |
#liquid? ⇒ Boolean
67 68 69 |
# File 'lib/fluid_cli/theme/file.rb', line 67 def liquid? path.extname == ".liquid" end |
#liquid_css? ⇒ Boolean
71 72 73 |
# File 'lib/fluid_cli/theme/file.rb', line 71 def liquid_css? relative_path.end_with?(".css.liquid") end |
#mime_type ⇒ Object
59 60 61 |
# File 'lib/fluid_cli/theme/file.rb', line 59 def mime_type @mime_type ||= FluidCLI::Theme::MimeType.by_filename(@relative_path) end |
#name(*args) ⇒ Object
94 95 96 |
# File 'lib/fluid_cli/theme/file.rb', line 94 def name(*args) ::File.basename(path, *args) end |
#read ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/fluid_cli/theme/file.rb', line 25 def read if text? path.read(universal_newline: true) else path.read(mode: "rb") end end |
#relative_path ⇒ Object
102 103 104 |
# File 'lib/fluid_cli/theme/file.rb', line 102 def relative_path @relative_path.to_s end |
#template? ⇒ Boolean
79 80 81 |
# File 'lib/fluid_cli/theme/file.rb', line 79 def template? TEMPLATE_TYPES.any? { |type| relative_path.start_with?(type) } end |
#text? ⇒ Boolean
63 64 65 |
# File 'lib/fluid_cli/theme/file.rb', line 63 def text? mime_type.text? end |
#validate_schema ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/fluid_cli/theme/file.rb', line 110 def validate_schema return [] if !liquid? FluidCLI::Theme::SchemaValidator .new(self, blocks_schema_type: template? ? "object" : "array") .validate end |
#validate_schema_and_raise ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fluid_cli/theme/file.rb', line 118 def validate_schema_and_raise diags = validate_schema return if diags.empty? = "Schema validation errors in #{relative_path}:\n" diags.each do |diag| += "- [#{diag[:severity]}] #{diag[:message]}\n" end raise FluidCLI::Theme::SchemaValidationError, end |
#write(content) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/fluid_cli/theme/file.rb', line 33 def write(content) path.parent.mkpath unless path.parent.directory? if text? path.write(content, universal_newline: true) else path.write(content, 0, mode: "wb") end rescue Encoding::UndefinedConversionError ## # The CLI tries to write the file and normalize EOL characters to avoid # errors on Windows when files are shared across different operational systems. # # The CLI fallbacks any error during the conversion by writing the file # in binary mode when the normalization fails (e.g., ASCII files), so no data is lost. # path.write(content, 0, mode: "wb") end |