|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _ = require('lodash') |
| 4 | +const fs = require('fs') |
| 5 | +const path = require('path') |
| 6 | +const semver = require('balena-semver') |
| 7 | +const shell = require('shelljs') |
| 8 | +const yaml = require('js-yaml'); |
| 9 | + |
| 10 | +const isESR = (version) => { |
| 11 | + return /^\d{4}\.(01|1|04|4|07|7|10)\.\d+$/.test(version) |
| 12 | +} |
| 13 | + |
| 14 | +const getMetaResinFromSubmodule = (documentedVersions, history, callback) => { |
| 15 | + const latestDocumented = _.trim(_.last(documentedVersions.sort(semver.compare))) |
| 16 | + // ESR releases do not update meta-balena versions |
| 17 | + if (isESR(latestDocumented)) { |
| 18 | + return callback(null, latestDocumented) |
| 19 | + } |
| 20 | + // This is a hack because git does not update all the relevant files when moving a |
| 21 | + // submodule. Because of this, older repos will still have references to meta-resin |
| 22 | + // and new ones will refer to meta-balena |
| 23 | + const metaName = fs.existsSync('.git/modules/layers/meta-resin', fs.constants.R_OK) |
| 24 | + ? 'meta-resin' |
| 25 | + : 'meta-balena' |
| 26 | + shell.exec(`git --git-dir .git/modules/layers/${metaName} describe --tags --exact-match`, (code, stdout, stderr) => { |
| 27 | + if (code != 0) { |
| 28 | + return callback(new Error(`Could not find ${metaName} submodule`)) |
| 29 | + } |
| 30 | + const metaVersion = stdout.replace(/\s/g,'').replace(/^v/g, '') |
| 31 | + if (!metaVersion) { |
| 32 | + return callback(new Error(`Could not determine ${metaName} version from version ${stdout}`)) |
| 33 | + } |
| 34 | + |
| 35 | + const latestDocumentedRevision = latestDocumented.includes('rev')? latestDocumented : `${semver.parse(latestDocumented).version}+rev0` |
| 36 | + // semver.gt will ignore the revision numbers but still compare the version |
| 37 | + // If metaVersion <= latestDocumented then the latestDocumented version is a revision of the current metaVersion |
| 38 | + const latestVersion = semver.gt(metaVersion, latestDocumentedRevision) ? metaVersion : latestDocumentedRevision |
| 39 | + return callback(null, latestVersion) |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +module.exports = { |
| 44 | + addEntryToChangelog: { |
| 45 | + preset: 'prepend', |
| 46 | + fromLine: 3 |
| 47 | + }, |
| 48 | + getChangelogDocumentedVersions: { |
| 49 | + preset: 'changelog-headers', |
| 50 | + clean: /^v/ |
| 51 | + }, |
| 52 | + |
| 53 | + includeCommitWhen: 'has-changelog-entry', |
| 54 | + getIncrementLevelFromCommit: (commit) => { |
| 55 | + return 'patch' |
| 56 | + }, |
| 57 | + incrementVersion: (currentVersion, incrementLevel) => { |
| 58 | + if (isESR(currentVersion)) { |
| 59 | + const [majorVersion, minorVersion, patchVersion] = currentVersion.split('.', 3); |
| 60 | + return `${majorVersion}.${minorVersion}.${Number(patchVersion) + 1}` |
| 61 | + } |
| 62 | + const parsedCurrentVersion = semver.parse(currentVersion) |
| 63 | + if ( ! _.isEmpty(parsedCurrentVersion.build) ) { |
| 64 | + let revision = Number(String(parsedCurrentVersion.build).split('rev').pop()) |
| 65 | + if (!_.isFinite(revision)) { |
| 66 | + throw new Error(`Could not extract revision number from ${currentVersion}`) |
| 67 | + } |
| 68 | + return `${parsedCurrentVersion.version}+rev${revision + 1}` |
| 69 | + } |
| 70 | + return `${parsedCurrentVersion.version}` |
| 71 | + }, |
| 72 | + updateContract: (cwd, version, callback) => { |
| 73 | + if (/^\d+\.\d+\.\d+$/.test(version) == false && |
| 74 | + /^\d+\.\d+\.\d+\+rev\d+$/.test(version) == false) { |
| 75 | + return callback(new Error(`Invalid version ${version}`)); |
| 76 | + } |
| 77 | + |
| 78 | + const contract = path.join(cwd, 'balena.yml'); |
| 79 | + if (!fs.existsSync(contract)) { |
| 80 | + return callback(null, version); |
| 81 | + } |
| 82 | + |
| 83 | + const content = yaml.load(fs.readFileSync(contract, 'utf8')); |
| 84 | + content.version = version; |
| 85 | + fs.writeFile(contract, yaml.dump(content), callback); |
| 86 | + }, |
| 87 | + getCurrentBaseVersion: getMetaResinFromSubmodule, |
| 88 | + updateVersion: 'update-version-file', |
| 89 | + |
| 90 | + transformTemplateDataAsync: { |
| 91 | + preset: 'nested-changelogs', |
| 92 | + upstream: [ |
| 93 | + {{#upstream}} |
| 94 | + { |
| 95 | + pattern: '{{{pattern}}}', |
| 96 | + repo: '{{repo}}', |
| 97 | + owner: '{{owner}}', |
| 98 | + ref: '{{ref}}' |
| 99 | + }, |
| 100 | + {{/upstream}} |
| 101 | + ] |
| 102 | + }, |
| 103 | + |
| 104 | + template: 'default' |
| 105 | +} |
0 commit comments