first commit

This commit is contained in:
空巷一人 2024-03-08 20:00:17 +08:00
commit 6e207856a2
14 changed files with 41547 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
node_modules/
# dist/
*.log
cache/
coverage/

143
README.md Normal file
View File

@ -0,0 +1,143 @@
# gitea-tool-cache
## 注意
1、需要指定具体版本号
2、因为我没有 mac,所以没有 mac 环境
## Usage
See [action.yml](action.yml)
<!-- start usage -->
```yaml
- uses: kongxiangyiren/gitea-tool-cache@v1
with:
# 需要指定具体版本号
node-version: 18.18.0
go-version: 1.21.1
```
<!-- end usage -->
**Basic:**
```yaml
jobs:
linux:
runs-on: ubuntu-latest
strategy:
matrix:
# 使用gitea-tool-cache需要指定具体的版本号
node: ['18.18.0']
go: ['1.21.1']
dotnet: ['6.0.100']
steps:
- name: Checkout
uses: https://gitea.cn/actions/checkout@v4
- id: tool-cache
uses: kongxiangyiren/gitea-tool-cache@v1
with:
# 需要指定具体版本号
node-version: ${{ matrix.node }}
go-version: ${{ matrix.go }}
dotnet-version: ${{ matrix.dotnet }}
- uses: https://gitea.cn/actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: node -v
- uses: https://gitea.cn/actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- run: go version
- name: Setup .NET Core
uses: https://gitea.cn/actions/setup-dotnet@v1
env:
# dotnet 安装位置
DOTNET_INSTALL_DIR: ${{ steps.tool-cache.outputs.dotnet-path }}
with:
dotnet-version: ${{ matrix.dotnet }}
- run: dotnet --version
```
## 只要安装 node
```yaml
jobs:
linux:
runs-on: ubuntu-latest
strategy:
matrix:
# 使用gitea-tool-cache需要指定具体的版本号
node: ['18.18.0']
steps:
- name: Checkout
uses: https://gitea.cn/actions/checkout@v4
- name: 安装环境
uses: kongxiangyiren/gitea-tool-cache@v1
with:
# 需要指定具体版本号
node-version: ${{ matrix.node }}
- uses: https://gitea.cn/actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: node -v
```
## 只要安装 go
```yaml
jobs:
linux:
runs-on: ubuntu-latest
strategy:
matrix:
# 使用gitea-tool-cache需要指定具体的版本号
go: ['1.21.1']
steps:
- name: Checkout
uses: https://gitea.cn/actions/checkout@v4
- name: 安装环境
uses: kongxiangyiren/gitea-tool-cache@v1
with:
# 需要指定具体版本号
go-version: ${{ matrix.go }}
- uses: https://gitea.cn/actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- run: go version
```
## 只要安装 dotnet
```yaml
jobs:
linux:
runs-on: ubuntu-latest
strategy:
matrix:
# 使用gitea-tool-cache需要指定具体的版本号
dotnet: ['6.0.100']
steps:
- name: Checkout
uses: https://gitea.cn/actions/checkout@v4
- id: tool-cache
uses: kongxiangyiren/gitea-tool-cache@v1
with:
# 需要指定具体版本号
dotnet-version: ${{ matrix.dotnet }}
- name: Setup .NET Core
uses: https://gitea.cn/actions/setup-dotnet@v1
env:
# dotnet 安装位置
DOTNET_INSTALL_DIR: ${{ steps.tool-cache.outputs.dotnet-path }}
with:
dotnet-version: ${{ matrix.dotnet }}
- run: dotnet --version
```

82
__tests__/index.test.ts Normal file
View File

@ -0,0 +1,82 @@
import * as core from '@actions/core';
import osm from 'os';
import { rmSync } from 'fs';
import { dotnetInstall, goInstall, nodeInstall } from '../src/lib';
import { join } from 'path';
// 超时时间
jest.setTimeout(200000);
describe('gitea-tool-cache', () => {
let inputs = {} as any;
let os = {} as any;
let inSpy: jest.SpyInstance;
let platSpy: jest.SpyInstance;
let archSpy: jest.SpyInstance;
beforeAll(() => {
process.env['RUNNER_TOOL_CACHE'] = join(process.cwd(), './cache/toolcache');
process.env['RUNNER_TEMP'] = join(process.cwd(), './cache/temp');
});
beforeEach(() => {
process.env['RUNNER_TOOL_CACHE'] = join(process.cwd(), './cache/toolcache');
process.env['RUNNER_TEMP'] = join(process.cwd(), './cache/temp');
// @actions/core
inputs = {};
inSpy = jest.spyOn(core, 'getInput');
inSpy.mockImplementation(name => inputs[name]);
os = {};
platSpy = jest.spyOn(osm, 'platform');
platSpy.mockImplementation(() => os['platform']);
archSpy = jest.spyOn(osm, 'arch');
archSpy.mockImplementation(() => os['arch']);
});
afterEach(() => {
//jest.resetAllMocks();
jest.clearAllMocks();
//jest.restoreAllMocks();
});
afterAll(async () => {
jest.restoreAllMocks();
}, 200000);
// 测试时需要把别的注释掉
it('installs windows', async () => {
rmSync('./cache', { recursive: true, force: true });
os['platform'] = 'win32';
os['arch'] = 'x64';
// node
// inputs['node-version'] = '18.18.0';
// await nodeInstall();
// // go
// inputs['go-version'] = '1.21.1';
// await goInstall();
// dotnet
inputs['dotnet-version'] = '6.0.100';
await dotnetInstall();
});
// it('installs linux', async () => {
// os['platform'] = 'linux';
// os['arch'] = 'x64';
// inputs['node-version'] = '18.18.0';
// await nodeInstall();
// inputs['go-version'] = '1.21.1';
// await goInstall();
// inputs['dotnet-version'] = '5.0.401';
// await dotnetInstall();
// });
// it('installs darwin', async () => {
// os['platform'] = 'darwin';
// os['arch'] = 'x64';
// inputs['node-version'] = '18.18.0';
// await nodeInstall();
// inputs['go-version'] = '1.21.1';
// await goInstall();
// inputs['dotnet-version'] = '5.0.401';
// await dotnetInstall();
// });
});

19
action.yml Normal file
View File

@ -0,0 +1,19 @@
name: 'Gitea Tool Cache'
description: 'A Gitea Action Tool Cache Image Download'
author: 'kongxiangyiren'
inputs:
node-version:
description: 'The node version to use'
required: false
go-version:
description: 'The go version to use'
required: false
dotnet-version:
description: 'The dotnet version to use'
required: false
outputs:
dotnet-path:
description: 'The path to the dotnet executable'
runs:
using: 'node16'
main: 'dist/index.js'

38580
dist/index.js vendored Normal file

File diff suppressed because one or more lines are too long

13
jest.config.cjs Normal file
View File

@ -0,0 +1,13 @@
/** @type {import("jest").Config} */
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
// type": "module",
}

25
package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "gitea-tool-cache",
"version": "1.0.0",
"main": "dist/index.js",
"license": "MIT",
"scripts": {
"test": "chcp 65001 && jest --coverage",
"build": "rimraf ./dist && ncc build -o dist src/index.ts"
},
"devDependencies": {
"@tsconfig/node16": "^16.1.1",
"@types/jest": "^29.5.12",
"@types/node": "^16",
"@vercel/ncc": "^0.38.1",
"jest": "^29.7.0",
"jest-circus": "^29.7.0",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
"typescript": "^5.4.2"
},
"dependencies": {
"@actions/tool-cache": "^2.0.1",
"axios": "^1.6.7"
}
}

9
src/index.ts Normal file
View File

@ -0,0 +1,9 @@
import { nodeInstall, goInstall, dotnetInstall } from './lib';
function run() {
// 不加await会并行执行,加快速度
nodeInstall();
goInstall();
dotnetInstall();
}
run();

80
src/lib/dotnet.ts Normal file
View File

@ -0,0 +1,80 @@
import { join } from 'path';
import { downloadTool, extractZip, extractTar, extractXar, cacheDir } from '@actions/tool-cache';
import { addPath, getInput, setOutput } from '@actions/core';
import { arch, platform as Platform } from 'os';
import { existsSync, renameSync } from 'fs';
import { get } from 'axios';
// 安装dotnet
export async function dotnetInstall() {
const platform = Platform();
const dotnetVersion = getInput('dotnet-version');
if (!dotnetVersion) {
console.log('没有dotnet-version,跳过dotnet安装');
return;
}
if (
process.env['RUNNER_TOOL_CACHE'] &&
existsSync(join(process.env['RUNNER_TOOL_CACHE'], 'dotnet', dotnetVersion, arch()))
) {
console.log('dotnet已经安装过了');
return setOutput(
'dotnet-path',
join(process.env['RUNNER_TOOL_CACHE'], 'dotnet', dotnetVersion, arch())
);
}
const versionList = dotnetVersion.split('.');
const channelVersion = `${versionList[0]}.${versionList[1]}`;
const releasesUrl = `https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/${channelVersion}/releases.json`;
const releases = await get(releasesUrl).catch(err => {
console.log(err);
return null;
});
if (!releases || !releases.data) {
console.log('获取dotnet版本失败');
return false;
}
const releasesList = (releases.data as Record<string, any>).releases;
const release = releasesList.find(
(item: Record<string, any>) => item.sdk.version === dotnetVersion
);
if (!release) {
console.log('没有找到dotnet版本');
return false;
}
const list = release.sdk.files.find((item: Record<string, any>) => {
if (platform === 'win32') {
return item.rid === 'win-' + arch() && item.url.endsWith('.zip');
} else if (platform === 'darwin') {
return item.rid === 'osx-' + arch() && item.url.endsWith('.pkg');
} else {
return item.rid === 'linux-' + arch() && item.url.endsWith('.tar.gz');
}
});
if (!list) {
console.log('没有找到dotnet版本');
return false;
}
// console.log(list);
console.log(list.url);
const dotnetPath = await downloadTool(list.url);
if (platform === 'win32') {
renameSync(dotnetPath, dotnetPath + '.zip');
const dotnetExtractedFolder = await extractZip(dotnetPath + '.zip', './cache/dotnet');
const cachedPath = await cacheDir(dotnetExtractedFolder, 'dotnet', dotnetVersion);
addPath(cachedPath);
setOutput('dotnet-path', cachedPath);
} else if (platform === 'darwin') {
console.log('没有mac,暂未测试');
} else {
const dotnetExtractedFolder = await extractTar(dotnetPath, './cache/dotnet');
const cachedPath = await cacheDir(dotnetExtractedFolder, 'dotnet', dotnetVersion);
addPath(cachedPath);
setOutput('dotnet-path', cachedPath);
}
}

47
src/lib/go.ts Normal file
View File

@ -0,0 +1,47 @@
import { join } from 'path';
import { downloadTool, extractZip, extractTar, extractXar, cacheDir } from '@actions/tool-cache';
import { addPath, getInput } from '@actions/core';
import { arch, platform as Platform } from 'os';
import { existsSync, renameSync } from 'fs';
// 安装golang
export async function goInstall() {
const platform = Platform();
const goVersion = getInput('go-version');
if (!goVersion) {
console.log('没有go-version,跳过go安装');
return;
}
if (
process.env['RUNNER_TOOL_CACHE'] &&
existsSync(join(process.env['RUNNER_TOOL_CACHE'], 'go', goVersion, arch()))
) {
console.log('go已经安装过了');
return;
}
if (platform === 'win32') {
console.log(`https://golang.google.cn/dl/go${goVersion}.windows-amd64.zip`);
const goPath = await downloadTool(
`https://golang.google.cn/dl/go${goVersion}.windows-amd64.zip`
);
renameSync(goPath, goPath + '.zip');
const goExtractedFolder = await extractZip(goPath + '.zip', './cache/go');
const cachedPath = await cacheDir(join(goExtractedFolder, 'go'), 'go', goVersion);
addPath(cachedPath);
} else if (platform === 'darwin') {
console.log('没有mac,暂未测试');
} else {
console.log(`https://golang.google.cn/dl/go${goVersion}.linux-amd64.tar.gz`);
const goPath = await downloadTool(
`https://golang.google.cn/dl/go${goVersion}.linux-amd64.tar.gz`
);
const goExtractedFolder = await extractTar(goPath, './cache/go');
const cachedPath = await cacheDir(join(goExtractedFolder, 'go'), 'go', goVersion);
addPath(cachedPath);
}
}

3
src/lib/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from './node';
export * from './go';
export * from './dotnet';

68
src/lib/node.ts Normal file
View File

@ -0,0 +1,68 @@
import { join } from 'path';
import { downloadTool, extractZip, extractTar, extractXar, cacheDir } from '@actions/tool-cache';
import { addPath, getInput } from '@actions/core';
import { arch, platform as Platform } from 'os';
import { existsSync, renameSync } from 'fs';
// 安装node
export async function nodeInstall() {
const platform = Platform();
const nodeVersion = getInput('node-version');
if (!nodeVersion) {
console.log('没有node-version,跳过node安装');
return;
}
const version = nodeVersion;
if (
process.env['RUNNER_TOOL_CACHE'] &&
existsSync(join(process.env['RUNNER_TOOL_CACHE'], 'node', version, arch()))
) {
console.log('node已经安装过了');
return;
}
if (platform === 'win32') {
console.log(
`https://registry.npmmirror.com/-/binary/node/v${version}/node-v${version}-win-${arch()}.zip`
);
const nodePath = await downloadTool(
`https://registry.npmmirror.com/-/binary/node/v${version}/node-v${version}-win-${arch()}.zip`
);
renameSync(nodePath, nodePath + '.zip');
const nodeExtractedFolder = await extractZip(nodePath + '.zip', './cache/node');
const cachedPath = await cacheDir(
join(nodeExtractedFolder, `node-v${version}-win-${arch()}`),
'node',
version
);
addPath(cachedPath);
} else if (platform === 'darwin') {
console.log('没有mac,暂未测试');
// const nodePath = await downloadTool(
// `https://registry.npmmirror.com/-/binary/node/v${version}/node-v${version}.pkg`
// );
// const nodeExtractedFolder = await extractXar(nodePath, './cache/node');
} else {
console.log(
`https://registry.npmmirror.com/-/binary/node/v${version}/node-v${version}-linux-${arch()}.tar.gz`
);
const nodePath = await downloadTool(
`https://registry.npmmirror.com/-/binary/node/v${version}/node-v${version}-linux-${arch()}.tar.gz`
);
const nodeExtractedFolder = await extractTar(nodePath, './cache/node');
const cachedPath = await cacheDir(
join(nodeExtractedFolder, `node-v${version}-linux-${arch()}`),
'node',
version
);
addPath(cachedPath);
}
}

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"extends": "@tsconfig/node16",
"compilerOptions": {
"outDir": "dist",
"types": ["node", "jest"],
"module": "node16",
"moduleResolution": "node16"
},
"include": ["src/**/*.ts", "**/*.test.ts"]
}

2463
yarn.lock Normal file

File diff suppressed because it is too large Load Diff