Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
r2ogs6
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ogs
OpenGeoSys Tools
r2ogs6
Commits
cf49903a
Commit
cf49903a
authored
3 years ago
by
Johannes Boog
Browse files
Options
Downloads
Patches
Plain Diff
"[base] make absolute paths by reference"
parent
5867e5eb
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!41
standardize files references during prj import/export (new MR)
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
R/utils.R
+77
-0
77 additions, 0 deletions
R/utils.R
with
77 additions
and
0 deletions
R/utils.R
+
77
−
0
View file @
cf49903a
...
...
@@ -190,6 +190,19 @@ as_dir_path <- function(path){
return
(
invisible
(
path
))
}
#' as_dir_path2
#' @description Removes leading \code{./} from a given path
#' @param file_path string: A path
#' @noRd
as_dir_path2
<-
function
(
file_path
){
assertthat
::
assert_that
(
assertthat
::
is.string
(
file_path
))
if
(
substr
(
file_path
,
1
,
2
)
%>%
stringr
::
str_detect
(
"./"
)){
file_path
<-
sub
(
"\\./"
,
""
,
file_path
)
}
return
(
invisible
(
file_path
))
}
#' filter_invalid_xml
#' @description Filters invalid XML paths out of a vector
...
...
@@ -403,3 +416,67 @@ is_null_or_has_class <- function(obj, class_name){
return
(
invisible
(
TRUE
))
}
#--- File utility ------------------------------------------------------------
#' make_abs_path
#' @description Creates an absolute file path based on a given file name or path
#' and a reference path. The reference path will be adjusted if the file path
#' is relative (e.g. \code{../foo/bar.baz}). A file with the created absolute
#' path should exist.
#' @param file_path string: Name or path to file.
#' @param ref_path string: Reference path whre file_path will be combined with.
#' Will be made absolute if relative path is given.
#' @noRd
make_abs_path
<-
function
(
file_path
,
ref_path
,
force
=
F
){
assertthat
::
assert_that
(
assertthat
::
is.string
(
file_path
))
assertthat
::
assert_that
(
assertthat
::
is.string
(
ref_path
))
# make shure that ref_path is absolute and ends with "/"
ref_path
<-
normalizePath
(
ref_path
,
mustWork
=
T
)
ref_path
<-
as_dir_path
(
ref_path
)
file_path
<-
as_dir_path2
(
file_path
)
# case1: if file_path is absolute
if
((
substr
(
file_path
,
1
,
1
)
==
"/"
)
|
# abspath on unix
(
substr
(
file_path
,
1
,
3
)
%>%
stringr
::
str_detect
(
"[:alpha:]:\\\\"
))){
# windows?
message
(
paste
(
"file_path"
,
file_path
,
"is already asolute."
))
if
(
isTRUE
(
force
)){
file_path
<-
paste0
(
ref_path
,
basename
(
file_path
))
message
(
paste0
(
"Forced to convert to: "
,
ref_path
,
file_path
))
}
}
# case2: if file_path consist of filename only
else
if
(
basename
(
file_path
)
==
file_path
){
file_path
<-
paste0
(
ref_path
,
file_path
)
}
# case3: if file_path is in parent dir(s) (contains "../")
else
if
(
stringr
::
str_detect
(
file_path
,
"\\.\\./"
)){
cds
<-
stringr
::
str_count
(
file_path
,
"\\.\\./"
)
file_path
<-
gsub
(
"\\.\\./"
,
""
,
file_path
)
# cd.. through ref_path
for
(
i
in
seq
(
cds
)){
ref_path
<-
sub
(
"\\w+/$"
,
""
,
ref_path
)
}
file_path
<-
paste0
(
ref_path
,
file_path
)
}
# case4: file is in child dir
else
{
file_path_norm
<-
normalizePath
(
dirname
(
file_path
),
mustWork
=
T
)
assertthat
::
assert_that
(
stringr
::
str_detect
(
file_path_norm
,
ref_path
),
msg
=
paste
(
file_path
,
"is neither an absolute path nor a reference to"
,
"a file in"
,
ref_path
,
"or in a child or parent directory."
))
file_path
<-
paste0
(
ref_path
,
file_path
)
}
assertthat
::
assert_that
(
file.exists
(
file_path
))
return
(
file_path
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment