Skip to content

Commit

Permalink
feat(python): add poetry support (#188)
Browse files Browse the repository at this point in the history
* feat(python): add poetry support

* chore: run autoformatter

---------

Co-authored-by: Timothy Jones <timothy.l.jones@gmail.com>
  • Loading branch information
jgrant216 and TimothyJones authored Oct 10, 2024
1 parent 959200c commit 01f08e9
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
- [.NET Support](#net-support)
- [YAML Support](#yaml-support)
- [OpenAPI Support](#openapi-support)
- [Python Support](#python-support)
- [Installing `commit-and-tag-version`](#installing-commit-and-tag-version)
- [As a local `npm run` script](#as-a-local-npm-run-script)
- [As global `bin`](#as-global-bin)
Expand Down Expand Up @@ -127,6 +128,14 @@ If you are using OpenAPI, then just point to your `openapi.yaml` file.
commit-and-tag-version --packageFiles openapi.yaml --bumpFiles openapi.yaml
```

### Python Support

If you are using Python ***with Poetry***, then point to your `pyproject.toml` file.

```sh
commit-and-tag-version --packageFiles pyproject.toml --bumpFiles pyproject.toml
```

## Installing `commit-and-tag-version`

### As a local `npm run` script
Expand Down
4 changes: 4 additions & 0 deletions lib/updaters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const updatersByType = {
csproj: require('./types/csproj'),
yaml: require('./types/yaml'),
openapi: require('./types/openapi'),
python: require('./types/python'),
};
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt'];

Expand Down Expand Up @@ -41,6 +42,9 @@ function getUpdaterByFilename(filename) {
if (/\.ya?ml$/.test(filename)) {
return getUpdaterByType('yaml');
}
if (/pyproject.toml/.test(filename)) {
return getUpdaterByType('python');
}
throw Error(
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`,
);
Expand Down
30 changes: 30 additions & 0 deletions lib/updaters/types/python.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const versionExtractRegex = /version[" ]*=[ ]*["'](.*)["']/i;

const getVersionIndex = function (lines) {
let version;
const lineNumber = lines.findIndex((line) => {
const versionMatcher = line.match(versionExtractRegex);
// if version not found in lines provided, return false
if (versionMatcher == null) {
return false;
}
version = versionMatcher[1];
return true;
});
return { version, lineNumber };
};

module.exports.readVersion = function (contents) {
const lines = contents.split('\n');
const versionIndex = getVersionIndex(lines);
return versionIndex.version;
};

module.exports.writeVersion = function (contents, version) {
const lines = contents.split('\n');
const versionIndex = getVersionIndex(lines);
const versionLine = lines[versionIndex.lineNumber];
const newVersionLine = versionLine.replace(versionIndex.version, version);
lines[versionIndex.lineNumber] = newVersionLine;
return lines.join('\n');
};
36 changes: 36 additions & 0 deletions test/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,42 @@ describe('cli', function () {
console.warn = origWarn;
}
});

it('bumps version in Python `pyproject.toml` file', async function () {
const expected = fs.readFileSync(
'./test/mocks/pyproject-1.1.0.toml',
'utf-8',
);

const filename = 'python.toml';
mock({
bump: 'minor',
realTestFiles: [
{
filename,
path: './test/mocks/pyproject-1.0.0.toml',
},
],
});

await exec({
packageFiles: [{ filename, type: 'python' }],
bumpFiles: [{ filename, type: 'python' }],
});

// filePath is the first arg passed to writeFileSync
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
writeFileSyncSpy,
filename,
});

if (!packageJsonWriteFileSynchCall) {
throw new Error(`writeFileSynch not invoked with path ${filename}`);
}

const calledWithContentStr = packageJsonWriteFileSynchCall[1];
expect(calledWithContentStr).toEqual(expected);
});
});

it('`packageFiles` are bumped along with `bumpFiles` defaults [commit-and-tag-version#533]', async function () {
Expand Down
12 changes: 12 additions & 0 deletions test/mocks/pyproject-1.0.0.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.poetry]
name = "test"
version = "1.0.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.8"

[build-system]
requires = ["poetry>=1"]
build-backend = "poetry.masonry.api"
12 changes: 12 additions & 0 deletions test/mocks/pyproject-1.1.0.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.poetry]
name = "test"
version = "1.1.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.8"

[build-system]
requires = ["poetry>=1"]
build-backend = "poetry.masonry.api"

0 comments on commit 01f08e9

Please sign in to comment.