( content: string, distributionName: string, versionFile: string )
| 128 | } |
| 129 | |
| 130 | export function getVersionFromFileContent( |
| 131 | content: string, |
| 132 | distributionName: string, |
| 133 | versionFile: string |
| 134 | ): string | null { |
| 135 | let javaVersionRegExp: RegExp; |
| 136 | |
| 137 | function getFileName(versionFile: string) { |
| 138 | return path.basename(versionFile); |
| 139 | } |
| 140 | |
| 141 | const versionFileName = getFileName(versionFile); |
| 142 | if (versionFileName == '.tool-versions') { |
| 143 | javaVersionRegExp = |
| 144 | /^java\s+(?:\S*-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im; |
| 145 | } else if (versionFileName == '.sdkmanrc') { |
| 146 | javaVersionRegExp = /^java\s*=\s*(?<version>[^-]+)/m; |
| 147 | } else { |
| 148 | javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/; |
| 149 | } |
| 150 | |
| 151 | const capturedVersion = content.match(javaVersionRegExp)?.groups?.version |
| 152 | ? (content.match(javaVersionRegExp)?.groups?.version as string) |
| 153 | : ''; |
| 154 | |
| 155 | core.debug( |
| 156 | `Parsed version '${capturedVersion}' from file '${versionFileName}'` |
| 157 | ); |
| 158 | if (!capturedVersion) { |
| 159 | return null; |
| 160 | } |
| 161 | |
| 162 | const tentativeVersion = avoidOldNotation(capturedVersion); |
| 163 | const rawVersion = tentativeVersion.split('-')[0]; |
| 164 | |
| 165 | let version = semver.validRange(rawVersion) |
| 166 | ? tentativeVersion |
| 167 | : semver.coerce(tentativeVersion); |
| 168 | |
| 169 | core.debug(`Range version from file is '${version}'`); |
| 170 | |
| 171 | if (!version) { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | if (DISTRIBUTIONS_ONLY_MAJOR_VERSION.includes(distributionName)) { |
| 176 | const coerceVersion = semver.coerce(version) ?? version; |
| 177 | version = semver.major(coerceVersion).toString(); |
| 178 | } |
| 179 | |
| 180 | return version.toString(); |
| 181 | } |
| 182 | |
| 183 | // By convention, action expects version 8 in the format `8.*` instead of `1.8` |
| 184 | function avoidOldNotation(content: string): string { |
no test coverage detected