----------------------------------------------------------------------- -- are-installer-merges -- Web file merge -- Copyright (C) 2020, 2021 Stephane Carrez -- Written by Stephane Carrez (Stephane.Carrez@gmail.com) -- -- Licensed under the Apache License, Version 2.0 (the "License"); -- you may not use this file except in compliance with the License. -- You may obtain a copy of the License at -- -- http://www.apache.org/licenses/LICENSE-2.0 -- -- Unless required by applicable law or agreed to in writing, software -- distributed under the License is distributed on an "AS IS" BASIS, -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. ----------------------------------------------------------------------- with Ada.Directories; with Ada.Strings.Fixed; with Ada.Streams.Stream_IO; with Ada.Exceptions; with Ada.IO_Exceptions; with Ada.Unchecked_Deallocation; with Util.Beans.Objects; with Util.Log.Loggers; with Util.Files; with Util.Strings; with Util.Streams.Files; with Util.Streams.Texts; with Util.Streams.Buffered; with EL.Expressions; with Are.Utils; package body Are.Installer.Merges is use Util.Log; use Ada.Strings.Fixed; use Util.Beans.Objects; use Ada.Strings.Unbounded; use type Ada.Calendar.Time; procedure Process_Property (Rule : in out Merge_Rule; Node : in DOM.Core.Node); procedure Process_Replace (Rule : in out Merge_Rule; Node : in DOM.Core.Node); Log : constant Loggers.Logger := Loggers.Create ("Are.Installer.Merges"); -- ------------------------------ -- Extract the {value} to setup the -- EL context to evaluate the source and target links for the merge. -- ------------------------------ procedure Process_Property (Rule : in out Merge_Rule; Node : in DOM.Core.Node) is use Util.Beans.Objects.Maps; Name : constant String := Are.Utils.Get_Attribute (Node, "name"); Value : constant String := Are.Utils.Get_Data_Content (Node); Pos : constant Natural := Util.Strings.Index (Name, '.'); begin if Pos = 0 then Rule.Variables.Bind (Name, To_Object (Value)); return; end if; -- A composed name such as 'jquery.path' must be split so that we store -- the 'path' value within a 'jquery' Map_Bean object. We handle only -- one such composition. declare Param : constant String := Name (Name'First .. Pos - 1); Tag : constant Unbounded_String := To_Unbounded_String (Param); Var : constant EL.Expressions.Expression := Rule.Variables.Get_Variable (Tag); Val : Object := Rule.Params.Get_Value (Param); Child : Map_Bean_Access; begin if Is_Null (Val) then Child := new Map_Bean; Val := To_Object (Child); Rule.Params.Set_Value (Param, Val); else Child := Map_Bean_Access (To_Bean (Val)); end if; Child.Set_Value (Name (Pos + 1 .. Name'Last), To_Object (Value)); if Var.Is_Null then Rule.Variables.Bind (Param, Val); end if; end; end Process_Property; procedure Process_Replace (Rule : in out Merge_Rule; Node : in DOM.Core.Node) is From : constant String := Are.Utils.Get_Data_Content (Node, "from"); To : constant String := Are.Utils.Get_Data_Content (Node, "to"); begin Rule.Replace.Include (From, To); end Process_Replace; procedure Iterate_Properties is new Are.Utils.Iterate_Nodes (Merge_Rule, Process_Property); procedure Iterate_Replace is new Are.Utils.Iterate_Nodes (Merge_Rule, Process_Replace); -- ------------------------------ -- Create a distribution rule to copy a set of files or directories. -- ------------------------------ function Create_Rule (Node : in DOM.Core.Node) return Distrib_Rule_Access is Result : constant Merge_Rule_Access := new Merge_Rule; begin Iterate_Properties (Result.all, Node, "property", False); Iterate_Replace (Result.all, Node, "replace", False); Result.Context.Set_Variable_Mapper (Result.Variables'Access); Result.Start_Mark := Are.Utils.Get_Attribute (Node, "merge-start", DEFAULT_MERGE_START); Result.End_Mark := Are.Utils.Get_Attribute (Node, "merge-end", DEFAULT_MERGE_END); return Result.all'Access; end Create_Rule; -- ------------------------------ -- Get a name to qualify the installation rule (used for logs). -- ------------------------------ overriding function Get_Install_Name (Rule : in Merge_Rule) return String is pragma Unreferenced (Rule); begin return "merge"; end Get_Install_Name; overriding procedure Install (Rule : in Merge_Rule; Path : in String; Files : in File_Vector; Context : in out Context_Type'Class) is use Ada.Streams; type Mode_Type is (MERGE_NONE, MERGE_LINK, MERGE_SCRIPT); procedure Error (Message : in String; Arg1 : in String := ""); function Get_Target_Name (Link : in String) return String; function Get_Target_Path (Link : in String) return String; function Get_Filename (Line : in String) return String; function Get_Source (Line : in String; Tag : in String) return String; function Find_Match (Buffer : in Stream_Element_Array) return Stream_Element_Offset; procedure Prepare_Merge (Line : in String); procedure Include (Line : in String; Pattern : in String); procedure Process (Line : in String); Root_Dir : constant String := Context.Get_Generation_Path (To_String (Rule.Dir)); Source : constant String := Get_Source_Path (Files, False); Target : constant String := Context.Get_Generation_Path (Path); Dir : constant String := Ada.Directories.Containing_Directory (Target); Build : constant String := "23"; -- Context.Get_Parameter ("dynamo.build"); Start_Mark : constant String := "