Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config/toml): support jinja templates in toml files #33123

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion lib/modules/manager/pep621/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export function parsePyProject(
content: string,
): PyProject | null {
try {
const jsonMap = parseToml(content);
const jsonMap = parseToml(content, { removeTemplates: true });
return PyProjectSchema.parse(jsonMap);
} catch (err) {
logger.debug(
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/poetry/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function getPoetryRequirement(
function getPoetrySources(content: string, fileName: string): PoetrySource[] {
let pyprojectFile: PoetryFile;
try {
pyprojectFile = parseToml(content) as PoetryFile;
pyprojectFile = parseToml(content, { removeTemplates: true }) as PoetryFile;
} catch (err) {
logger.debug({ err }, 'Error parsing pyproject.toml file');
return [];
Expand Down
21 changes: 21 additions & 0 deletions lib/util/toml.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,25 @@ describe('util/toml', () => {

expect(() => parseToml(input)).toThrow(SyntaxError);
});

it('should parse content with templates', () => {
const input = codeBlock`
[project]
name = "{{ value }}"

{# comment #}
[tool.poetry.dependencies]
{% if enabled %}
python = "^3.12"
{%+ endif -%}
`;
expect(parseToml(input, { removeTemplates: true })).toStrictEqual({
project: { name: '' },
tool: {
poetry: {
dependencies: { python: '^3.12' },
},
},
});
});
});
20 changes: 18 additions & 2 deletions lib/util/toml.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { getStaticTOMLValue, parseTOML } from 'toml-eslint-parser';
import { regEx } from './regex';

export function parse(input: string): unknown {
const ast = parseTOML(input);
interface TomlOptions {
jiaweikho marked this conversation as resolved.
Show resolved Hide resolved
removeTemplates?: boolean;
}

export function parse(input: string, options?: TomlOptions): unknown {
const massagedContent = massageContent(input, options);
const ast = parseTOML(massagedContent);
return getStaticTOMLValue(ast);
}

function massageContent(content: string, options?: TomlOptions): string {
if (options?.removeTemplates) {
return content
.replace(regEx(/{%.+?%}/gs), '')
.replace(regEx(/{{.+?}}/gs), '')
.replace(regEx(/{#.+?#}/gs), '');
jiaweikho marked this conversation as resolved.
Show resolved Hide resolved
}
return content;
}
Loading