11# -*- tab-width: 4; -*-
22
3- # Copyright (C) 2009 - 2013 Dirk Eddelbuettel and Romain Francois
3+ # Copyright (C) 2009 - 2014 Dirk Eddelbuettel and Romain Francois
44#
55# This file is part of Rcpp.
66#
@@ -23,190 +23,184 @@ Rcpp.package.skeleton <- function(name = "anRpackage", list = character(),
2323 code_files = character (), cpp_files = character (),
2424 example_code = TRUE , attributes = TRUE , module = FALSE ,
2525 author = " Who wrote it" ,
26- maintainer = if (missing( author )) " Who to complain to" else author ,
26+ maintainer = if (missing(author )) " Who to complain to"
27+ else author ,
2728 email = " yourfault@somewhere.net" ,
2829 license = " What Licence is it under ?" ) {
2930
30- call <- match.call()
31- call [[1 ]] <- as.name(" package.skeleton" )
32- env <- parent.frame(1 )
31+ call <- match.call()
32+ call [[1 ]] <- as.name(" package.skeleton" )
33+ env <- parent.frame(1 )
3334
3435 if (! is.character(cpp_files ))
3536 stop(" 'cpp_files' must be a character vector" )
3637
37- if ( ! length(list ) ) {
38+ if ( ! length(list )) {
3839 fake <- TRUE
39- assign( " Rcpp.fake.fun" , function (){}, envir = env )
40- if ( example_code && ! isTRUE(attributes )){
41- assign( " rcpp_hello_world" , function (){}, envir = env )
40+ assign(" Rcpp.fake.fun" , function () {}, envir = env )
41+ if ( example_code && ! isTRUE(attributes )) {
42+ assign(" rcpp_hello_world" , function () {}, envir = env )
4243 remove_hello_world <- TRUE
4344 } else {
4445 remove_hello_world <- FALSE
4546 }
4647 } else {
47- if ( example_code && ! isTRUE(attributes )){
48- if ( ! " rcpp_hello_world" %in% list ){
49- assign( " rcpp_hello_world" , function (){}, envir = env )
50- call [[" list" ]] <- as.call( c(
51- as.name(" c" ), as.list(c( " rcpp_hello_world" , list ))
52- ) )
53- }
48+ if (example_code && ! isTRUE(attributes )) {
49+ if (! " rcpp_hello_world" %in% list ) {
50+ assign( " rcpp_hello_world" , function () {}, envir = env )
51+ call [[" list" ]] <- as.call(c(as.name(" c" ),
52+ as.list(c(" rcpp_hello_world" , list ))))
53+ }
5454 remove_hello_world <- TRUE
5555 } else {
5656 remove_hello_world <- FALSE
5757 }
5858 fake <- FALSE
5959 }
6060
61- # first let the traditional version do its business
62- # remove Rcpp specific arguments
61+ # # first let the traditional version do its business
62+ # # remove Rcpp specific arguments
6363
64- call <- call [ c( 1L , which( names(call ) %in% names(formals(package.skeleton )))) ]
64+ call <- call [ c(1L , which(names(call ) %in% names(formals(package.skeleton )))) ]
6565
66- if ( fake ){
67- call [[" list" ]] <- c( if ( isTRUE(example_code ) && ! isTRUE(attributes )) " rcpp_hello_world" , " Rcpp.fake.fun" )
66+ if (fake ) {
67+ call [[" list" ]] <- c(if (isTRUE(example_code )
68+ && ! isTRUE(attributes )) " rcpp_hello_world" , " Rcpp.fake.fun" )
6869 }
6970
70- tryCatch( eval( call , envir = env ), error = function (e ){
71- stop( sprintf( " error while calling `package.skeleton` : %s" , conditionMessage(e ) ) )
72- } )
71+ tryCatch(eval(call , envir = env ), error = function (e ){
72+ stop(sprintf(" error while calling `package.skeleton` : %s" , conditionMessage(e )) )
73+ })
7374
74- message( " \n Adding Rcpp settings" )
75+ message(" \n Adding Rcpp settings" )
7576
76- # now pick things up
77- root <- file.path( path , name )
77+ # # now pick things up
78+ root <- file.path(path , name )
7879
7980 # Add Rcpp to the DESCRIPTION
80- DESCRIPTION <- file.path( root , " DESCRIPTION" )
81- if ( file.exists( DESCRIPTION ) ){
82- depends <- c(
83- if ( isTRUE(module ) ) " methods" ,
84- sprintf( " Rcpp (>= %s)" , packageDescription(" Rcpp" )[[" Version" ]] )
85- )
86- x <- cbind( read.dcf( DESCRIPTION ),
87- " Depends" = paste( depends , collapse = " , " ) ,
88- " LinkingTo" = " Rcpp"
89- )
90- if ( isTRUE( module ) ){
91- x <- cbind( x , " RcppModules" = " yada, stdVector, NumEx" )
92- message( " >> added RcppModules: yada" )
81+ DESCRIPTION <- file.path(root , " DESCRIPTION" )
82+ if (file.exists(DESCRIPTION )) {
83+ imports <- c(if (isTRUE(module )) " methods" ,
84+ sprintf(" Rcpp (>= %s)" , packageDescription(" Rcpp" )[[" Version" ]]))
85+ x <- cbind(read.dcf(DESCRIPTION ),
86+ " Imports" = paste(imports , collapse = " , " ),
87+ " LinkingTo" = " Rcpp" )
88+ if (isTRUE(module )) {
89+ x <- cbind(x , " RcppModules" = " yada, stdVector, NumEx" )
90+ message(" >> added RcppModules: yada, stdVector, NumEx" )
9391 }
94- x [, " Author" ] <- author
95- x [, " Maintainer" ] <- sprintf( " %s <%s>" , maintainer , email )
92+ x [, " Author" ] <- author
93+ x [, " Maintainer" ] <- sprintf(" %s <%s>" , maintainer , email )
9694 x [, " License" ] <- license
97- message( " >> added Depends : Rcpp" )
95+ message( " >> added Imports : Rcpp" )
9896 message( " >> added LinkingTo: Rcpp" )
99- write.dcf( x , file = DESCRIPTION )
97+ write.dcf(x , file = DESCRIPTION )
10098
10199 }
102100
103- # if there is a NAMESPACE, add a useDynLib
104- NAMESPACE <- file.path( root , " NAMESPACE" )
105- if ( file.exists( NAMESPACE ) ){
106- lines <- readLines( NAMESPACE )
107- ns <- file( NAMESPACE , open = " w" )
108- if ( ! grepl( " useDynLib" , lines ) ){
109- lines <- c( sprintf( " useDynLib(%s)" , name ), lines )
110- writeLines( lines , con = ns )
111- message( " >> added useDynLib directive to NAMESPACE" )
112- }
113-
114- if (isTRUE(module )){
115- writeLines( ' import( Rcpp )' , ns )
116- }
117- close( ns )
118- }
119-
120- # update the package description help page
121- package_help_page <- file.path( root , " man" , sprintf( " %s-package.Rd" , name ) )
122- if ( file.exists(package_help_page ) ){
101+ # # add useDynLib and importFrom to NAMESPACE
102+ NAMESPACE <- file.path(root , " NAMESPACE" )
103+ lines <- readLines(NAMESPACE )
104+ ns <- file(NAMESPACE , open = " w" )
105+ if (! grepl(" useDynLib" , lines )) {
106+ lines <- c(sprintf( " useDynLib(%s)" , name ), lines )
107+ writeLines(lines , con = ns )
108+ message(" >> added useDynLib directive to NAMESPACE" )
109+ }
110+ if (isTRUE(module )) {
111+ writeLines(' import(methods)' , ns )
112+ }
113+ writeLines(' importFrom(Rcpp, evalCpp)' , ns )
114+ message(" >> added importFrom(Rcpp, evalCpp) directive to NAMESPACE" )
115+ close( ns )
116+
117+ # # update the package description help page
118+ package_help_page <- file.path(root , " man" , sprintf( " %s-package.Rd" , name ))
119+ if (file.exists(package_help_page )) {
123120 lines <- readLines(package_help_page )
124- lines <- gsub( " What license is it under?" , license , lines , fixed = TRUE )
125- lines <- gsub( " Who to complain to <yourfault@somewhere.net>" ,
126- sprintf( " %s <%s>" , maintainer , email ),
127- lines ,
128- fixed = TRUE
129- )
130- lines <- gsub( " Who wrote it" , author , lines , fixed = TRUE )
131- writeLines( lines , package_help_page )
121+ lines <- gsub(" What license is it under?" , license , lines , fixed = TRUE )
122+ lines <- gsub(" Who to complain to <yourfault@somewhere.net>" ,
123+ sprintf( " %s <%s>" , maintainer , email ),
124+ lines , fixed = TRUE )
125+ lines <- gsub( " Who wrote it" , author , lines , fixed = TRUE )
126+ writeLines(lines , package_help_page )
132127 }
133128
134- # lay things out in the src directory
135- src <- file.path( root , " src" )
136- if ( ! file.exists( src )) {
137- dir.create( src )
129+ # # lay things out in the src directory
130+ src <- file.path(root , " src" )
131+ if ( ! file.exists(src )) {
132+ dir.create(src )
138133 }
139- skeleton <- system.file( " skeleton" , package = " Rcpp" )
134+ skeleton <- system.file(" skeleton" , package = " Rcpp" )
140135
141- if ( length(cpp_files ) > 0L ) {
136+ if (length(cpp_files ) > 0L ) {
142137 for (file in cpp_files ) {
143138 file.copy(file , src )
144- message( " >> copied " , file , " to src directory" )
139+ message(" >> copied " , file , " to src directory" )
145140 }
146141 compileAttributes(root )
147142 }
148143
149- if ( example_code ) {
150- if ( isTRUE( attributes ) ) {
151- file.copy( file.path( skeleton , " rcpp_hello_world_attributes.cpp" ),
152- file.path( src , " rcpp_hello_world.cpp" ) )
153- message( " >> added example src file using Rcpp attributes" )
144+ if ( example_code ) {
145+ if (isTRUE(attributes ) ) {
146+ file.copy(file.path( skeleton , " rcpp_hello_world_attributes.cpp" ),
147+ file.path( src , " rcpp_hello_world.cpp" ) )
148+ message(" >> added example src file using Rcpp attributes" )
154149 compileAttributes(root )
155- message( " >> compiled Rcpp attributes" )
150+ message(" >> compiled Rcpp attributes" )
156151 } else {
157- header <- readLines( file.path( skeleton , " rcpp_hello_world.h" ) )
158- header <- gsub( " @PKG@" , name , header , fixed = TRUE )
159- writeLines( header , file.path( src , " rcpp_hello_world.h" ) )
160- message( " >> added example header file using Rcpp classes" )
161-
162- file.copy( file.path( skeleton , " rcpp_hello_world.cpp" ), src )
163- message( " >> added example src file using Rcpp classes" )
164-
165- rcode <- readLines( file.path( skeleton , " rcpp_hello_world.R" ) )
166- rcode <- gsub( " @PKG@" , name , rcode , fixed = TRUE )
167- writeLines( rcode , file.path( root , " R" , " rcpp_hello_world.R" ) )
168- message( " >> added example R file calling the C++ example" )
152+ header <- readLines(file.path(skeleton , " rcpp_hello_world.h" ) )
153+ header <- gsub(" @PKG@" , name , header , fixed = TRUE )
154+ writeLines(header , file.path(src , " rcpp_hello_world.h" ) )
155+ message(" >> added example header file using Rcpp classes" )
156+
157+ file.copy(file.path(skeleton , " rcpp_hello_world.cpp" ), src )
158+ message(" >> added example src file using Rcpp classes" )
159+
160+ rcode <- readLines(file.path( skeleton , " rcpp_hello_world.R" ) )
161+ rcode <- gsub(" @PKG@" , name , rcode , fixed = TRUE )
162+ writeLines( rcode , file.path( root , " R" , " rcpp_hello_world.R" ) )
163+ message(" >> added example R file calling the C++ example" )
169164 }
170165
171- hello.Rd <- file.path( root , " man" , " rcpp_hello_world.Rd" )
172- unlink( hello.Rd )
173- file.copy(
174- system.file(" skeleton" , " rcpp_hello_world.Rd" , package = " Rcpp" ),
175- hello.Rd
176- )
166+ hello.Rd <- file.path(root , " man" , " rcpp_hello_world.Rd" )
167+ unlink(hello.Rd )
168+ file.copy(system.file(" skeleton" , " rcpp_hello_world.Rd" , package = " Rcpp" ), hello.Rd )
177169 message( " >> added Rd file for rcpp_hello_world" )
178-
179170 }
180171
181- if ( isTRUE( module ) ){
182- file.copy(system.file( " skeleton" , " rcpp_module.cpp" , package = " Rcpp" ), file.path( root , " src" ))
183- file.copy(system.file( " skeleton" , " Num.cpp" , package = " Rcpp" ), file.path( root , " src" ))
184- file.copy(system.file( " skeleton" , " stdVector.cpp" , package = " Rcpp" ), file.path( root , " src" ))
185- file.copy(system.file( " skeleton" , " zzz.R" , package = " Rcpp" ), file.path( root , " R" ))
186- message( " >> copied the example module file " )
187-
172+ if (isTRUE( module )) {
173+ file.copy(system.file(" skeleton" , " rcpp_module.cpp" , package = " Rcpp" ),
174+ file.path(root , " src" ))
175+ file.copy(system.file(" skeleton" , " Num.cpp" , package = " Rcpp" ),
176+ file.path(root , " src" ))
177+ file.copy(system.file(" skeleton" , " stdVector.cpp" , package = " Rcpp" ),
178+ file.path(root , " src" ))
179+ file.copy(system.file( " skeleton" , " zzz.R" , package = " Rcpp" ),
180+ file.path(root , " R" ))
181+ message(" >> copied the example module file " )
188182 }
189183
190- lines <- readLines( package.doc <- file.path( root , " man" , sprintf( " %s-package.Rd" , name ) ) )
191- lines <- sub( " ~~ simple examples" , " %% ~~ simple examples" , lines )
184+ lines <- readLines(package.doc <- file.path( root , " man" , sprintf(" %s-package.Rd" , name )) )
185+ lines <- sub(" ~~ simple examples" , " %% ~~ simple examples" , lines )
192186
193- lines <- lines [ ! grepl( " ~~ package title" , lines ) ]
194- lines <- lines [ ! grepl( " ~~ The author and" , lines ) ]
195- lines <- sub( " Who wrote it" , author , lines )
196- lines <- sub( " Who to complain to.*" , sprintf( " %s <%s>" , maintainer , email ), lines )
187+ lines <- lines [! grepl(" ~~ package title" , lines )]
188+ lines <- lines [! grepl(" ~~ The author and" , lines )]
189+ lines <- sub(" Who wrote it" , author , lines )
190+ lines <- sub(" Who to complain to.*" , sprintf(" %s <%s>" , maintainer , email ), lines )
197191
198- writeLines( lines , package.doc )
192+ writeLines(lines , package.doc )
199193
200- if ( fake ) {
201- rm( " Rcpp.fake.fun" , envir = env )
202- unlink( file.path( root , " R" , " Rcpp.fake.fun.R" ) )
203- unlink( file.path( root , " man" , " Rcpp.fake.fun.Rd" ) )
194+ if ( fake ) {
195+ rm(" Rcpp.fake.fun" , envir = env )
196+ unlink(file.path(root , " R" , " Rcpp.fake.fun.R" ) )
197+ unlink(file.path(root , " man" , " Rcpp.fake.fun.Rd" ) )
204198 }
205199
206- if ( isTRUE(remove_hello_world ) ) {
207- rm( " rcpp_hello_world" , envir = env )
200+ if ( isTRUE(remove_hello_world )) {
201+ rm(" rcpp_hello_world" , envir = env )
208202 }
209203
210- invisible ( NULL )
204+ invisible (NULL )
211205}
212206
0 commit comments