UNPKG

semantic-release-pypi

Version:

semantic-release plugin to publish a python package to PyPI

111 lines 5.07 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import fs from 'fs'; import _ from 'lodash'; import os from 'os'; import path from 'path'; import { DefaultConfig } from './default-options.js'; import { __dirname, normalizeVersion, pipe, setopt, spawn } from './util.js'; import { assertExitCode, isLegacyBuildInterface } from './verify.js'; function setVersionPy(setupPy, version) { return __awaiter(this, void 0, void 0, function* () { try { yield setopt(setupPy, 'metadata', 'version', version); } catch (err) { throw Error(`failed to set release version ${version}\n${err}`); } }); } function setVersionToml(srcDir, version, options) { return __awaiter(this, void 0, void 0, function* () { yield assertExitCode('python3', [path.resolve(__dirname, 'py/set_version.py'), '-v', version, srcDir], options, 0); }); } function sDistPackage(srcDir, distDir, options) { return __awaiter(this, void 0, void 0, function* () { yield spawn('python3', ['-m', 'build', '--sdist', '--outdir', distDir], Object.assign(Object.assign({}, options), { cwd: srcDir })); }); } function bDistPackage(srcDir, distDir, options) { return __awaiter(this, void 0, void 0, function* () { try { yield spawn('python3', ['-m', 'build', '--wheel', '--outdir', distDir], Object.assign(Object.assign({}, options), { cwd: srcDir })); } catch (err) { throw Error(`failed to build wheel`); } }); } function installPackages(packages, options) { return __awaiter(this, void 0, void 0, function* () { yield spawn('pip3', ['install', ...packages], options); }); } function createVenv(envDir, options) { return __awaiter(this, void 0, void 0, function* () { const envPath = path.resolve(envDir, 'bin'); if (!fs.existsSync(envPath)) { yield spawn('python3', ['-m', 'venv', envDir], options); } if (os.platform() == 'win32') { return Object.assign(Object.assign({}, options), { env: { Path: envPath + ';' + process.env.Path, } }); } return Object.assign(Object.assign({}, options), { env: { PATH: envPath + ':' + process.env.PATH, } }); }); } function prepare(pluginConfig, context) { return __awaiter(this, void 0, void 0, function* () { const { logger, nextRelease } = context; const { srcDir, setupPath, distDir, envDir, installDeps, versionCmd } = new DefaultConfig(pluginConfig); if (nextRelease === undefined) { throw new Error('nextRelease is undefined'); } let execaOptions = pipe(context); if (envDir) { logger.log(`Creating virtual environment ${envDir}`); execaOptions = yield createVenv(envDir, execaOptions); } if (installDeps) { const requirementsFile = path.resolve(__dirname, 'py/requirements.txt'); const requirements = fs .readFileSync(requirementsFile, 'utf8') .split('\n') .filter((value) => value.length >= 0); logger.log(`Installing required python packages (${requirements.join(', ')})`); yield installPackages(requirements, execaOptions); } const version = yield normalizeVersion(nextRelease.version, execaOptions); if (versionCmd) { const cmd = _.template(versionCmd)({ version }); logger.log(`Running versionCmd: ${cmd}`); const [file, ...args] = cmd.split(' '); yield assertExitCode(file, args, Object.assign(Object.assign({}, execaOptions), { cwd: srcDir }), 0); } else if (isLegacyBuildInterface(srcDir)) { logger.log(`Set version to ${version} (setup.cfg)`); yield setVersionPy(setupPath, version); } else { logger.log(`Set version to ${version} (pyproject.toml)`); yield setVersionToml(srcDir, version, execaOptions); } logger.log(`Build source archive`); yield sDistPackage(srcDir, distDir, execaOptions); logger.log(`Build wheel`); yield bDistPackage(srcDir, distDir, execaOptions); }); } export { bDistPackage, createVenv, installPackages, prepare, sDistPackage, setVersionPy, setVersionToml, }; //# sourceMappingURL=prepare.js.map