Skip to content

Commit

Permalink
Instead of which, I implemented my own PATH checker.
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ-Ianozi committed Apr 15, 2024
1 parent e57d228 commit 00515d9
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 80 deletions.
25 changes: 22 additions & 3 deletions src/commands.adb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.

with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Strings.Fixed;
with GNAT.Expect; use GNAT.Expect;
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.OS_Lib;
with Platform; use Platform;
package body Commands is
Expand All @@ -35,6 +35,25 @@ package body Commands is
return Successfully_Executed;
end Test_Command;

function Is_Executable (Path : String) return Boolean is
Test_Arg : constant GNAT.OS_Lib.Argument_List (1 .. 1) :=
(1 => new String'(Path));
begin
declare
Status : aliased Integer := 0;
Response : constant String :=
Get_Command_Output
(Command => "file",
Arguments => Test_Arg,
Input => "",
Status => Status'Access);
begin
return Ada.Strings.Fixed.Index (Response, "executable") > 0;
exception
when others => return False;
end;
end Is_Executable;

function Test_Commands return Command_Supported is
Test_Args : constant GNAT.OS_Lib.Argument_List (1 .. 1) :=
(1 => new String'("--version"));
Expand Down Expand Up @@ -120,7 +139,7 @@ package body Commands is
end;
Tested (Macos_xattr) := True;
else -- not chmod_tried
return false;
return False;
end if;
else
if not Ignore_IO then
Expand All @@ -129,7 +148,7 @@ package body Commands is
exit Test_Alire;
end if;
end loop Test_Alire;
return true;
return True;
end Test_Binary;

end Commands;
2 changes: 2 additions & 0 deletions src/commands.ads
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ package Commands is
function Test_Binary
(Binary_To_Test : String; IO : C_IO; Ignore_IO : Boolean := False)
return Boolean;
-- Checks if a file based on the full path is executable or not.
function Is_Executable (Path : String) return Boolean;
end Commands;
51 changes: 51 additions & 0 deletions src/common.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- Copyright (C) 2024 A.J. Ianozi <aj@ianozi.com>
--
-- This file is part of GetAda: the Unofficial Alire Installer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
with Ada.Strings.Fixed;

package body Common is
use Ada.Strings.Fixed;
function Split
(Item : String; Delimiter : String)
return String_Vectors.Vector is
Result : String_Vectors.Vector;
begin
if Delimiter'Length > 0 and then Item'Length > 0 then
declare
Next_Begin : Natural := Item'First;
Next_Delimiter : Natural := Index (Item, Delimiter);
begin
loop
if Next_Delimiter > 0 then
if Next_Delimiter = 1 then
Result.Append ("");
else
Result.Append (Item (Next_Begin .. Next_Delimiter - 1));
end if;
Next_Begin := Next_Delimiter + Delimiter'Length;
Next_Delimiter := Index (Item (Next_Begin .. Item'Last),
Delimiter);
else
Result.Append (Item (Next_Begin .. Item'Last));
exit;
end if;
exit when Next_Delimiter > Item'Last;
end loop;
end;
end if;
return Result;
end Split;
end Common;
24 changes: 24 additions & 0 deletions src/common.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Copyright (C) 2024 A.J. Ianozi <aj@ianozi.com>
--
-- This file is part of GetAda: the Unofficial Alire Installer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
with Ada.Containers.Indefinite_Vectors;

package Common is
package String_Vectors is new
Ada.Containers.Indefinite_Vectors (Positive, String);
function Split (Item : String; Delimiter : String)
return String_Vectors.Vector;
end Common;
2 changes: 1 addition & 1 deletion src/defaults.ads
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ package Defaults is

-- Alire binary file
Alire_Command : constant String := "alr";
-- Getada binary file
-- Getada binary file
Getada_Command : constant String := "getada";

-- Messages
Expand Down
22 changes: 22 additions & 0 deletions src/files.adb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ with Prompts; use Prompts;
with Defaults;
package body Files is

-- Returns true if "Dir_Path" contains "File"
function Directory_Contains
(Dir_Path : String;
File : String;
Which_Kind : Ada.Directories.File_Kind)
return Boolean
is
Next_Item : Directory_Entry_Type;
Directory_Search : Search_Type;
begin
Start_Search (Directory_Search, Dir_Path, "");
while More_Entries (Directory_Search) loop
Get_Next_Entry (Directory_Search, Next_Item);
if Kind (Next_Item) = Which_Kind and then
Simple_Name (Next_Item) = File
then
return True;
end if;
end loop;
return False;
end Directory_Contains;

-- Creates a random string.
function Random_String (Str_Len : Natural) return String is
Alpha_Num : constant array (1 .. 62) of Character :=
Expand Down
7 changes: 6 additions & 1 deletion src/files.ads
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.

with Ada.Directories;
package Files is
-- Returns if a line exists in a given file. If the file doesn't exist,
-- it will return False.
Expand All @@ -24,6 +24,11 @@ package Files is
function Unique_Dir
(Parent : String; No_Prompt : Boolean := True)
return String;
-- Returns true if "Dir_Path" contains "File"
function Directory_Contains (Dir_Path : String;
File : String;
Which_Kind : Ada.Directories.File_Kind)
return Boolean;
private
function Random_String (Str_Len : Natural) return String;
end Files;
61 changes: 55 additions & 6 deletions src/getada.adb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.

pragma Assertion_Policy (Check);
with Defaults;
with Defaults; use Defaults;
with Platform; use Platform;
with Installer; use Installer;
with Uninstaller; use Uninstaller;
with Options; use Options;
with Settings; use Settings;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure Getada is

Expand All @@ -32,19 +34,66 @@ begin

-- Welcome the user to our program :)
if not Options.Quiet and then not Options.Show_Version then
Put_Line (Defaults.Welcome_Message);
Put_Line (Welcome_Message);
end if;

if Options.Show_Help then
-- Just show the help message and exit. -q won't count.
Put_Line (Defaults.Help_Message);
Put_Line (Help_Message);
elsif Options.Show_Version then
-- Just show the version and exit. -q won't count.
Put_Line (Defaults.Getada_Command & " " & Defaults.Getada_Version);
Put_Line (Getada_Command & " " & Getada_Version);
else
-- Check if the platform is currently supported by alire.
-- If etiher alire or I start building/distributing binaries for alr
-- then this can be updated.
case OS is
when MacOS =>
if Arch not in x86_64 | aarch64 then
raise Platform_Not_Yet_Supported with NL &
"----------------------------------------" &
"----------------------------------------" & NL &
"Currently only x86_64/aarch64 is supported on MacOS" & NL &
"Alire may be built from source code from " & NL &
"https://github.com/alire-project/alire" & NL &
"----------------------------------------" &
"----------------------------------------";
end if;
when Linux =>
if Arch /= x86_64 then
raise Platform_Not_Yet_Supported with NL &
"----------------------------------------" &
"----------------------------------------" & NL &
"Currently only x86_64 is supported on Linux" & NL &
"Alire may be built from source code from " & NL &
"https://github.com/alire-project/alire" & NL &
"----------------------------------------" &
"----------------------------------------";
end if;
when Windows =>
raise Platform_Not_Yet_Supported with NL &
"----------------------------------------" &
"----------------------------------------" & NL &
"NOTE: Windows installation is not supported by this tool!" & NL &
" Please use alire's installer on https://alire.ada.dev/" &
NL & "----------------------------------------" &
"----------------------------------------";
when FreeBSD =>
raise Platform_Not_Yet_Supported with NL &
"----------------------------------------" &
"----------------------------------------" & NL &
"NOTE: FreeBSD installation is not ready yet!" & NL &
"Please install Alire via ports: " & NL &
"https://cgit.freebsd.org/ports/log/devel/alire" & NL &
"----------------------------------------" &
"----------------------------------------";
end case;
-- Create settings and start installer or uninstaller
declare
Settings : constant Program_Settings := Init_Settings (Options);
begin
-- Put the location getada executable has been found.
Put_Line ("""getada"" detected at " & To_String (Settings.Exec_Path));
if Options.Uninstall then
-- Start our uninstaller.
Uninstall (Settings);
Expand All @@ -55,7 +104,7 @@ begin
end;
end if;
exception
when Defaults.User_Aborted =>
when User_Aborted =>
Put_Line ("Aborted... Closing program.");
when Defaults.Graceful_Exit => null;
when Graceful_Exit => null;
end Getada;
Loading

0 comments on commit 00515d9

Please sign in to comment.