-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prepare): add support for bumping package build number (#155)
* feat(versionCode): implement version code generation logic * docs: add description for new option item `useVersionCode` * test(versionCode): add test case for bumping version codes * refactor(versionCode): rename option `useVersionCode` to `updateBuildNumber` * refactor(versionCode): make version code modifying logic to throw suitable error * refactor(versionCode): now logging for `prepare` step will print actual version number * docs(readme): update description for `updateBuildNumber` option * docs(readme): update `updateBuildNumber` option description Co-authored-by: Joshua Tang <joshuaystang@gmail.com> --------- Co-authored-by: Joshua Tang <joshuaystang@gmail.com>
- Loading branch information
Showing
8 changed files
with
100 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,46 @@ | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import { PrepareContext } from "semantic-release"; | ||
import { parse } from "yaml"; | ||
import { PrepareContext } from "semantic-release"; | ||
import SemanticReleaseError from "@semantic-release/error"; | ||
|
||
import { Pubspec } from "./schemas.js"; | ||
import { PluginConfig } from "./types.js"; | ||
import { getConfig } from "./utils.js"; | ||
|
||
const PUBSPEC_PATH = "pubspec.yaml"; | ||
|
||
export const prepare = async ( | ||
_pluginConfig: PluginConfig, | ||
pluginConfig: PluginConfig, | ||
{ nextRelease: { version }, logger }: PrepareContext, | ||
) => { | ||
const { updateBuildNumber } = getConfig(pluginConfig); | ||
|
||
const data = readFileSync(PUBSPEC_PATH, "utf-8"); | ||
const pubspec = Pubspec.parse(parse(data)); | ||
const pubspecVersionEscaped = pubspec.version.replace( | ||
/[/\-\\^$*+?.()|[\]{}]/g, | ||
"\\$&", | ||
); | ||
|
||
let nextVersion = version; | ||
if (updateBuildNumber) { | ||
const parts = pubspec.version.split("+"); | ||
const buildNumber = parts.length > 1 ? Number(parts[1]) : 0; | ||
|
||
if (isNaN(buildNumber)) { | ||
throw new SemanticReleaseError( | ||
`Invalid build number: ${buildNumber} in ${pubspec.version}`, | ||
); | ||
} | ||
|
||
nextVersion = `${version}+${buildNumber + 1}`; | ||
} | ||
|
||
const newData = data.replace( | ||
new RegExp(`version:[ \t]+${pubspecVersionEscaped}`), | ||
`version: ${version}`, | ||
`version: ${nextVersion}`, | ||
); | ||
|
||
logger.log(`Writing version ${version} to ${PUBSPEC_PATH}`); | ||
logger.log(`Writing version ${nextVersion} to ${PUBSPEC_PATH}`); | ||
writeFileSync(PUBSPEC_PATH, newData); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export type PluginConfig = { | ||
cli: "dart" | "flutter"; | ||
publishPub: boolean; | ||
updateBuildNumber: boolean; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters