From 6e237241ff7502a4d48c45a8c10760d0cfef0d1c Mon Sep 17 00:00:00 2001 From: mcloonan Date: Fri, 31 Jan 2025 09:07:50 -0500 Subject: [PATCH] Scripts to automatically generate boilerplate (#504) * Adding bash portion of new generator for boilerplate files. Next commit will contain ruby/erb portion. Change-Id: I7c91064413ebec4639b8afef17aa8a981a88cedd * Adding ERB generation for boilerplate files. Change-Id: I9ed25d53bb73882d7f5daecb5f946f94a802725d * More useful command line message. Change-Id: I146e0b986efaa21076a233f3a6898291c4438520 --- codegen/boilerplate.rb | 39 +++++++++++++++++++ codegen/{main.rb => factories.rb} | 0 codegen/templates/api_versions.rb.erb | 20 ++++++++++ codegen/templates/path_lookup_util.rb.erb | 31 +++++++++++++++ codegen/templates/version.rb.erb | 27 +++++++++++++ scripts/codegen.sh | 2 +- scripts/regen-boilerplate.sh | 47 +++++++++++++++++++++++ 7 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 codegen/boilerplate.rb rename codegen/{main.rb => factories.rb} (100%) create mode 100644 codegen/templates/api_versions.rb.erb create mode 100644 codegen/templates/path_lookup_util.rb.erb create mode 100644 codegen/templates/version.rb.erb create mode 100755 scripts/regen-boilerplate.sh diff --git a/codegen/boilerplate.rb b/codegen/boilerplate.rb new file mode 100644 index 000000000..377331995 --- /dev/null +++ b/codegen/boilerplate.rb @@ -0,0 +1,39 @@ +DIR = File.dirname(__FILE__) +GEM_ROOT = File.expand_path("..", DIR) +$: << DIR + +require 'src/rendering' + +library_version = ARGV[0] +all_api_versions = ARGV[1].split(",") +default_api_version = ARGV[2] + +puts library_version +puts all_api_versions +puts default_api_version + +gads_dir = File.join(GEM_ROOT, "lib", "google", "ads", "google_ads") +render_template( + File.join(DIR, "templates", "version.rb.erb"), + File.join(gads_dir, "version.rb"), + {library_version: library_version} +) + +render_template( + File.join(DIR, "templates", "api_versions.rb.erb"), + File.join(gads_dir, "api_versions.rb"), + { + all_api_versions: all_api_versions.map {|v| ":v#{v}"}.join(", "), + default_api_version: ":v#{default_api_version}", + } +) + +utils_dir = File.join(gads_dir, "utils") +all_api_versions.each do |version| + path_util_dir = File.join(utils_dir, "v#{version}") + render_template( + File.join(DIR, "templates", "path_lookup_util.rb.erb"), + File.join(path_util_dir, "path_lookup_util.rb"), + { version: "V#{version}" } + ) +end diff --git a/codegen/main.rb b/codegen/factories.rb similarity index 100% rename from codegen/main.rb rename to codegen/factories.rb diff --git a/codegen/templates/api_versions.rb.erb b/codegen/templates/api_versions.rb.erb new file mode 100644 index 000000000..999d3c252 --- /dev/null +++ b/codegen/templates/api_versions.rb.erb @@ -0,0 +1,20 @@ +module Google + module Ads + module GoogleAds + KNOWN_API_VERSIONS = [<%= all_api_versions %>] + DEFAULT_API_VERSION = <%= default_api_version %> + + def self.default_api_version + DEFAULT_API_VERSION + end + + def self.known_api_versions + KNOWN_API_VERSIONS + end + + def self.valid_version?(version) + known_api_versions.include?(version) + end + end + end +end diff --git a/codegen/templates/path_lookup_util.rb.erb b/codegen/templates/path_lookup_util.rb.erb new file mode 100644 index 000000000..72323558f --- /dev/null +++ b/codegen/templates/path_lookup_util.rb.erb @@ -0,0 +1,31 @@ +# Encoding: utf-8 +# +# Copyright 2022 Google LLC +# +# 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 +# +# https://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. +# +# Utility that generates up resource names for entities given IDs. + +require "google/ads/google_ads/utils/build_path_lookup_class" + +module Google + module Ads + module GoogleAds + module Utils + module <%= version %> + PathLookupUtil = Utils.build_path_lookup_class(:<%= version.downcase %>) + end + end + end + end +end diff --git a/codegen/templates/version.rb.erb b/codegen/templates/version.rb.erb new file mode 100644 index 000000000..221ada3f9 --- /dev/null +++ b/codegen/templates/version.rb.erb @@ -0,0 +1,27 @@ +# Encoding: utf-8 +# +# Copyright 2018 Google LLC +# +# 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 +# +# https://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. +# +# Module to keep the current library version. + +module Google + module Ads + module GoogleAds + CLIENT_LIB_NAME = 'gccl'.freeze + CLIENT_LIB_VERSION = '<%= library_version %>'.freeze + VERSION = CLIENT_LIB_VERSION + end + end +end diff --git a/scripts/codegen.sh b/scripts/codegen.sh index 5793209ac..388a91aa8 100755 --- a/scripts/codegen.sh +++ b/scripts/codegen.sh @@ -3,7 +3,7 @@ set -euxo pipefail rm -rf lib/google/ads/google_ads/factories mkdir -p lib/google/ads/google_ads/factories -bundle exec ruby codegen/main.rb +bundle exec ruby codegen/factories.rb bundle exec standardrb --fix -- lib/google/ads/google_ads/factories/**/*.rb lib/google/ads/google_ads/factories.rb GEM_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../" diff --git a/scripts/regen-boilerplate.sh b/scripts/regen-boilerplate.sh new file mode 100755 index 000000000..26f6257a7 --- /dev/null +++ b/scripts/regen-boilerplate.sh @@ -0,0 +1,47 @@ +#!/bin/bash +set -euxo pipefail + +# Initialize variables to indicate if the arguments are set +all_api_versions_set=false +default_api_version_set=false +library_version_set=false + +# Use getopts to parse the arguments +while getopts ":a:d:l:" opt; do + case $opt in + a) all_api_versions="$OPTARG"; all_api_versions_set=true ;; + d) default_api_version="$OPTARG"; default_api_version_set=true ;; + l) library_version="$OPTARG"; library_version_set=true ;; + \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; + :) echo "Option -$OPTARG requires an argument." >&2; exit 1 ;; + esac +done + +# Check if all required arguments are set +if ! $all_api_versions_set || ! $default_api_version_set || ! $library_version_set; then + echo "Error: Missing required arguments." >&2 + echo "Usage: $0 -a -d -l " >&2 + exit 1 +fi + +echo "All API Versions: $all_api_versions" +echo "Default API Version: $default_api_version" +echo "Library Version: $library_version" +all_api_versions_commas=$(echo "$all_api_versions" | tr ' ' ',') +echo "All API Versions (commas): $all_api_versions_commas" + +# Remove all path utils, then make new empty folders for new versions +find ../lib/google/ads/google_ads/utils/ -maxdepth 1 -type d -name 'v[0-9]*' -exec rm -r {} + || true +for version in $all_api_versions; do + # Create the directory for each version + mkdir -p "../lib/google/ads/google_ads/utils/v${version}" +done + +# Remove the API versions file +rm ../lib/google/ads/google_ads/api_versions.rb || true + +# Remove the version.rb file +rm ../lib/google/ads/google_ads/version.rb || true + +# Run the generator to create all the new files +ruby codegen/boilerplate.rb "${library_version}" "${all_api_versions_commas}" "${default_api_version}"