Class: ShopifyCLI::Theme::File
- Inherits:
-
Struct
- Object
- Struct
- ShopifyCLI::Theme::File
show all
- Defined in:
- lib/shopify_cli/theme/file.rb
Defined Under Namespace
Classes: JsonTemplateNormalizer
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path, root) ⇒ File
Returns a new instance of File.
10
11
12
13
14
15
16
17
|
# File 'lib/shopify_cli/theme/file.rb', line 10
def initialize(path, root)
super(Pathname.new(path))
@relative_path = self.path.expand_path.relative_path_from(root.expand_path)
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path
6
7
8
|
# File 'lib/shopify_cli/theme/file.rb', line 6
def path
@path
end
|
#remote_checksum ⇒ Object
Returns the value of attribute remote_checksum.
7
8
9
|
# File 'lib/shopify_cli/theme/file.rb', line 7
def remote_checksum
@remote_checksum
end
|
#warnings ⇒ Object
108
109
110
|
# File 'lib/shopify_cli/theme/file.rb', line 108
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.
92
93
94
|
# File 'lib/shopify_cli/theme/file.rb', line 92
def ==(other)
relative_path == other.relative_path
end
|
#absolute_path ⇒ Object
100
101
102
|
# File 'lib/shopify_cli/theme/file.rb', line 100
def absolute_path
path.realpath.to_s
end
|
#checksum ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/shopify_cli/theme/file.rb', line 77
def checksum
content = read
if mime_type.json?
begin
content = normalize_json(content)
rescue JSON::JSONError
end
end
Digest::MD5.hexdigest(content)
end
|
#delete ⇒ Object
45
46
47
|
# File 'lib/shopify_cli/theme/file.rb', line 45
def delete
path.delete
end
|
#exist? ⇒ Boolean
49
50
51
|
# File 'lib/shopify_cli/theme/file.rb', line 49
def exist?
path.exist?
end
|
#json? ⇒ Boolean
69
70
71
|
# File 'lib/shopify_cli/theme/file.rb', line 69
def json?
path.extname == ".json"
end
|
#liquid? ⇒ Boolean
61
62
63
|
# File 'lib/shopify_cli/theme/file.rb', line 61
def liquid?
path.extname == ".liquid"
end
|
#liquid_css? ⇒ Boolean
65
66
67
|
# File 'lib/shopify_cli/theme/file.rb', line 65
def liquid_css?
relative_path.end_with?(".css.liquid")
end
|
#mime_type ⇒ Object
53
54
55
|
# File 'lib/shopify_cli/theme/file.rb', line 53
def mime_type
@mime_type ||= MimeType.by_filename(@relative_path)
end
|
#name(*args) ⇒ Object
96
97
98
|
# File 'lib/shopify_cli/theme/file.rb', line 96
def name(*args)
::File.basename(path, *args)
end
|
#read ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/shopify_cli/theme/file.rb', line 19
def read
if text?
path.read(universal_newline: true)
else
path.read(mode: "rb")
end
end
|
#relative_path ⇒ Object
104
105
106
|
# File 'lib/shopify_cli/theme/file.rb', line 104
def relative_path
@relative_path.to_s
end
|
#template? ⇒ Boolean
73
74
75
|
# File 'lib/shopify_cli/theme/file.rb', line 73
def template?
relative_path.start_with?("templates/")
end
|
#text? ⇒ Boolean
57
58
59
|
# File 'lib/shopify_cli/theme/file.rb', line 57
def text?
mime_type.text?
end
|
#write(content) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/shopify_cli/theme/file.rb', line 27
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
path.write(content, 0, mode: "wb")
end
|