text: # 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
  #
  #     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.
  
  """This script is used to synthesize generated parts of this library."""
  
  import synthtool as s
  import synthtool.gcp as gcp
  import synthtool.languages.ruby as ruby
  import logging
  import re
  from textwrap import dedent
  from subprocess import call
  
  logging.basicConfig(level=logging.DEBUG)
  
  gapic = gcp.GAPICGenerator()
  
  v4beta1 = gapic.ruby_library(
      'talent', 'v4beta1',
      artman_output_name='google-cloud-ruby/google-cloud-talent',
      config_path='artman_talent_v4beta1.yaml'
  )
  s.copy(v4beta1 / 'acceptance')
  s.copy(v4beta1 / 'lib')
  s.copy(v4beta1 / 'test')
  s.copy(v4beta1 / 'README.md')
  s.copy(v4beta1 / 'LICENSE')
  s.copy(v4beta1 / '.gitignore')
  s.copy(v4beta1 / '.yardopts')
  s.copy(v4beta1 / 'google-cloud-talent.gemspec', merge=ruby.merge_gemspec)
  
  # Copy common templates
  templates = gcp.CommonTemplates().ruby_library()
  s.copy(templates)
  
  # https://github.com/googleapis/gapic-generator/issues/2243
  s.replace(
      'lib/google/cloud/talent/*/*_client.rb',
      '(\n\\s+class \\w+Client\n)(\\s+)(attr_reader :\\w+_stub)',
      '\\1\\2# @private\n\\2\\3')
  
  # https://github.com/googleapis/gapic-generator/issues/2279
  s.replace(
      'lib/**/*.rb',
      '\\A(((#[^\n]*)?\n)*# (Copyright \\d+|Generated by the protocol buffer compiler)[^\n]+\n(#[^\n]*\n)*\n)([^\n])',
      '\\1\n\\6')
  
  # https://github.com/googleapis/gapic-generator/issues/2323
  s.replace(
      [
          'lib/**/*.rb',
          'README.md'
      ],
      'https://github\\.com/GoogleCloudPlatform/google-cloud-ruby',
      'https://github.com/googleapis/google-cloud-ruby'
  )
  s.replace(
      [
          'lib/**/*.rb',
          'README.md'
      ],
      'https://googlecloudplatform\\.github\\.io/google-cloud-ruby',
      'https://googleapis.github.io/google-cloud-ruby'
  )
  
  # https://github.com/googleapis/gapic-generator/issues/2393
  s.replace(
      'google-cloud-talent.gemspec',
      'gem.add_development_dependency "rubocop".*$',
      'gem.add_development_dependency "rubocop", "~> 0.64.0"'
  )
  
  services = ['Application', 'Company', 'Job', 'Profile', 'Tenant']
  # https://github.com/googleapis/gapic-generator/issues/2492
  for service in services:
      s.replace(
          [
              'lib/google/cloud/talent.rb',
              'lib/google/cloud/talent/v*.rb'
          ],
          f'module {service}\n',
          f'module {service}Service\n'
      )
  
      s.replace(
          'lib/google/cloud/talent.rb',
          f'{service}\\.new',
          f'{service}Service.new'
      )
  
      s.replace(
          [
              f'lib/google/cloud/talent/v*/{service.lower()}_service_client.rb',
              f'test/google/cloud/talent/v*/{service.lower()}_service_client_test.rb'
          ],
          f'{service}\\.new\\(version:',
          f'{service}Service.new(version:'
      )
  
  # Require the helpers file
  s.replace(
      'lib/google/cloud/talent/v4beta1.rb',
      'require "google/cloud/talent/v4beta1/application_service_client"',
      '\n'.join([
          'require "google/cloud/talent/v4beta1/application_service_client"',
          'require "google/cloud/talent/v4beta1/helpers"',
      ])
  )
  
  # Correct README url
  s.replace(
      'README.md',
      'https://cloud.google.com/talent',
      'https://cloud.google.com/talent-solution'
  )
  
  s.replace(
      'google-cloud-talent.gemspec',
      '"README.md", "LICENSE"',
      '"README.md", "AUTHENTICATION.md", "LICENSE"'
  )
  s.replace(
      '.yardopts',
      'README.md\n',
      'README.md\nAUTHENTICATION.md\nLICENSE\n'
  )
  
  # https://github.com/googleapis/google-cloud-ruby/issues/3058
  s.replace(
      'google-cloud-talent.gemspec',
      '\nGem::Specification.new do',
      'require File.expand_path("../lib/google/cloud/talent/version", __FILE__)\n\nGem::Specification.new do'
  )
  s.replace(
      'google-cloud-talent.gemspec',
      '(gem.version\s+=\s+).\d+.\d+.\d.*$',
      '\\1Google::Cloud::Talent::VERSION'
  )
  s.replace(
      'lib/google/cloud/talent/v4beta1/*_client.rb',
      '(require \".*credentials\"\n)\n',
      '\\1require "google/cloud/talent/version"\n\n'
  )
  s.replace(
      'lib/google/cloud/talent/v4beta1/*_client.rb',
      'Gem.loaded_specs\[.*\]\.version\.version',
      'Google::Cloud::Talent::VERSION'
  )
  
  # Generate the helper methods
  call(f'bundle update && bundle exec rake generate_partials TALENT_SERVICES={",".join(services)}', shell=True)
  
  # Exception tests have to check for both custom errors and retry wrapper errors
  for version in ['v4beta1']:
      s.replace(
          f'test/google/cloud/talent/{version}/*_client_test.rb',
          'err = assert_raises Google::Gax::GaxError do',
          f'err = assert_raises Google::Gax::GaxError, CustomTestError_{version} do'
      )
  