import("$tizen_core/build/hash.gni")
import("$tizen_core/build/web/minify.gni")

template("copy_files") {
    forward_variables_from(invoker, [ "proj_deps" ])
    copy(target_name) {
        sources = [ target_name ]
        outputs = [
            "${root_out_dir}/$web_out_dir/{{source_target_relative}}",
        ]
        deps = proj_deps
    }
}

template("web_project") {
    forward_variables_from(invoker, "*", [])


    out_files = []
    proj_deps = []

    if (defined(deps)) {
        foreach(proj, deps) {
            proj_deps += [ "//${proj}:all"]
        }
        deps = []
    }
    # copy files
    foreach(file_to_copy, files) {
        copy_files(file_to_copy) {}
        proj_deps += [ ":$file_to_copy" ]
        out_files += get_target_outputs(":$file_to_copy")
    }

    #minify
    if (opt) {
        foreach(opt_file, opt_files) {
            minify_folder(opt_file) {}
            proj_deps += [ ":$opt_file" ]
            out_files += get_target_outputs(":$opt_file")
        }
    }

    # create hashes
    hash_deps = []
    foreach(file, out_files) {
        target = rebase_path(file, root_out_dir)
        calc_hash("$target.hash") {
            file_to_hash = file
            deps = proj_deps
        }
        hash_deps += [":$target.hash"]
    }

    group("all") { deps = proj_deps }
    group("hash") { deps = hash_deps }
}
