Skip to content

Commit

Permalink
Generate version header
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Holecek <hluk@email.cz>
  • Loading branch information
hluk committed Sep 23, 2019
1 parent 02e69ff commit e35aab3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
41 changes: 15 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,21 @@ if(WITH_TESTS)
list(APPEND copyq_LIBRARIES Qt5::Test)
endif()

# Get application version.
if (EXISTS "version.txt")
file(STRINGS "version.txt" copyq_version)
endif()

if (NOT copyq_version)
find_package(Git)
if(GIT_FOUND)
execute_process(COMMAND
"${GIT_EXECUTABLE}" describe
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
RESULT_VARIABLE copyq_git_describe_result
OUTPUT_VARIABLE copyq_git_describe_output
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(copyq_git_describe_result EQUAL 0)
set(copyq_version "${copyq_git_describe_output}")
endif()
endif()
endif()

if (copyq_version)
message(STATUS "Building CopyQ version ${copyq_version}.")
add_definitions( -DCOPYQ_VERSION="${copyq_version}" )
endif()
# Generate version.h from current git commit if available.
add_custom_target(
generate_version_header
ALL
COMMAND ${CMAKE_COMMAND}
-D INPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/src/common/version.h.in
-D OUTPUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/src/common/version.h
-P ${CMAKE_CURRENT_SOURCE_DIR}/version.cmake
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/src/common/version.h
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
set_source_files_properties(
${CMAKE_CURRENT_BINARY_DIR}/src/common/version.h
PROPERTIES GENERATED TRUE
)

if (UNIX AND NOT APPLE)
install(FILES ${copyq_ICON_NORMAL} DESTINATION ${ICON_INSTALL_PREFIX} RENAME ${ICON_NAME}.svg)
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ file(GLOB copyq_SOURCES
main.cpp
app/*.cpp
common/*.cpp
common/version.h
gui/*.cpp
item/*.cpp
scriptable/*.cpp
Expand Down
8 changes: 0 additions & 8 deletions src/common/version.h

This file was deleted.

6 changes: 6 additions & 0 deletions src/common/version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef VERSION_H
#define VERSION_H

#define COPYQ_VERSION "${copyq_version}"

#endif // VERSION_H
24 changes: 13 additions & 11 deletions utils/bump_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set -euo pipefail

version=$1

version_file=src/common/version.h
version_file=version.cmake
appdata_file=shared/com.github.hluk.copyq.appdata.xml
itemwidget_file=src/item/itemwidget.h
changes_file=CHANGES
Expand All @@ -35,26 +35,28 @@ check_changes() {
fi
}

fix_header() {
fix_file() {
file=$1
prefix=$2
format=$2

sed -i 's#\('"$prefix"'\).*#\1'"$version"'"#' "$file"
pattern=$(printf "$format" '[0-9]\.[0-9]\.[0-9]')
text=$(printf "$format" "$version")
sed -i "s|$pattern|$text|" "$file"

new_version=$(grep -o "$prefix"'[0-9]\.[0-9]\.[0-9]"$' "$file")
if [[ "$new_version" != "$prefix$version\"" ]]; then
new_version=$(grep -o "$pattern" "$file")
if [[ "$new_version" != "$text" ]]; then
echo "Failed to replace version in $file"
exit 1
fi
}

fix_version_header() {
fix_header "$version_file" 'define COPYQ_VERSION "v'
fix_version_file() {
fix_file "$version_file" 'set(copyq_version "v%s")'
}

fix_itemwidget() {
fix_header "$itemwidget_file" \
'define COPYQ_PLUGIN_ITEM_LOADER_ID "com.github.hluk.copyq.itemloader/'
fix_file "$itemwidget_file" \
'#define COPYQ_PLUGIN_ITEM_LOADER_ID "com.github.hluk.copyq.itemloader/%s"'
}

fix_appdata() {
Expand All @@ -79,7 +81,7 @@ fix_appdata() {

check_version_format
check_changes
fix_version_header
fix_version_file
fix_itemwidget
fix_appdata
git commit -a -m "v$version"
Expand Down
19 changes: 19 additions & 0 deletions version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set(copyq_version "v3.9.2")

find_package(Git)
if(GIT_FOUND)
execute_process(COMMAND
"${GIT_EXECUTABLE}" describe --tags
RESULT_VARIABLE copyq_git_describe_result
OUTPUT_VARIABLE copyq_git_describe_output
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(copyq_git_describe_result EQUAL 0)
set(copyq_version "${copyq_git_describe_output}")
endif()
endif()

message(STATUS "Building CopyQ version ${copyq_version}.")

configure_file("${INPUT_FILE}" "${OUTPUT_FILE}")

0 comments on commit e35aab3

Please sign in to comment.