Permalink
Browse files

Hadley-fication

  • Loading branch information...
1 parent 0fc4ad7 commit d77b9ddab5b837dc7d3c55c718209af02ff388e8 @hadley committed Aug 12, 2012
Showing with 72 additions and 88 deletions.
  1. +1 −3 DESCRIPTION
  2. +1 −3 NAMESPACE
  3. +0 −15 R/create-package-doc.r
  4. +49 −35 R/create.r
  5. +0 −13 R/save-package-description.R
  6. +3 −1 README.md
  7. +2 −5 inst/templates/packagename-package.r
  8. +16 −13 man/create.Rd
View
@@ -12,7 +12,7 @@ Imports:
utils,
tools,
memoise,
- brew
+ whisker
@yoni

yoni Aug 14, 2012

Contributor

Nice. I didn't know this package existed. I like keeping logic out of my templates. :)

Suggests:
testthat,
roxygen2,
@@ -64,5 +64,3 @@ Collate:
'inst.r'
'metadata.r'
'create.r'
- 'create-package-doc.r'
- 'save-package-description.R'
View
@@ -9,7 +9,6 @@ export(check_doc)
export(check)
export(clean_source)
export(clean_vignettes)
-export(create_package_doc)
export(create)
export(dev_meta)
export(dev_mode)
@@ -41,7 +40,6 @@ export(revdep_check)
export(revdep_maintainers)
export(revdep)
export(run_examples)
-export(save_package_description)
export(set_path)
export(show_news)
export(show_rd)
@@ -58,7 +56,6 @@ export(with_locale)
export(with_options)
export(with_par)
export(with_path)
-importFrom(brew,brew)
importFrom(httr,config)
importFrom(httr,content)
importFrom(httr,GET)
@@ -71,3 +68,4 @@ importFrom(tools,file_ext)
importFrom(tools,package_dependencies)
importFrom(tools,Rd2txt)
importFrom(utils,install.packages)
+importFrom(whisker,whisker.render)
View
@@ -1,15 +0,0 @@
-#' Creates package-level documentation, where the developer can add
-#' documentation for data, vignettes, etc.
-#' @param path path to the package
-#' @param name of the package
-#' @importFrom brew brew
-#' @export
-create_package_doc <- function(path, name) {
-
- package_doc_file <- file.path(path, 'R', sprintf("%s-package.r", name))
- brew(
- file=(system.file("templates/packagename-package.r", package="devtools")),
- output=package_doc_file
- )
-
-}
View
@@ -1,50 +1,64 @@
#' Creates a new package, following all devtools package conventions.
#'
-#' @param name character string: the package name and directory name for
-#' your package.
-#' @param path to put the package directory in
-#' @param description list of description values to override default values or add additional values.
+#' Similar to \code{\link{package.skeleton}}, except that it only creates
+#' the standard devtools directory structures, it doesn't try and create
+#' source code and data files by inspecting the global environment.
+#'
+#' @param path location to create new package. The last component of the path
@yoni

yoni Aug 14, 2012

Contributor

I think I get the rationale, but not sure I like combining the path with the package name. This way of doing it seems less explicit and less readable. But that's a matter of taste.

@hadley

hadley Aug 16, 2012

Owner

I was thinking about adding back the name parameter so you could have it either way. If not specified, it would default to basename(path). What do you think?

@yoni

yoni Aug 18, 2012

Contributor

In the scenario you mentioned, there would be two ways of calling the function, with the 'path' parameter meaning something different in each case. That seems confusing and error-prone. Personally, I prefer for functions to have a single simple interface, without too many cases for folks to understand. Specifically, these patterns seem confusing:

# same result:
create(path='.', name='fancyPackage')
create(path='./fancyPackage')

# likely error:
create(path='./fancyPackage', name='fancyPackage')

I'd have both parameters required, with 'path' not including the package name.

@hadley

hadley Aug 29, 2012

Owner

I went with the parameterisation I did because it's more inline with the other devtools packages. i.e you could do:

path <- "mypath/..."
create(path)
install(path)
check(path)
+#' will be used as the package name.
+#' @param description list of description values to override default values or
+#' add additional values.
+#' @param ... other options passed to \code{package.skeleton}
@yoni

yoni Aug 14, 2012

Contributor

This parameter is not being used.

@hadley

hadley Aug 16, 2012

Owner

Fixed - thanks.

#' @seealso Text with \code{\link{package.skeleton}}
#' @export
#' @examples
-#'
#' # Create a package using all defaults:
-#' path_to_package <- tempdir()
-#' create(name='myDefaultPackage', path=path_to_package)
+#' path <- file.path(tempdir(), "myDefaultPackage")
+#' create(path)
#'
#' # Override a description attribute.
-#' # Note that overriding one atribute implies that you must override
-#' # all default attributes, as well.
-#' my_description <- list("Maintainer"="'Yoni Ben-Meshulam' <yoni@@opower.com>")
-#' create(name='myCustomPackage', path=path_to_package, description=my_description)
-create <- function(
- name,
- path,
- description=list()
- ) {
+#' path <- file.path(tempdir(), "myCustomPackage")
+#' my_description <- list("Maintainer" =
+#' "'Yoni Ben-Meshulam' <yoni@@opower.com>")
+#' create("../myCustomPackage", my_description)
+create <- function(path, description = list()) {
+ name <- basename(path)
+ message("Creating package ", name, " in ", dirname(path))
- description.defaults <- list(
- Package=name,
- Maintainer="Who to complain to <yourfault@somewhere.net>",
- Author=Sys.getenv("USER"),
- Version="1.0",
- License="GPL-3",
- Title=name,
- Description=name,
- Suggests="\ntestthat,\nroxygen2"
- )
+ if (file.exists(path)) {
+ stop("Directory already exists", call. = FALSE)
+ }
+ if (!file.exists(dirname(path))) {
+ stop("Parent directory does not exist.", call. = FALSE)
+ }
- package_path <- file.path(path, name)
- message(sprintf("Creating package [%s] in path [%s]", name, package_path))
+ dir.create(path)
- dir.create(package_path)
- dir.create(file.path(package_path, 'R'))
+ defaults <- list(
@yoni

yoni Aug 14, 2012

Contributor

Do you have this style codified someplace I can access it? I've been using R-Vim plugin, which aligns parens differently. Would love to switch to this style when working on devtools, but I don't want to reinvent the wheel.

@yoni

yoni Aug 18, 2012

Contributor

Right, I've read that. What I meant was do you have an automated way of enforcing it in your editor? I use vim with the vim R plugin, which enforces a somewhat different style.

Anyhow, I guess I'll just have to codify it manually in my vim settings.

+ Package = name,
+ Maintainer = "Who to complain to <yourfault@somewhere.net>",
+ Author = Sys.getenv("USER"),
+ Version = "1.0",
+ License = "GPL-3",
+ Title = "",
+ Description = "",
+ Suggests = "\ntestthat,\nroxygen2"
+ )
+ description <- modifyList(defaults, description)
+ write.dcf(description, file.path(path, 'DESCRIPTION'))
+
+ dir.create(file.path(path, "R"))
+ create_package_doc(path, name)
- description <- modifyList(description.defaults, description)
-
- save_package_description(package_path, description)
- create_package_doc(package_path, name)
+ check(path)
+}
- check(package_path)
+#' @importFrom whisker whisker.render
+create_package_doc <- function(path, name) {
+ template <- readLines(system.file("templates", "packagename-package.r",
+ package = "devtools"))
+ out <- whisker.render(template, list(name = name))
+ target <- file.path(path, "R", paste(name, "-package.r", sep = ""))
+ writeLines(out, target)
}
@@ -1,13 +0,0 @@
-#' Writes the DESCRIPTION parameters to the given package.
-#' @param path to the package
-#' @param description names vector of description values
-#' @export
-save_package_description <- function(path, description) {
-
- fields <- names(description)
-
- description.matrix <-
- matrix(description, ncol=length(fields),dimnames=list(NULL,fields))
- write.dcf(x=description.matrix, file=file.path(path, 'DESCRIPTION'))
-
-}
View
@@ -38,7 +38,9 @@ Building and installing:
arbitrary url. `install_version` installs a specified version from cran,
Creating a new package:
-* `create("pkg")` creates a new R package, followint all devtools conventions.
+
+* `create("path/to/pkg")` creates a new R package following devtools
+ conventions. called `pkg` in `path/to`,
Check and release:
@@ -1,8 +1,5 @@
-#' <%= name %>
+#' {{{ name }}}
#'
-#' @name <%= name %>-package
-#' @aliases <%= name %>
+#' @name {{{ name }}}
#' @docType package
-#' @title <%= name %>
-#' @keywords package
NULL
View
@@ -2,31 +2,34 @@
\alias{create}
\title{Creates a new package, following all devtools package conventions.}
\usage{
- create(name, path, description = list())
+ create(path, description = list())
}
\arguments{
- \item{name}{character string: the package name and
- directory name for your package.}
-
- \item{path}{to put the package directory in}
+ \item{path}{location to create new package. The last
+ component of the path will be used as the package name.}
\item{description}{list of description values to override
default values or add additional values.}
+
+ \item{...}{other options passed to
+ \code{package.skeleton}}
}
\description{
- Creates a new package, following all devtools package
- conventions.
+ Similar to \code{\link{package.skeleton}}, except that it
+ only creates the standard devtools directory structures,
+ it doesn't try and create source code and data files by
+ inspecting the global environment.
}
\examples{
# Create a package using all defaults:
-path_to_package <- tempdir()
-create(name='myDefaultPackage', path=path_to_package)
+path <- file.path(tempdir(), "myDefaultPackage")
+create(path)
# Override a description attribute.
-# Note that overriding one atribute implies that you must override
-# all default attributes, as well.
-my_description <- list("Maintainer"="'Yoni Ben-Meshulam' <yoni@opower.com>")
-create(name='myCustomPackage', path=path_to_package, description=my_description)
+path <- file.path(tempdir(), "myCustomPackage")
+my_description <- list("Maintainer" =
+ "'Yoni Ben-Meshulam' <yoni@opower.com>")
+create("../myCustomPackage", my_description)
}
\seealso{
Text with \code{\link{package.skeleton}}

0 comments on commit d77b9dd

Please sign in to comment.