mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 21:03:05 -07:00
refactor(cli): use standardized hydrateString for custom command prompts
This replaces the manual regex for ${extensionPath} with the standardized
hydrateString utility, automatically enabling support for other variables
like ${workspacePath} and ${/} in custom extension commands.
Added a test case to verify ${workspacePath} hydration.
This commit is contained in:
@@ -955,6 +955,64 @@ describe('FileCommandLoader', () => {
|
|||||||
assert.fail('Incorrect action type');
|
assert.fail('Incorrect action type');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('correctly injects ${workspacePath} into extension commands', async () => {
|
||||||
|
const workspaceDir = process.cwd();
|
||||||
|
const extensionDir = path.join(
|
||||||
|
workspaceDir,
|
||||||
|
GEMINI_DIR,
|
||||||
|
'extensions',
|
||||||
|
'ws-test-ext',
|
||||||
|
);
|
||||||
|
|
||||||
|
mock({
|
||||||
|
[extensionDir]: {
|
||||||
|
'gemini-extension.json': JSON.stringify({
|
||||||
|
name: 'ws-test-ext',
|
||||||
|
version: '1.0.0',
|
||||||
|
}),
|
||||||
|
commands: {
|
||||||
|
'ws-cmd.toml': 'prompt = "WS: ${workspacePath}"',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const mockConfig = {
|
||||||
|
getProjectRoot: vi.fn(() => workspaceDir),
|
||||||
|
getExtensions: vi.fn(() => [
|
||||||
|
{
|
||||||
|
name: 'ws-test-ext',
|
||||||
|
version: '1.0.0',
|
||||||
|
isActive: true,
|
||||||
|
path: extensionDir,
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
getFolderTrust: vi.fn(() => false),
|
||||||
|
isTrustedFolder: vi.fn(() => false),
|
||||||
|
} as unknown as Config;
|
||||||
|
const loader = new FileCommandLoader(mockConfig);
|
||||||
|
const commands = await loader.loadCommands(signal);
|
||||||
|
|
||||||
|
expect(commands).toHaveLength(1);
|
||||||
|
const command = commands[0];
|
||||||
|
|
||||||
|
const result = await command.action?.(
|
||||||
|
createMockCommandContext({
|
||||||
|
invocation: {
|
||||||
|
raw: '/ws-cmd',
|
||||||
|
name: 'ws-cmd',
|
||||||
|
args: '',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
'',
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result?.type === 'submit_prompt') {
|
||||||
|
expect(result.content).toEqual([{ text: `WS: ${workspaceDir}` }]);
|
||||||
|
} else {
|
||||||
|
assert.fail('Incorrect action type');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Argument Handling Integration (via ShellProcessor)', () => {
|
describe('Argument Handling Integration (via ShellProcessor)', () => {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import {
|
|||||||
} from './prompt-processors/shellProcessor.js';
|
} from './prompt-processors/shellProcessor.js';
|
||||||
import { AtFileProcessor } from './prompt-processors/atFileProcessor.js';
|
import { AtFileProcessor } from './prompt-processors/atFileProcessor.js';
|
||||||
import { sanitizeForDisplay } from '../ui/utils/textUtils.js';
|
import { sanitizeForDisplay } from '../ui/utils/textUtils.js';
|
||||||
|
import { hydrateString } from '../config/extensions/variables.js';
|
||||||
|
|
||||||
interface CommandDirectory {
|
interface CommandDirectory {
|
||||||
path: string;
|
path: string;
|
||||||
@@ -239,13 +240,13 @@ export class FileCommandLoader implements ICommandLoader {
|
|||||||
|
|
||||||
const validDef = validationResult.data;
|
const validDef = validationResult.data;
|
||||||
|
|
||||||
// Hydrate extensionPath if this is an extension command
|
// Hydrate variables in the prompt
|
||||||
if (extensionPath) {
|
validDef.prompt = hydrateString(validDef.prompt, {
|
||||||
validDef.prompt = validDef.prompt.replace(
|
extensionPath,
|
||||||
/\$\{extensionPath\}/g,
|
workspacePath: this.projectRoot,
|
||||||
() => extensionPath,
|
'/': path.sep,
|
||||||
);
|
pathSeparator: path.sep,
|
||||||
}
|
});
|
||||||
|
|
||||||
const relativePathWithExt = path.relative(baseDir, filePath);
|
const relativePathWithExt = path.relative(baseDir, filePath);
|
||||||
const relativePath = relativePathWithExt.substring(
|
const relativePath = relativePathWithExt.substring(
|
||||||
|
|||||||
Reference in New Issue
Block a user