## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(stamp)
old_alias <- st_alias_get()

## -----------------------------------------------------------------------------
root_a <- tempfile("projA-")
root_b <- tempfile("projB-")
alias_a <- basename(root_a)
alias_b <- basename(root_b)

# Note: st_init() creates the folder if missing
st_init(root_a, alias = alias_a)
st_init(root_b, alias = alias_b)

# Inspect what was created
fs::dir_tree(fs::path(root_a, ".stamp"), recurse = TRUE, all = TRUE)
fs::dir_tree(fs::path(root_b, ".stamp"), recurse = TRUE, all = TRUE)

## -----------------------------------------------------------------------------
st_alias_list()
# Get alias details
st_alias_get(alias_a)

## -----------------------------------------------------------------------------
pA <- fs::path(root_a, "data.qs")
pB <- fs::path(root_b, "data.qs")

# Save different data to A and B to ensure versions are created
st_save(data.frame(id = 1:2), pA, alias = alias_a)
st_save(data.frame(id = 3:4), pB, alias = alias_b)

# Each alias has its own version history
st_versions(pA, alias = alias_a)
st_versions(pB, alias = alias_b)

# Loading respects the alias
objA <- st_load(pA, alias = alias_a)
objB <- st_load(pB, alias = alias_b)
list(A = objA, B = objB)

# Inspect what was created within the stamp folders
# Note: Artifacts are stored as <root>/<path>/<filename>/<filename>
# with versions in <root>/<path>/<filename>/versions/
cat("\nStructure in root_a:\n")
fs::dir_tree(root_a, recurse = 2)

cat("\nStructure in root_b:\n")
fs::dir_tree(root_b, recurse = 2)

## -----------------------------------------------------------------------------
# Load latest (using default version = NULL for latest)
latestA <- st_load(pA, alias = alias_a)
latestB <- st_load(pB, alias = alias_b)

list(latestA = latestA, latestB = latestB)

## -----------------------------------------------------------------------------
# Re-base default to alias A's folder (silent)
st_switch(alias_a)

# Now calls without alias use whatever folder default points to
st_save(data.frame(id = 5), fs::path(root_a, "more.qs"))

## ----cleanup, include=FALSE---------------------------------------------------
if (!is.null(old_alias)) {
  st_init(old_alias$root, state_dir = old_alias$state_dir, verbose = FALSE)
}
unlink(c(root_a, root_b), recursive = TRUE)

