1#!/usr/bin/env bash 2 3# Copyright (C) 2016 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18# Nominally used to generate the "Version.java" file, but in theory could be used 19# for any super simple template generation as well. 20 21if [[ $# -lt 4 ]]; then 22 echo "Usage: $(basename $0) <template-file> <template-variable> <value-file> <value-variable>" >&2 23 echo "" 24 echo 'For example, ./generate-version-file src/main/resources/org/testng/internal/VersionTemplateJava "@version@" kobalt/src/Build.kt "VERSION"' 25 exit 1 26fi 27 28template_file="$1" 29template_variable="$2" 30value_file="$3" 31value_variable="$4" 32 33if ! [[ -f $template_file ]]; then 34 echo "Error: Template file $template_file does not exist." >&2 35 exit 1 36fi 37 38if ! [[ -f $value_file ]]; then 39 echo "Error: Value file $value_File does not exist." >&2 40 exit 1 41fi 42 43# Read a 'val VERSION = "SOME_VERSION"' from the file, trim down to $SOME_VERSION. 44stored_value="$(egrep "val[[:space:]]+$value_variable" "$value_file" | awk 'NF>1{print $NF}' | tr -d '"')" 45if [[ $? -ne 0 ]]; then 46 echo "Error: Could not find value $value_variable in $value_file of syntax 'val $value_variable = \"SOME_VALUE\"'" >&2 47 exit 1 48fi 49 50# Ensure that the template does indeed have @version@ 51if ! grep --silent "$template_variable" "$template_file"; then 52 echo "Error: Template file $template_file has no instances of template variable $template_variable." >&2 53 exit 1 54fi 55 56set -e 57 58# Apply the template, replacing @version@ with the VERSION. 59sed -e "s:$template_variable:$stored_value:g" "$template_file" 60