# see https://crates.io/crates/cargo-make

[env]
RUST_TEST_THREADS = "1"
CARGO_MAKE_TEST_COVERAGE_BINARY_FILTER = "cli-[a-z0-9]*$\\|makers-[a-z0-9]*$\\|${CARGO_MAKE_TEST_COVERAGE_DEFAULT_BINARY_FILTER}"
GITHUB_REPO="ffizer"
GITHUB_USER="ffizer"
# GITHUB_TOKEN=
DIST_NAME = "${CARGO_MAKE_CRATE_NAME}_${CARGO_MAKE_CRATE_VERSION}-${TARGET}"
DIST_EXT="tar.gz"
DIST_PATH="target/dist/${DIST_NAME}"
LIBZ_SYS_STATIC="1"
PKG_CONFIG_ALLOW_CROSS="1"
# see https://github.com/naftulikay/rust-openssl-static-example
OPENSSL_STATIC="1"
# OPENSSL_LIB_DIR="/usr/lib64"
# OPENSSL_INCLUDE_DIR="/usr/include/openssl"
GITHUB_RELEASE_VERSION="v0.7.2"

[env.linux]
GITHUB_RELEASE_PATH="target/bin/linux/amd64"
GITHUB_RELEASE_PATH_REV="../../../"

[env.mac]
GITHUB_RELEASE_PATH="target/bin/darwin/amd64"
GITHUB_RELEASE_PATH_REV="../../../"

[env.windows]
DIST_PATH="target\\dist\\${DIST_NAME}"
DIST_EXT="zip"
GITHUB_RELEASE_PATH="target\\bin\\windows\\amd64"
GITHUB_RELEASE_PATH_REV="..\\..\\..\\"

[tasks.build-verbose]
# override
#args = ["build", "--verbose", "--all-features"]
args = ["build", "--all-features"]
#args = ["build", "--release", "--all-features"]

[tasks.test-verbose]
# override
#args = ["test", "--verbose", "--all-features"]
args = ["test"]
#args = ["test", "--release"]

[tasks.zip-release-ci-flow]
description = "Compiles the binary in release mode and zips it up"
category = "CI"
condition = { env_set = ["TARGET"] }
dependencies = [
  # "clean",
  "build-release-for-target",
  "zip-release-binary-for-target"
]

[tasks.build-release-for-target]
description = "Makes a release build for a given target"
condition = { env_set = [ "TARGET" ] }
command = "cargo"
args = [
  "build",
  "--release",
  "--all-features",
  "--target",
  "${TARGET}"
]

[tasks.zip-release-binary-for-target]
description = "Zips up the release binary, README, and license(s)"
category = "Publish"
condition = { env_set = [ "TARGET" ] }
# env = { "LIBZ_SYS_STATIC" = "1", "PKG_CONFIG_ALLOW_CROSS" = "1" }
script_runner = "@shell"
script = [
  "rm -Rf ${DIST_PATH}*",
  "mkdir -p ${DIST_PATH}",
  "cp target/${TARGET}/release/${CARGO_MAKE_CRATE_NAME} ${DIST_PATH}/",
  "strip ${DIST_PATH}/${CARGO_MAKE_CRATE_NAME}",
  # "cp README.md LICENSE* ${OUTPUT_NAME}/",
  # "zip -r ${OUTPUT_NAME}.zip ${OUTPUT_NAME}",
  "tar -czvf ${DIST_PATH}.${DIST_EXT} -C ${DIST_PATH} ."
]

[tasks.zip-release-binary-for-target.windows]
script_runner = "@shell"
script = [
  "setlocal enableextensions",
  "rm -Rf ${DIST_PATH}*",
  "md ${DIST_PATH}",
  "powershell copy-item -path target\\${TARGET}\\release\\${CARGO_MAKE_CRATE_NAME}.exe -destination ${DIST_PATH}",
  # "powershell copy-item -path README.md -destination ${OUTPUT_NAME}",
  # "powershell copy-item -path LICENSE -destination ${OUTPUT_NAME}",
  #"dir ${OUTPUT_NAME}",
  "powershell Compress-Archive -Path ${DIST_PATH}\\* -DestinationPath ${DIST_PATH}.${DIST_EXT} -CompressionLevel Optimal",
]

[tasks.update-changelog]
category="Publish"
install_crate = {crate_name="gitmoji-changelog", binary = "gitmoji-changelog", test_arg = "--help"}
script = [
  "rm -Rf CHANGELOG.md",
  "gitmoji-changelog -r x.y.z-dev -o CHANGELOG.md .",
]

# [tasks.pre-publish]
# dependencies = [
#   "update-changelog"
# ]

[tasks.publish]
command = "cargo"
args = ["release", "${@}"]

[tasks.github-upload-flow]
description = "Upload dist on githup release"
condition = { env_set = [ "GITHUB_TOKEN", "GITHUB_REPO", "GITHUB_USER", "DIST_NAME", "DIST_PATH", "DIST_EXT"], channels = ["stable"] }
dependencies = [
  "github-release-install",
  "github-upload",
]

[tasks.github-release-install]

[task.github-release-install.mac]
script= [
'''
cd target
echo download "https://github.com/aktau/github-release/releases/download/${GITHUB_RELEASE_VERSION}/darwin-amd64-github-release.tar.bz2"
curl -o darwin-amd64-github-release.tar.bz2 -sS -L "https://github.com/aktau/github-release/releases/download/${GITHUB_RELEASE_VERSION}/darwin-amd64-github-release.tar.bz2"
tar -xzvf darwin-amd64-github-release.tar.bz2
'''
]

[tasks.github-release-install.linux]
script= [
'''
cd target
echo download "https://github.com/aktau/github-release/releases/download/${GITHUB_RELEASE_VERSION}/linux-amd64-github-release.tar.bz2"
curl -o linux-amd64-github-release.tar.bz2 -sS -L "https://github.com/aktau/github-release/releases/download/${GITHUB_RELEASE_VERSION}/linux-amd64-github-release.tar.bz2"
tar -xjvf linux-amd64-github-release.tar.bz2
'''
]

[tasks.github-release-install.windows]
script_runner = "powershell"
script_extension = "ps1"
# see https://blog.jourdant.me/post/3-ways-to-download-files-with-powershell
script= [
'''
$url ="https://github.com/aktau/github-release/releases/download/$env:GITHUB_RELEASE_VERSION/windows-amd64-github-release.zip"
$output = "windows-amd64-github-release.zip"
cd target
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
echo $url
Invoke-WebRequest -Uri $url -OutFile $output
Expand-Archive -LiteralPath $output -Destination .
'''
]

[tasks.github-upload]
condition = { env_set = [ "GITHUB_RELEASE_PATH", "GITHUB_TOKEN", "GITHUB_REPO", "GITHUB_USER", "DIST_NAME", "DIST_PATH", "DIST_EXT"] }
script_runner = "@shell"
script= [
# multi-line command with \ at EOL doesn't work on windows
#  github-release upload --user "${GITHUB_USER}" --repo "${GITHUB_REPO}" --tag "${CARGO_MAKE_CRATE_VERSION}" --name "${DIST_NAME}.${DIST_EXT}" --file "../../../${DIST_PATH}.${DIST_EXT}"
'''
cd ${GITHUB_RELEASE_PATH}
github-release upload --tag "${CARGO_MAKE_CRATE_VERSION}" --name "${DIST_NAME}.${DIST_EXT}" --file "${GITHUB_RELEASE_PATH_REV}${DIST_PATH}.${DIST_EXT}"
''',
]

[tasks.echo]
script_runner = "@shell"
script=["echo ${CARGO_MAKE_PROFILE} : ${DIST_PATH} : ${GITHUB_RELEASE_PATH}"]
