2020-06-24 22:02:05 +00:00
import * as core from '@actions/core' ;
import * as semver from 'semver' ;
export default class AndroidVersioning {
2023-03-04 00:25:40 +00:00
static determineVersionCode ( version : string , inputVersionCode : string ) : string {
if ( inputVersionCode === '' ) {
2020-06-24 22:02:05 +00:00
return AndroidVersioning . versionToVersionCode ( version ) ;
}
2022-04-11 22:43:41 +00:00
2020-06-24 22:02:05 +00:00
return inputVersionCode ;
}
2023-03-04 00:25:40 +00:00
static versionToVersionCode ( version : string ) : string {
2021-11-15 17:12:37 +00:00
if ( version === 'none' ) {
core . info ( ` Versioning strategy is set to ${ version } , so android version code should not be applied. ` ) ;
2022-04-11 22:43:41 +00:00
2023-03-04 00:25:40 +00:00
return '0' ;
2021-11-15 17:12:37 +00:00
}
2020-06-24 22:02:05 +00:00
const parsedVersion = semver . parse ( version ) ;
if ( ! parsedVersion ) {
core . warning ( ` Could not parse " ${ version } " to semver, defaulting android version code to 1 ` ) ;
2022-04-11 22:43:41 +00:00
2023-03-04 00:25:40 +00:00
return '1' ;
2020-06-24 22:02:05 +00:00
}
// The greatest value Google Plays allows is 2100000000.
// Allow for 3 patch digits, 3 minor digits and 3 major digits.
2021-03-13 23:44:01 +00:00
const versionCode = parsedVersion . major * 1000000 + parsedVersion . minor * 1000 + parsedVersion . patch ;
2020-06-24 22:02:05 +00:00
2021-11-15 17:12:37 +00:00
if ( versionCode >= 2050000000 ) {
2020-06-24 22:02:05 +00:00
throw new Error (
` Generated versionCode ${ versionCode } is dangerously close to the maximum allowed number 2100000000. Consider a different versioning scheme to be able to continue updating your application. ` ,
) ;
}
core . info ( ` Using android versionCode ${ versionCode } ` ) ;
2022-04-11 22:43:41 +00:00
2023-03-04 00:25:40 +00:00
return versionCode . toString ( ) ;
2020-06-24 22:02:05 +00:00
}
2021-11-24 12:51:52 +00:00
2023-03-04 00:25:40 +00:00
static determineSdkManagerParameters ( targetSdkVersion : string ) {
2021-11-24 12:51:52 +00:00
const parsedVersion = Number . parseInt ( targetSdkVersion . slice ( - 2 ) , 10 ) ;
2022-04-11 22:43:41 +00:00
2021-11-24 12:51:52 +00:00
return Number . isNaN ( parsedVersion ) ? '' : ` platforms;android- ${ parsedVersion } ` ;
}
2020-06-24 22:02:05 +00:00
}