|
| 1 | +!> Implementation of the meta data for dependencies. |
| 2 | +! |
| 3 | +! A dependency table can currently have the following fields |
| 4 | +! |
| 5 | +! ```toml |
| 6 | +! [dependencies] |
| 7 | +! "dep1" = { git = "url" } |
| 8 | +! "dep2" = { git = "url", branch = "name" } |
| 9 | +! "dep3" = { git = "url", tag = "name" } |
| 10 | +! "dep4" = { git = "url", rev = "sha1" } |
| 11 | +! "dep0" = { path = "path" } |
| 12 | +! ``` |
| 13 | +module fpm_config_dependency |
| 14 | + use fpm_error, only : error_t, syntax_error |
| 15 | + use fpm_git, only : git_target_t, git_target_tag, git_target_branch, & |
| 16 | + & git_target_revision, git_target_default |
| 17 | + use fpm_toml, only : toml_table, toml_key, toml_stat, get_value |
| 18 | + implicit none |
| 19 | + private |
| 20 | + |
| 21 | + public :: dependency_t, new_dependency, new_dependencies |
| 22 | + |
| 23 | + |
| 24 | + !> Configuration meta data for a dependency |
| 25 | + type :: dependency_t |
| 26 | + |
| 27 | + !> Name of the dependency |
| 28 | + character(len=:), allocatable :: name |
| 29 | + |
| 30 | + !> Local target |
| 31 | + character(len=:), allocatable :: path |
| 32 | + |
| 33 | + !> Git descriptor |
| 34 | + type(git_target_t), allocatable :: git |
| 35 | + |
| 36 | + contains |
| 37 | + |
| 38 | + !> Print information on this instance |
| 39 | + procedure :: info |
| 40 | + |
| 41 | + end type dependency_t |
| 42 | + |
| 43 | + |
| 44 | +contains |
| 45 | + |
| 46 | + |
| 47 | +!> Construct a new dependency configuration from a TOML data structure |
| 48 | +subroutine new_dependency(self, table, error) |
| 49 | + |
| 50 | + !> Instance of the dependency configuration |
| 51 | + type(dependency_t), intent(out) :: self |
| 52 | + |
| 53 | + !> Instance of the TOML data structure |
| 54 | + type(toml_table), intent(inout) :: table |
| 55 | + |
| 56 | + !> Error handling |
| 57 | + type(error_t), allocatable, intent(out) :: error |
| 58 | + |
| 59 | + character(len=:), allocatable :: url, obj |
| 60 | + integer :: stat |
| 61 | + |
| 62 | + call check(table, error) |
| 63 | + if (allocated(error)) return |
| 64 | + |
| 65 | + call table%get_key(self%name) |
| 66 | + |
| 67 | + call get_value(table, "path", url) |
| 68 | + if (allocated(url)) then |
| 69 | + call move_alloc(url, self%path) |
| 70 | + else |
| 71 | + call get_value(table, "git", url) |
| 72 | + |
| 73 | + call get_value(table, "tag", obj) |
| 74 | + if (allocated(obj)) then |
| 75 | + self%git = git_target_tag(url, obj) |
| 76 | + end if |
| 77 | + |
| 78 | + if (.not.allocated(self%git)) then |
| 79 | + call get_value(table, "branch", obj) |
| 80 | + if (allocated(obj)) then |
| 81 | + self%git = git_target_branch(url, obj) |
| 82 | + end if |
| 83 | + end if |
| 84 | + |
| 85 | + if (.not.allocated(self%git)) then |
| 86 | + call get_value(table, "revision", obj) |
| 87 | + if (allocated(obj)) then |
| 88 | + self%git = git_target_revision(url, obj) |
| 89 | + end if |
| 90 | + end if |
| 91 | + |
| 92 | + if (.not.allocated(self%git)) then |
| 93 | + self%git = git_target_default(url) |
| 94 | + end if |
| 95 | + |
| 96 | + end if |
| 97 | + |
| 98 | +end subroutine new_dependency |
| 99 | + |
| 100 | + |
| 101 | +!> Check local schema for allowed entries |
| 102 | +subroutine check(table, error) |
| 103 | + |
| 104 | + !> Instance of the TOML data structure |
| 105 | + type(toml_table), intent(inout) :: table |
| 106 | + |
| 107 | + !> Error handling |
| 108 | + type(error_t), allocatable, intent(out) :: error |
| 109 | + |
| 110 | + character(len=:), allocatable :: name |
| 111 | + type(toml_key), allocatable :: list(:) |
| 112 | + logical :: url_present, git_target_present |
| 113 | + integer :: ikey |
| 114 | + |
| 115 | + url_present = .false. |
| 116 | + git_target_present = .false. |
| 117 | + |
| 118 | + call table%get_key(name) |
| 119 | + call table%get_keys(list) |
| 120 | + |
| 121 | + if (.not.allocated(list)) then |
| 122 | + call syntax_error(error, "Dependency "//name//" does not provide sufficient entries") |
| 123 | + return |
| 124 | + end if |
| 125 | + |
| 126 | + do ikey = 1, size(list) |
| 127 | + select case(list(ikey)%key) |
| 128 | + case default |
| 129 | + call syntax_error(error, "Key "//list(ikey)%key//" is not allowed in dependency "//name) |
| 130 | + exit |
| 131 | + |
| 132 | + case("git", "path") |
| 133 | + if (url_present) then |
| 134 | + call syntax_error(error, "Dependency "//name//" cannot have both git and path entries") |
| 135 | + exit |
| 136 | + end if |
| 137 | + url_present = .true. |
| 138 | + |
| 139 | + case("branch", "rev", "tag") |
| 140 | + if (git_target_present) then |
| 141 | + call syntax_error(error, "Dependency "//name//" can only have one of branch, rev or tag present") |
| 142 | + exit |
| 143 | + end if |
| 144 | + git_target_present = .true. |
| 145 | + |
| 146 | + end select |
| 147 | + end do |
| 148 | + if (allocated(error)) return |
| 149 | + |
| 150 | + if (.not.url_present .and. git_target_present) then |
| 151 | + call syntax_error(error, "Dependency "//name//" uses a local path, therefore no git identifiers are allowed") |
| 152 | + end if |
| 153 | + |
| 154 | +end subroutine check |
| 155 | + |
| 156 | + |
| 157 | +!> Construct new dependency array from a TOML data structure |
| 158 | +subroutine new_dependencies(deps, table, error) |
| 159 | + |
| 160 | + !> Instance of the dependency configuration |
| 161 | + type(dependency_t), allocatable, intent(out) :: deps(:) |
| 162 | + |
| 163 | + !> Instance of the TOML data structure |
| 164 | + type(toml_table), intent(inout) :: table |
| 165 | + |
| 166 | + !> Error handling |
| 167 | + type(error_t), allocatable, intent(out) :: error |
| 168 | + |
| 169 | + class(toml_table), pointer :: node |
| 170 | + type(toml_key), allocatable :: list(:) |
| 171 | + integer :: idep, stat |
| 172 | + |
| 173 | + call table%get_keys(list) |
| 174 | + ! An empty table is okay |
| 175 | + if (.not.allocated(list)) return |
| 176 | + |
| 177 | + allocate(deps(size(list))) |
| 178 | + do idep = 1, size(list) |
| 179 | + call get_value(table, list(idep)%key, node, stat=stat) |
| 180 | + if (stat /= toml_stat%success) then |
| 181 | + call syntax_error(error, "Dependency "//list(idep)%key//" must be a table entry") |
| 182 | + exit |
| 183 | + end if |
| 184 | + call new_dependency(deps(idep), node, error) |
| 185 | + if (allocated(error)) exit |
| 186 | + end do |
| 187 | + |
| 188 | +end subroutine new_dependencies |
| 189 | + |
| 190 | + |
| 191 | +!> Write information on instance |
| 192 | +subroutine info(self, unit, verbosity) |
| 193 | + |
| 194 | + !> Instance of the dependency configuration |
| 195 | + class(dependency_t), intent(in) :: self |
| 196 | + |
| 197 | + !> Unit for IO |
| 198 | + integer, intent(in) :: unit |
| 199 | + |
| 200 | + !> Verbosity of the printout |
| 201 | + integer, intent(in), optional :: verbosity |
| 202 | + |
| 203 | + integer :: pr |
| 204 | + character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)' |
| 205 | + |
| 206 | + if (present(verbosity)) then |
| 207 | + pr = verbosity |
| 208 | + else |
| 209 | + pr = 1 |
| 210 | + end if |
| 211 | + |
| 212 | + write(unit, fmt) "Dependency" |
| 213 | + if (allocated(self%name)) then |
| 214 | + write(unit, fmt) "- name", self%name |
| 215 | + end if |
| 216 | + |
| 217 | + if (allocated(self%git)) then |
| 218 | + write(unit, fmt) "- kind", "git" |
| 219 | + call self%git%info(unit, pr - 1) |
| 220 | + end if |
| 221 | + |
| 222 | + if (allocated(self%path)) then |
| 223 | + write(unit, fmt) "- kind", "local" |
| 224 | + write(unit, fmt) "- path", self%path |
| 225 | + end if |
| 226 | + |
| 227 | +end subroutine info |
| 228 | + |
| 229 | + |
| 230 | +end module fpm_config_dependency |
0 commit comments