Skip to content

Commit

Permalink
Allow increment after successful upload + tidyup (#4)
Browse files Browse the repository at this point in the history
* Use guard file to prevent increment if upload fail
  • Loading branch information
pfeerick authored Jun 4, 2020
1 parent 6afffab commit 435289f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 90 deletions.
45 changes: 30 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
# Platformio Version Increment
Simple version increment script for Platformio.
# PlatformIO Version Increment
Simple version increment script for PlatformIO.

_Platformio does not have a tool to automatically increment the version number of an app when building it or when uploading it to a microcontroller so I decided to write a script to do it._
_PlatformIO does not have a tool to automatically increment the version number of an app when building it or when uploading it to a microcontroller so I decided to write a script to do it._

[![GitHub version](https://img.shields.io/github/v/release/sblantipodi/platformio_version_increment.svg)](https://img.shields.io/github/v/release/sblantipodi/platformio_version_increment.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://GitHub.com/sblantipodi/platformio_version_increment/graphs/commit-activity)
[![DPsoftware](https://img.shields.io/static/v1?label=DP&message=Software&color=orange)](https://www.dpsoftware.org)

If you like **Platformio Version Increment**, give it a star, or fork it and contribute!
If you like **PlatformIO Version Increment**, give it a star, or fork it and contribute!

[![GitHub stars](https://img.shields.io/github/stars/sblantipodi/platformio_version_increment.svg?style=social&label=Star)](https://github.com/sblantipodi/platformio_version_increment/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/sblantipodi/platformio_version_increment.svg?style=social&label=Fork)](https://github.com/sblantipodi/platformio_version_increment/network)

## Credits
- Davide Perini

## How To
To use it please add this line in your platformio.ini
## How To Use
1) From the root of your project run
```
extra_scripts = pre:platformio_version_increment/version_increment.py
git submodule add https://github.com/sblantipodi/platformio_version_increment.git platformio_version_increment
```

from the root of your project run
2) Add the following in your `platformio.ini`:
```
git submodule add https://github.com/sblantipodi/platformio_version_increment.git platformio_version_increment
extra_scripts =
pre:platformio_version_increment/version_increment_pre.py
post:platformio_version_increment/version_increment_post.py
```

upload your software to your microcontroller, in the root of your project you will find two files:
3) Add `#include "Version.h"` to the top of your main source file (i.e. `main.cpp`).

4) Build your software for your microcontroller. In the root of your project you will find two files:
- version
- include/Version.h

The `version` file by default will have `0.1.0`, but you can edit it with the version number you wish to start incrementing from.
The `version` file will default to `0.1.0`, but you can edit this with the version number you wish to start incrementing from.

Every upload will trigger a +1 on the hotfix number.
Every completed upload will trigger a +1 on the patch number.

In the `Version.h` file (which you'll need to include in order to access the incrementing version and timestamp variables) you'll have this:
```c++
// AUTO GENERATED FILE FROM version_increment.py, DO NOT EDIT THIS FILE
// AUTO GENERATED FILE, DO NOT EDIT THIS FILE
#ifndef VERSION
#define VERSION "0.1.0"
#endif
#ifndef BUILD_TIMESTAMP
#define BUILD_TIMESTAMP "2020-04-10 17:58:52.937616"
#endif
```
use variables as you desire.

Note: This script does not check for a successfull upload, in the case of an upload failure, please discard the increment manually.
You now have auto-incrementing VERSION and BUILD_TIMESTAMP variables you can use in your program as you wish!

For example:
```c++
Serial.println("Project version: " + String(VERSION));
Serial.println("Build timestamp:" + String(BUILD_TIMESTAMP));
```

or

```c++
Serial.printf("Project version v%s, built %s\n",VERSION,BUILD_TIMESTAMP);
```

## License
This program is licensed under MIT License
75 changes: 0 additions & 75 deletions version_increment.py

This file was deleted.

31 changes: 31 additions & 0 deletions version_increment_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
""" Remove version increment guard file if present """
import os

Import("env")

## DO NOT EDIT THIS FILE, edit version file if you want to start from a different version
#
# version_increment_post.py - Simple versioning script for Platformio
#
# Copyright (C) 2020 Davide Perini
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# You should have received a copy of the MIT License along with this program.
# If not, see <https://opensource.org/licenses/MIT/>.
#

def remove_guard_file(source, target, env):
""" Remove version increment guard file if present """
if os.path.exists(".version_no_increment"):
os.remove(".version_no_increment")

env.AddPostAction("upload", remove_guard_file)
74 changes: 74 additions & 0 deletions version_increment_pre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
""" Create version header and tracker file if missing """
import datetime
import os

Import("env")

## DO NOT EDIT THIS FILE, edit version file if you want to start from a different version
#
# version_increment_pre.py - Simple versioning script for Platformio
#
# Copyright (C) 2020 Davide Perini
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# You should have received a copy of the MIT License along with this program.
# If not, see <https://opensource.org/licenses/MIT/>.
#

VERSION_FILE = 'version'
VERSION_HEADER = 'Version.h'
VERSION_PREFIX = '0.1.'
VERSION_PATCH_NUMBER = 0

if not os.path.exists(".version_no_increment"):
try:
with open(VERSION_FILE) as FILE:
VERSION_PATCH_NUMBER = FILE.readline()
VERSION_PREFIX = VERSION_PATCH_NUMBER[0:VERSION_PATCH_NUMBER.rindex('.')+1]
VERSION_PATCH_NUMBER = int(VERSION_PATCH_NUMBER[VERSION_PATCH_NUMBER.rindex('.')+1:]) + 1
except:
print('No version file found or incorrect data in it. Starting from 0.1.0')
VERSION_PATCH_NUMBER = 0
with open(VERSION_FILE, 'w+') as FILE:
FILE.write(VERSION_PREFIX + str(VERSION_PATCH_NUMBER))
print('Build number: {}'.format(VERSION_PREFIX + str(VERSION_PATCH_NUMBER)))

HEADER_FILE = """
// AUTO GENERATED FILE, DO NOT EDIT
#ifndef VERSION
#define VERSION "{}"
#endif
#ifndef BUILD_TIMESTAMP
#define BUILD_TIMESTAMP "{}"
#endif
""".format(VERSION_PREFIX + str(VERSION_PATCH_NUMBER), datetime.datetime.now())

if os.environ.get('PLATFORMIO_INCLUDE_DIR') is not None:
VERSION_HEADER = os.environ.get('PLATFORMIO_INCLUDE_DIR') + "/" + VERSION_HEADER
elif os.path.exists("include"):
VERSION_HEADER = "include/" + VERSION_HEADER
else:
PROJECT_DIR = env.subst("$PROJECT_DIR")
os.mkdir(PROJECT_DIR + "/include")
VERSION_HEADER = "include/" + VERSION_HEADER

with open(VERSION_HEADER, 'w+') as FILE:
FILE.write(HEADER_FILE)

open('.version_no_increment', 'a').close()
else:
if os.path.exists("version"):
FILE = open(VERSION_FILE)
VERSION_NUMBER = FILE.readline()
print('Build number: {} (waiting for upload before next increment)'.format(str(VERSION_NUMBER)))
else:
print('No version file found or incorrect data in it!!')

0 comments on commit 435289f

Please sign in to comment.