-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathhomebrew.js
More file actions
executable file
·140 lines (111 loc) · 4.93 KB
/
homebrew.js
File metadata and controls
executable file
·140 lines (111 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import crypto from 'node:crypto'
import fs from 'node:fs'
import path from 'node:path'
import {pipeline} from 'node:stream'
import {fileURLToPath} from 'node:url'
import {promisify} from 'node:util'
import {rimrafSync} from 'rimraf'
import getHerokuS3Bucket from '../utils/get-heroku-s3-bucket.js'
import isStableRelease from '../utils/is-stable-release.js'
import {run, shell, x} from '../utils/script-exec.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const {GITHUB_REF_NAME, GITHUB_REF_TYPE, GITHUB_SHA_SHORT} = process.env
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'package.json'), 'utf8'))
const VERSION = packageJson.version
if (!isStableRelease(GITHUB_REF_TYPE, GITHUB_REF_NAME)) {
console.log('Not on stable release; skipping releasing homebrew')
// eslint-disable-next-line n/no-process-exit, unicorn/no-process-exit
process.exit(0)
}
let HEROKU_S3_BUCKET
async function calculateSHA256(fileName) {
const hash = crypto.createHash('sha256')
hash.setEncoding('hex')
await promisify(pipeline)(fs.createReadStream(fileName), hash)
return hash.read()
}
const ROOT = path.join(__dirname, 'homebrew')
const TEMPLATES = path.join(ROOT, 'templates')
const fileSuffix = '.tar.xz'
const ARCH_INTEL = 'x64'
const ARCH_ARM = 'arm64'
function downloadFileFromS3(s3Path, fileName, downloadPath) {
const downloadTo = path.join(downloadPath, fileName)
const commandStr = `aws s3 cp s3://${HEROKU_S3_BUCKET}/${s3Path}/${fileName} ${downloadTo}`
return shell(commandStr)
}
async function updateHerokuFormula(brewDir) {
const templatePath = path.join(TEMPLATES, 'heroku.rb')
const template = fs.readFileSync(templatePath).toString('utf8')
const formulaPath = path.join(brewDir, 'Formula', 'heroku.rb')
const fileNamePrefix = `heroku-v${VERSION}-${GITHUB_SHA_SHORT}`
const s3KeyPrefix = `versions/${VERSION}/${GITHUB_SHA_SHORT}`
const urlPrefix = `https://cli-assets.heroku.com/${s3KeyPrefix}`
const fileNameMacIntel = `${fileNamePrefix}-darwin-${ARCH_INTEL}${fileSuffix}`
const fileNameMacArm = `${fileNamePrefix}-darwin-${ARCH_ARM}${fileSuffix}`
const fileNameLinuxIntel = `${fileNamePrefix}-linux-${ARCH_INTEL}${fileSuffix}`
const fileNameLinuxArm = `${fileNamePrefix}-linux-arm${fileSuffix}`
// download files from S3 for SHA calc
await Promise.all([
downloadFileFromS3(s3KeyPrefix, fileNameMacIntel, __dirname),
downloadFileFromS3(s3KeyPrefix, fileNameMacArm, __dirname),
downloadFileFromS3(s3KeyPrefix, fileNameLinuxIntel, __dirname),
downloadFileFromS3(s3KeyPrefix, fileNameLinuxArm, __dirname),
])
const sha256MacIntel = await calculateSHA256(path.join(__dirname, fileNameMacIntel))
const sha256MacArm = await calculateSHA256(path.join(__dirname, fileNameMacArm))
const sha256LinuxIntel = await calculateSHA256(path.join(__dirname, fileNameLinuxIntel))
const sha256LinuxArm = await calculateSHA256(path.join(__dirname, fileNameLinuxArm))
const templateReplaced
= template
.replace('__CLI_VERSION__', VERSION)
.replace('__CLI_MAC_INTEL_DOWNLOAD_URL__', `${urlPrefix}/${fileNameMacIntel}`)
.replace('__CLI_MAC_INTEL_SHA256__', sha256MacIntel)
.replace('__CLI_MAC_ARM_DOWNLOAD_URL__', `${urlPrefix}/${fileNameMacArm}`)
.replace('__CLI_MAC_ARM_SHA256__', sha256MacArm)
.replace('__CLI_LINUX_DOWNLOAD_URL__', `${urlPrefix}/${fileNameLinuxIntel}`)
.replace('__CLI_LINUX_SHA256__', sha256LinuxIntel)
.replace('__CLI_LINUX_ARM_DOWNLOAD_URL__', `${urlPrefix}/${fileNameLinuxArm}`)
.replace('__CLI_LINUX_ARM_SHA256__', sha256LinuxArm)
fs.writeFileSync(formulaPath, templateReplaced)
console.log(`done updating heroku Formula in ${formulaPath}`)
}
async function setupGit() {
const githubSetupPath = path.join(__dirname, '..', 'utils', '_github_setup')
await x(githubSetupPath, [])
}
async function updateHomebrew() {
HEROKU_S3_BUCKET = await getHerokuS3Bucket()
const tmp = path.join(__dirname, 'tmp')
const homebrewDir = path.join(tmp, 'homebrew-brew')
fs.mkdirSync(tmp, {recursive: true})
rimrafSync(homebrewDir)
await setupGit()
console.log(`cloning https://github.com/heroku/homebrew-brew to ${homebrewDir}`)
await x(
'git',
[
'clone',
'git@github.com:heroku/homebrew-brew.git',
homebrewDir,
],
)
console.log(`done cloning heroku/homebrew-brew to ${homebrewDir}`)
await updateHerokuFormula(homebrewDir)
// run in git in cloned heroku/homebrew-brew git directory
const git = async (args, opts = {}) => {
await x('git', ['-C', homebrewDir, ...args], opts)
}
console.log('updating local git...')
await git(['add', 'Formula'])
await git(['config', '--local', 'core.pager', 'cat'])
await git(['diff', '--cached'], {stdio: 'inherit'})
await git(['commit', '-m', `heroku v${VERSION}`])
if (process.env.SKIP_GIT_PUSH === undefined) {
await git(['push', 'origin', 'main'])
}
}
await run(async () => {
await updateHomebrew()
})