# 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


logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICGenerator()

# Temporary until we get Ruby-specific tools into synthtool


def merge_gemspec(src, dest, path):
    regex = re.compile(r'^\s+gem.version\s*=\s*"[\d\.]+"$', flags=re.MULTILINE)
    match = regex.search(dest)
    if match:
        src = regex.sub(match.group(0), src, count=1)
    regex = re.compile(r'^\s+gem.homepage\s*=\s*"[^"]+"$', flags=re.MULTILINE)
    match = regex.search(dest)
    if match:
        src = regex.sub(match.group(0), src, count=1)
    return src


v1_library = gapic.ruby_library(
    'redis', 'v1',
    config_path='artman_redis_v1.yaml',
    artman_output_name='google-cloud-ruby/google-cloud-redis'
)
s.copy(v1_library / 'lib')
s.copy(v1_library / 'test')
s.copy(v1_library / 'Rakefile')
s.copy(v1_library / 'README.md')
s.copy(v1_library / 'LICENSE')
s.copy(v1_library / '.gitignore')
s.copy(v1_library / '.yardopts')
s.copy(v1_library / 'google-cloud-redis.gemspec', merge=merge_gemspec)

v1beta1_library = gapic.ruby_library(
    'redis', 'v1beta1',
    artman_output_name='google-cloud-ruby/google-cloud-redis'
)
s.copy(v1beta1_library / 'lib/google/cloud/redis/v1beta1')
s.copy(v1beta1_library / 'lib/google/cloud/redis/v1beta1.rb')
s.copy(v1beta1_library / 'test/google/cloud/redis/v1beta1')

# https://github.com/googleapis/gapic-generator/issues/2196
s.replace(
    [
        'README.md',
        'lib/google/cloud/redis.rb',
        'lib/google/cloud/redis/v1.rb',
        'lib/google/cloud/redis/v1beta1.rb'
    ],
    '\\[Product Documentation\\]: https://cloud\\.google\\.com/redis\n',
    '[Product Documentation]: https://cloud.google.com/memorystore\n')

# https://github.com/googleapis/gapic-generator/issues/2232
s.replace(
    [
        'lib/google/cloud/redis/v1/cloud_redis_client.rb',
        'lib/google/cloud/redis/v1beta1/cloud_redis_client.rb'
    ],
    '\n\n(\\s+)class OperationsClient < Google::Longrunning::OperationsClient',
    '\n\n\\1# @private\n\\1class OperationsClient < Google::Longrunning::OperationsClient')

# https://github.com/googleapis/gapic-generator/issues/2242


def escape_braces(match):
    expr = re.compile('([^#\\$\\\\])\\{([\\w,]+)\\}')
    content = match.group(0)
    while True:
        content, count = expr.subn('\\1\\\\\\\\{\\2}', content)
        if count == 0:
            return content


s.replace(
    'lib/google/cloud/**/*.rb',
    '\n(\\s+)#[^\n]*[^\n#\\$\\\\]\\{[\\w,]+\\}',
    escape_braces)

# https://github.com/googleapis/gapic-generator/issues/2243
s.replace(
    'lib/google/cloud/redis/*/*_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')

@@ -835,480 +835,8 @@
 ()%0A%0A
-# Temporary until we get Ruby-specific tools into synthtool%0A%0A%0Adef merge_gemspec(src, dest, path):%0A    regex = re.compile(r'%5E%5Cs+gem.version%5Cs*=%5Cs*%22%5B%5Cd%5C.%5D+%22$', flags=re.MULTILINE)%0A    match = regex.search(dest)%0A    if match:%0A        src = regex.sub(match.group(0), src, count=1)%0A    regex = re.compile(r'%5E%5Cs+gem.homepage%5Cs*=%5Cs*%22%5B%5E%22%5D+%22$', flags=re.MULTILINE)%0A    match = regex.search(dest)%0A    if match:%0A        src = regex.sub(match.group(0), src, count=1)%0A    return src%0A%0A%0A
 v1_l
@@ -887,48 +887,8 @@
 1',%0A
-    config_path='artman_redis_v1.yaml',%0A
     
@@ -971,16 +971,38 @@
 y / 'lib
+/google/cloud/redis.rb
 ')%0As.cop
@@ -1021,44 +1021,135 @@
  / '
-test')%0As.copy(v1_library / 'Rakefile
+lib/google/cloud/redis/v1')%0As.copy(v1_library / 'lib/google/cloud/redis/v1.rb')%0As.copy(v1_library / 'test/google/cloud/redis/v1
 ')%0As
@@ -1334,16 +1334,21 @@
 , merge=
+ruby.
 merge_ge
@@ -2473,18 +2473,16 @@
 es/2242%0A
-%0A%0A
 def esca
@@ -2685,16 +2685,16 @@
 t == 0:%0A
+
         
@@ -2712,18 +2712,16 @@
 content%0A
-%0A%0A
 s.replac

Fix redis synth script (#2438)