Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated dependency tree update #843

Merged
merged 8 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/fpm.f90
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ subroutine build_model(model, settings, package, error)
call model%deps%add(package, error)
if (allocated(error)) return

! Update dependencies where needed
call model%deps%update(error)
if (allocated(error)) return

! build/ directory should now exist
if (.not.exists("build/.gitignore")) then
call filewrite(join_path("build", ".gitignore"),["*"])
Expand Down
6 changes: 2 additions & 4 deletions src/fpm/cmd/update.f90
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ subroutine cmd_update(settings)
if (settings%fetch_only) return

if (size(settings%name) == 0) then
do ii = 1, deps%ndep
call deps%update(deps%dep(ii)%name, error)
call handle_error(error)
end do
call deps%update(error)
call handle_error(error)
else
do ii = 1, size(settings%name)
call deps%update(trim(settings%name(ii)), error)
Expand Down
152 changes: 142 additions & 10 deletions src/fpm/dependency.f90
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ module fpm_dependency
use fpm_environment, only : get_os_type, OS_WINDOWS
use fpm_error, only : error_t, fatal_error
use fpm_filesystem, only : exists, join_path, mkdir, canon_path, windows_path
use fpm_git, only : git_target_revision, git_target_default, git_revision
use fpm_git, only : git_target_revision, git_target_default, git_revision, operator(==)
use fpm_manifest, only : package_config_t, dependency_config_t, &
get_package_data
use fpm_manifest_dependency, only: manifest_has_changed
use fpm_strings, only : string_t, operator(.in.)
use fpm_toml, only : toml_table, toml_key, toml_error, toml_serializer, &
toml_parse, get_value, set_value, add_table
Expand Down Expand Up @@ -95,6 +96,8 @@ module fpm_dependency
contains
!> Update dependency from project manifest
procedure :: register
!> Print information on this instance
procedure :: info
end type dependency_node_t


Expand All @@ -118,7 +121,7 @@ module fpm_dependency
contains
!> Overload procedure to add new dependencies to the tree
generic :: add => add_project, add_project_dependencies, add_dependencies, &
add_dependency
add_dependency, add_dependency_node
!> Main entry point to add a project
procedure, private :: add_project
!> Add a project and its dependencies to the dependency tree
Expand All @@ -127,6 +130,8 @@ module fpm_dependency
procedure, private :: add_dependencies
!> Add a single dependency to the dependency tree
procedure, private :: add_dependency
!> Add a single dependency node to the dependency tree
procedure, private :: add_dependency_node
!> Resolve dependencies
generic :: resolve => resolve_dependencies, resolve_dependency
!> Resolve dependencies
Expand Down Expand Up @@ -158,9 +163,11 @@ module fpm_dependency
!> Write dependency tree to TOML data structure
procedure, private :: dump_to_toml
!> Update dependency tree
generic :: update => update_dependency
generic :: update => update_dependency,update_tree
!> Update a list of dependencies
procedure, private :: update_dependency
!> Update all dependencies in the tree
procedure, private :: update_tree
end type dependency_tree_t

!> Common output format for writing to the command line
Expand Down Expand Up @@ -191,7 +198,7 @@ subroutine new_dependency_tree(self, verbosity, cache)
end subroutine new_dependency_tree

!> Create a new dependency node from a configuration
pure subroutine new_dependency_node(self, dependency, version, proj_dir, update)
subroutine new_dependency_node(self, dependency, version, proj_dir, update)
!> Instance of the dependency node
type(dependency_node_t), intent(out) :: self
!> Dependency configuration data
Expand Down Expand Up @@ -219,6 +226,49 @@ pure subroutine new_dependency_node(self, dependency, version, proj_dir, update)

end subroutine new_dependency_node

!> Write information on instance
subroutine info(self, unit, verbosity)

!> Instance of the dependency configuration
class(dependency_node_t), intent(in) :: self

!> Unit for IO
integer, intent(in) :: unit

!> Verbosity of the printout
integer, intent(in), optional :: verbosity

integer :: pr
character(:), allocatable :: ver
character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)'

if (present(verbosity)) then
pr = verbosity
else
pr = 1
end if

!> Call base object info
call self%dependency_config_t%info(unit,pr)

if (allocated(self%version)) then
call self%version%to_string(ver)
write(unit, fmt) "- version", ver
end if

if (allocated(self%proj_dir)) then
write(unit, fmt) "- dir", self%proj_dir
end if

if (allocated(self%revision)) then
write(unit, fmt) "- revision", self%revision
end if

write(unit, fmt) "- done", merge('YES','NO ',self%done)
write(unit, fmt) "- update", merge('YES','NO ',self%update)

end subroutine info

!> Add project dependencies, each depth level after each other.
!>
!> We implement this algorithm in an interative rather than a recursive fashion
Expand Down Expand Up @@ -356,22 +406,58 @@ subroutine add_dependencies(self, dependency, error)

end subroutine add_dependencies

!> Add a single dependency to the dependency tree
pure subroutine add_dependency(self, dependency, error)
!> Add a single dependency node to the dependency tree
!> Dependency nodes contain additional information (version, git, revision)
subroutine add_dependency_node(self, dependency, error)
!> Instance of the dependency tree
class(dependency_tree_t), intent(inout) :: self
!> Dependency configuration to add
type(dependency_config_t), intent(in) :: dependency
type(dependency_node_t), intent(in) :: dependency
!> Error handling
type(error_t), allocatable, intent(out) :: error

integer :: id
logical :: needs_update

id = self%find(dependency)
if (id == 0) then

exists: if (id > 0) then

!> A dependency with this same name is already in the dependency tree.

!> check if it needs to be updated
needs_update = dependency_has_changed(self%dep(id), dependency)

!> Ensure an update is requested whenever the dependency has changed
if (needs_update) then
write(self%unit, out_fmt) "Dependency change detected:", dependency%name
self%dep(id) = dependency
self%dep(id)%update = .true.
endif

else exists

!> New dependency: add from scratch
self%ndep = self%ndep + 1
call new_dependency_node(self%dep(self%ndep), dependency)
end if
self%dep(self%ndep) = dependency

end if exists

end subroutine add_dependency_node

!> Add a single dependency to the dependency tree
subroutine add_dependency(self, dependency, error)
!> Instance of the dependency tree
class(dependency_tree_t), intent(inout) :: self
!> Dependency configuration to add
type(dependency_config_t), intent(in) :: dependency
!> Error handling
type(error_t), allocatable, intent(out) :: error

type(dependency_node_t) :: node

call new_dependency_node(node, dependency)
call add_dependency_node(self, node, error)

end subroutine add_dependency

Expand Down Expand Up @@ -400,6 +486,7 @@ subroutine update_dependency(self, name, error)
if (self%verbosity > 1) then
write(self%unit, out_fmt) "Update:", dep%name
end if
write(self%unit, out_fmt) "Update:", dep%name
proj_dir = join_path(self%dep_dir, dep%name)
call dep%git%checkout(proj_dir, error)
if (allocated(error)) return
Expand All @@ -419,6 +506,23 @@ subroutine update_dependency(self, name, error)

end subroutine update_dependency

!> Update whole dependency tree
subroutine update_tree(self, error)
!> Instance of the dependency tree
class(dependency_tree_t), intent(inout) :: self
!> Error handling
type(error_t), allocatable, intent(out) :: error

integer :: i

! Update dependencies where needed
do i = 1, self%ndep
call self%update(self%dep(i)%name,error)
if (allocated(error)) return
end do

end subroutine update_tree

!> Resolve all dependencies in the tree
subroutine resolve_dependencies(self, root, error)
!> Instance of the dependency tree
Expand Down Expand Up @@ -811,4 +915,32 @@ pure subroutine resize_dependency_node(var, n)

end subroutine resize_dependency_node

!> Check if a dependency node has changed
logical function dependency_has_changed(this,that) result(has_changed)
!> Two instances of the same dependency to be compared
type(dependency_node_t), intent(in) :: this,that

has_changed = .true.

!> All the following entities must be equal for the dependency to not have changed
if (manifest_has_changed(this, that)) return

!> For now, only perform the following checks if both are available. A dependency in cache.toml
!> will always have this metadata; a dependency from fpm.toml which has not been fetched yet
!> may not have it
if (allocated(this%version) .and. allocated(that%version)) then
if (this%version/=that%version) return
endif
if (allocated(this%revision) .and. allocated(that%revision)) then
if (this%revision/=that%revision) return
endif
if (allocated(this%proj_dir) .and. allocated(that%proj_dir)) then
if (this%proj_dir/=that%proj_dir) return
endif

!> All checks passed: the two dependencies have no differences
has_changed = .false.

end function dependency_has_changed

end module fpm_dependency
17 changes: 17 additions & 0 deletions src/fpm/git.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module fpm_git
public :: git_target_default, git_target_branch, git_target_tag, &
& git_target_revision
public :: git_revision
public :: operator(==)


!> Possible git target
Expand Down Expand Up @@ -54,6 +55,10 @@ module fpm_git
end type git_target_t


interface operator(==)
module procedure git_target_eq
end interface

contains


Expand Down Expand Up @@ -128,6 +133,18 @@ function git_target_tag(url, tag) result(self)

end function git_target_tag

!> Check that two git targets are equal
logical function git_target_eq(this,that) result(is_equal)

!> Two input git targets
type(git_target_t), intent(in) :: this,that

is_equal = this%descriptor == that%descriptor .and. &
this%url == that%url .and. &
this%object == that%object

end function git_target_eq


subroutine checkout(self, local_path, error)

Expand Down
27 changes: 24 additions & 3 deletions src/fpm/manifest/dependency.f90
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
module fpm_manifest_dependency
use fpm_error, only : error_t, syntax_error
use fpm_git, only : git_target_t, git_target_tag, git_target_branch, &
& git_target_revision, git_target_default
& git_target_revision, git_target_default, operator(==)
use fpm_toml, only : toml_table, toml_key, toml_stat, get_value
use fpm_filesystem, only: windows_path
use fpm_environment, only: get_os_type, OS_WINDOWS
implicit none
private

public :: dependency_config_t, new_dependency, new_dependencies
public :: dependency_config_t, new_dependency, new_dependencies, manifest_has_changed


!> Configuration meta data for a dependency
Expand Down Expand Up @@ -159,7 +159,7 @@ subroutine check(table, error)
exit
end if
url_present = .true.

case("path")
if (url_present) then
call syntax_error(error, "Dependency "//name//" cannot have both git and path entries")
Expand Down Expand Up @@ -266,5 +266,26 @@ subroutine info(self, unit, verbosity)

end subroutine info

!> Check if two dependency configurations are different
logical function manifest_has_changed(this, that) result(has_changed)

!> Two instances of the dependency configuration
class(dependency_config_t), intent(in) :: this, that

has_changed = .true.

!> Perform all checks
if (this%name/=that%name) return
if (this%path/=that%path) return
if (allocated(this%git).neqv.allocated(that%git)) return
if (allocated(this%git)) then
if (.not.(this%git==that%git)) return
end if

!> All checks passed! The two instances are equal
has_changed = .false.

end function manifest_has_changed


end module fpm_manifest_dependency
Loading