From 3411c2fe19409994793479b0d6bc6951d7985e46 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Wed, 13 Sep 2023 02:31:52 +0200
Subject: [PATCH] Improve RStudio project template

* implement copying template files (placeholer replacements do not yet work)
* Start vignette
---
 R/create_LIC_project.R                        | 94 ++++++++++++-------
 ...ny-specific-implementations-as-package.Rmd | 46 +++++++++
 2 files changed, 106 insertions(+), 34 deletions(-)
 create mode 100644 vignettes/creating-company-specific-implementations-as-package.Rmd

diff --git a/R/create_LIC_project.R b/R/create_LIC_project.R
index 6c07a5a..5289652 100644
--- a/R/create_LIC_project.R
+++ b/R/create_LIC_project.R
@@ -5,45 +5,71 @@
 # and heavily modified
 
 create_LIC_project <- function(path, ...) {
-    # ensure path exists
+    dots <- list(...)
+    CompanyName = dots$Company
+
+
+
+    ########################################
+    #####  1. ensure destination path exists, sanity checks on the company name
+
     dir.create(path, recursive = TRUE, showWarnings = FALSE)
 
 
+    ########################################
+    #####  2. copy over all files from the template directory
 
-    # LIC.src <- function (..., lib.loc = NULL, mustWork = FALSE){
-    #     system.file(..., package = "LifeInsuranceContracts", lib.loc = lib.loc, mustWork = mustWork)
-    # }
-    #
-    # from <- LIC.src("templatedemo")
-    #
-    # fs::dir_copy(
-    #     path = from,
-    #     new_path = path,
-    #     overwrite = TRUE
-    # )
-
-    # generate header for file
-    header <- c(
-        "# This file was generated by a call to 'ptexamples::hello_world()'.",
-        "# The following inputs were received:",
-        ""
-    )
+    LIC.src <- function (..., lib.loc = NULL, mustWork = FALSE){
+        system.file("rstudio", "templates", "project", ...,
+                    package = "LifeInsuranceContracts",
+                    lib.loc = lib.loc, mustWork = mustWork)
+    }
+    from <- LIC.src("LifeInsuranceContracts")
+    copied_files <- fs::dir_copy(path = from, new_path = path, overwrite = TRUE)
 
-    # collect inputs and paste together as 'Parameter: Value'
-    dots <- list(...)
-    text <- lapply(seq_along(dots), function(i) {
-        key <- names(dots)[[i]]
-        val <- dots[[i]]
-        paste0(key, ": ", val)
-    })
-
-    # collect into single text string
-    contents <- paste(
-        paste(header, collapse = "\n"),
-        paste(text, collapse = "\n"),
-        sep = "\n"
+
+    ########################################
+    #####  3. Rename all files with XXXCOMPANYXXX -> CompanyName
+
+    LIC.replaceFilename <- function(...) {
+
+    }
+
+
+    ########################################
+    #####  4. Replace all file contents with XXXCOMPANYXXX -> CompanyName
+
+    LIC.replaceFileContents <- function(file, pattern, replace) {
+        suppressWarnings(tx <- readLines(file))
+        tx2 <- gsub(pattern = pattern, replacement = replace, x = tx)
+        writeLines(tx2, con = file)
+    }
+
+    LIC.replaceAllFilesContents <- function(
+        copied_files, pattern, replace, path) {
+        # Going through copied files to replace package name
+        for (f in copied_files) {
+            copied_file <- file.path(path, f)
+
+            try({
+                LIC.replaceFileContents(
+                    file = copied_file,
+                    pattern = pattern,
+                    replace = replace
+                )
+            }, silent = TRUE)
+        }
+    }
+    LIC.replaceAllFilesContents(
+        copied_files,
+        pattern = "XXXCOMPANYXXX",
+        replace = CompanyName,
+        path
     )
 
-    # write to index file
-    writeLines(contents, con = file.path(path, "RechnungGesamtbestand.R"))
+
+    ########################################
+    #####  5. All other small tasks
+
+
 }
diff --git a/vignettes/creating-company-specific-implementations-as-package.Rmd b/vignettes/creating-company-specific-implementations-as-package.Rmd
new file mode 100644
index 0000000..50dd350
--- /dev/null
+++ b/vignettes/creating-company-specific-implementations-as-package.Rmd
@@ -0,0 +1,46 @@
+---
+title: "Creating Company-Specific LifeInsuranceContracts Implementations (using an RStudio Package Template)"
+author:
+- name: Reinhold Kainhofer
+  affiliation: Open Tools
+  email: reinhold@kainhofer.com
+
+date: "`r Sys.Date()`"
+output:
+    rmarkdown::html_vignette:
+        toc: true
+        toc_depth: 3
+        fig_width: 7
+        fig_height: 5
+        number_sections: true
+vignette: >
+  %\VignetteIndexEntry{Using the LifeInsuranceContracts Package}
+  %\VignetteEngine{knitr::rmarkdown}
+  %\VignetteEncoding{UTF-8}
+---
+
+
+```{r setup, echo = FALSE, message=FALSE}
+knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
+library(knitr)
+library(kableExtra)
+library(LifeInsuranceContracts)
+library(dplyr)
+library(tibble)
+library(lubridate)
+options(scipen=5)
+
+library(pander)
+```
+
+The LifeInsuranceContracts package provides a full-featured framework to model classical life insurance contracts (non-unit linked). This is typically sufficient to implement simple example calculations or validate a single contract or tariff by a single individual.
+
+However, when working for a company (eith from inside the company or as an external consultant), one typically wants the implementation to be nicely structured, easily available for the whole team and still have the chance to improve the implementation.
+This can be achieved by encapsulating the company-specific tariff implementations
+in an R package that provides and exports the individual products of the company.
+
+The LifeInsuranceContracts package even provides an RStudio project template 
+to create a new skeleton of a company-specific implementation to use as the
+foundation of such an implementation.
+
+Once, 
-- 
GitLab