# Copyright 2016 Google Inc.
#
# 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.

"""Bazel rule to generate copybara reference docs."""

def _doc_impl(ctx):
  jars = []
  for dep in ctx.attr.deps:
    for jar in dep.java.transitive_source_jars:
      jars.append(jar)
  tmp = ctx.new_file("tmp.md")
  ctx.action(
      inputs = [ctx.executable._doc_tool] + jars,
      outputs = [tmp],
      progress_message = "Generating reference documentation for %s" % ctx.label,
      command = "%s %s %s" % (ctx.executable._doc_tool.path, tmp.path, " ".join([f.path for f in jars])))
  # If suffix file exists, concat, copy otherwise
  if ctx.attr.suffix_file != None:
    ctx.action(
        inputs = [ctx.files.suffix_file[0], tmp],
        outputs = [ctx.outputs.out],
        progress_message = "Appending suffix from %s" % ctx.files.suffix_file,
        command = "cat %s %s >> %s" %  (tmp.path, ctx.files.suffix_file[0].path, ctx.outputs.out.path)
    )
  else:
    ctx.action(
        inputs = [tmp],
        outputs = [ctx.outputs.out],
        command = "cp %s %s" %  (tmp.path, ctx.outputs.out.path))

# Generates documentation by scanning the transitive set of dependencies of a Java binary.
doc_generator = rule(
    attrs = {
        "deps": attr.label_list(allow_rules = [
            "java_binary",
            "java_library",
        ]),
        "_doc_tool": attr.label(
            executable = True,
            cfg = "host",
            allow_files = True,
            default = Label("//java/com/google/copybara:doc_skylark.sh"),
        ),
        "suffix_file": attr.label(mandatory=False, allow_files=True, single_file=True)
    },
    outputs = {"out": "%{name}.md"},
    implementation = _doc_impl,
)

# Copyright 2016 Google Inc.
#
# 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.

"""Bazel rule to generate copybara reference docs."""

def _doc_impl(ctx):
  jars = []
  for dep in ctx.attr.deps:
    for jar in dep.java.transitive_source_jars:
      jars.append(jar)
  tmp = ctx.new_file("tmp.md")
  ctx.actions.run(
      inputs = [ctx.executable._doc_tool] + jars,
      outputs = [tmp],
      progress_message = "Generating reference documentation for %s" % ctx.label,
      use_default_shell_env = True,
      executable = ctx.executable._doc_tool.path,
      arguments = [tmp.path] + [f.path for f in jars],
  )
  # If suffix file exists, concat, copy otherwise
  if ctx.attr.suffix_file != None:
    ctx.actions.run_shell(
        inputs = [ctx.files.suffix_file[0], tmp],
        outputs = [ctx.outputs.out],
        progress_message = "Appending suffix from %s" % ctx.files.suffix_file,
        command = "cat %s %s >> %s" %  (tmp.path, ctx.files.suffix_file[0].path, ctx.outputs.out.path),
    )
  else:
    ctx.actions.run(
        inputs = [tmp],
        outputs = [ctx.outputs.out],
        executable = "cp",
        arguments = [tmp.path, ctx.outputs.out.path],
    )

# Generates documentation by scanning the transitive set of dependencies of a Java binary.
doc_generator = rule(
    attrs = {
        "deps": attr.label_list(allow_rules = [
            "java_binary",
            "java_library",
        ]),
        "_doc_tool": attr.label(
            executable = True,
            cfg = "host",
            allow_files = True,
            default = Label("//java/com/google/copybara:doc_skylark.sh"),
        ),
        "suffix_file": attr.label(mandatory=False, allow_files=True, single_file=True)
    },
    outputs = {"out": "%{name}.md"},
    implementation = _doc_impl,
)

Update deprecated ctx.action usage to ctx.actions.{run,run_shell}