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