# Copyright 2019 The Chromium Authors.  All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("./node.gni")
import("./vars.gni")

template("copy_to_gen") {
  node_action(target_name) {
    forward_variables_from(invoker,
                           [
                             "sources",
                             "visibility",
                             "deps",
                             "testonly",
                           ])

    script = "scripts/build/ninja/copy-files.js"

    _copy_src = rebase_path(get_path_info(target_name, "dir"))
    _copy_dest = rebase_path(target_gen_dir)

    args = [
      _copy_src,
      _copy_dest,
      string_join(",", sources),
    ]

    outputs = []
    foreach(_input, sources) {
      outputs += [ "$target_gen_dir/$_input" ]
    }
  }
}

template("copy_sources_to_resources") {
  _copy_action_name = "$target_name-copy"
  node_action(_copy_action_name) {
    forward_variables_from(invoker,
                           [
                             "sources",
                             "deps",
                             "public_deps",
                             "testonly",
                           ])

    script = "scripts/build/ninja/copy-files.js"

    # Figure out what the relative path is to the front_end folder.
    # We have to get the full path to the target_gen_dir
    # and compare that to the full location of the front_end
    # folder in the gen_dir, to make sure nested packages
    # resolve to the correct location.
    _front_end_module_name =
        string_replace(get_path_info(target_gen_dir, "abspath"),
                       root_gen_dir + "/" + devtools_location + "front_end",
                       "")

    _copy_src = rebase_path(get_path_info(target_name, "dir"))
    _copy_dest =
        rebase_path("$root_out_dir/resources/inspector$_front_end_module_name")

    args = [
      _copy_src,
      _copy_dest,
      string_join(",", sources),
    ]

    outputs = []
    foreach(_input, sources) {
      outputs += [ "$resources_out_dir$_front_end_module_name/$_input" ]
    }
  }

  group(target_name) {
    public_deps = [ ":$_copy_action_name" ]
    data_deps = [ ":$_copy_action_name" ]

    forward_variables_from(invoker, [ "visibility" ])
  }
}

template("copy_gen_to_resources") {
  _copy_action_name = "$target_name-copy"
  copy(_copy_action_name) {
    forward_variables_from(invoker,
                           [
                             "sources",
                             "deps",
                             "public_deps",
                             "testonly",
                             "data",
                           ])

    # Figure out what the relative path is to the front_end folder.
    # We have to get the full path to the target_gen_dir
    # and compare that to the full location of the front_end
    # folder in the gen_dir, to make sure nested packages
    # resolve to the correct location.
    _front_end_module_name =
        string_replace(get_path_info(target_gen_dir, "abspath"),
                       root_gen_dir + "/" + devtools_location + "front_end",
                       "")

    outputs =
        [ "$resources_out_dir/$_front_end_module_name/{{source_file_part}}" ]
  }

  group(target_name) {
    public_deps = [ ":$_copy_action_name" ]
    data_deps = [ ":$_copy_action_name" ]

    forward_variables_from(invoker, [ "visibility" ])
  }
}
