Skip to content

Commit

Permalink
Scripts to automatically generate boilerplate (#504)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mcloonan authored Jan 31, 2025
1 parent 12530cc commit 6e23724
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 1 deletion.
39 changes: 39 additions & 0 deletions codegen/boilerplate.rb
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
20 changes: 20 additions & 0 deletions codegen/templates/api_versions.rb.erb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions codegen/templates/path_lookup_util.rb.erb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions codegen/templates/version.rb.erb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion scripts/codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 )/../"
Expand Down
47 changes: 47 additions & 0 deletions scripts/regen-boilerplate.sh
Original file line number Diff line number Diff line change
@@ -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 <all_api_versions (space-delimited)> -d <default_api_version> -l <library_version>" >&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}"

0 comments on commit 6e23724

Please sign in to comment.