---
params:
  script: 'dev'
  renv: TRUE
  tmpdir: "."
  converted_qmd: "temp_script.qmd"
---

```{r}
#| label: Setup
#| include: false
knitr::opts_chunk$set(
  error = TRUE,
  warning = TRUE,
  message = TRUE,
  echo = TRUE
)

file.path(params$tmpdir, "with_options.rds") |>
  readRDS() |>
  options()
```

```{r}
#| label: Convert R script to qmd
#| eval: !expr base::grepl("\\.R$", params$script)
#| echo: false
# Conversion is done to avoid problems caused by the warnings thrown
# by knitr::knit(), see e.g. issue #151
knitr::spin(
  text = readLines(con = params$script, warn = FALSE),
  knit = FALSE,
  format = "qmd"
) |>
  writeLines(
    con = file.path(params$tmpdir, params$converted_qmd)
  )
```

```{r}
#| label: Log R
#| child: !expr file.path(params$tmpdir, params$converted_qmd)
#| eval: !expr file.exists(file.path(params$tmpdir, params$converted_qmd))
#| include: !expr file.exists(file.path(params$tmpdir, params$converted_qmd))
```

```{r}
#| label: Log Quarto
#| child: !expr params$script
#| eval: !expr base::grepl("\\.qmd$", params$script)
#| include: !expr base::grepl("\\.qmd$", params$script)
```

```{r}
#| label: Log Rmarkdown
#| child: !expr params$script
#| eval: !expr base::grepl("\\.Rmd$", params$script)
#| include: !expr base::grepl("\\.Rmd$", params$script)
```

```{python}
#| label: Setup Python tracking
#| eval: !expr base::grepl("\\.py$", params$script)
#| include: false
#| error: false
import sys
sys.path.append(r.params["tmpdir"])
from python_modules import get_package_status
get_package_status(r.params["tmpdir"]+"/"+'py_old_status.json')
```

```{python}
#| label: Log Python
#| file: !expr params$script
#| eval: !expr base::grepl("\\.py$", params$script)
#| include: !expr base::grepl("\\.py$", params$script)
```

```{python}
#| label: Log Python modules
#| eval: !expr base::grepl("\\.py$", params$script)
#| include: false
#| error: false
get_package_status(r.params["tmpdir"]+"/"+'py_new_status.json')
```

```{r}
#| label: Python modules
#| eval: !expr base::grepl("\\.py$", params$script)
#| include: false
#| error: false
saveRDS(
  object = reticulate::py_list_packages(),
  file = file.path(params$tmpdir, "py_pkg_list.rds")
)
```

```{r}
#| label: renv status
#| include: false
#| error: false
if (is.character(params$renv) && params$renv == "yes" ||
      is.logical(params$renv) && params$renv) {
  saveRDS(
    object = whirl:::renv_status(),
    file = file.path(params$tmpdir, "renv_status.rds")
  )
}
```

```{r}
#| label: Log options and environment
#| include: false
#| error: false
saveRDS(
      object = options(),
      file = file.path(params$tmpdir, "options.rds")
)

saveRDS(
  object = Sys.getenv(),
  file = file.path(params$tmpdir, "environment.rds")
)
```

```{r}
#| label: Session info
#| include: false
#| error: false
info <- list( # Due to https://github.com/r-lib/sessioninfo/issues/96
  platform = sessioninfo::session_info(info = "platform")[["platform"]],
  packages = sessioninfo::session_info(info = "packages")[["packages"]],
  python = sessioninfo::session_info(info = "python")[["python"]]
)

if (!"quarto" %in% names(info$platform)) { # Due to older versions of sessioninfo
  info$platform$quarto <- paste(quarto::quarto_version(), "@", quarto::quarto_path())
}

saveRDS(
  object = info,
  file = file.path(params$tmpdir, "session_info.rds")
)
```
