Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions ui/scripts/R/gendata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' Generate unique test data files
#'
#' Creates \code{n} tab-delimited files. When \code{size} is \code{NULL},
#' file \code{i} contains \code{i} rows (so every file has a unique hash).
#' When \code{size} is given, every file has \code{size} rows but a unique
#' random seed per file ensures distinct hashes.
#'
#' @param files Directory to write the files into (created if it doesn't exist).
#' @param n Number of files to generate. Defaults to \code{1}.
#' @param size Number of rows per file. If \code{NULL} (default), file \code{i}
#' gets \code{i} rows (legacy behaviour).
#' @param data Source data frame. Defaults to \code{Theoph}.
#' @return Invisibly returns \code{NULL}. Called for its side effect of writing
#' \code{Theoph_n_1.tab}, \code{Theoph_n_2.tab}, \ldots, \code{Theoph_n_<n>.tab}
#' into \code{files}.
# TODO: rename `files` to `path` and check that it is one single path too
gendata <- function(files, n = 1, size = NULL, data = Theoph) {
dir.create(files, recursive = TRUE, showWarnings = FALSE)
for (i in seq_len(n)) {
nrows <- if (is.null(size)) i else size + i - 1L
rows <- rep_len(seq_len(nrow(data)), nrows)
write.table(
data[rows, ],
file = file.path(files, sprintf("Theoph_n_%d.tab", i)),
eol = "\n"
)
}
}
64 changes: 64 additions & 0 deletions ui/scripts/R/run.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' Source a script and capture echoed output in a file
#'
#' Runs [source()] while redirecting standard output and messages to `out`.
#' If `out` already exists, a warning is issued and the file is overwritten
#' (truncated).
#'
#' @param script Character scalar. Path to the script passed to [source()].
#' @param out Character scalar. Output file path. Defaults to `paste0(script, ".out")`.
#' @param echo Logical. Passed to [source()]. Defaults to `TRUE`.
#' @param max.deparse.length Numeric/integer. Passed to [source()].
#' Use `Inf` to avoid `"[TRUNCATED]"` markers.
#' @param split Logical. Passed to [sink()] for standard output.
#' If `TRUE`, output is written to both console and file.
#' @param ... Additional arguments passed to [source()].
#'
#' @return Invisibly returns the value from [source()].
#'
#' @examples
#' \dontrun{
#' source_to_file("script.R", out = "script.R.out")
#' source_to_file("script.R", out = "script.R.out", split = TRUE)
#' }
source_to_file <- function(
script,
out = paste0(script, ".out"),
echo = TRUE,
max.deparse.length = Inf,
split = FALSE,
...
) {
if (file.exists(out)) {
warning(
sprintf("'%s' already exists and will be overwritten.", out),
call. = FALSE
)
}

con <- file(out, open = "wt") # truncate/overwrite
out_n <- sink.number()
msg_n <- sink.number(type = "message")

sink(con, split = split)
sink(con, type = "message")

on.exit(
{
while (sink.number(type = "message") > msg_n) {
sink(type = "message")
}
while (sink.number() > out_n) {
sink()
}
close(con)
},
add = TRUE
)

invisible(source(
script,
echo = echo,
max.deparse.length = max.deparse.length,
...
))
}
37 changes: 37 additions & 0 deletions ui/scripts/R/tree.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
fs_manual_dir_tree <- function(path = ".", recurse = TRUE, ...) {
files <- fs::dir_ls(path, recurse = recurse, ...)
by_dir <- split(files, fs::path_dir(files))
ch <- fs:::box_chars()
get_coloured_name <- function(x) {
coloured <- fs:::colourise_fs_path(x)
sub(x, fs::path_file(x), coloured, fixed = TRUE)
}
print_leaf <- function(x, indent) {
leafs <- by_dir[[x]]
for (i in seq_along(leafs)) {
if (i == length(leafs)) {
cat(
indent,
fs:::pc(ch$l, ch$h, ch$h, " "),
get_coloured_name(leafs[[i]]),
"\n",
sep = ""
)
print_leaf(leafs[[i]], paste0(indent, " "))
} else {
cat(
indent,
fs:::pc(ch$j, ch$h, ch$h, " "),
get_coloured_name(leafs[[i]]),
"\n",
sep = ""
)
print_leaf(leafs[[i]], paste0(indent, fs:::pc(ch$v, " ")))
}
}
}
cat(fs:::colourise_fs_path(path), "\n", sep = "")
# print_leaf(fs::path_expand(path), "")
print_leaf(path, "")
invisible(files)
}
153 changes: 153 additions & 0 deletions ui/scripts/add_01.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@

if (!exists("dvs_workspace")) {
dvs_workspace <- getwd()
}
source(file.path(dvs_workspace, "ui/scripts/R", "tree.R"), echo = TRUE)

dvs_workspace
withr::with_dir(
dvs_workspace,
system2(
"just",
"install-cli",
)
)

system2("dvs")

message("dvs repository where storage directory is different location")
proj_root <- file.path(tempfile(), "projectA")
proj_root
dir.create(proj_root, recursive = TRUE)
setwd(proj_root)
dir.create(file.path(proj_root, ".git/"))
message("define global storage directory")
storage_directory <- file.path(tempdir(), "dvs_data_directory")
list(
project_root = proj_root,
storage_root = storage_directory
)

message("storage directory is an ancestor to the project(s)")

message(
"open visual studio code in `tempdir()` (session constant)",
" to have an overview over all file changes"
)
browseURL(url = tempdir(), browser = "code")

message("dvs with a storage directory provided")
system2(
"dvs",
c("init", storage_directory)
)

cat("# DVS repository\n", file = file.path(proj_root, "README.md"))

# fs::dir_tree(
# tempdir()
# ) |>
# print() |>
# fs::path_rel(file.path(tempdir())) |>
# fs_manual_dir_tree()

fs::dir_tree(
tempdir()
)
tempdir() |> unclass()
tempdir() |> fs::path_expand() |> unclass()

fs_manual_dir_tree(
# tempdir()
tempdir() |> fs::path_expand()
)


#'
#' Let's add something
#'

fs::dir_create(proj_root, "data")

write.table(
file = file.path(proj_root, "data", "theoph_head.tab"),
head(Theoph, 15),
eol = "\n"
)

fs::dir_tree(
tempdir()
)

message("data/theoph_head.tab:\n")

readLines(
file.path(proj_root, "data", "theoph_head.tab")
) |>
cat(sep = "\n")

system2(
"dvs",
c("add", "--help")
)

readLines(
file.path(proj_root, "dvs.toml")
) |>
cat(sep = "\n")

system2(
"dvs",
c(
"add",
file.path(proj_root, "data", "theoph_head.tab"),
"--message",
r"("added head of theoph in tab format")"
)
)
# COMMENT: The backtrace need to be removed.

fs::dir_ls(proj_root, recurse = TRUE) |>
fs::path_rel(start = fs::path(proj_root, ".."))


fs::dir_ls(storage_directory, recurse = TRUE) |>
fs::path_rel(start = fs::path(storage_directory, ".."))

readLines(fs::path(storage_directory, "audit.log.jsonl")) |>
cat(sep = "\n")

# Let's add the same file again

system2(
"dvs",
c(
"add",
file.path(proj_root, "data", "theoph_head.tab"),
"--message",
r"("ssadded head of theoph in tab format")"
)
)
file.edit(fs::path(storage_directory, "audit.log.jsonl"))
readLines(fs::path(storage_directory, "audit.log.jsonl")) |>
cat(sep = "\n")
#' The audit log has two entries, for files that has the exact same hash, but the time
#' is different.
#'
#'

write.table(
file = file.path(proj_root, "data", "theoph_head3.tab"),
head(Theoph, 12),
eol = "\n"
)

system2(
"dvs",
c(
"add",
file.path(proj_root, "data", "theoph_head3.tab"),
"--message",
r"("added head of theoph in tab format")"
)
)
Loading