Class: FluidCLI::Theme::File

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

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.expand_path.relative_path_from(root.expand_path)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



7
8
9
# File 'lib/fluid_cli/theme/file.rb', line 7

def path
  @path
end

#remote_checksumObject

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

#warningsObject



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_pathObject



98
99
100
# File 'lib/fluid_cli/theme/file.rb', line 98

def absolute_path
  path.realpath.to_s
end

#checksumObject



83
84
85
86
# File 'lib/fluid_cli/theme/file.rb', line 83

def checksum
  content = read
  Digest::SHA256.hexdigest(content)
end

#deleteObject



51
52
53
# File 'lib/fluid_cli/theme/file.rb', line 51

def delete
  path.delete
end

#exist?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/fluid_cli/theme/file.rb', line 55

def exist?
  path.exist?
end

#json?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/fluid_cli/theme/file.rb', line 75

def json?
  path.extname == ".json"
end

#liquid?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/fluid_cli/theme/file.rb', line 67

def liquid?
  path.extname == ".liquid"
end

#liquid_css?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/fluid_cli/theme/file.rb', line 71

def liquid_css?
  relative_path.end_with?(".css.liquid")
end

#mime_typeObject



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

#readObject



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_pathObject



102
103
104
# File 'lib/fluid_cli/theme/file.rb', line 102

def relative_path
  @relative_path.to_s
end

#template?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


63
64
65
# File 'lib/fluid_cli/theme/file.rb', line 63

def text?
  mime_type.text?
end

#validate_schemaObject



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_raiseObject



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?

  message = "Schema validation errors in #{relative_path}:\n"
  diags.each do |diag|
    message += "- [#{diag[:severity]}] #{diag[:message]}\n"
  end
  raise FluidCLI::Theme::SchemaValidationError, message
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