Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ GitHub Action that allows you to setup a V environment.
# Only applies when the `version` input is specified.
# Default: true
cache: true

# Remove non-essential files (examples, tests, benchmarks, docs) from the
# V installation directory after building. This prevents V's own repository
# files from interfering with project-level commands like `v fmt -verify .`.
# Default: true
clean: true
```

## Output
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ inputs:
cache:
description: 'Cache the V executable to speed up subsequent runs. Only applies when the version input is specified.'
default: 'true'
clean:
description: 'Remove non-essential files (examples, tests, benchmarks, docs) from the V installation directory after building. Helps prevent interference with project-level V commands like fmt -verify.'
default: 'true'
outputs:
bin-path:
description: 'Path to the directory that contains the V binary'
Expand Down
42 changes: 40 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ async function run() {
const token = core.getInput('token', { required: true });
const stable = strToBoolean(core.getInput('stable') || 'false');
const checkLatest = strToBoolean(core.getInput('check-latest') || 'false');
const clean = strToBoolean(core.getInput('clean') || 'true');
const binPath = await installer.getVlang({
authToken: token,
version,
checkLatest,
stable,
arch,
installPath: customPath
installPath: customPath,
clean
});
core.info('Adding v to the cache...');
const installedVersion = await getVersion(binPath);
Expand Down
143 changes: 143 additions & 0 deletions src/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import {
cleanInstallation,
getInstallDir,
getVExecutable,
getWindowsBuildCommand,
Expand Down Expand Up @@ -127,3 +128,145 @@ describe('getWindowsBuildCommand', () => {
)
})
})

describe('cleanInstallation', () => {
let tempDir = ''

beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-v-clean-'))
})

afterEach(() => {
if (tempDir) {
fs.rmSync(tempDir, {recursive: true, force: true})
}
})

function mockVInstallation(dir: string): void {
const nonEssentialDirs = [
'examples',
'tests',
'test',
'benchmarks',
'bench',
'doc',
'ci',
'.github',
'.git'
]
for (const sub of nonEssentialDirs) {
const subDir = path.join(dir, sub)
fs.mkdirSync(subDir, {recursive: true})
fs.writeFileSync(path.join(subDir, 'stub.txt'), 'stub')
}

const nonEssentialFiles = [
'CHANGELOG.md',
'CONTRIBUTING.md',
'README.md',
'CODE_OF_CONDUCT.md'
]
for (const file of nonEssentialFiles) {
fs.writeFileSync(path.join(dir, file), 'stub')
}

const essentialDirs = ['vlib', 'cmd', 'thirdparty']
for (const sub of essentialDirs) {
const subDir = path.join(dir, sub)
fs.mkdirSync(subDir, {recursive: true})
fs.writeFileSync(path.join(subDir, 'stub.txt'), 'stub')
}

const essentialFiles = [
'v',
'v.mod',
'GNUmakefile',
'makev.bat',
'make.bat'
]
for (const file of essentialFiles) {
fs.writeFileSync(path.join(dir, file), 'stub')
}
}

test('removes non-essential directories', () => {
mockVInstallation(tempDir)

cleanInstallation(tempDir)

for (const dir of [
'examples',
'tests',
'test',
'benchmarks',
'bench',
'doc',
'ci',
'.github',
'.git'
]) {
expect(fs.existsSync(path.join(tempDir, dir))).toBe(false)
}
})

test('removes non-essential files', () => {
mockVInstallation(tempDir)

cleanInstallation(tempDir)

for (const file of [
'CHANGELOG.md',
'CONTRIBUTING.md',
'README.md',
'CODE_OF_CONDUCT.md'
]) {
expect(fs.existsSync(path.join(tempDir, file))).toBe(false)
}
})

test('keeps essential files and directories', () => {
mockVInstallation(tempDir)

cleanInstallation(tempDir)

expect(fs.existsSync(getVExecutable(tempDir))).toBe(true)
expect(fs.existsSync(path.join(tempDir, 'vlib'))).toBe(true)
expect(fs.existsSync(path.join(tempDir, 'cmd'))).toBe(true)
expect(fs.existsSync(path.join(tempDir, 'thirdparty'))).toBe(true)
expect(fs.existsSync(path.join(tempDir, 'v.mod'))).toBe(true)
expect(fs.existsSync(path.join(tempDir, 'GNUmakefile'))).toBe(true)
expect(fs.existsSync(path.join(tempDir, 'makev.bat'))).toBe(true)
expect(fs.existsSync(path.join(tempDir, 'make.bat'))).toBe(true)
})

test('keeps essential file contents intact', () => {
mockVInstallation(tempDir)

cleanInstallation(tempDir)

expect(
fs.readFileSync(path.join(tempDir, 'vlib', 'stub.txt'), 'utf8')
).toBe('stub')
expect(fs.readFileSync(path.join(tempDir, 'cmd', 'stub.txt'), 'utf8')).toBe(
'stub'
)
})

test('does not throw when install dir has nothing to clean', () => {
fs.writeFileSync(path.join(tempDir, 'v'), 'stub')
fs.mkdirSync(path.join(tempDir, 'vlib'), {recursive: true})

expect(() => cleanInstallation(tempDir)).not.toThrow()
expect(fs.existsSync(getVExecutable(tempDir))).toBe(true)
expect(fs.existsSync(path.join(tempDir, 'vlib'))).toBe(true)
})

test('is idempotent', () => {
mockVInstallation(tempDir)

expect(() => {
cleanInstallation(tempDir)
cleanInstallation(tempDir)
}).not.toThrow()
})
})
44 changes: 43 additions & 1 deletion src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,32 @@ import {execSync} from 'child_process'
const VLANG_GITHUB_OWNER = 'vlang'
const VLANG_GITHUB_REPO = 'v'

const NON_ESSENTIAL_DIRS = [
'examples',
'tests',
'test',
'benchmarks',
'bench',
'doc',
'ci',
'.github',
'.git'
]
const NON_ESSENTIAL_FILES = [
'CHANGELOG.md',
'CONTRIBUTING.md',
'README.md',
'CODE_OF_CONDUCT.md'
]

export interface GetVlangRequest {
authToken: string
version: string
checkLatest: boolean
stable?: boolean
arch?: string
installPath?: string
clean?: boolean
}

export function getInstallDir(
Expand Down Expand Up @@ -72,7 +91,8 @@ export async function getVlang({
checkLatest,
stable,
arch = os.arch(),
installPath
installPath,
clean = true
}: GetVlangRequest): Promise<string> {
const installDir = getInstallDir(arch, installPath)
const vBinPath = getVExecutable(installDir)
Expand Down Expand Up @@ -110,6 +130,10 @@ export async function getVlang({
buildV(installDir)
}

if (clean) {
cleanInstallation(installDir)
}

return installDir
}

Expand Down Expand Up @@ -169,6 +193,24 @@ function buildV(installDir: string): void {
console.log(execSync('make', {cwd: installDir, stdio: 'pipe'}).toString())
}

export function cleanInstallation(installDir: string): void {
core.info(`Cleaning non-essential files from ${installDir}...`)

for (const dir of NON_ESSENTIAL_DIRS) {
const dirPath = path.join(installDir, dir)
if (fs.existsSync(dirPath)) {
fs.rmSync(dirPath, {recursive: true, force: true})
}
}

for (const file of NON_ESSENTIAL_FILES) {
const filePath = path.join(installDir, file)
if (fs.existsSync(filePath)) {
fs.rmSync(filePath, {force: true})
}
}
}

function translateArchToDistUrl(arch: string): string {
const platformMap: Record<string, string> = {
darwin: 'macos',
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ async function run(): Promise<void> {
const token = core.getInput('token', {required: true})
const stable = strToBoolean(core.getInput('stable') || 'false')
const checkLatest = strToBoolean(core.getInput('check-latest') || 'false')
const clean = strToBoolean(core.getInput('clean') || 'true')

const binPath = await installer.getVlang({
authToken: token,
version,
checkLatest,
stable,
arch,
installPath: customPath
installPath: customPath,
clean
})

core.info('Adding v to the cache...')
Expand Down
Loading