Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save beccasaurus/570ae7bb4fb819c87ef029bafd61f28e to your computer and use it in GitHub Desktop.
Save beccasaurus/570ae7bb4fb819c87ef029bafd61f28e to your computer and use it in GitHub Desktop.
_-:: 🎨 Generate Code Samples for 'GAPIC' Client Libraries πŸ“‘ ::-_

$ samplegen

Simple utility script for generating and managing code samples for GAPIC client libraries.

FYI:Β this can also be used as a general purpose tool for generating GAPIC client libraries.

Here Be Dragons πŸ‰

This is my personal swiss army knife for performing sample generation operations.
It may change at any time. It's all held together with scotch tape and rubber bands.

Authoring Samples

# Download samplegen script (latest)
curl -O https://gist.githubusercontent.com/beccasaurus/570ae7bb4fb819c87ef029bafd61f28e/raw/samplegen

# Clone required repositories (clones repos into your workspace)
samplegen --clone

# Print out all of the samples for the Language API (right now there should be none)
samplegen videointelligence --print

Note: run ./samplegen [command] if you're running the script directly versus adding to your PATH

That's all, folks!


If you want to add a sample and generate it...

# Edit the GAPIC config file for the api/library you'd like to add samples to
samplegen speech v1p1beta1 --edit --gapic-config

# Generate the library with samples
samplegen speech v1p1beta1 --print

If you want to edit the .snip template file for generated samples...

# Edit the .snip for a given language
samplegen --edit -l php

# Print out the sample after changed were made to the template file
# You must --rebuild which builds gapic-generator to use the updated .snip
samplegen speech --print --rebuild

$ samplegen

Generates Code Samples for GAPIC-generated client libraries
    https://github.com/googleapis/gapic-generator

Usage:
  samplegen --help
  samplegen --clone
  samplegen speech --print
  samplegen speech v1 --print
  samplegen speech v1 --edit
  samplegen speech v1 --gapic-generator ~/my-local-gapic-generator
                      --googleapis      ~/my-local-googleapis
                      --output          ~/mysamples/
                      --region-tag      speech_transcribe_file
                      --language        python

Options:
  -a, --artman           Reserved (for passing additional info/flags to arman)
                           Passed the provided arguments to `artman` invocation
                           Not currently useful

  -b, --bash             Rather than generating the library, open the bash
                           prompt in the provided Docker image (with mounts)

  -c, --config           Path to persisted configuration YAML file
                           The following are searched, in order:
                             - Explicitly provided --config argument
                             - SAMPLEGEN_CONFIG environment variable
                             - samplegen.yml in local directory
                             - [workspace]/config.yml

  --clone                Clones googleapis & gapic-generator repositories
                           into your workspace, e.g. ~/.samplegen by default

  -d, --docker           Path to docker command
                           Default: docker

  --dry-run              Performs no code generation, prints commands instead
                           Example: speech v1 --dry-run

  -e, --edit             Opens gapic config and language .snip in your $EDITOR
                           Pass --gapic-config or --snip to open only one file

  --enable-dev_samples   Edits GAPIC files to enable sample generation in
                           unstable languages. Run with --rebuild to apply

  --gapic-config         Print full path to api_gapic.yaml in googleapis
                           Example: language --gapic-config

  --gapic-generator      Directory to gapic-generator project
                           Default: [workspace]/gapic-generator

  --googleapis           Directory to googleapis project
                           Default: [workspace]/googleapis

  -i, --image            Docker image to execute `artman`
                           Default: googleapis/artman

  --info                 Prints configuration values and version

  -l, --language         Programming language to genererate samples for
                           Default: python

  --no-gen               Sample generation is not run but other operations are
                           whereas --dry-run does not execute any operations

  -o, --output           Directory to save sample code files
                           Default: .

  -p, --print            Print samples rather than saving them
                           Example: --print (pygmentize used when available)
                           Files will also be saved if explicit --output provided

  --pull                 Pull latest googleapis, gapic-generator, and googleapis/artman

  --rebuild              Rebuilds gapic-generator before running
                           Executes: ./gradlew fatJar (for .snip changes)

  -r, --region-tag       Filter subset of region tags to save
                           Example: -r sentiment_text

  -s, --save             Persist passed options to config file
                           Example: --googleapis ../api --save

  --snip                 Print full path to .snip sample template in gapic-generator
                           Example: --language python --snip

  --version              Prints the version number of this script

  -w, --workspace        Root directory of typical directory structure
                           Default: ~/.samplegen
                           Environment Variable: SAMPLEGEN_WORKSPACE

This is stupid, I don't want to use a lame Ruby script

Um, okay. Fine then. 🀷🏼

Here is a 2-line script that works today for generating any APIs in googleapis/ and will save output to your current directory

Quickstart Script (no dependencies except docker)

Pick an API by finding an artman_[api].yaml file in https://github.com/googleapis/googleapis/tree/master/google

The following example is for cloudkms:

docker pull googleapis/artman
docker run --rm --workdir /googleapis --volume `pwd`:/googleapis/artman-genfiles \
  --env RUNNING_IN_ARTMAN_DOCKER=True googleapis/artman /bin/bash -c    \
  "artman --local --config /googleapis/google/cloud/kms/artman_cloudkms.yaml generate python_gapic"

Unabridged Tutorial

Here are unabridged instructions, Ms "Do It Yourself" ...

# NOTE: The following instructions are not specific to Code Samples.
#       These instructions can be used to generate client libraries.

# Dependencies: docker, java

# (Optional) Clone the 2 repositories you'll need

# [gapic-generator] – The GAPIC generator (Java project)
git clone https://github.com/googleapis/gapic-generator.git

# [googleapis] – Repository containing necessary artifacts for generating
#                client libraries, e.g. .proto files, and [api]_gapic.yaml
#                configuration files (where samples are authored as YAML),
#                and artman_[api].yaml files which are simple files containing
#                paths/options which `artman` used to invoke GAPIC generator.
#
#                Why use artman? Because we have a published Docker image pre-built
#                with BAGILLIONS of dependencies needed to successfully run the generator.
#                               ... yes "bagillions" ... I stand by this assessment ;)
git clone https://github.com/googleapis/googleapis.git

# Cloning these repos locally is optional!
# The docker image we'll be using pulls the latest HEAD versions of googleapis and gapic-generator by default
# But I'll show how to use local directories because these instructions are for setting up a DEVELOPMENT environment

# The commands will need your repository paths, so I'll put them into variables:
GOOGLEAPIS_PATH="`pwd`/googleapis"
GAPIC_GENERATOR_PATH="`pwd`/googleapis"

# Now choose an existing, published API from googleapis –
# We will use its existing artman_[api].yaml config file to generate that API.
# You can find this file alongside the API's .proto, gapic config, etc.
# Let's pick an API... um... KMS! Let's search for the artman file...
cd $GOOGLEAPIS_PATH
ARTMAN_CONFIG_PATH=`git ls-files | grep artman_ | grep kms | grep .yaml`

# ARTMAN_CONFIG_PATH should equal google/cloud/kms/artman_cloudkms.yaml (relative to googleapis)
#
# Feel free to take a look at the artman config, it's very small:
# https://github.com/googleapis/googleapis/blob/master/google/cloud/kms/artman_cloudkms.yaml

# Now let's generate a KMS client library!

# Pull the docker image we'll be using...
#   (you can think of this as the GAPIC generator's official Docker image)
#   Source Dockerfile: https://github.com/googleapis/artman/blob/master/Dockerfile
#                      ^--- work a quick skim/read – shows how to install dependencies to run GAPIC generator
DOCKER_IMAGE=googleapis/artman
docker pull $DOCKER_IMAGE

# Now you're ready!

# Pick a language – we'll just generate 1 in this tutorial
LANGUAGE=python

# We'll be MOUNTING your local googleapis and gapic-generator repos
# and invoking artman passing our artman config file for KMS

# Let's create the artman command first... noting that googleapis will be mounted at /googleapis
ARTMAN_COMMAND="artman --local --config /googleapis/$ARTMAN_CONFIG_PATH generate $LANGUAGE_gapic"

# Sweet, let's create the main docker command (we'll kick off the ^ ARTMAN_COMMAND against the container we run)
DOCKER_COMMAND="docker run --rm
--volume  $GAPIC_GENERATOR_PATH:/toolkit
--volume  $GOOGLEAPIS_PATH:/googleapis
--workdir /googleapis
--env     RUNNING_IN_ARTMAN_DOCKER=True
$DOCKER_IMAGE"

# Sweet! Let's run it y'll! 🀘

FULL_DOCKER_COMMAND="$DOCKER_COMMAND /bin/bash -c \"$ARTMAN_COMMAND\""

$FULL_DOCKER_COMMAND

# If this worked, you should see: "Code generated: /googleapis/artman-genfiles/python/kms-v1"

# Take a look at all of the generated files :)
find $GOOGLEAPIS/artman-genfiles/

# πŸ“ MAKING EDITS / CHANGES

# If you want to add/change code samples, you'll want to edit the API's _gapic.yaml "GAPIC config"
# The GAPIC config is used to configure TONS of things for library generation – *including samples*

# Following with the KMS theme...
GAPIC_CONFIG_PATH=`git ls-files | grep gapic | grep kms | grep .yaml`

# Change something!
vi $GAPIC_CONFIG_PATH

# Here's an example of adding a code sample...
# We'll make a sample which lists KMS key rings under the ListKeyRings rpc method
# https://cloud.google.com/kms/docs/reference/rpc/google.cloud.kms.v1#google.cloud.kms.v1.KeyManagementService.ListKeyRings

# Add the following to the yaml config under the `- name: ListKeyRings` config section for the ListKeyRings rpc API method

methods:
- name: ListKeyRings
  samples:
    standalone:
    - calling_forms: ".*"
      value_sets: list_key_rings
      region_tag: list_key_rings
  sample_value_sets:
  - id: list_key_rings
    title: "List Key Rings"
    description: "List Key Rings"
    parameters:
      defaults:
      - parent: "Full path to location, e.g. projects/MY-GCP-PROJECT/locations/global"
      attributes:
      - parameter: parent
        sample_argument: true
    on_success:
    - loop:
      variable: key
      collection: $resp.key_rings
      body:
        print:
          - "Key ring name: %s"
          - key.name

# Now that you've added a sample, run the generator.
# There will be a new ./samples/ directory in the folder of the generated client library.

$FULL_DOCKER_COMMAND

find $GOOGLEAPIS_PATH/artman-genfiles/python/kms-v1/samples/google/cloud/kms_v1/gapic/list_key_rings/
# => artman-genfiles/python/kms-v1/samples/google/cloud/kms_v1/gapic/list_key_rings/list_key_rings_request_paged_list_key_rings.py
# => artman-genfiles/python/kms-v1/samples/google/cloud/kms_v1/gapic/list_key_rings/list_key_rings_request_paged_all_list_key_rings.py

# Sweet, right?

# Now let's edit the template file for generated Python samples

# 1. Edit the template and change something
cd $GAPIC_GENERATOR_PATH
vi src/main/resources/com/google/api/codegen/py/standalone_sample.snip

# 2. Recompile gapic-generator (required)
./gradlew fatJar

# 3. Run generation again & you should see the changes!
cd $GOOGLEAPIS_PATH
$FULL_DOCKER_COMMAND
#! /usr/bin/env ruby
def usage
puts %{Generates Code Samples for GAPIC-generated client libraries
https://github.com/googleapis/gapic-generator
Usage:
samplegen --help
samplegen --clone
samplegen speech --print
samplegen speech v1 --print
samplegen speech v1 --edit
samplegen speech v1 --gapic-generator ~/my-local-gapic-generator
--googleapis ~/my-local-googleapis
--output ~/mysamples/
--region-tag speech_transcribe_file
--language python
Options:
-a, --artman Reserved (for passing additional info/flags to arman)
Passed the provided arguments to `artman` invocation
Not currently useful
-b, --bash Rather than generating the library, open the bash
prompt in the provided Docker image (with mounts)
-c, --config Path to persisted configuration YAML file
The following are searched, in order:
- Explicitly provided --config argument
- SAMPLEGEN_CONFIG environment variable
- samplegen.yml in local directory
- [workspace]/config.yml
--clone Clones googleapis & gapic-generator repositories
into your workspace, e.g. ~/.samplegen by default
-d, --docker Path to docker command
Default: docker
--dry-run Performs no code generation, prints commands instead
Example: speech v1 --dry-run
-e, --edit Opens gapic config and language .snip in your $EDITOR
Pass --gapic-config or --snip to open only one file
--enable-dev_samples Edits GAPIC files to enable sample generation in
unstable languages. Run with --rebuild to apply
--gapic-config Print full path to api_gapic.yaml in googleapis
Example: language --gapic-config
--gapic-generator Directory to gapic-generator project
Default: [workspace]/gapic-generator
--googleapis Directory to googleapis project
Default: [workspace]/googleapis
-i, --image Docker image to execute `artman`
Default: googleapis/artman
--info Prints configuration values and version
-l, --language Programming language to genererate samples for
Default: python
--no-gen Sample generation is not run but other operations are
whereas --dry-run does not execute any operations
-o, --output Directory to save sample code files
Default: .
-p, --print Print samples rather than saving them
Example: --print (pygmentize used when available)
Files will also be saved if explicit --output provided
--pull Pull latest googleapis, gapic-generator, and googleapis/artman
--rebuild Rebuilds gapic-generator before running
Executes: ./gradlew fatJar (for .snip changes)
-r, --region-tag Filter subset of region tags to save
Example: -r sentiment_text
-s, --save Persist passed options to config file
Example: --googleapis ../api --save
--snip Print full path to .snip sample template in gapic-generator
Example: --language python --snip
--version Prints the version number of this script
-w, --workspace Root directory of typical directory structure
Default: ~/.samplegen
Environment Variable: SAMPLEGEN_WORKSPACE
}
true # return true for `usage && ...` expressions
end
usage && exit if ARGV.empty?
# HERE BE DRAGONS πŸ‰
#
# I'm looking forward to taking this procedural code
# and refactoring it into a proper structure
# if it becomes necessary or truly useful
#
# As we develop sample generation, it becomes
# easier to use and a swiss army knife such and
# this may or may not be useful in the near future
#
# But, for now... I continue to pile onto this procedural trash fire 😜
require "mkmf"
require "yaml"
require "ostruct"
require "optparse"
require "fileutils"
VERSION = "0.0.1"
$options = OpenStruct.new
def abs path
File.expand_path path
end
$options.workspace_dir = abs(ENV["SAMPLEGEN_WORKSPACE"]) if ENV["SAMPLEGEN_WORKSPACE"]
if ARGV.include?("-w") || ARGV.include?("--workspace")
index = ARGV.index("-w") || ARGV.index("--workspace")
_ = ARGV.delete_at index
$options.workspace_dir = abs(ARGV.delete_at index)
else
$options.workspace_dir ||= abs(File.join ENV["HOME"], ".samplegen")
end
# - Explicitly provided --config argument
# - SAMPLEGEN_CONFIG environment variable
# - samplegen.yml in local directory
# - [workspace]/config.yml
$options.config_yaml = abs(ENV["SAMPLEGEN_CONFIG"]) if ENV["SAMPLEGEN_CONFIG"]
if ARGV.include?("-c") || ARGV.include?("--config")
index = ARGV.index("-c") || ARGV.index("--config")
_ = ARGV.delete_at index
$options.config_yaml = abs(ARGV.delete_at index)
elsif File.exists? abs("samplegen.yml")
$options.config_yaml ||= abs("samplegen.yml")
else
$options.config_yaml ||= abs(File.join $options.workspace_dir, "config.yaml")
end
$config = YAML.load_file $options.config_yaml if File.file? $options.config_yaml
$config.each { |key, value| $options[key] = value } if $config
OptionParser.new { |opts|
opts.on("-h", "--help") { |_| usage && exit }
opts.on("-p", "--print") { |_| $options.print_only = true }
opts.on("-s", "--save") { |_| $options.save_config = true }
opts.on("-e", "--edit") { |_| $edit_config_files = true }
opts.on("-b", "--bash") { |_| $open_bash_prompt = true }
opts.on("-i", "--image NAME") { |name| $options.docker_image = name }
opts.on("-l", "--language LANG") { |lang| $options.language = lang.downcase }
opts.on("-r", "--region-tag FILTER") { |filter| $options.filter = filter }
opts.on("-a", "--artman ARGS") { |args| $options.artman_args = args }
opts.on("-o", "--output PATH") { |path| $options.output_dir = abs(path); $force_output = true } # lol fix later
opts.on("-d", "--docker PATH") { |path| $options.docker_bin = abs(path) }
opts.on( "--googleapis PATH") { |path| $options.googleapis = abs(path) }
opts.on( "--gapic-generator PATH") { |path| $options.gapic_generator = abs(path) }
opts.on( "--snip") { |_| $print_snip = true }
opts.on( "--dry-run") { |_| $dry_run = true }
opts.on( "--no-gen") { |_| $do_not_generate = true }
opts.on( "--gapic-config") { |_| $print_gapic_config = true }
opts.on( "--enable-dev_samples") { |_| $enable_dev_samples = true }
opts.on( "--rebuild") { |_| $rebuild_gapic = true }
opts.on( "--clone") { |_| $clone_repos = true }
opts.on( "--pull") { |_| $pull_latest_repos = true }
opts.on( "--info") { |_| $print_info = true }
opts.on( "--version") { |_| puts "samplegen #{VERSION}"; exit }
}.parse!
$options.output_dir ||= "."
$options.docker_bin ||= "docker"
$options.docker_image ||= "googleapis/artman"
$options.language ||= "python"
$options.googleapis ||= abs(File.join $options.workspace_dir, "googleapis")
$options.gapic_generator ||= abs(File.join $options.workspace_dir, "gapic-generator")
# Runs command
# If --dry-run, prints command instead. Sets command to fail. Returns ""
def run! command, dry_run: $dry_run
if dry_run
puts "[DRY RUN] #{command.inspect}"
def $?.exitstatus(); 5; end
""
else
`#{command}`
end
end
# Runs code. Will execute passed block if not --dry-run (and print message)
# If --dry-run, prints information about operation instead
# Message is always printed
def run_code! message, dry_run: $dry_run, &block
if dry_run
puts "[DRY RUN] #{message}"
else
puts message
block.call
end
end
def pull_docker_image! force_pull_latest = false
if $dry_run
puts "[DRY RUN] docker pull #{$options.docker_image}"
return
end
if force_pull_latest
unless system "docker pull #{$options.docker_image}"
docker_image_pulled_locally = false
puts "failed to `docker pull #{$options.docker_image}`"
end
return
end
print "checking for docker image... "
if run!("docker images").gsub(/\s/, "").include? $options.docker_image.sub(":", "")
puts "yes"
else
puts "no"
puts "installing docker image... "
unless system "docker pull #{$options.docker_image}"
docker_image_pulled_locally = false
puts "failed to `docker pull #{$options.docker_image}`"
end
end
end
##
# Pull latest googleapis/gapic-generator `git pull origin master`
##
if $pull_latest_repos
puts "updating googleapis... "
output = `cd "#{$options.googleapis}" && git pull origin master`
status = $?
puts output
if status.success?
puts "ok"
else
puts "failed (exit #{status.exitstatus})"
end
puts "updating gapic-generator... "
output = run! %{cd "#{$options.gapic_generator}" && git pull origin master}
status = $?
puts output
if status.success?
puts "ok"
else
puts "failed (exit #{status.exitstatus})"
end
puts "pulling latest docker image... "
pull_docker_image! true
end
##
# API NAME & VERSION DETECTION
##
VERSION_PATTERN = /(?<major_version>v\d+)(?<patch_level>p?\d*)(?<release_level>alpha\d+|beta\d+)?/
ARGV.size.times do
argument = ARGV.shift
if argument =~ VERSION_PATTERN
$options.api_version = argument
else
$options.api_name = argument
end
end
def latest_stable_version version_names
sort_versions(version_names).first
end
def sort_versions version_names, order_by_release_level: true
sorted_names = version_names.sort_by { |name|
match = name.match VERSION_PATTERN
level_priority = { "GA" => 2, "beta" => 1, "alpha" => 0 }
level_priority = { "GA" => 0, "beta" => 0, "alpha" => 0 } if order_by_release_level == false
release_level_name = "GA"
major_version = match[:major_version].sub('v','').to_i
minor_version = 0
patch_version = 0
unless match[:release_level].nil? || match[:release_level].empty?
release_level_name, minor_version = match[:release_level].match(/^(alpha|beta)(\d+)$/).captures
minor_version = minor_version.to_i
end
unless match[:patch_level].nil? || match[:patch_level].empty?
patch_version = match[:patch_level].match(/^p(\d+)$/).captures.first.to_i
end
[ level_priority[release_level_name], major_version, minor_version, patch_version ]
}.reverse
end
##
# Config file naming conventions –
#
# artman config: artman_APINAME.yaml, artman_APINAME_version.yaml
# gapic config: APINAME.yaml
##
if $options.api_name
#files_related_to_this_api_in_any_way = []
#if $options.api_version
# files_related_to_this_api_in_any_way = Dir[File.join $options.googleapis, "google/**/*"].select { |path| path.include?($options.api_name) && path.include?($options.api_version) }
#elsif $options.api_name
files_related_to_this_api_in_any_way = Dir[File.join $options.googleapis, "google/**/*"].select { |path| path.include? $options.api_name }
#end
# ...
files_related_regardless_of_version = Dir[File.join $options.googleapis, "google/**/*"].select { |path| path.include? $options.api_name }
versions_available = files_related_to_this_api_in_any_way.map { |path| path.match(VERSION_PATTERN)&.to_a&.first }.compact.sort.uniq
unless $options.api_version
if versions_available.empty?
puts "No versions of #{$options.api_name} API found"
elsif versions_available.size == 1
$options.api_version = versions_available.first
puts "Choosing only API version available: #{$options.api_version}" unless $print_snip || $print_gapic_config
else
puts "Multiple versions of this API available:" unless $print_snip || $print_gapic_config
versions_available.each { |version| puts " - #{version}" } unless $print_snip || $print_gapic_config
$options.api_version = latest_stable_version versions_available
puts "Choosing most stable, recent API version: #{$options.api_version}" unless $print_snip || $print_gapic_config
end
end
only_for_this_version = files_related_to_this_api_in_any_way.select { |path| path.include? $options.api_version }
gapic_configs = only_for_this_version.select { |path|
filename = File.basename path
filename.end_with? "_gapic.yaml"
}
gapic_config = gapic_configs.select { |path|
path.include? "#{File::SEPARATOR}#{$options.api_version}#{File::SEPARATOR}"
}
if gapic_config.size > 1
paths_including_api_directory = gapic_config.select { |path|
path.include? "#{File::SEPARATOR}#{$options.api_name}#{File::SEPARATOR}"
}
gapic_config = paths_including_api_directory if paths_including_api_directory.any?
end
if gapic_config.size == 1
$options.gapic_config = gapic_config.first
elsif gapic_config.empty?
puts "No GAPIC configuration found for #{$options.api_name} #{$options.api_version}"
exit 1
else
puts "Do any files have this exact name? -> #{$options.api_name}_gapic.yaml"
exact_matching_file_basenames = gapic_config.select { |path|
File.basename(path) == "#{$options.api_name}_gapic.yaml"
}
if exact_matching_file_basenames.size == 1
$options.gapic_config = exact_matching_file_basenames.first
elsif exact_matching_file_basenames.any?
puts "Multiple possible GAPIC configurations found:"
exact_matching_file_basenames.each { |path| puts " - #{path}" }
else
puts "Multiple possible GAPIC configurations found:"
gapic_config.each { |path| puts " - #{path}" }
exit 1
end
end
artman_configs = only_for_this_version.select { |path|
filename = File.basename path
filename.start_with?("artman") && filename.end_with?(".yaml")
}
artman_config = artman_configs.select { |path|
filename = File.basename path
filename.end_with? "_#{$options.api_version}.yaml"
}
if artman_config.empty?
artman_config = only_for_this_version.select { |path|
filename = File.basename path
filename == "artman_#{$options.api_name}.yaml"
}
end
if artman_config.size > 1
with_version_directory = artman_config.select {|path| path.include? "#{File::SEPARATOR}#{$options.api_version}#{File::SEPARATOR}" }
artman_config = with_version_directory if with_version_directory.any?
end
if artman_config.size > 1
with_api_name_directory = artman_config.select {|path| path.include? "#{File::SEPARATOR}#{$options.api_name}#{File::SEPARATOR}" }
artman_config = with_api_name_directory if with_api_name_directory.any?
end
if artman_config.size == 1
$options.artman_config = artman_config.first
$options.artman_config.sub! $options.googleapis, ""
$options.artman_config = File.join "#{File::SEPARATOR}googleapis", $options.artman_config
elsif artman_config.empty?
# πŸ€” Hmm. Let's throw some cloud* into the mix for a certain API...
# let's look at files which don't include version # in the path
artman_configs = files_related_to_this_api_in_any_way.select {|path| path.include?("artman") && path.include?("#{$options.api_name}.yaml") }
if artman_configs.size == 1
$options.artman_config = artman_configs.first
$options.artman_config.sub! $options.googleapis, ""
$options.artman_config = File.join "#{File::SEPARATOR}googleapis", $options.artman_config
elsif artman_configs.size > 1
puts "Multiple possible artman configurations found:"
artman_configs.each { |path| puts " - #{path}" }
exit 1
else
puts "No artman configuration found for #{$options.api_name}"
exit 1
end
else
puts "Multiple possible artman configurations found:"
artman_configs.each { |path| puts " - #{path}" }
exit 1
end
end
def local_snip_template_path language = $options.language
language = $options.language
language = "py" if language == "python"
File.join $options.gapic_generator, "src/main/resources/com/google/api/codegen/#{language}/standalone_sample.snip"
end
##
# Find Paths to Gapic Config & Snip Template
##
if $print_info
$options.each_pair do |key, value|
puts "#{key} = #{value}"
end
end
if $edit_config_files
if editor = ENV["EDITOR"].strip
language = $options.language
language = "py" if language == "python"
files_to_edit = []
if $print_gapic_config && ! $print_snip
files_to_edit << $options.gapic_config
elsif $print_snip && ! $print_gapic_config
files_to_edit << local_snip_template_path(language)
else
files_to_edit << $options.gapic_config
files_to_edit << local_snip_template_path(language)
end
command = "#{editor} #{files_to_edit.join(' ')}"
puts command
exec command
else
puts "Please set your EDITOR variable to use --edit"
end
end
if $print_snip
print local_snip_template_path
exit
end
if $print_gapic_config
if $options.api_name && $options.api_version
print $options.gapic_config
exit
else
puts "Must pass API name [ version] arguments"
end
end
def save_config config
print "saving config... "
config = $options.to_h
config.delete :save_config
FileUtils.mkdir_p File.dirname($options.config_yaml)
File.write $options.config_yaml, config.to_yaml
puts "yes"
end
save_config $options if $options.save_config
unless find_executable $options.docker_bin
puts "Docker executable not found: [#{$options.docker_bin.inspect}]"
usage && exit(1)
end
# # # # # # # # # # # # # #
directories_all_present_and_accounted_for = true
%w[ output_dir googleapis gapic_generator ].each do |option|
directory_path = $options[option]
print "checking for #{option} directory... "
if Dir.exists? directory_path
puts "yes"
else
if $clone_repos && %w[ googleapis gapic_generator ].include?(option)
print "\ncloning #{option}... "
dir_path = $options[option]
repo_name = File.basename dir_path
parent_dir = File.dirname $options[option]
FileUtils.mkdir_p parent_dir
puts "\ncd #{parent_dir}"
Dir.chdir parent_dir do
puts "git clone https://github.com/googleapis/#{repo_name}.git"
output = run! "git clone https://github.com/googleapis/#{repo_name}.git"
status = $?
if status.success?
puts "... ok"
else
puts "Looks like the clone failed or... something? (exit #{status.exitstatus})"; exit(1)
end
end
else
directories_all_present_and_accounted_for = false
puts "directory not found (#{directory_path})"
end
end
end
docker_image_pulled_locally = true
pull_docker_image!
exit(1) unless directories_all_present_and_accounted_for && docker_image_pulled_locally
def check_for_java!
unless find_executable "java"
puts "Looks like you may not have Java installed? No `java` in PATH"
exit 1
end
end
GAPIC_GEN_FACTORY_FILE_PATH = "src/main/java/com/google/api/codegen/gapic/GapicGeneratorFactory.java"
if $enable_dev_samples
puts "enabling --dev_samples"
java_file_path = File.join $options.gapic_generator, GAPIC_GEN_FACTORY_FILE_PATH
java_source = File.read java_file_path
if java_source.include? "if (devSamples)"
run_code! "modifying #{java_file_path}" do
File.write java_file_path, java_source.gsub("if (devSamples)", "if (true)")
end
else
puts "--dev_samples already enabled"
end
end
if $rebuild_gapic
puts "rebuilding gapic... "
check_for_java!
output = run! %{cd "#{$options.gapic_generator}" && ./gradlew fatJar}
status = $?
puts output
if status.success?
puts "ok"
else
puts "failed (exit #{status.exitstatus})"
end
end
unless $options.api_name && $options.api_version
puts "No arguments passed, expected: api-name [, api-version]"
exit unless $open_bash_prompt
end
docker_command = [
$options.docker_bin, "run", "--rm",
]
docker_command << "--interactive --tty" if $open_bash_prompt
docker_command.concat [
"-e", "RUNNING_IN_ARTMAN_DOCKER=True",
"-v", "#{$options.gapic_generator}:/toolkit",
"-v", "#{$options.googleapis}:/googleapis",
"-w", "/googleapis",
$options.docker_image,
"/bin/bash"
]
docker_command << "-c" unless $open_bash_prompt
artman_command = [
"artman", "--local",
"--config", $options.artman_config,
"generate", "#{$options.language}_gapic"
]
artman_comment << artman_args if $options.artman_args
final_command = docker_command.join(" ") + ' "' + artman_command.join(" ") + '"' + " 2>&1"
if $open_bash_prompt
final_command = docker_command.join(" ")
puts "Starting BASH session"
end
puts final_command
if $open_bash_prompt
if $do_not_generate
echo "[DO NOT RUN] #{final_command}"
else
exec final_command
end
end
if $do_not_generate
puts "[DO NOT GENERATE #{final_command}]"
puts "exiting..."
exit 0
else
output = run! final_command
status = $?
puts output
end
exit if $dry_run
unless status.success?
puts "docker command failed (exit #{status.exitstatus})"
exit 1
end
output_without_colors = output.gsub /\e\[(\d+)m/, ""
samples_directory = nil
output_directory_match = output_without_colors.match /^artman> Code generated: (?<output_dir>\/googleapis\/.*)$/
if output_directory_match
output_directory = output_directory_match[:output_dir]
output_directory.sub! "#{File::SEPARATOR}googleapis#{File::SEPARATOR}", ""
output_directory = File.join $options.googleapis, output_directory
puts "Generated output directory: #{output_directory}"
sample_directories = Dir[File.join output_directory, "**", "samples"].select {|path| Dir.exists? path }
if sample_directories.none?
puts "No samples generated for this library (no generated 'samples' directory found)"
exit 1
elsif sample_directories.size > 1
puts "Multiple generated 'samples' directories found"
puts "(haven't added logic to evaluate which to use, I didn't know there could be multiple...)"
sample_directories.each {|path| puts " - #{path}" }
puts
exit 1
else
samples_directory = sample_directories.first
end
else
puts "Code generation may have failed, did not find expected 'Code generated' output"
exit 1
end
unless Dir.exist? samples_directory
puts "No samples generated for this library"
exit
end
sample_code_files = Dir[File.join(samples_directory, "**", "*")].select { |p| File.file? p }
unless sample_code_files.any?
puts "No samples found for this library"
exit
end
if $options.filter
if $options.filter.start_with?("/") && $options.filter.end_with?("/")
pattern = $options.filter[1..-2]
sample_code_files.select! {|path| path =~ %r(#{pattern}) }
else
sample_code_files.select! {|path| path.include? $options.filter }
end
if sample_code_files.empty?
puts "No samples matched filter [#{$options.filter.inspect}]"
exit
end
end
if $options.print_only
sample_code_files.each do |path|
puts "[#{File.basename path}]"
if find_executable "pygmentize"
system "pygmentize -g #{path}"
else
puts File.read(path)
end
end
end
if ! $options.print_only || $force_output
FileUtils.mkdir_p $options.output_dir
files_saved = []
sample_code_files.each do |path|
files_saved << path
FileUtils.cp path, $options.output_dir, verbose: true
end
puts "\nβœ“ Wrote #{files_saved.size} sample files to #{$options.output_dir}"
files_saved.each {|path| puts " - #{File.basename path}" }
end
# TODO(beccataylor) add option to pull repos from master
# If gapic-generator has git changes, recommend a --rebuild
# If gapic-generator or googleapis are out-of-date, recommend a --pull
# Can we check if we have the latest Docker using the Docker CLI? see if we can & provide a --pull recommendation
# Yeah, yeah, this is a draft – if I continue to use this, I'll make it clean and pretty (with tests)
# It'll be fun refactoring this mess of wires & scotch tape into a sane structure πŸ˜‡
Apache License
do
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment