|
65 | 65 |
|
66 | 66 | generate = |
67 | 67 | name: value: |
68 | | - pkgs.runCommand name |
| 68 | + pkgs.callPackage ( |
69 | 69 | { |
70 | | - # Requirements |
71 | | - # ============ |
72 | | - # |
73 | | - # 1. Strings in Nix carry over to the same |
74 | | - # strings in Java => need proper escapes |
75 | | - # 2. Generate files quickly |
76 | | - # - A JVM would have to match the app's |
77 | | - # JVM to avoid build closure bloat |
78 | | - # - Even then, JVM startup would slow |
79 | | - # down config generation. |
80 | | - # |
81 | | - # |
82 | | - # Implementation |
83 | | - # ============== |
84 | | - # |
85 | | - # Escaping has two steps |
86 | | - # |
87 | | - # 1. jq |
88 | | - # Escape known separators, in order not |
89 | | - # to break up the keys and values. |
90 | | - # This handles typical whitespace correctly, |
91 | | - # but may produce garbage for other control |
92 | | - # characters. |
93 | | - # |
94 | | - # 2. iconv |
95 | | - # Escape >ascii code points to java escapes, |
96 | | - # as .properties files are supposed to be |
97 | | - # encoded in ISO 8859-1. It's an old format. |
98 | | - # UTF-8 behavior may exist in some apps and |
99 | | - # libraries, but we can't rely on this in |
100 | | - # general. |
| 70 | + runCommand, |
| 71 | + jq, |
| 72 | + libiconvReal, |
| 73 | + }: |
| 74 | + runCommand name |
| 75 | + { |
| 76 | + # Requirements |
| 77 | + # ============ |
| 78 | + # |
| 79 | + # 1. Strings in Nix carry over to the same |
| 80 | + # strings in Java => need proper escapes |
| 81 | + # 2. Generate files quickly |
| 82 | + # - A JVM would have to match the app's |
| 83 | + # JVM to avoid build closure bloat |
| 84 | + # - Even then, JVM startup would slow |
| 85 | + # down config generation. |
| 86 | + # |
| 87 | + # |
| 88 | + # Implementation |
| 89 | + # ============== |
| 90 | + # |
| 91 | + # Escaping has two steps |
| 92 | + # |
| 93 | + # 1. jq |
| 94 | + # Escape known separators, in order not |
| 95 | + # to break up the keys and values. |
| 96 | + # This handles typical whitespace correctly, |
| 97 | + # but may produce garbage for other control |
| 98 | + # characters. |
| 99 | + # |
| 100 | + # 2. iconv |
| 101 | + # Escape >ascii code points to java escapes, |
| 102 | + # as .properties files are supposed to be |
| 103 | + # encoded in ISO 8859-1. It's an old format. |
| 104 | + # UTF-8 behavior may exist in some apps and |
| 105 | + # libraries, but we can't rely on this in |
| 106 | + # general. |
101 | 107 |
|
102 | | - preferLocalBuild = true; |
103 | | - passAsFile = [ "value" ]; |
104 | | - value = builtins.toJSON value; |
105 | | - nativeBuildInputs = [ |
106 | | - pkgs.jq |
107 | | - pkgs.libiconvReal |
108 | | - ]; |
| 108 | + preferLocalBuild = true; |
| 109 | + passAsFile = [ "value" ]; |
| 110 | + value = builtins.toJSON value; |
| 111 | + nativeBuildInputs = [ |
| 112 | + jq |
| 113 | + libiconvReal |
| 114 | + ]; |
109 | 115 |
|
110 | | - jqCode = |
111 | | - let |
112 | | - main = '' |
113 | | - to_entries |
114 | | - | .[] |
115 | | - | "\( |
116 | | - .key |
117 | | - | ${commonEscapes} |
118 | | - | gsub(" "; "\\ ") |
119 | | - | gsub("="; "\\=") |
120 | | - ) = \( |
121 | | - .value |
122 | | - | ${commonEscapes} |
123 | | - | gsub("^ "; "\\ ") |
124 | | - | gsub("\\n "; "\n\\ ") |
125 | | - )" |
126 | | - ''; |
127 | | - # Most escapes are equal for both keys and values. |
128 | | - commonEscapes = '' |
129 | | - gsub("\\\\"; "\\\\") |
130 | | - | gsub("\\n"; "\\n\\\n") |
131 | | - | gsub("#"; "\\#") |
132 | | - | gsub("!"; "\\!") |
133 | | - | gsub("\\t"; "\\t") |
134 | | - | gsub("\r"; "\\r") |
135 | | - ''; |
136 | | - in |
137 | | - main; |
| 116 | + jqCode = |
| 117 | + let |
| 118 | + main = '' |
| 119 | + to_entries |
| 120 | + | .[] |
| 121 | + | "\( |
| 122 | + .key |
| 123 | + | ${commonEscapes} |
| 124 | + | gsub(" "; "\\ ") |
| 125 | + | gsub("="; "\\=") |
| 126 | + ) = \( |
| 127 | + .value |
| 128 | + | ${commonEscapes} |
| 129 | + | gsub("^ "; "\\ ") |
| 130 | + | gsub("\\n "; "\n\\ ") |
| 131 | + )" |
| 132 | + ''; |
| 133 | + # Most escapes are equal for both keys and values. |
| 134 | + commonEscapes = '' |
| 135 | + gsub("\\\\"; "\\\\") |
| 136 | + | gsub("\\n"; "\\n\\\n") |
| 137 | + | gsub("#"; "\\#") |
| 138 | + | gsub("!"; "\\!") |
| 139 | + | gsub("\\t"; "\\t") |
| 140 | + | gsub("\r"; "\\r") |
| 141 | + ''; |
| 142 | + in |
| 143 | + main; |
138 | 144 |
|
139 | | - inputEncoding = "UTF-8"; |
| 145 | + inputEncoding = "UTF-8"; |
140 | 146 |
|
141 | | - inherit comment; |
| 147 | + inherit comment; |
142 | 148 |
|
143 | | - } |
144 | | - '' |
145 | | - ( |
146 | | - echo "$comment" | while read -r ln; do echo "# $ln"; done |
147 | | - echo |
148 | | - jq -r --arg hash '#' "$jqCode" "$valuePath" \ |
149 | | - | iconv --from-code "$inputEncoding" --to-code JAVA \ |
150 | | - ) > "$out" |
151 | | - ''; |
| 149 | + } |
| 150 | + '' |
| 151 | + ( |
| 152 | + echo "$comment" | while read -r ln; do echo "# $ln"; done |
| 153 | + echo |
| 154 | + jq -r --arg hash '#' "$jqCode" "$valuePath" \ |
| 155 | + | iconv --from-code "$inputEncoding" --to-code JAVA \ |
| 156 | + ) > "$out" |
| 157 | + '' |
| 158 | + ) { }; |
152 | 159 | }; |
153 | 160 | } |
0 commit comments