Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ninja 1.12.1 support, including improved platform detection #29

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:

strategy:
matrix:
image: [ 'windows-latest', 'ubuntu-latest', 'macos-latest' ]
version: [ '1.9.0', '1.10.2', '1.11.1' ]
image: [ 'windows-latest', 'ubuntu-latest', 'ubuntu-24.04-arm', 'macos-latest' ]
version: [ '1.9.0', '1.10.2', '1.11.1', '1.12.1' ]

steps:
- uses: actions/checkout@master
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Supports Windows, Linux, and macOS.

Inputs:

- `version`: Version of ninja to install (default: 1.11.1)
- `version`: Version of ninja to install (default: 1.12.1)
- `platform`: Override platform detection logic
- `destination`: Target directory for download, added to `PATH`
(default: `${GITHUB_WORKSPACE}/ninja-build`)
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ branding:
inputs:
version:
description: 'Version of ninja-build to install'
default: '1.11.1'
default: '1.12.1'
required: true
platform:
description: 'Override default platform with one of [win, mac, linux]'
description: 'Override default platform with one of [win, mac, linux, winarm64, linux-aarch64]'
required: false
destination:
description: 'Destination directory, will be added to PATH'
Expand Down
205 changes: 122 additions & 83 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31424,89 +31424,128 @@ module.exports = parseParams
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const core = __nccwpck_require__(2186)
const process = __nccwpck_require__(7282)
const spawn = (__nccwpck_require__(2081).spawnSync)
const path = __nccwpck_require__(1017)
const fs = __nccwpck_require__(7147)
const URL = (__nccwpck_require__(7310).URL)
const { https } = __nccwpck_require__(7707)
const AdmZip = __nccwpck_require__(6761)
const HttpsProxyAgent = __nccwpck_require__(7219)

const selectPlatforn = (platform) =>
platform ? [null, platform] :
process.platform === 'win32' ? [null, 'win'] :
process.platform === 'darwin' ? [null, 'mac'] :
process.platform === 'linux' ? [null, 'linux'] :
[new Error(`Unsupported platform '${process.platform}'`), '']

try {
const version = core.getInput('version', {required: true})
const destDir = core.getInput('destination') || 'ninja-build'
const proxyServer = core.getInput('http_proxy')

const [error, platform] = selectPlatforn(core.getInput('platform'));
if (error) throw error

const url = new URL(`https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${platform}.zip`)

if (proxyServer) {
console.log(`using proxy ${proxyServer}`)
url.agent = new HttpsProxyAgent(proxyServer)
}

console.log(`downloading ${url}`)
const request = https.get(url, {followAllRedirects: true}, result => {
const data = []

result.on('data', chunk => data.push(chunk))

result.on('end', () => {
const length = data.reduce((len, chunk) => len + chunk.length, 0)
const buffer = Buffer.alloc(length)

data.reduce((pos, chunk) => {
chunk.copy(buffer, pos)
return pos + chunk.length
}, 0)

const zip = new AdmZip(buffer)
const entry = zip.getEntries()[0]
const ninjaName = entry.entryName

const fullDestDir = path.resolve(process.cwd(), destDir)
if (!fs.existsSync(fullDestDir)) fs.mkdirSync(fullDestDir, {recursive: true})

zip.extractEntryTo(ninjaName, fullDestDir, /*maintainEntryPath*/false, /*overwrite*/true)

const fullFileDir = path.join(fullDestDir, ninjaName)
if (!fs.existsSync(fullFileDir)) throw new Error(`failed to extract to '${fullFileDir}'`)

fs.chmodSync(fullFileDir, '755')

console.log(`extracted '${ninjaName}' to '${fullFileDir}'`)

core.addPath(fullDestDir)
console.log(`added '${fullDestDir}' to PATH`)

const result = spawn(ninjaName, ['--version'], {encoding: 'utf8'})
if (result.error) throw error

const installedVersion = result.stdout.trim()

console.log(`$ ${ninjaName} --version`)
console.log(installedVersion)

if (installedVersion != version) {
throw new Error('incorrect version detected (bad PATH configuration?)')
}
})
})
request.on('error', error => { throw error })
} catch (error) {
core.setFailed(error.message)
}
const core = __nccwpck_require__(2186)
const process = __nccwpck_require__(7282)
const spawn = (__nccwpck_require__(2081).spawnSync)
const path = __nccwpck_require__(1017)
const fs = __nccwpck_require__(7147)
const URL = (__nccwpck_require__(7310).URL)
const { https } = __nccwpck_require__(7707)
const AdmZip = __nccwpck_require__(6761)
const HttpsProxyAgent = __nccwpck_require__(7219)

function selectPlatform(platform, version) {
if (platform) {
return [null, platform]
}

let major, minor, patch = version.split('.').map((s) => parseInt(s))
if (process.platform === 'win32') {
if (process.arch === 'arm64') {
if (major < 1 || major == 1 && minor < 12) {
return [new Error(`Windows ARM builds are only available for 1.12.0 and later`), '']
}
else {
return [null, 'winarm64']
}
}
else if (process.arch === 'x64') {
return [null, 'win']
}
else {
return [new Error(`Unsupported architecture '${process.arch}'`), '']
}
}
else if (process.platform === 'linux') {
if (process.arch === 'arm64') {
if (major < 1 || major == 1 && minor < 12) {
return [new Error(`Linux ARM builds are only available for 1.12.0 and later`), '']
}
else {
return [null, 'linux-aarch64']
}
}
else if (process.arch === 'x64') {
return [null, 'linux']
}
else {
return [new Error(`Unsupported architecture '${process.arch}'`), '']
}
}
else if (process.platform === 'darwin') {
return [null, 'mac']
}
else {
return [new Error(`Unsupported platform '${process.platform}'`), '']
}
}

try {
const version = core.getInput('version', {required: true})
const destDir = core.getInput('destination') || 'ninja-build'
const proxyServer = core.getInput('http_proxy')

const [error, platform] = selectPlatform(core.getInput('platform'), version)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from the above function, this is the only changed line

if (error) throw error

const url = new URL(`https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${platform}.zip`)

if (proxyServer) {
console.log(`using proxy ${proxyServer}`)
url.agent = new HttpsProxyAgent(proxyServer)
}

console.log(`downloading ${url}`)
const request = https.get(url, {followAllRedirects: true}, result => {
const data = []

result.on('data', chunk => data.push(chunk))

result.on('end', () => {
const length = data.reduce((len, chunk) => len + chunk.length, 0)
const buffer = Buffer.alloc(length)

data.reduce((pos, chunk) => {
chunk.copy(buffer, pos)
return pos + chunk.length
}, 0)

const zip = new AdmZip(buffer)
const entry = zip.getEntries()[0]
const ninjaName = entry.entryName

const fullDestDir = path.resolve(process.cwd(), destDir)
if (!fs.existsSync(fullDestDir)) fs.mkdirSync(fullDestDir, {recursive: true})

zip.extractEntryTo(ninjaName, fullDestDir, /*maintainEntryPath*/false, /*overwrite*/true)

const fullFileDir = path.join(fullDestDir, ninjaName)
if (!fs.existsSync(fullFileDir)) throw new Error(`failed to extract to '${fullFileDir}'`)

fs.chmodSync(fullFileDir, '755')

console.log(`extracted '${ninjaName}' to '${fullFileDir}'`)

core.addPath(fullDestDir)
console.log(`added '${fullDestDir}' to PATH`)

const result = spawn(ninjaName, ['--version'], {encoding: 'utf8'})
if (result.error) throw error

const installedVersion = result.stdout.trim()

console.log(`$ ${ninjaName} --version`)
console.log(installedVersion)

if (installedVersion != version) {
throw new Error('incorrect version detected (bad PATH configuration?)')
}
})
})
request.on('error', error => { throw error })
} catch (error) {
core.setFailed(error.message)
}

})();

Expand Down
53 changes: 46 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,58 @@ const { https } = require('follow-redirects')
const AdmZip = require('adm-zip')
const HttpsProxyAgent = require('https-proxy-agent')

const selectPlatforn = (platform) =>
platform ? [null, platform] :
process.platform === 'win32' ? [null, 'win'] :
process.platform === 'darwin' ? [null, 'mac'] :
process.platform === 'linux' ? [null, 'linux'] :
[new Error(`Unsupported platform '${process.platform}'`), '']
function selectPlatform(platform, version) {
if (platform) {
return [null, platform]
}

let major, minor, patch = version.split('.').map((s) => parseInt(s))
if (process.platform === 'win32') {
if (process.arch === 'arm64') {
if (major < 1 || major == 1 && minor < 12) {
return [new Error(`Windows ARM builds are only available for 1.12.0 and later`), '']
}
else {
return [null, 'winarm64']
}
}
else if (process.arch === 'x64') {
return [null, 'win']
}
else {
return [new Error(`Unsupported architecture '${process.arch}'`), '']
}
}
else if (process.platform === 'linux') {
if (process.arch === 'arm64') {
if (major < 1 || major == 1 && minor < 12) {
return [new Error(`Linux ARM builds are only available for 1.12.0 and later`), '']
}
else {
return [null, 'linux-aarch64']
}
}
else if (process.arch === 'x64') {
return [null, 'linux']
}
else {
return [new Error(`Unsupported architecture '${process.arch}'`), '']
}
}
else if (process.platform === 'darwin') {
return [null, 'mac']
}
else {
return [new Error(`Unsupported platform '${process.platform}'`), '']
}
}

try {
const version = core.getInput('version', {required: true})
const destDir = core.getInput('destination') || 'ninja-build'
const proxyServer = core.getInput('http_proxy')

const [error, platform] = selectPlatforn(core.getInput('platform'));
const [error, platform] = selectPlatform(core.getInput('platform'), version)
if (error) throw error

const url = new URL(`https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${platform}.zip`)
Expand Down