Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/changelog/pre_commit_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function latestRelease() {

async function latestDevRelease() {
const buildEntries = await fs.readdir(SWIFT_ORG_BUILD, { withFileTypes: true });
const devBranchRegex = /swift-(.*)-branch/;
const devBranchRegex = /swift-([^-]*)-branch/;
const devDirs = buildEntries.flatMap(entry => {
if (!entry.isDirectory() || !devBranchRegex.exec(entry.name)) {
return [];
Expand Down
2 changes: 1 addition & 1 deletion __tests__/snapshot/xcode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('fetch macos tool data based on options', () => {

function getVersions(folders: string[], suffix: string) {
return folders.flatMap(folder => {
const match = folder.match(`swift-([0-9_]+)-${suffix}`)
const match = folder.match(`swift-([^-]*)-${suffix}`)
if (match?.length) {
return match[1]
}
Expand Down
40 changes: 31 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/installer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {getExecOutput} from '@actions/exec'
import * as cache from '@actions/cache'
import * as toolCache from '@actions/tool-cache'
import {coerce as parseSemVer} from 'semver'
import {SWIFT_BRANCH_REGEX} from '../version'
import {ToolchainSnapshot} from '../snapshot'

export type SnapshotForInstaller<Installer> =
Expand All @@ -16,7 +17,7 @@ export abstract class ToolchainInstaller<Snapshot extends ToolchainSnapshot> {
constructor(readonly data: Snapshot) {}

protected get version() {
const match = /swift-(.*)-/.exec(this.data.branch)
const match = SWIFT_BRANCH_REGEX.exec(this.data.branch)
return match && match.length > 1 ? parseSemVer(match[1]) : undefined
}

Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import {ToolchainVersion} from './version'
import {ToolchainVersion, SWIFT_BRANCH_REGEX} from './version'
import {Swiftorg} from './swiftorg'
import {ToolchainSnapshot} from './snapshot'
import {Platform} from './platform'
Expand All @@ -26,7 +26,7 @@ export async function run() {
} else {
throw new Error(`No Swift toolchain found for ${version}`)
}
const match = /swift-(.*)-/.exec(toolchain.branch)
const match = SWIFT_BRANCH_REGEX.exec(toolchain.branch)
if (match && match.length > 1) {
installedVersion = match[1]
} else {
Expand Down
31 changes: 20 additions & 11 deletions src/platform/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as path from 'path'
import {promises as fs} from 'fs'
import * as core from '@actions/core'
import * as yaml from 'js-yaml'
import {ToolchainVersion} from '../version'
import semver from 'semver'
import {ToolchainVersion, SWIFT_BRANCH_REGEX} from '../version'
import {ToolchainSnapshot, SwiftRelease} from '../snapshot'
import {ToolchainInstaller, SnapshotForInstaller} from '../installer'
import {MODULE_DIR} from '../const'
Expand Down Expand Up @@ -32,6 +33,22 @@ export abstract class Platform<
return yaml.load(data) as SwiftRelease[]
}

private sortSnapshots(snapshots: SnapshotForInstaller<Installer>[]) {
return snapshots.sort((item1, item2) => {
const t1 = item1 as ToolchainSnapshot
const t2 = item2 as ToolchainSnapshot
const ver1 = semver.coerce(SWIFT_BRANCH_REGEX.exec(t1.branch)?.[0])
const ver2 = semver.coerce(SWIFT_BRANCH_REGEX.exec(t2.branch)?.[0])
if (ver1 && ver2) {
const comparison = semver.compare(ver2, ver1)
if (comparison !== 0) {
return comparison
}
}
return t2.date.getTime() - t1.date.getTime()
})
}

protected abstract releasedTools(
version: ToolchainVersion
): Promise<SnapshotForInstaller<Installer>[]>
Expand All @@ -41,11 +58,7 @@ export abstract class Platform<
): Promise<SnapshotForInstaller<Installer>[]> {
const snapshots = await this.releasedTools(version)
if (snapshots.length && !version.dev) {
return snapshots.sort(
(item1, item2) =>
(item2 as ToolchainSnapshot).date.getTime() -
(item1 as ToolchainSnapshot).date.getTime()
)
return this.sortSnapshots(snapshots)
}
const files = await this.toolFiles(version)
core.debug(`Using files "${files}" to get toolchains snapshot data`)
Expand Down Expand Up @@ -76,11 +89,7 @@ export abstract class Platform<
})
.filter(item => version.satisfiedBy((item as ToolchainSnapshot).dir))
snapshots.push(...devSnapshots)
return snapshots.sort(
(item1, item2) =>
(item2 as ToolchainSnapshot).date.getTime() -
(item1 as ToolchainSnapshot).date.getTime()
)
return this.sortSnapshots(snapshots)
}

abstract install(
Expand Down
4 changes: 2 additions & 2 deletions src/platform/linux.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path'
import {promises as fs} from 'fs'
import {VersionedPlatform} from './versioned'
import {ToolchainVersion} from '../version'
import {ToolchainVersion, SWIFT_BRANCH_REGEX} from '../version'
import {LinuxToolchainSnapshot} from '../snapshot'
import {LinuxToolchainInstaller} from '../installer'
import {MODULE_DIR} from '../const'
Expand All @@ -26,7 +26,7 @@ export class LinuxPlatform extends VersionedPlatform<LinuxToolchainInstaller> {
return tool
}
let headingPattern: RegExp
const match = /swift-(.*)-/.exec(tool.branch)
const match = SWIFT_BRANCH_REGEX.exec(tool.branch)
if (match && match.length > 1) {
const ver = match[1]
headingPattern = new RegExp(`Swift ${ver}`, 'g')
Expand Down
3 changes: 2 additions & 1 deletion src/version/name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {coerce as parseSemVer} from 'semver'
import {ToolchainVersion} from './base'

export const DEVELOPMENT_SNAPSHOT = 'DEVELOPMENT-SNAPSHOT'
export const SWIFT_BRANCH_REGEX = /swift-([^-]*)-.*/

export class ToolchainSnapshotName extends ToolchainVersion {
constructor(readonly name: string) {
Expand All @@ -16,7 +17,7 @@ export class ToolchainSnapshotName extends ToolchainVersion {
}

private get version() {
const match = /swift-([^-]*)-/.exec(this.dir)
const match = SWIFT_BRANCH_REGEX.exec(this.dir)
if (!match || match.length < 2 || !parseSemVer(match[1])) {
return
}
Expand Down