1
1
# -*- tab-width: 4; -*-
2
2
3
- # Copyright (C) 2009 - 2013 Dirk Eddelbuettel and Romain Francois
3
+ # Copyright (C) 2009 - 2014 Dirk Eddelbuettel and Romain Francois
4
4
#
5
5
# This file is part of Rcpp.
6
6
#
@@ -23,190 +23,184 @@ Rcpp.package.skeleton <- function(name = "anRpackage", list = character(),
23
23
code_files = character (), cpp_files = character (),
24
24
example_code = TRUE , attributes = TRUE , module = FALSE ,
25
25
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 ,
27
28
28
29
license = " What Licence is it under ?" ) {
29
30
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 )
33
34
34
35
if (! is.character(cpp_files ))
35
36
stop(" 'cpp_files' must be a character vector" )
36
37
37
- if ( ! length(list ) ) {
38
+ if ( ! length(list )) {
38
39
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 )
42
43
remove_hello_world <- TRUE
43
44
} else {
44
45
remove_hello_world <- FALSE
45
46
}
46
47
} 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
+ }
54
54
remove_hello_world <- TRUE
55
55
} else {
56
56
remove_hello_world <- FALSE
57
57
}
58
58
fake <- FALSE
59
59
}
60
60
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
63
63
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 )))) ]
65
65
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" )
68
69
}
69
70
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
+ })
73
74
74
- message( " \n Adding Rcpp settings" )
75
+ message(" \n Adding Rcpp settings" )
75
76
76
- # now pick things up
77
- root <- file.path( path , name )
77
+ # # now pick things up
78
+ root <- file.path(path , name )
78
79
79
80
# 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" )
93
91
}
94
- x [, " Author" ] <- author
95
- x [, " Maintainer" ] <- sprintf( " %s <%s>" , maintainer , email )
92
+ x [, " Author" ] <- author
93
+ x [, " Maintainer" ] <- sprintf(" %s <%s>" , maintainer , email )
96
94
x [, " License" ] <- license
97
- message( " >> added Depends : Rcpp" )
95
+ message( " >> added Imports : Rcpp" )
98
96
message( " >> added LinkingTo: Rcpp" )
99
- write.dcf( x , file = DESCRIPTION )
97
+ write.dcf(x , file = DESCRIPTION )
100
98
101
99
}
102
100
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 )) {
123
120
lines <- readLines(package_help_page )
124
- lines <- gsub( " What license is it under?" , license , lines , fixed = TRUE )
125
- lines <- gsub(
" Who to complain to <[email protected] >" ,
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 <[email protected] >" ,
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 )
132
127
}
133
128
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 )
138
133
}
139
- skeleton <- system.file( " skeleton" , package = " Rcpp" )
134
+ skeleton <- system.file(" skeleton" , package = " Rcpp" )
140
135
141
- if ( length(cpp_files ) > 0L ) {
136
+ if (length(cpp_files ) > 0L ) {
142
137
for (file in cpp_files ) {
143
138
file.copy(file , src )
144
- message( " >> copied " , file , " to src directory" )
139
+ message(" >> copied " , file , " to src directory" )
145
140
}
146
141
compileAttributes(root )
147
142
}
148
143
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" )
154
149
compileAttributes(root )
155
- message( " >> compiled Rcpp attributes" )
150
+ message(" >> compiled Rcpp attributes" )
156
151
} 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" )
169
164
}
170
165
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 )
177
169
message( " >> added Rd file for rcpp_hello_world" )
178
-
179
170
}
180
171
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 " )
188
182
}
189
183
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 )
192
186
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 )
197
191
198
- writeLines( lines , package.doc )
192
+ writeLines(lines , package.doc )
199
193
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" ) )
204
198
}
205
199
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 )
208
202
}
209
203
210
- invisible ( NULL )
204
+ invisible (NULL )
211
205
}
212
206
0 commit comments