fastlane-plugin-plist_surgeon 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e2e62e85c8e6796d2c4c70e8c4fe1b343b522d7d199662018185b0176d172f2e
|
|
4
|
+
data.tar.gz: 1baf91af0fe8f8fdf22032a74cf4a1db2a85cd65d3ef06f327353d38df3f0e81
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f18b64b3e54100c1861af402d66983189ab87b37ab4bd7e9b45fb6e82da702bf5609466b2024c1730e0fe394141c57e74f040c70c86868973bda7e7c0ffdb217
|
|
7
|
+
data.tar.gz: 23234c26f559a682406ccd9d0d0fad48b44340b1582b5545f3ac9eb857e494f59b77b8c2fb903730614bf39d0520f52fb7f03353120e0482e50a02426b71675a
|
data/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# plist_surgeon plugin
|
|
2
2
|
_The all-in-one tool to edit iOS configuration files from one plugin._
|
|
3
3
|
|
|
4
|
-
[](https://badge.fury.io/rb/fastlane-plugin-plist_surgeon)
|
|
5
5
|
|
|
6
6
|
## Getting Started
|
|
7
7
|
|
|
@@ -82,6 +82,40 @@ fastlane run plist_surgeon \
|
|
|
82
82
|
value:'{"CFBundlePrimaryIcon":{"CFBundleIconFiles":["AppIcon"]}}'
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
### Array Search Syntax
|
|
86
|
+
|
|
87
|
+
When dealing with arrays of dictionaries, you can use the `(key=value)` syntax to find the correct element instead of relying on a fixed index.
|
|
88
|
+
|
|
89
|
+
For example, given an `Info.plist` with:
|
|
90
|
+
|
|
91
|
+
```xml
|
|
92
|
+
<key>service_api_key</key>
|
|
93
|
+
<array>
|
|
94
|
+
<dict>
|
|
95
|
+
<key>test</key>
|
|
96
|
+
<string>other_app</string>
|
|
97
|
+
<key>live</key>
|
|
98
|
+
<string>old_key_1</string>
|
|
99
|
+
</dict>
|
|
100
|
+
<dict>
|
|
101
|
+
<key>test</key>
|
|
102
|
+
<string>my_app</string>
|
|
103
|
+
<key>live</key>
|
|
104
|
+
<string>old_key_2</string>
|
|
105
|
+
</dict>
|
|
106
|
+
</array>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
You can update the `live` key for the dictionary where `test` is `my_app`:
|
|
110
|
+
|
|
111
|
+
```rb
|
|
112
|
+
plist_surgeon(
|
|
113
|
+
path: "Info.plist",
|
|
114
|
+
key: "service_api_key.(test=my_app).live",
|
|
115
|
+
value: "new_live_key"
|
|
116
|
+
)
|
|
117
|
+
```
|
|
118
|
+
|
|
85
119
|
### Run tests for this plugin
|
|
86
120
|
|
|
87
121
|
To run both the tests, and code style validation, run
|
|
@@ -17,33 +17,86 @@ module Fastlane
|
|
|
17
17
|
set_value(plist, key, value)
|
|
18
18
|
plist_content = plist.to_plist
|
|
19
19
|
File.write(path, plist_content)
|
|
20
|
-
|
|
20
|
+
"#{File.basename(path)} saved with #{key} change."
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def self.set_value(plist, key, value)
|
|
24
|
+
value = parse_json_if_needed(value)
|
|
25
|
+
|
|
26
|
+
if plist.kind_of?(Hash) && plist.key?(key)
|
|
27
|
+
plist[key] = value
|
|
28
|
+
elsif key.include?('.')
|
|
29
|
+
set_nested_value(plist, key, value)
|
|
30
|
+
else
|
|
31
|
+
set_direct_value(plist, key, value)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.set_direct_value(plist, key, value)
|
|
36
|
+
if plist.kind_of?(Array)
|
|
37
|
+
index = find_index_for_key(plist, key)
|
|
38
|
+
plist[index] = value
|
|
39
|
+
else
|
|
40
|
+
plist[key] = value
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.find_index_for_key(array, key)
|
|
45
|
+
if key.start_with?('(') && key.end_with?(')')
|
|
46
|
+
search_term = key[1...-1]
|
|
47
|
+
search_key, search_value = search_term.split('=', 2)
|
|
48
|
+
if search_key && search_value
|
|
49
|
+
index = array.index { |item| item.kind_of?(Hash) && item[search_key].to_s == search_value }
|
|
50
|
+
return index if index
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
key.to_i
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.parse_json_if_needed(value)
|
|
24
57
|
if value.kind_of?(String) && value.start_with?('{', '[')
|
|
25
58
|
begin
|
|
26
59
|
require 'json'
|
|
27
|
-
|
|
60
|
+
return JSON.parse(value)
|
|
28
61
|
rescue JSON::ParserError
|
|
29
62
|
# Not JSON, keep as string
|
|
30
63
|
end
|
|
31
64
|
end
|
|
65
|
+
value
|
|
66
|
+
end
|
|
32
67
|
|
|
33
|
-
|
|
34
|
-
|
|
68
|
+
def self.set_nested_value(plist, key, value)
|
|
69
|
+
# Attempt to find if any prefix of the key exists as a literal key
|
|
70
|
+
parts = key.split('.')
|
|
71
|
+
prefix_key = find_prefix_key(plist, parts)
|
|
72
|
+
|
|
73
|
+
if prefix_key
|
|
74
|
+
prefix, remainder = prefix_key
|
|
75
|
+
set_value(plist[prefix], remainder, value)
|
|
35
76
|
else
|
|
36
|
-
plist
|
|
77
|
+
set_deep_nested_value(plist, parts, value)
|
|
37
78
|
end
|
|
38
79
|
end
|
|
39
80
|
|
|
40
|
-
def self.
|
|
41
|
-
|
|
81
|
+
def self.find_prefix_key(plist, parts)
|
|
82
|
+
return nil unless plist.kind_of?(Hash)
|
|
83
|
+
|
|
84
|
+
(parts.size - 1).downto(1) do |i|
|
|
85
|
+
prefix = parts[0...i].join('.')
|
|
86
|
+
if plist.key?(prefix)
|
|
87
|
+
return [prefix, parts[i..-1].join('.')]
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
nil
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def self.set_deep_nested_value(plist, keys, value)
|
|
42
94
|
last_key = keys.pop
|
|
43
95
|
current = plist
|
|
44
96
|
keys.each do |k|
|
|
45
97
|
if current.kind_of?(Array)
|
|
46
|
-
|
|
98
|
+
index = find_index_for_key(current, k)
|
|
99
|
+
current = current[index]
|
|
47
100
|
else
|
|
48
101
|
current[k] ||= {}
|
|
49
102
|
current = current[k]
|
|
@@ -51,7 +104,8 @@ module Fastlane
|
|
|
51
104
|
end
|
|
52
105
|
|
|
53
106
|
if current.kind_of?(Array)
|
|
54
|
-
|
|
107
|
+
index = find_index_for_key(current, last_key)
|
|
108
|
+
current[index] = value
|
|
55
109
|
else
|
|
56
110
|
current[last_key] = value
|
|
57
111
|
end
|