Skip to content

Commit 66deb35

Browse files
committed
WIP with several alice commands
1 parent 9c572c0 commit 66deb35

9 files changed

+324
-21
lines changed

attic/init/alice_cmd-source-init.adb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-------------------------------------------------------------------------------
2+
--
3+
-- ALICE - Adventures for Learning and Inspiring Coding Excellence
4+
-- Copyright (c) 2023 Francesc Rocher <francesc.rocher@gmail.com>
5+
-- SPDX-License-Identifier: MIT
6+
--
7+
-------------------------------------------------------------------------------
8+
9+
with GNAT.OS_Lib;
10+
with Simple_Logging;
11+
12+
use all type GNAT.OS_Lib.String_Access;
13+
14+
package body Alice_Cmd.Source.Init is
15+
16+
package Log renames Simple_Logging;
17+
18+
function Project_Euler return Boolean is separate;
19+
20+
-------------
21+
-- Execute --
22+
-------------
23+
24+
overriding procedure Execute
25+
(Cmd : in out Cmd_Type; Args : AAA.Strings.Vector)
26+
is
27+
Args_Length : constant Natural := Natural (Args.Length);
28+
begin
29+
30+
if Args_Length = 0 then
31+
Log.Error ("Too few arguments: Problem Source Tag required");
32+
return;
33+
end if;
34+
35+
if Args_Length > 1 then
36+
Log.Error ("Too many arguments");
37+
return;
38+
end if;
39+
40+
declare
41+
Problem_Source : constant String := Args.First_Element;
42+
Success : Boolean;
43+
begin
44+
pragma Unreferenced (Success);
45+
if Problem_Source = "project_euler" then
46+
Success := Source.Init.Project_Euler;
47+
Log.Always ("Project Euler successfully setup");
48+
else
49+
Log.Error ("Unknown Problem Source '" & Problem_Source & "'");
50+
end if;
51+
end;
52+
end Execute;
53+
54+
end Alice_Cmd.Source.Init;

attic/init/alice_cmd-source-init.ads

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-------------------------------------------------------------------------------
2+
--
3+
-- ALICE - Adventures for Learning and Inspiring Coding Excellence
4+
-- Copyright (c) 2023 Francesc Rocher <francesc.rocher@gmail.com>
5+
-- SPDX-License-Identifier: MIT
6+
--
7+
-------------------------------------------------------------------------------
8+
9+
with AAA.Strings;
10+
with CLIC.Subcommand;
11+
12+
package Alice_Cmd.Source.Init is
13+
14+
type Cmd_Type is new CLIC.Subcommand.Command with null record;
15+
16+
overriding function Name
17+
(Cmd : Cmd_Type) return CLIC.Subcommand.Identifier is
18+
("init");
19+
20+
overriding function Usage_Custom_Parameters
21+
(Cmd : Cmd_Type) return String is
22+
("<problem_source>");
23+
24+
overriding function Short_Description (Cmd : Cmd_Type) return String is
25+
("Initialize new Problem Source");
26+
27+
--!pp off
28+
pragma Style_Checks (off);
29+
30+
overriding function Long_Description
31+
(Cmd : Cmd_Type) return AAA.Strings.Vector is
32+
(AAA.Strings.Empty_Vector
33+
.Append ("Initializes a new Problem Source directory, populating it with all required repositories and resources.")
34+
.New_Line
35+
.Append ("• Installs the repository 'alice-adventures/<problem_source>', which is, in turn, the crate '<problem_source>' in the 'alice' index (cloned with 'git', maintained with 'alr').")
36+
.New_Line
37+
.Append ("• Creates a new git repository in your GitHub account. This repository is named 'alice-<problem_source>', which is cloned from the repository 'alice-adventures/<problem_source>-template' repository.")
38+
.New_Line
39+
.Append ("• Your new repository is created in '<problem_source>/usr/<your_login>/'.")
40+
.New_Line
41+
.Append ("• Clone the repository '<problem_source>-share' to access all the required resources. This repository is cloned in '<problem_source>/share'.")
42+
.New_Line
43+
.Append ("Note: Check all the available Problem Sources with 'alice list'.")
44+
);
45+
46+
pragma Style_Checks (on);
47+
--!pp on
48+
49+
overriding function Switch_Parsing
50+
(Cmd : Cmd_Type) return CLIC.Subcommand.Switch_Parsing_Kind is
51+
(CLIC.Subcommand.Parse_All);
52+
53+
overriding procedure Setup_Switches
54+
(Cmd : in out Cmd_Type;
55+
Config : in out CLIC.Subcommand.Switches_Configuration) is null;
56+
57+
overriding procedure Execute
58+
(Cmd : in out Cmd_Type; Args : AAA.Strings.Vector);
59+
60+
private
61+
62+
function Project_Euler return Boolean;
63+
64+
end Alice_Cmd.Source.Init;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
-------------------------------------------------------------------------------
2+
--
3+
-- ALICE - Adventures for Learning and Inspiring Coding Excellence
4+
-- Copyright (c) 2023 Francesc Rocher <francesc.rocher@gmail.com>
5+
-- SPDX-License-Identifier: MIT
6+
--
7+
-------------------------------------------------------------------------------
8+
9+
with Ada.Text_IO;
10+
separate (Alice_Cmd.Source.Init)
11+
12+
-------------------
13+
-- Project_Euler --
14+
-------------------
15+
16+
function Project_Euler return Boolean is
17+
Alr_Cmd : GNAT.OS_Lib.String_Access;
18+
Git_Cmd : GNAT.OS_Lib.String_Access;
19+
20+
Exists_Project_Euler_Repository : constant Boolean :=
21+
GNAT.OS_Lib.Is_Directory ("project_euler");
22+
23+
Status : Integer;
24+
begin
25+
Log.Info ("setup project_euler");
26+
27+
Ada.Text_IO.Put_Line
28+
(GNAT.OS_Lib.Argument_String_To_List
29+
("git pull --all --tags --force").all'
30+
Image);
31+
32+
if Exists_Project_Euler_Repository then
33+
Log.Warning ("Project Euler already exists");
34+
return False;
35+
end if;
36+
37+
Alr_Cmd := GNAT.OS_Lib.Locate_Exec_On_Path ("alr");
38+
if Alr_Cmd = null then
39+
Log.Error ("alr cannot be found in PATH");
40+
return False;
41+
else
42+
Log.Debug ("found alr at '" & Alr_Cmd.all & "'");
43+
end if;
44+
45+
Git_Cmd := GNAT.OS_Lib.Locate_Exec_On_Path ("git");
46+
if Git_Cmd = null then
47+
Log.Error ("git cannot be found in PATH");
48+
return False;
49+
else
50+
Log.Debug ("found git at '" & Git_Cmd.all & "'");
51+
end if;
52+
53+
-- clone project-euler
54+
declare
55+
Str_Clone : aliased String := "clone";
56+
Str_Repository : aliased String :=
57+
"git@github.com:alice-adventures/project-euler";
58+
Str_Project_Euler : aliased String := "project_euler";
59+
begin
60+
Log.Detail ("clone project_euler");
61+
Status :=
62+
GNAT.OS_Lib.Spawn
63+
(Git_Cmd.all,
64+
[Str_Clone'Unchecked_Access, Str_Repository'Unchecked_Access,
65+
Str_Project_Euler'Unchecked_Access]);
66+
Log.Debug ("git clone returns" & Status'Image);
67+
if Status > 0 then
68+
return False;
69+
end if;
70+
end;
71+
72+
-- update Alire crate
73+
declare
74+
Str_Update : aliased String := "update";
75+
begin
76+
Log.Detail ("alr update");
77+
Status := GNAT.OS_Lib.Spawn (Alr_Cmd.all, [Str_Update'Unchecked_Access]);
78+
Log.Debug ("alr update returns" & Status'Image);
79+
if Status > 0 then
80+
return False;
81+
end if;
82+
end;
83+
84+
return True;
85+
end Project_Euler;

src/alice_cmd/alice_cmd.adb

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ with Alice_Cmd.Work.Source;
1515

1616
with CLIC.Subcommand.Instance;
1717
with CLIC.TTY;
18+
1819
with GNAT.OS_Lib;
1920

2021
with Simple_Logging;
@@ -162,13 +163,15 @@ package body Alice_Cmd is
162163
CLI_Command.Execute;
163164
Log.Detail ("end Command.Execute");
164165
end if;
166+
167+
GNAT.OS_Lib.OS_Exit (0);
165168
end Execute;
166169

167170
-----------------------------
168171
-- Check_Unique_Subcommand --
169172
-----------------------------
170173

171-
procedure Check_Unique_Subcommand (Number : Positive) is
174+
procedure Check_Unique_Subcommand (Number : Natural) is
172175
begin
173176
if Number > 1 then
174177
Abort_Execution ("Only one subcommand can be specified");
@@ -179,7 +182,7 @@ package body Alice_Cmd is
179182
-- Check_Argument_Length --
180183
---------------------------
181184

182-
procedure Check_Argument_Length (Number, Length : Positive) is
185+
procedure Check_Argument_Length (Number, Length : Natural) is
183186
begin
184187
if Number < Length then
185188
Abort_Execution ("Too few arguments provided");

src/alice_cmd/alice_cmd.ads

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ package Alice_Cmd is
1616
procedure Execute;
1717
-- Execute the Alice command specified by the arguments.
1818

19-
procedure Check_Unique_Subcommand (Number : Positive);
19+
procedure Check_Unique_Subcommand (Number : Natural);
2020
-- Check that only one subcommand has been specified.
2121

22-
procedure Check_Argument_Length (Number, Length : Positive);
22+
procedure Check_Argument_Length (Number, Length : Natural);
2323
-- Check that the Number of arguments is exactly the Length required.
2424

2525
procedure Subcommand_Not_Found;

src/alice_cmd/work/source/alice_cmd-work-source.adb

+14-7
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ package body Alice_Cmd.Work.Source is
6262
begin
6363
Text_IO.Put_Line ("Available Problem Sources");
6464
Text_IO.Put_Line ("");
65-
Table.Append (" Tag").Append ("Name").Append ("URL").New_Row;
66-
Table.Append (" ---").Append ("----").Append ("---").New_Row;
65+
Table.Append (" Name").Append ("Id").Append ("Tag").Append ("URL")
66+
.New_Row;
67+
Table.Append (" ----").Append ("--").Append ("---").Append ("---")
68+
.New_Row;
6769
for Source of Available_Sources loop
68-
Table.Append (" " & To_String (Source.Tag)).Append
69-
(To_String (Source.Name))
70+
Table.Append (" " & To_String (Source.Name)).Append
71+
(To_String (Source.Id))
72+
.Append
73+
(To_String (Source.Tag))
7074
.Append
7175
(To_String (Source.URL))
7276
.New_Row;
@@ -78,7 +82,7 @@ package body Alice_Cmd.Work.Source is
7882
pragma Style_Checks (off);
7983

8084
AAA.Text_IO.Put_Paragraph
81-
("Note: when required, use the 'tag' in alice commands to refer to a specific Problem Source");
85+
("Note: when required, use the Id or the tag in alice commands to refer to a particular Problem Source");
8286

8387
pragma Style_Checks (on);
8488
end Execute_List;
@@ -87,7 +91,10 @@ package body Alice_Cmd.Work.Source is
8791
-- Execute_Init --
8892
------------------
8993

90-
procedure Execute_Init (Source : String) is null;
94+
procedure Execute_Init (Source : String) is
95+
begin
96+
Ensure_Alice_Alire_Index;
97+
end Execute_Init;
9198

9299
--------------------
93100
-- Execute_Status --
@@ -126,7 +133,7 @@ package body Alice_Cmd.Work.Source is
126133
if Cmd.Init or else Cmd.Status or else Cmd.Update then
127134
Check_Argument_Length (Args_Length, 1);
128135
if not Is_Valid_Source_Tag (Args.First_Element) then
129-
Abort_Execution ("Invalid Source tag, see 'alice --list'");
136+
Abort_Execution ("Invalid Source Id or tag, see 'source --list'");
130137
end if;
131138
end if;
132139

0 commit comments

Comments
 (0)