Skip to content

Commit

Permalink
feat: add support for versioning by version.py for python packages (#560
Browse files Browse the repository at this point in the history
)

* feat: add support for versioning by version.py for python packages
  • Loading branch information
crwilcox committed Oct 5, 2020
1 parent 70bcfa0 commit a185192
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
18 changes: 18 additions & 0 deletions __snapshots__/version-py.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
exports['version.py updateContent updates version in version.py 1'] = `
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__version__ = "0.6.0"
`
15 changes: 15 additions & 0 deletions src/releasers/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {Changelog} from '../updaters/changelog';
// Python specific.
import {SetupPy} from '../updaters/python/setup-py';
import {SetupCfg} from '../updaters/python/setup-cfg';
import {VersionPy} from '../updaters/python/version-py';

const CHANGELOG_SECTIONS = [
{type: 'feat', section: 'Features'},
Expand Down Expand Up @@ -110,6 +111,20 @@ export class Python extends ReleasePR {
})
);

// There should be only one version.py, but foreach in case that is incorrect
const versionPyFilesSearch = this.gh.findFilesByFilename('version.py');
const versionPyFiles = await versionPyFilesSearch;
versionPyFiles.forEach(path => {
updates.push(
new VersionPy({
path: this.addPath(path),
changelogEntry,
version: candidate.version,
packageName: this.packageName,
})
);
});

await this.openPR({
sha: commits[0].sha!,
changelogEntry: `${changelogEntry}\n---\n`,
Expand Down
40 changes: 40 additions & 0 deletions src/updaters/python/version-py.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {Update, UpdateOptions, VersionsMap} from '../update';
import {GitHubFileContents} from '../../github';

export class VersionPy implements Update {
path: string;
changelogEntry: string;
version: string;
versions?: VersionsMap;
packageName: string;
create: boolean;
contents?: GitHubFileContents;

constructor(options: UpdateOptions) {
this.create = false;
this.path = options.path;
this.changelogEntry = options.changelogEntry;
this.version = options.version;
this.packageName = options.packageName;
}
updateContent(content: string): string {
return content.replace(
/__version__ ?= ?["'][0-9]+\.[0-9]+\.[0-9](-\w+)?["']/,
`__version__ = "${this.version}"`
);
}
}
15 changes: 15 additions & 0 deletions test/updaters/fixtures/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "0.5.0"
40 changes: 40 additions & 0 deletions test/updaters/version-py.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {readFileSync} from 'fs';
import {resolve} from 'path';
import * as snapshot from 'snap-shot-it';
import {describe, it} from 'mocha';
import {VersionPy} from '../../src/updaters/python/version-py';

const fixturesPath = './test/updaters/fixtures';

describe('version.py', () => {
describe('updateContent', () => {
it('updates version in version.py', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './version.py'),
'utf8'
).replace(/\r\n/g, '\n');
const version = new VersionPy({
path: 'version.py',
changelogEntry: '',
version: '0.6.0',
packageName: '',
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
});
});

0 comments on commit a185192

Please sign in to comment.