1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ndkports 18 19 import java.io.File 20 21 /** 22 * A version number that is compatible with CMake's package version format. 23 * 24 * https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#package-version-file 25 * 26 * CMake package versions *must* be numeric with a maximum of four dot separated 27 * components. 28 */ 29 data class CMakeCompatibleVersion( 30 val major: Int, 31 val minor: Int?, 32 val patch: Int?, 33 val tweak: Int? 34 ) { 35 init { 36 if (tweak != null) { 37 require(patch != null) 38 } 39 40 if (patch != null) { 41 require(minor != null) 42 } 43 } 44 toStringnull45 override fun toString(): String = 46 listOfNotNull(major, minor, patch, tweak).joinToString(".") 47 48 companion object { 49 private val versionRegex = Regex( 50 """^(\d+)(?:\.(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?$""" 51 ) 52 53 fun parse(versionString: String): CMakeCompatibleVersion { 54 val match = versionRegex.find(versionString) 55 require(match != null) { 56 "$versionString is not in major[.minor[.patch[.tweak]]] format" 57 } 58 return CMakeCompatibleVersion( 59 match.groups[1]!!.value.toInt(), 60 match.groups[2]?.value?.toInt(), 61 match.groups[3]?.value?.toInt(), 62 match.groups[4]?.value?.toInt() 63 ) 64 } 65 } 66 }