@@ -3,6 +3,8 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3
3
import java.net.URL
4
4
import java.util.Locale
5
5
import javax.xml.parsers.DocumentBuilderFactory
6
+ import java.io.ByteArrayOutputStream
7
+ import java.io.PrintWriter
6
8
7
9
plugins {
8
10
kotlin(" multiplatform" )
@@ -275,7 +277,8 @@ tasks {
275
277
276
278
val downloadWindowsZonesMapping by tasks.registering {
277
279
description = " Updates the mapping between Windows-specific and usual names for timezones"
278
- val output = " $projectDir /nativeMain/cinterop/public/windows_zones.hpp"
280
+ val output = " $projectDir /native/cinterop/public/windows_zones.hpp"
281
+ val initialFileContents = File (output).readBytes()
279
282
outputs.file(output)
280
283
doLast {
281
284
val documentBuilderFactory = DocumentBuilderFactory .newInstance()
@@ -302,17 +305,19 @@ val downloadWindowsZonesMapping by tasks.registering {
302
305
}
303
306
}
304
307
}
305
- File (output).printWriter().use { out ->
308
+ val sortedMapping = mapping.toSortedMap()
309
+ val bos = ByteArrayOutputStream ()
310
+ PrintWriter (bos).use { out ->
306
311
out .println (""" // generated with gradle task `$name `""" )
307
312
out .println (""" #include <unordered_map>""" )
308
313
out .println (""" #include <string>""" )
309
314
out .println (""" static const std::unordered_map<std::string, std::string> standard_to_windows = {""" )
310
- for ((usualName, windowsName) in mapping ) {
315
+ for ((usualName, windowsName) in sortedMapping ) {
311
316
out .println (" \t { \" $usualName \" , \" $windowsName \" }," )
312
317
}
313
318
out .println (" };" )
314
319
out .println (""" static const std::unordered_map<std::string, std::string> windows_to_standard = {""" )
315
- val reverseMap = mutableMapOf <String , String >()
320
+ val reverseMap = sortedMapOf <String , String >()
316
321
for ((usualName, windowsName) in mapping) {
317
322
if (reverseMap[windowsName] == null ) {
318
323
reverseMap[windowsName] = usualName
@@ -324,11 +329,17 @@ val downloadWindowsZonesMapping by tasks.registering {
324
329
out .println (" };" )
325
330
out .println (""" static const std::unordered_map<std::string, size_t> zone_ids = {""" )
326
331
var i = 0
327
- for ((usualName, windowsName) in mapping ) {
332
+ for ((usualName, windowsName) in sortedMapping ) {
328
333
out .println (" \t { \" $usualName \" , $i }," )
329
334
++ i
330
335
}
331
336
out .println (" };" )
332
337
}
338
+ val newFileContents = bos.toByteArray()
339
+ if (! (initialFileContents contentEquals newFileContents)) {
340
+ File (output).writeBytes(newFileContents)
341
+ throw GradleException (" The mappings between Windows and IANA timezone names changed. " +
342
+ " The new mappings were written to the filesystem." )
343
+ }
333
344
}
334
345
}
0 commit comments