Skip to content

Commit

Permalink
feat: partial readonly fs support
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Sep 30, 2024
1 parent 8ff6f31 commit 7d7f15b
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/cli/install-tool/base-install.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { codeBlock } from 'common-tags';
import { injectable } from 'inversify';
import type { EnvService, PathService } from '../services';
import { NoInitTools, NoPrepareTools } from '../tools';
import { isValid } from '../utils';
import { isValid, tool2path } from '../utils';

export interface ShellWrapperConfig {
name?: string;
Expand Down Expand Up @@ -78,7 +78,7 @@ export abstract class BaseInstallService {
}: ShellWrapperConfig): Promise<void> {
const tgt = join(this.pathSvc.binDir, name ?? this.name);

const envs = [...(extraToolEnvs ?? []), this.name];
const envs = [...(extraToolEnvs ?? []), this.name].map(tool2path);

Check warning on line 81 in src/cli/install-tool/base-install.service.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/install-tool/base-install.service.ts#L81

Added line #L81 was not covered by tests
let content = codeBlock`
#!/bin/bash
Expand Down
16 changes: 15 additions & 1 deletion src/cli/install-tool/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { describe, expect, test, vi } from 'vitest';
import fs from 'node:fs/promises';
import { beforeAll, describe, expect, test, vi } from 'vitest';
import { rootPath } from '../../../test/path';
import { installTool, resolveVersion } from '.';

vi.mock('del');
Expand All @@ -7,6 +9,18 @@ vi.mock('../tools/bun');
vi.mock('../tools/php/composer');

describe('index', () => {
beforeAll(async () => {
for (const p of [
'var/lib/containerbase/tool.prep.d',
'tmp/containerbase/tool.init.d',
]) {
const prepDir = rootPath(p);
await fs.mkdir(prepDir, {
recursive: true,
});
}
});

test('installTool', async () => {
expect(await installTool('bun', '1.0.0')).toBeUndefined();
expect(await installTool('dummy', '1.0.0')).toBeUndefined();
Expand Down
8 changes: 8 additions & 0 deletions src/cli/prepare-tool/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,27 @@ describe('index', () => {
for (const p of [
'var/lib/containerbase/tool.prep.d',
'tmp/containerbase/tool.init.d',
'usr/local/containerbase/tools/v2',
]) {
const prepDir = rootPath(p);
await fs.mkdir(prepDir, {
recursive: true,
});
}

await fs.writeFile(
rootPath('usr/local/containerbase/tools/v2/dummy.sh'),
'',
);
});

test('prepareTools', async () => {
expect(await prepareTools(['bun', 'dummy'])).toBeUndefined();
expect(await prepareTools(['not-exist'])).toBe(1);
});

test('initializeTools', async () => {
expect(await initializeTools(['bun', 'dummy'])).toBeUndefined();
expect(await initializeTools(['not-exist'])).toBeUndefined();
});
});
32 changes: 24 additions & 8 deletions src/cli/services/path.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { env } from 'node:process';
import { inject, injectable } from 'inversify';
import { fileRights, logger, pathExists } from '../utils';
import { fileRights, logger, pathExists, tool2path } from '../utils';
import { EnvService } from './env.service';

export interface FileOwnerConfig {
Expand All @@ -28,10 +28,16 @@ export class PathService {
return join(this.varPath, 'tool.prep.d');
}

/**
* Path to `/opt/containerbase/bin`.
*/
get binDir(): string {
return join(this.installDir, 'bin');
}

/**
* Path to `/tmp/containerbase/cache`.
*/
get cachePath(): string {
return join(this.tmpDir, 'cache');
}
Expand All @@ -47,6 +53,9 @@ export class PathService {
return join(this.envSvc.rootDir, 'opt/containerbase');
}

/**
* Path to `/opt/containerbase/ssl`.
*/
get sslPath(): string {
return join(this.installDir, 'ssl');
}
Expand All @@ -58,6 +67,9 @@ export class PathService {
return join(this.envSvc.tmpDir, 'containerbase');
}

/**
* Path to `/opt/containerbase/tools`.
*/
get toolsPath(): string {
return join(this.installDir, 'tools');
}
Expand All @@ -76,6 +88,9 @@ export class PathService {
return join(this.envSvc.rootDir, 'var/lib/containerbase');
}

/**
* Path to `/opt/containerbase/versions`.
*/
get versionPath(): string {
return join(this.installDir, 'versions');
}
Expand All @@ -100,10 +115,6 @@ export class PathService {
return toolPath;
}

async dirExists(filePath: string): Promise<boolean> {
return await pathExists(filePath, true);
}

async createVersionedToolPath(
tool: string,
version: string,
Expand All @@ -113,13 +124,18 @@ export class PathService {
return toolPath;
}

async dirExists(filePath: string): Promise<boolean> {
return await pathExists(filePath, true);
}

Check warning on line 129 in src/cli/services/path.service.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/services/path.service.ts#L128-L129

Added lines #L128 - L129 were not covered by tests

async ensureBasePaths(): Promise<void> {
if (!(await pathExists(this.varPath, true))) {
throw new Error('System not initialized for containerbase');
}

Check warning on line 134 in src/cli/services/path.service.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/services/path.service.ts#L133-L134

Added lines #L133 - L134 were not covered by tests
await this.createDir(this._toolPrepPath);
await this.createDir(this.toolsPath);
await this.createDir(this.versionPath);
await this.createDir(this.binDir);
await this.createDir(this.sslPath);
await this.createDir(this._toolInitPath);
}
Expand Down Expand Up @@ -185,15 +201,15 @@ export class PathService {
}

toolInitPath(tool: string): string {
return join(this._toolInitPath, tool);
return join(this._toolInitPath, tool2path(tool));
}

toolPath(tool: string): string {
return join(this.toolsPath, tool);
return join(this.toolsPath, tool2path(tool));
}

toolPreparePath(tool: string): string {
return join(this._toolPrepPath, tool);
return join(this._toolPrepPath, tool2path(tool));
}

versionedToolPath(tool: string, version: string): string {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/services/version.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { chmod, readFile, stat, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { inject, injectable } from 'inversify';
import { fileRights, logger } from '../utils';
import { fileRights, logger, tool2path } from '../utils';
import { PathService } from './path.service';

@injectable()
export class VersionService {
constructor(@inject(PathService) private pathSvc: PathService) {}

async find(tool: string): Promise<string | null> {
const path = join(this.pathSvc.versionPath, tool);
const path = join(this.pathSvc.versionPath, tool2path(tool));
try {
return (await readFile(path, { encoding: 'utf8' })).trim() || null;
} catch (err) {
Expand All @@ -24,7 +24,7 @@ export class VersionService {
}

async update(tool: string, version: string): Promise<void> {
const path = join(this.pathSvc.versionPath, tool);
const path = join(this.pathSvc.versionPath, tool2path(tool));
try {
await writeFile(path, version, { encoding: 'utf8' });
const s = await stat(path);
Expand Down
7 changes: 6 additions & 1 deletion src/cli/tools/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ export abstract class NpmBaseInstallService extends NodeBaseInstallService {
}

override async test(version: string): Promise<void> {
await execa(this.tool(version), ['--version'], { stdio: 'inherit' });
let name = this.tool(version);
const idx = name.lastIndexOf('/');
if (idx > 0) {
name = name.slice(idx + 1);
}
await execa(name, ['--version'], { stdio: 'inherit' });

Check warning on line 195 in src/cli/tools/node/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/utils.ts#L190-L195

Added lines #L190 - L195 were not covered by tests
}

override async validate(version: string): Promise<boolean> {
Expand Down
5 changes: 5 additions & 0 deletions src/cli/utils/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
parseBinaryName,
pathExists,
reset,
tool2path,
validateSystem,
} from '.';
import { rootPath } from '~test/path';
Expand Down Expand Up @@ -133,4 +134,8 @@ UBUNTU_CODENAME=jammy`);
expect(await isDockerBuild()).toBe(false);
});
});

test('tool2path', () => {
expect(tool2path('@microsoft/rush//path')).toBe('@microsoft__rush____path');
});
});
4 changes: 4 additions & 0 deletions src/cli/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,7 @@ async function checkDocker(): Promise<boolean> {
export function isDockerBuild(): Promise<boolean> {
return (isDocker ??= checkDocker());
}

export function tool2path(tool: string): string {
return tool.replace(/\//g, '__');
}
22 changes: 16 additions & 6 deletions src/usr/local/containerbase/utils/filesystem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function get_install_dir () {

function find_tool_path () {
local tool=${1:-$TOOL_NAME}
tool=${tool//\//__}
install_dir=$(get_install_dir)
if [[ -d "${install_dir}/${tool}" ]]; then
echo "${install_dir}/${tool}"
Expand All @@ -20,6 +21,7 @@ function find_tool_path () {
function find_versioned_tool_path () {
local tool=${1:-$TOOL_NAME}
local version=${2:-$TOOL_VERSION}
tool=${tool//\//__}
tool_dir=$(find_tool_path "$tool")
if [[ -d "${tool_dir}/${version}" ]]; then
echo "${tool_dir}/${version}"
Expand All @@ -28,6 +30,7 @@ function find_versioned_tool_path () {

function create_versioned_tool_path () {
local tool=${1:-$TOOL_NAME}
tool=${tool//\//__}
install_dir=$(get_install_dir)

local umask=775
Expand Down Expand Up @@ -188,14 +191,18 @@ function get_tool_prep_path () {
}

function set_tool_prep () {
if [[ ! -f "$(get_tool_prep_path)/${TOOL_NAME}" ]]; then
touch "$(get_tool_prep_path)/${TOOL_NAME}"
local tool=${1:-${TOOL_NAME}}
tool=${tool//\//__}
if [[ ! -f "$(get_tool_prep_path)/${tool}" ]]; then
touch "$(get_tool_prep_path)/${tool}"
fi
}

function get_tool_prep () {
if [[ -f "$(get_tool_prep_path)/${TOOL_NAME}" ]]; then
echo "$(get_tool_prep_path)/${TOOL_NAME}"
local tool=${1:-${TOOL_NAME}}
tool=${tool//\//__}
if [[ -f "$(get_tool_prep_path)/${tool}" ]]; then
echo "$(get_tool_prep_path)/${tool}"
fi
}

Expand All @@ -208,13 +215,16 @@ function get_tool_init_path () {
}

function set_tool_init () {
if [[ ! -f "$(get_tool_init_path)/${TOOL_NAME}" ]]; then
touch "$(get_tool_init_path)/${TOOL_NAME}"
local tool=${1:-${TOOL_NAME}}
tool=${tool//\//__}
if [[ ! -f "$(get_tool_init_path)/${tool}" ]]; then
touch "$(get_tool_init_path)/${tool}"
fi
}

function get_tool_init () {
local tool=${1:-${TOOL_NAME}}
tool=${tool//\//__}
if [[ -f "$(get_tool_init_path)/${tool}" ]]; then
echo "$(get_tool_init_path)/${tool}"
fi
Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/containerbase/utils/linking.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function shell_wrapper () {
check SOURCE true
check_command "$SOURCE"

tool_init=$(get_tool_init "${TOOL_NAME}")
tool_init=$(get_tool_init "${TOOL_NAME//\//__}")

cat > "$TARGET" <<- EOM
#!/bin/bash
Expand Down

0 comments on commit 7d7f15b

Please sign in to comment.