|
| 1 | +import * as core from '@actions/core' |
| 2 | +import {ChildProcess, spawn} from 'child_process' |
| 3 | +import {Octokit} from '@octokit/rest' |
| 4 | +import {delimiter} from 'path' |
| 5 | + |
| 6 | +const gitForWindowsUsrBinPath = 'C:/Program Files/Git/usr/bin' |
| 7 | + |
| 8 | +async function clone( |
| 9 | + url: string, |
| 10 | + destination: string, |
| 11 | + verbose: number | boolean, |
| 12 | + cloneExtraOptions: string[] = [] |
| 13 | +): Promise<void> { |
| 14 | + if (verbose) core.notice(`Cloning ${url} to ${destination}`) |
| 15 | + const child = spawn( |
| 16 | + 'git.exe', |
| 17 | + [ |
| 18 | + 'clone', |
| 19 | + '--depth=1', |
| 20 | + '--single-branch', |
| 21 | + '--branch=main', |
| 22 | + ...cloneExtraOptions, |
| 23 | + url, |
| 24 | + destination |
| 25 | + ], |
| 26 | + { |
| 27 | + stdio: [undefined, 'inherit', 'inherit'] |
| 28 | + } |
| 29 | + ) |
| 30 | + return new Promise<void>((resolve, reject) => { |
| 31 | + child.on('close', code => { |
| 32 | + if (code === 0) { |
| 33 | + resolve() |
| 34 | + } else { |
| 35 | + reject(new Error(`tar: exited with code ${code}`)) |
| 36 | + } |
| 37 | + }) |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +export async function getViaGit( |
| 42 | + flavor: string, |
| 43 | + architecture: string |
| 44 | +): Promise<{ |
| 45 | + artifactName: string |
| 46 | + id: string |
| 47 | + download: ( |
| 48 | + outputDirectory: string, |
| 49 | + verbose?: number | boolean |
| 50 | + ) => Promise<void> |
| 51 | +}> { |
| 52 | + const bitness = architecture === 'i686' ? '32' : '64' |
| 53 | + const owner = 'git-for-windows' |
| 54 | + const repo = `git-sdk-${bitness}` |
| 55 | + const artifactName = `${repo}-${flavor}` |
| 56 | + |
| 57 | + const octokit = new Octokit() |
| 58 | + const info = await octokit.repos.getBranch({ |
| 59 | + owner, |
| 60 | + repo, |
| 61 | + branch: 'main' |
| 62 | + }) |
| 63 | + const id = info.data.commit.sha |
| 64 | + core.notice(`Got ID ${id} for ${repo}`) |
| 65 | + |
| 66 | + return { |
| 67 | + artifactName, |
| 68 | + id, |
| 69 | + download: async ( |
| 70 | + outputDirectory: string, |
| 71 | + verbose: number | boolean = false |
| 72 | + ): Promise<void> => { |
| 73 | + core.startGroup(`Cloning ${repo}`) |
| 74 | + const partialCloneArg = flavor === 'full' ? [] : ['--filter=blob:none'] |
| 75 | + await clone(`https://github.com/${owner}/${repo}`, `.tmp`, verbose, [ |
| 76 | + '--bare', |
| 77 | + ...partialCloneArg |
| 78 | + ]) |
| 79 | + core.endGroup() |
| 80 | + |
| 81 | + let child: ChildProcess |
| 82 | + if (flavor === 'full') { |
| 83 | + core.startGroup(`Checking out ${repo}`) |
| 84 | + child = spawn( |
| 85 | + 'git.exe', |
| 86 | + [`--git-dir=.tmp`, 'worktree', 'add', outputDirectory, id], |
| 87 | + { |
| 88 | + stdio: [undefined, 'inherit', 'inherit'] |
| 89 | + } |
| 90 | + ) |
| 91 | + } else { |
| 92 | + core.startGroup('Cloning build-extra') |
| 93 | + await clone( |
| 94 | + `https://github.com/${owner}/build-extra`, |
| 95 | + '.tmp/build-extra', |
| 96 | + verbose |
| 97 | + ) |
| 98 | + core.endGroup() |
| 99 | + |
| 100 | + core.startGroup(`Creating ${flavor} artifact`) |
| 101 | + const traceArg = verbose ? ['-x'] : [] |
| 102 | + child = spawn( |
| 103 | + `${gitForWindowsUsrBinPath}/bash.exe`, |
| 104 | + [ |
| 105 | + ...traceArg, |
| 106 | + '.tmp/build-extra/please.sh', |
| 107 | + 'create-sdk-artifact', |
| 108 | + `--bitness=${bitness}`, |
| 109 | + `--out=${outputDirectory}`, |
| 110 | + '--sdk=.tmp', |
| 111 | + flavor |
| 112 | + ], |
| 113 | + { |
| 114 | + env: { |
| 115 | + COMSPEC: |
| 116 | + process.env.COMSPEC || |
| 117 | + `${process.env.WINDIR}\\system32\\cmd.exe`, |
| 118 | + LC_CTYPE: 'C.UTF-8', |
| 119 | + CHERE_INVOKING: '1', |
| 120 | + MSYSTEM: 'MINGW64', |
| 121 | + PATH: `${gitForWindowsUsrBinPath}${delimiter}${process.env.PATH}` |
| 122 | + }, |
| 123 | + stdio: [undefined, 'inherit', 'inherit'] |
| 124 | + } |
| 125 | + ) |
| 126 | + } |
| 127 | + return new Promise<void>((resolve, reject) => { |
| 128 | + child.on('close', code => { |
| 129 | + core.endGroup() |
| 130 | + if (code === 0) { |
| 131 | + resolve() |
| 132 | + } else { |
| 133 | + reject(new Error(`process exited with code ${code}`)) |
| 134 | + } |
| 135 | + }) |
| 136 | + }) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments