Skip to content

Commit 01f08e9

Browse files
feat(python): add poetry support (#188)
* feat(python): add poetry support * chore: run autoformatter --------- Co-authored-by: Timothy Jones <[email protected]>
1 parent 959200c commit 01f08e9

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
2424
- [.NET Support](#net-support)
2525
- [YAML Support](#yaml-support)
2626
- [OpenAPI Support](#openapi-support)
27+
- [Python Support](#python-support)
2728
- [Installing `commit-and-tag-version`](#installing-commit-and-tag-version)
2829
- [As a local `npm run` script](#as-a-local-npm-run-script)
2930
- [As global `bin`](#as-global-bin)
@@ -127,6 +128,14 @@ If you are using OpenAPI, then just point to your `openapi.yaml` file.
127128
commit-and-tag-version --packageFiles openapi.yaml --bumpFiles openapi.yaml
128129
```
129130

131+
### Python Support
132+
133+
If you are using Python ***with Poetry***, then point to your `pyproject.toml` file.
134+
135+
```sh
136+
commit-and-tag-version --packageFiles pyproject.toml --bumpFiles pyproject.toml
137+
```
138+
130139
## Installing `commit-and-tag-version`
131140

132141
### As a local `npm run` script

lib/updaters/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const updatersByType = {
88
csproj: require('./types/csproj'),
99
yaml: require('./types/yaml'),
1010
openapi: require('./types/openapi'),
11+
python: require('./types/python'),
1112
};
1213
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt'];
1314

@@ -41,6 +42,9 @@ function getUpdaterByFilename(filename) {
4142
if (/\.ya?ml$/.test(filename)) {
4243
return getUpdaterByType('yaml');
4344
}
45+
if (/pyproject.toml/.test(filename)) {
46+
return getUpdaterByType('python');
47+
}
4448
throw Error(
4549
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`,
4650
);

lib/updaters/types/python.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const versionExtractRegex = /version[" ]*=[ ]*["'](.*)["']/i;
2+
3+
const getVersionIndex = function (lines) {
4+
let version;
5+
const lineNumber = lines.findIndex((line) => {
6+
const versionMatcher = line.match(versionExtractRegex);
7+
// if version not found in lines provided, return false
8+
if (versionMatcher == null) {
9+
return false;
10+
}
11+
version = versionMatcher[1];
12+
return true;
13+
});
14+
return { version, lineNumber };
15+
};
16+
17+
module.exports.readVersion = function (contents) {
18+
const lines = contents.split('\n');
19+
const versionIndex = getVersionIndex(lines);
20+
return versionIndex.version;
21+
};
22+
23+
module.exports.writeVersion = function (contents, version) {
24+
const lines = contents.split('\n');
25+
const versionIndex = getVersionIndex(lines);
26+
const versionLine = lines[versionIndex.lineNumber];
27+
const newVersionLine = versionLine.replace(versionIndex.version, version);
28+
lines[versionIndex.lineNumber] = newVersionLine;
29+
return lines.join('\n');
30+
};

test/core.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,42 @@ describe('cli', function () {
11301130
console.warn = origWarn;
11311131
}
11321132
});
1133+
1134+
it('bumps version in Python `pyproject.toml` file', async function () {
1135+
const expected = fs.readFileSync(
1136+
'./test/mocks/pyproject-1.1.0.toml',
1137+
'utf-8',
1138+
);
1139+
1140+
const filename = 'python.toml';
1141+
mock({
1142+
bump: 'minor',
1143+
realTestFiles: [
1144+
{
1145+
filename,
1146+
path: './test/mocks/pyproject-1.0.0.toml',
1147+
},
1148+
],
1149+
});
1150+
1151+
await exec({
1152+
packageFiles: [{ filename, type: 'python' }],
1153+
bumpFiles: [{ filename, type: 'python' }],
1154+
});
1155+
1156+
// filePath is the first arg passed to writeFileSync
1157+
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1158+
writeFileSyncSpy,
1159+
filename,
1160+
});
1161+
1162+
if (!packageJsonWriteFileSynchCall) {
1163+
throw new Error(`writeFileSynch not invoked with path ${filename}`);
1164+
}
1165+
1166+
const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1167+
expect(calledWithContentStr).toEqual(expected);
1168+
});
11331169
});
11341170

11351171
it('`packageFiles` are bumped along with `bumpFiles` defaults [commit-and-tag-version#533]', async function () {

test/mocks/pyproject-1.0.0.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[tool.poetry]
2+
name = "test"
3+
version = "1.0.0"
4+
description = ""
5+
authors = []
6+
7+
[tool.poetry.dependencies]
8+
python = "^3.8"
9+
10+
[build-system]
11+
requires = ["poetry>=1"]
12+
build-backend = "poetry.masonry.api"

test/mocks/pyproject-1.1.0.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[tool.poetry]
2+
name = "test"
3+
version = "1.1.0"
4+
description = ""
5+
authors = []
6+
7+
[tool.poetry.dependencies]
8+
python = "^3.8"
9+
10+
[build-system]
11+
requires = ["poetry>=1"]
12+
build-backend = "poetry.masonry.api"

0 commit comments

Comments
 (0)