Home

Awesome

Version Stamping Tool (Rust Edition)

coverage Crates.io Docs.rs

A Rust package and command line tool for updating version information in ANY type of project.

Overview

Releasing a new project typically involves:

All of the above steps can be simplified with the use of this tool.

To use the tool for your project, simply:

  1. Place a version.json5 file in your project root that:
    • Describes the files that hold version numbers in your project
    • Which of the three actions (update, write or copy-in) to perform on each file
    • The types of version update operations you want to perform (incrMajor, incrMinor, etc..)
  2. Run stampver as part of your project's release script

Once you start using stampver you will be able to copy the version.json5 file in from another project and tweak it slightly in order to get set up quickly.

Command Line

The command line tool stampver is included in this crate using the cli feature flag, which is installed by default.

USAGE:
    stampver [OPTIONS] [OPERATION]

ARGS:
    <OPERATION>    The versioning operation to perform

OPTIONS:
    -h, --help                  Print help information
    -i, --input <INPUT_FILE>    Specify the version file explicitly
    -u, --update                Actually do the update
    -V, --version               Print version information

The tool will describe the actions that it is taking on each file so you can check that it is doing what you expect.

Expressions

This package uses the evalexpr to provide the ability to customize the different calculations and operations. All functions are available as described in the evalexpr except the regex functions. stampver adds the following variables/functions:

IdentifierArgument AmountArgument TypesDescription
now::year0Current UTC year
now::month0Current UTC month
now::day0Current UTC day of the month
if3Boolean/Any/AnyIf expression a is true then the value of b, else the value of c

stampver uses the Regex crate for regular expressions. You can use the amazing Regex101 site to develop and test your own regular expressions. Use the PCRE2 flavor of regular expressions for the most compatability with the Regex crate.

Schema File Format

Here's an annotated schema file format:

{
  vars: {
    major: 3,
    minor: 0,
    patch: 0,
    build: 20210902,
    revision: 0,
    sequence: 6,
    buildType: "test",
    debug: true,
  },
  calcVars: {
    nextBuild: "now::year * 10000 + now::month * 100 + now::day",
    nextSequence: "sequence + 1",
  },
  operations: {
    incrMajor: "major += 1; minor = 0; patch = 0; revision = 0; build = nextBuild",
    incrMinor: "minor += 1; patch = 0; revision = 0; build = nextBuild",
    incrPatch: "patch += 1; revision = 0; build = nextBuild",
    incrRevision: "revision += 1; build = nextBuild",
    incrSequence: "sequence += 1",
    setBetaBuild: 'buildType = "beta"',
    setProdBuild: 'buildType = "prod"',
  },
  targets: [
    {
      description: "JavaScript Files",
      files: ["src/version.js"],
      updates: [
        {
          search: '^(?P<begin>\\s*export\\s*const\\s*version\\s*=\\s*")\\d+\\.\\d+\\.\\d+(?P<end>";?)$',
          replace: 'begin + str::from(major) + "." + str::from(minor) + "." + str::from(patch) + end',
        },
        {
          search: '^(?P<begin>\\s*export\\s*const\\s*fullVersion\\s*=\\s*")\\d+\\.\\d+\\.\\d+\\+\\d+\\.\\d+(?P<end>";?)$',
          replace: 'begin + str::from(major) + "." + str::from(minor) + "." + str::from(patch) + "+" + str::from(build) + "." + str::from(revision) + end',
        },
      ],
    },
    {
      description: "Git Version Tag",
      files: ["scratch/version.tag.txt"],
      write: 'str::from(major) + "." + str::from(minor) + "." + str::from(patch)',
    },
    {
      description: "iOS PList",
      files: ["some-file.plist"],
      copyFrom: '"src/some-file" + if(buildType == "test", "-test", "-prod") + ".plist"',
    },
  ],
}

Because the format is JSON5 and a superset of JSON you can freely use comments. It is recommended to use Prettier or equivalent. This is not just to keep your file nicely formatted, but also because stampver needs to update the file it might get confused if the formatting is too different from the above.

The 4 main sections are as follows.

vars

This is where the version information lives, so in effect it is a simple version information database for you project. This is also the only section that the tool rewrites when version information is updated. It does it such a way as to preserve comments, but the tool does expect the layout to be like the example above.

calcVars

These are any variables that need to get generated each time the tool runs. This can include things like a build number that is based on the date, or a nextSequence number. The values in this section are merged with the vars, so be wary of naming conflicts.

operations

These are the different version operations for your project. incrMajor, incrMinor, incrPatch are typical operations, but you can add whatever makes sense for your project.

targets

targets is a array of objects containing a description, an array of files to update and then an action which must be exactly one of:

License

This package is distributed under the terms of the Unlicense license. See the UNLICENSE file for details.