Skip to content

Commit

Permalink
bugfixes for non-Rstudio
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliot McIntire committed Oct 24, 2024
1 parent 92b6679 commit 3ad477a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 87 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Description: Quickly setup a 'SpaDES' project directories and add modules using
URL:
https://spades-project.predictiveecology.org/,
https://github.com/PredictiveEcology/SpaDES.project
Date: 2024-10-23
Version: 0.1.0.9014
Date: 2024-10-24
Version: 0.1.0.9015
Authors@R: c(
person("Eliot J B", "McIntire", email = "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6914-8316")),
Expand Down
166 changes: 88 additions & 78 deletions R/setupProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -2432,8 +2432,12 @@ setupRestart <- function(updateRprofile, paths, name, inProject,
}
}

if ( (interactive() && (isTRUE(Restart) || is.character(Restart)) ) && isRstudio()
|| !(useGit %in% FALSE)) {# getOption("SpaDES.project.Restart", TRUE))
if (!isRstudio()) {
Restart <- FALSE
}

if ( (interactive() && (isTRUE(Restart) || is.character(Restart)) ) && isRstudio() ||
!(useGit %in% FALSE)) {# getOption("SpaDES.project.Restart", TRUE))

on.exit(setwd(origGetWd), add = TRUE)

Expand All @@ -2443,16 +2447,18 @@ setupRestart <- function(updateRprofile, paths, name, inProject,
}
pp <- path.expand(paths[["projectPath"]])
isRstudioProj <- rprojroot::is_rstudio_project$testfun[[1]](pp)
curRstudioProj <- rstudioapi::getActiveProject()
isRstudioProj <- isRstudioProj && isTRUE(basename2(curRstudioProj) %in% basename(pp))
if (isRstudio()) {
curRstudioProj <- rstudioapi::getActiveProject()
isRstudioProj <- isRstudioProj && isTRUE(basename2(curRstudioProj) %in% basename(pp))
}
# inProject <- isInProject(name)

if (!inProject || !isRstudioProj) {
if (requireNamespace("rstudioapi", lib.loc = paths[["packagePath"]])) {
messageVerbose("... restarting Rstudio inside the project",
verbose = verbose)
if ((!inProject || !isRstudioProj)) {
if (requireNamespace("rstudioapi", lib.loc = paths[["packagePath"]]) && isRstudio() ) {
wasUnsaved <- FALSE
wasLastActive <- FALSE
messageVerbose("... restarting Rstudio inside the project",
verbose = verbose)
activeFile <- rstudioapi::getSourceEditorContext()$path
if (!is.character(Restart)) {
rstudioUnsavedFile <- "~/.active-rstudio-document"
Expand Down Expand Up @@ -2549,95 +2555,99 @@ setupRestart <- function(updateRprofile, paths, name, inProject,
")")
cat(addToTempFile, file = tempfileInOther, sep = "\n")
cat(newRprofile, file = RprofileInOther, sep = "\n")
cloned <- FALSE

if (((!useGit %in% FALSE) && requireNamespace("usethis") && requireNamespace("gh") &&
requireNamespace("gitcreds")) && cloned %in% FALSE ) {
}

cloned <- FALSE
if (((!useGit %in% FALSE) && requireNamespace("usethis") && requireNamespace("gh") &&
requireNamespace("gitcreds")) && cloned %in% FALSE ) {

basenameName <- basename(name)
needGitUserName <- TRUE
if (is.character(useGit)) {
if (useGit != "sub") {
needGitUserName <- FALSE
gitUserName <- useGit
}
basenameName <- basename(name)
needGitUserName <- TRUE
if (is.character(useGit)) {
if (useGit != "sub") {
needGitUserName <- FALSE
gitUserName <- useGit
}
}

if (needGitUserName) {
mess <- capture.output(
type = "message",
gitUserNamePoss <- gh::gh_whoami()$login)
if (is.null(gitUserNamePoss)) {
stop(paste(c(mess, "or try gitcreds::gitcreds_set()"), collapse = "\n"))
}
messageVerbose(msgNeedGitUserName(gitUserNamePoss), verbose = interactive() * 10)
gitUserName <- if (interactive()) readline() else gitUserNamePoss
if (!nzchar(gitUserName)) {
gitUserName <- gitUserNamePoss
needGitUserName <- FALSE
}

if (needGitUserName) {
mess <- capture.output(
type = "message",
gitUserNamePoss <- gh::gh_whoami()$login)
if (is.null(gitUserNamePoss)) {
stop(paste(c(mess, "or try gitcreds::gitcreds_set()"), collapse = "\n"))
}
messageVerbose(msgNeedGitUserName(gitUserNamePoss), verbose = interactive() * 10)
gitUserName <- if (interactive()) readline() else gitUserNamePoss
if (!nzchar(gitUserName)) {
gitUserName <- gitUserNamePoss
needGitUserName <- FALSE
}

}


if (!rprojroot::is_rstudio_project$testfun[[1]](pp)) {
host <- "https://github.com"
tf <- tempfile2();
out <- .downloadFileMasterMainAuth(file.path("https://api.github.com/repos",gitUserName, basenameName),
destfile = tf, verbose = verbose - 10)
# The suppressWarnings is for "incomplete final line"
checkExists <- if (file.exists(tf)) suppressWarnings(readLines(tf)) else "Not Found"
if (!rprojroot::is_rstudio_project$testfun[[1]](pp)) {
host <- "https://github.com"
tf <- tempfile2();
out <- .downloadFileMasterMainAuth(file.path("https://api.github.com/repos",gitUserName, basenameName),
destfile = tf, verbose = verbose - 10)
# The suppressWarnings is for "incomplete final line"
checkExists <- if (file.exists(tf)) suppressWarnings(readLines(tf)) else "Not Found"

if (any(grepl("Not Found", checkExists))) {
usethis::create_project(pp, open = FALSE, rstudio = isRstudio())
if (any(grepl("Not Found", checkExists))) {
usethis::create_project(pp, open = FALSE, rstudio = isRstudio())
} else {
repo <- file.path(host, gitUserName, basenameName)
messageVerbose(.messages$gitRepoExistsCloneNowTxt(repo))
# messageVerbose(paste0("The github repository already exists: ", repo),
# "\nWould you like to clone it now to ", getwd(), "\nType (y)es or any other key for no: ")
out <- if (interactive() && getOption("SpaDES.project.ask", TRUE)) readline() else "yes"
if (grepl("y|yes", tolower(out))) {
setwd(dirname(getwd()))
unlink(basenameName, recursive = TRUE)
gert::git_clone(repo, path = basenameName)
cloned <- TRUE
setwd(paths[["projectPath"]])
} else {
repo <- file.path(host, gitUserName, basenameName)
messageVerbose(.messages$gitRepoExistsCloneNowTxt(repo))
# messageVerbose(paste0("The github repository already exists: ", repo),
# "\nWould you like to clone it now to ", getwd(), "\nType (y)es or any other key for no: ")
out <- if (interactive() && getOption("SpaDES.project.ask", TRUE)) readline() else "yes"
if (grepl("y|yes", tolower(out))) {
setwd(dirname(getwd()))
unlink(basenameName, recursive = TRUE)
gert::git_clone(repo, path = basenameName)
cloned <- TRUE
setwd(paths[["projectPath"]])
} else {
stop("Can't proceed: either delete existing github repo, change the ",
"project name, or change the Github account and try again")
}
stop("Can't proceed: either delete existing github repo, change the ",
"project name, or change the Github account and try again")
}
}
}

if (!isFALSE(getOption("SpaDES.project.gitignore", TRUE))) {
igs <- gitIgnoreInitials(paths)
usethis::use_git_ignore(igs)
# gert::git_add(".gitignore")
# gert::git_commit("add more .gitignores")
}
if (!isFALSE(getOption("SpaDES.project.gitignore", TRUE))) {
igs <- gitIgnoreInitials(paths)
usethis::use_git_ignore(igs)
# gert::git_add(".gitignore")
# gert::git_commit("add more .gitignores")
}

if (!rprojroot::is_git_root$testfun[[1]](pp)) {
if (needGitUserName) {
messageVerbose(msgNeedGitUserName(gitUserNamePoss), verbose = interactive() * 10)
gitUserName <- readline()
}
if (!nzchar(gitUserName))
gitUserName <- NULL
bbb <- try(usethis::use_git())
if (!(exists("gitUserNamePoss", inherits = FALSE)))
gitUserNamePoss <- gh::gh_whoami()$login
if (identical(gitUserName, gitUserNamePoss))
gitUserName <- NULL

githubRepoExists <- usethis::use_github(gitUserName) # This will fail if not an organization
if (!rprojroot::is_git_root$testfun[[1]](pp)) {
if (needGitUserName) {
messageVerbose(msgNeedGitUserName(gitUserNamePoss), verbose = interactive() * 10)
gitUserName <- readline()
}
if (!nzchar(gitUserName))
gitUserName <- NULL
bbb <- try(usethis::use_git())
if (!(exists("gitUserNamePoss", inherits = FALSE)))
gitUserNamePoss <- gh::gh_whoami()$login
if (identical(gitUserName, gitUserNamePoss))
gitUserName <- NULL

githubRepoExists <- usethis::use_github(gitUserName) # This will fail if not an organization
}
}
if (isRstudio()) {
on.exit(rstudioapi::openProject(path = paths[["projectPath"]], newSession = TRUE))
message("Starting a new Rstudio session with projectPath (", green(paths[["projectPath"]]), ") as its root")
stop_quietly()
} else {
stop("Please open this in a new Rstudio project at ", paths[["projectPath"]])
}
#} else {
# stop("Please open this in a new Rstudio project at ", paths[["projectPath"]])
#}
}
} else {
if (!Restart %in% FALSE) {
Expand Down
6 changes: 5 additions & 1 deletion man/spadesProjectOptions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions vignettes/i-getting-started.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ editor_options:
chunk_output_type: console
---

```{r setCRAN,echo=FALSE}
# cleanup / restore state
if (is.null(getOption("repos")) || !"CRAN" %in% names(getOption("repos"))) {
options(repos = c(CRAN = "https://cloud.r-project.org"))
}
```


```{r setup, include = FALSE}
Expand All @@ -22,6 +28,7 @@ knitr::opts_chunk$set(
)
# Most are eval=FALSE within each chunk; but a few are not. Those must be eval=FALSE on GA and CRAN
if (is.null(getOption("repos"))) chooseCRANmirror(ind = 1)
SuggestedPkgsNeeded <- c("SpaDES.core", "igraph", "visNetwork", "ellipsis", "terra")
hasSuggests <- all(sapply(SuggestedPkgsNeeded, requireNamespace, quietly = TRUE))
useSuggests <- !(tolower(Sys.getenv("_R_CHECK_DEPENDS_ONLY_")) == "true")
Expand All @@ -46,12 +53,6 @@ origLibPath = .libPaths()[1]



```{r setCRAN,echo=FALSE}
# cleanup / restore state
if (is.null(getOption("repos")) || !"CRAN" %in% names(getOption("repos"))) {
options(repos = c(CRAN = "https://cloud.r-project.org"))
}
```

`SpaDES` is a collection of R packages designed to provide a flexible framework for ecological analysis, simulations, forecasting, and other ecological tasks. At its core, a `SpaDES` analysis revolves around the concept of a "module" - a self-contained unit of code that performs a specific function and includes both machine-readable and human-readable metadata. In a typical `SpaDES` project, users often employ multiple modules, whether they've authored them or they've been contributed by others. This scalability introduces several challenges, including managing file paths (e.g., for data inputs, outputs, figures, modules, and packages), handling package dependencies, and coordinating the development of multiple modules simultaneously. While it is possible to develop a `SpaDES` project using methods familiar to the user, the `SpaDES.project` package streamlines this process, facilitating a clean, reproducible and reusable project setup for any project. Default settings ensure that project files and packages are kept isolated, preventing potential conflicts with other projects on the same computer.

Expand Down

0 comments on commit 3ad477a

Please sign in to comment.