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(python): add poetry support #188

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"