Skip to content

Commit 8b4a245

Browse files
authored
feat: added support for multiple Linux distros (#4)
1 parent 86e007c commit 8b4a245

File tree

6 files changed

+95
-13
lines changed

6 files changed

+95
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Setup Swift
22

33
[![GitHub Action](https://img.shields.io/github/v/tag/SwiftyLab/setup-swift?logo=github&label=GitHub)](https://badge.fury.io/gh/SwiftyLab%2Fsetup-swift)
4-
[![Supports macOS, Ubuntu & Windows](https://img.shields.io/badge/platform-macOS%20%7C%20Ubuntu%20%7C%20Windows-blue?label=platform)](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources)
4+
[![Supports macOS, Linux & Windows](https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-blue?label=platform)](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources)
55
[![Swift 5.9](https://img.shields.io/badge/Swift-5.9-orange?logo=swift&logoColor=white)](https://swift.org)
66
[![CI/CD](https://github.com/SwiftyLab/setup-swift/actions/workflows/main.yml/badge.svg)](https://github.com/SwiftyLab/setup-swift/actions/workflows/main.yml)
77
[![CodeFactor](https://www.codefactor.io/repository/github/swiftylab/setup-swift/badge)](https://www.codefactor.io/repository/github/swiftylab/setup-swift)
88
[![codecov](https://codecov.io/gh/SwiftyLab/setup-swift/graph/badge.svg?token=XWfSpWQ6gC)](https://codecov.io/gh/SwiftyLab/setup-swift)
99

1010
![swift.org](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2FSwiftyLab%2Fsetup-swift%2Fmain%2Fpackage.json&query=%24.swiftorg&logo=swift&logoColor=white&label=swift.org)
1111

12-
[GitHub Action](https://github.com/features/actions) that will setup [Swift](https://swift.org) environment with specified version. Works on both Ubuntu and macOS runners with limited support on Windows runners.
12+
[GitHub Action](https://github.com/features/actions) that will setup [Swift](https://swift.org) environment with specified version. Works on both Linux and macOS runners with limited support on Windows runners.
1313

1414
## Usage
1515

__tests__/platform.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ describe('platform detection', () => {
5252
expect(platform.file).toBe('ubuntu1804-aarch64')
5353
})
5454

55+
it('detects centos', async () => {
56+
setos({os: 'linux', dist: 'CentOS', release: '7'})
57+
jest.spyOn(os, 'arch').mockReturnValue('x64')
58+
const platform = await Platform.currentPlatform()
59+
expect(platform.name).toBe('centos')
60+
expect(platform).toBeInstanceOf(LinuxPlatform)
61+
expect((platform as LinuxPlatform).version).toBe(7)
62+
expect(platform.arch).toBe('x86_64')
63+
expect(platform.file).toBe('centos7')
64+
})
65+
5566
it('detects windows', async () => {
5667
setos({os: 'win32', dist: 'Windows', release: '10.0.17063'})
5768
jest.spyOn(os, 'arch').mockReturnValue('x64')

__tests__/snapshot.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ describe('fetch tool data based on options', () => {
145145
}
146146
})
147147

148-
// Ubuntu tests
148+
// Linux tests
149149

150150
it('fetches ubuntu 18.04 latest swift 4 tool', async () => {
151151
setos({os: 'linux', dist: 'Ubuntu', release: '18.04'})
@@ -297,6 +297,20 @@ describe('fetch tool data based on options', () => {
297297
expect(lTool.docker).toBe('5.2.5-focal')
298298
})
299299

300+
it('fetches centos 7 latest swift tool', async () => {
301+
setos({os: 'linux', dist: 'CentOS', release: '7'})
302+
jest.spyOn(os, 'arch').mockReturnValue('x64')
303+
const tool = await Platform.toolchain(latest)
304+
expect(tool).toBeTruthy()
305+
const lTool = tool as LinuxToolchainSnapshot
306+
expect(lTool.download).toBeTruthy()
307+
expect(lTool.dir).toBeTruthy()
308+
expect(lTool.platform).toBeTruthy()
309+
expect(lTool.branch).toBeTruthy()
310+
expect(lTool.download_signature).toBeTruthy()
311+
expect(lTool.docker).toBeTruthy()
312+
})
313+
300314
// Windows tests
301315

302316
it('fetches windows 10 latest swift 5 tool', async () => {

dist/index.js

Lines changed: 43 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/installer/linux.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as path from 'path'
22
import {promises as fs} from 'fs'
33
import * as core from '@actions/core'
4-
import {exec} from '@actions/exec'
54
import * as toolCache from '@actions/tool-cache'
65
import {VerifyingToolchainInstaller} from './verify'
76
import {LinuxToolchainSnapshot} from '../snapshot'
7+
import {PackageManager} from './package_manager'
88
import {MODULE_DIR} from '../const'
99

1010
export class LinuxToolchainInstaller extends VerifyingToolchainInstaller<LinuxToolchainSnapshot> {
@@ -29,16 +29,15 @@ export class LinuxToolchainInstaller extends VerifyingToolchainInstaller<LinuxTo
2929
.trim()
3030
.replace(/^\\+|\\+$|^\$+|\$+$/g, '')
3131
.trim()
32-
return command.length ? command : []
32+
return command.length ? command.split(' ') : []
3333
})
3434
if (!commands.length) {
3535
core.debug(`Skipping dependencies install as no commands in "${content}"`)
3636
return
3737
}
3838
try {
39-
// TODO: make update generalized accross linux platforms
40-
await exec('sudo', ['apt-get', 'update'])
41-
await exec('sudo', [...commands, '-y'])
39+
const packageManager = new PackageManager(commands)
40+
await packageManager.install()
4241
} catch (error) {
4342
core.debug(`Dependencies installation failed with "${error}"`)
4443
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {exec} from '@actions/exec'
2+
3+
export class PackageManager {
4+
readonly name: string
5+
readonly installationCommands: string[]
6+
7+
constructor(installationCommands: string[]) {
8+
this.name = installationCommands[0]
9+
this.installationCommands = installationCommands
10+
}
11+
12+
protected async update() {
13+
await exec('sudo', [this.name, 'update'])
14+
}
15+
16+
async install() {
17+
await this.update()
18+
await exec('sudo', [...this.installationCommands, '-y'])
19+
}
20+
}

0 commit comments

Comments
 (0)