-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
395 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
with Ada.Calendar; | ||
|
||
with Alire.Directories; | ||
with Alire.Paths; | ||
with Alire.Platforms.Folders; | ||
with Alire.Settings.Builtins; | ||
with Alire.Settings.Edit; | ||
|
||
with Ncdu; | ||
|
||
package body Alire.Cache is | ||
|
||
use Alire.Directories.Operators; | ||
|
||
package Adirs renames Ada.Directories; | ||
package Du is new Ncdu; | ||
|
||
---------- | ||
-- Path -- | ||
---------- | ||
|
||
function Path return Absolute_Path | ||
is (if Settings.Builtins.Cache_Dir.Get /= "" then | ||
Settings.Builtins.Cache_Dir.Get | ||
elsif not Settings.Edit.Is_At_Default_Dir then | ||
Settings.Edit.Path / Paths.Cache_Folder_Inside_Working_Folder | ||
else | ||
Platforms.Folders.Cache); | ||
|
||
----------- | ||
-- Usage -- | ||
----------- | ||
|
||
function Usage return Usages is | ||
|
||
Busy_Top : Simple_Logging.Ongoing := | ||
Simple_Logging.Activity ("Listing"); | ||
|
||
Busy : Simple_Logging.Ongoing := Simple_Logging.Activity (""); | ||
|
||
Last_Check : Ada.Calendar.Time := Ada.Calendar.Clock; | ||
|
||
-------------- | ||
-- Progress -- | ||
-------------- | ||
|
||
procedure Progress (Path : String) is | ||
use Ada.Calendar; | ||
begin | ||
if Clock - Last_Check >= 0.1 | ||
and then Directories.Is_File (Path / Alire.Paths.Crate_File_Name) | ||
then | ||
Busy_Top.Step; | ||
Busy.Step (Adirs.Simple_Name (Path)); | ||
Last_Check := Clock; | ||
end if; | ||
end Progress; | ||
|
||
Tree : constant Du.Tree := Du.List (Path, | ||
Progress => Progress'Access); | ||
|
||
---------------- | ||
-- Usage_Wrap -- | ||
---------------- | ||
|
||
procedure Usage_Wrap (Parent : in out Usages; | ||
Children : Du.Tree; | ||
Depth : Depths; | ||
Branch : String := "" | ||
-- Says if toolchains, releases, or builds | ||
) | ||
is | ||
begin | ||
for Child of Children loop | ||
declare | ||
Branch : constant String | ||
:= (if Usage_Wrap.Branch /= "" | ||
then Usage_Wrap.Branch | ||
else Adirs.Simple_Name (Child.Element.Path)); | ||
Wrapped_Children : Usages; | ||
begin | ||
|
||
-- Wrap the children if we still have room to go down | ||
|
||
if Depth < Release or else | ||
(Depth < Build | ||
and then Branch = Paths.Build_Folder_Inside_Working_Folder) | ||
then | ||
Usage_Wrap (Wrapped_Children, | ||
Child.Element.Children, | ||
Depth => Depths'Succ (Depth), | ||
Branch => Branch); | ||
end if; | ||
|
||
-- Create the wrapped node at the current depth | ||
|
||
Parent.Insert | ||
(Item' | ||
(Depth => Depth, | ||
Name => +Adirs.Simple_Name (Child.Element.Path), | ||
Path => +Child.Element.Path, | ||
Size => Child.Tree_Size, | ||
Children => Wrapped_Children)); | ||
end; | ||
end loop; | ||
end Usage_Wrap; | ||
|
||
begin | ||
-- The root node should be the cache dir itself, unless there is still | ||
-- no cache at all. | ||
if Tree.Is_Empty then | ||
return Item_Sets.Empty_Set; | ||
elsif Tree.Length not in 1 then | ||
raise Program_Error | ||
with "Cache tree root length /= 1:" & Tree.Length'Image; | ||
end if; | ||
|
||
-- Iterate the obtained tree wrapping contents as our usage type | ||
return Result : Usages do | ||
Usage_Wrap (Result, | ||
Tree.First_Element.Element.Children, | ||
Depths'First); | ||
end return; | ||
end Usage; | ||
|
||
end Alire.Cache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
with Ada.Containers.Indefinite_Ordered_Multisets; | ||
with Ada.Directories; | ||
|
||
package Alire.Cache is | ||
|
||
-- Cache inspection and management. The cache is where we store all data | ||
-- that, if not found, is re-downloaded or regenerated. This currently | ||
-- comprises toolchains, pristine releases (the vault), builds, and the | ||
-- user index fork clone when publishing. | ||
|
||
function Path return Absolute_Path; | ||
-- The location for data that will be recreated if missing; its value in | ||
-- precedence order is: | ||
-- 1) Setting builtin 'cache.dir' | ||
-- 2) if Alire.Settings.Path is overridden, Settings.Path/cache | ||
-- 3) Platforms.Folders.Cache | ||
|
||
subtype Sizes is Ada.Directories.File_Size; | ||
-- A size, in bytes | ||
|
||
-- The following builds a tree of items in the cache, that can be queried | ||
-- to present information up to a level of detail. | ||
|
||
type Depths is (Location, Release, Build); | ||
-- Locations are the top-level folders: toolchains, releases, builds. | ||
-- Releases are a unique release milestone plus short commit. | ||
-- Builds are synced copies for a release, named as the release + build id. | ||
|
||
type Base_Item is abstract tagged null record; | ||
|
||
function "<" (L, R : Base_Item'Class) return Boolean; | ||
|
||
function Depth (This : Base_Item'Class) return Depths; | ||
|
||
function Name (This : Base_Item'Class) return String; | ||
|
||
function Path (This : Base_Item'Class) return Absolute_Path; | ||
|
||
function Size (This : Base_Item'Class) return Sizes; | ||
|
||
package Item_Sets is | ||
new Ada.Containers.Indefinite_Ordered_Multisets (Base_Item'Class); | ||
|
||
subtype Usages is Item_Sets.Set; | ||
|
||
function Children (This : Base_Item'Class) return Usages; | ||
|
||
function Usage return Usages; | ||
-- Compute cache usage. First level is locations, second level is releases, | ||
-- third level is builds. Within level, childen are sorted by size. | ||
|
||
type Item is new Base_Item with record | ||
Depth : Depths; | ||
Name : UString; | ||
Path : Unbounded_Absolute_Path; | ||
Size : Sizes; -- Accumulated size below this item | ||
Children : Usages; | ||
end record; | ||
|
||
function Element (This : Base_Item'Class) return Item is (Item (This)) | ||
with Inline; | ||
|
||
private | ||
|
||
use type Sizes; | ||
|
||
-------------- | ||
-- Children -- | ||
-------------- | ||
|
||
function Children (This : Base_Item'Class) return Usages | ||
is (This.Element.Children); | ||
|
||
----------- | ||
-- Depth -- | ||
----------- | ||
|
||
function Depth (This : Base_Item'Class) return Depths | ||
is (This.Element.Depth); | ||
|
||
---------- | ||
-- Name -- | ||
---------- | ||
|
||
function Name (This : Base_Item'Class) return String | ||
is (UStrings.To_String (This.Element.Name)); | ||
|
||
---------- | ||
-- Path -- | ||
---------- | ||
|
||
function Path (This : Base_Item'Class) return Absolute_Path | ||
is (Absolute_Path (UStrings.To_String (This.Element.Path))); | ||
|
||
---------- | ||
-- Size -- | ||
---------- | ||
|
||
function Size (This : Base_Item'Class) return Sizes is (This.Element.Size); | ||
|
||
--------- | ||
-- "<" -- | ||
--------- | ||
|
||
function "<" (L, R : Base_Item'Class) return Boolean | ||
is (L.Size > R.Size | ||
or else | ||
(L.Size = R.Size | ||
and then L.Name < R.Name)); | ||
|
||
end Alire.Cache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.