aboutsummaryrefslogtreecommitdiff
path: root/generate-version-file
blob: d17edc32c7998b81598db97e96e7353bdd66b7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash

# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Nominally used to generate the "Version.java" file, but in theory could be used
# for any super simple template generation as well.

if [[ $# -lt 4 ]]; then
  echo "Usage: $(basename $0) <template-file> <template-variable> <value-file> <value-variable>" >&2
  echo ""
  echo 'For example, ./generate-version-file src/main/resources/org/testng/internal/VersionTemplateJava "@version@" kobalt/src/Build.kt "VERSION"'
  exit 1
fi

template_file="$1"
template_variable="$2"
value_file="$3"
value_variable="$4"

if ! [[ -f $template_file ]]; then
  echo "Error: Template file $template_file does not exist." >&2
  exit 1
fi

if ! [[ -f $value_file ]]; then
  echo "Error: Value file $value_File does not exist." >&2
  exit 1
fi

# Read a 'val VERSION = "SOME_VERSION"' from the file, trim down to $SOME_VERSION.
stored_value="$(egrep "val[[:space:]]+$value_variable" "$value_file" | awk 'NF>1{print $NF}' | tr -d '"')"
if [[ $? -ne 0 ]]; then
  echo "Error: Could not find value $value_variable in $value_file of syntax 'val $value_variable = \"SOME_VALUE\"'" >&2
  exit 1
fi

# Ensure that the template does indeed have @version@
if ! grep --silent "$template_variable" "$template_file"; then
  echo "Error: Template file $template_file has no instances of template variable $template_variable." >&2
  exit 1
fi

set -e

# Apply the template, replacing @version@ with the VERSION.
sed -e "s:$template_variable:$stored_value:g" "$template_file"