Git commit 399806c1efd4514b75c9f1254bb4b408f02a7221 by Andrew Shark.
Committed on 02/02/2024 at 08:07.
Pushed by ashark into branch 'master'.

RecursiveFH: Support variables in include lines

M  +12   -0    doc/kdesrc-buildrc.docbook
M  +1    -1    modules/ksb/Application.pm
M  +22   -1    modules/ksb/RecursiveFH.pm

https://invent.kde.org/sdk/kdesrc-build/-/commit/399806c1efd4514b75c9f1254bb4b408f02a7221

diff --git a/doc/kdesrc-buildrc.docbook b/doc/kdesrc-buildrc.docbook
index 1ad21432..dd038296 100644
--- a/doc/kdesrc-buildrc.docbook
+++ b/doc/kdesrc-buildrc.docbook
@@ -222,6 +222,18 @@ end global
 the file will be searched for starting from the directory containing the source
 file. This works recursively as well.</para></note>
 
+<para>You can use variables in the value of include instruction:</para>
+<programlisting>
+global
+  <replaceable>_ver</replaceable> <replaceable>6</replaceable>
+  source-dir ~/kde<replaceable>${_ver}</replaceable>/src
+  ...
+  persistent-data-file 
~/kde<replaceable>${_ver}</replaceable>/persistent-options.json
+end global
+
+include 
~/kde6/src/kdesrc-build/data/build-include/kf<replaceable>${_ver}</replaceable>-qt<replaceable>${_ver}</replaceable>.ksb
+
+</programlisting>
 </sect2>
 
 <sect2 id="kdesrc-buildrc-common">
diff --git a/modules/ksb/Application.pm b/modules/ksb/Application.pm
index 3d33a988..3aaa41e7 100644
--- a/modules/ksb/Application.pm
+++ b/modules/ksb/Application.pm
@@ -1097,7 +1097,7 @@ sub _readConfigurationOptions ($ctx, $fh, 
$cmdlineGlobalOptions, $deferredOption
     my $rcfile = $ctx->rcFile();
     my ($option, %readModules);
 
-    my $fileReader = ksb::RecursiveFH->new($rcfile);
+    my $fileReader = ksb::RecursiveFH->new($rcfile, $ctx);
     $fileReader->addFile($fh, $rcfile);
 
     # Read in global settings
diff --git a/modules/ksb/RecursiveFH.pm b/modules/ksb/RecursiveFH.pm
index 86d752b3..c5fcdb42 100644
--- a/modules/ksb/RecursiveFH.pm
+++ b/modules/ksb/RecursiveFH.pm
@@ -12,13 +12,14 @@ use File::Basename; # dirname
 # TODO: Replace make_exception with appropriate croak_* function.
 sub new
 {
-    my ($class, $rcfile) = @_;
+    my ($class, $rcfile, $ctx) = @_;
     my $data = {
         'filehandles' => [],    # Stack of filehandles to read
         'filenames'   => [],    # Corresponding tack of filenames (full paths)
         'base_path'   => [],    # Base directory path for relative includes
         'current'     => undef, # Current filehandle to read
         'current_fn'  => undef, # Current filename
+        'ctx'         => $ctx,
     };
 
     my $self = bless($data, $class);
@@ -158,6 +159,26 @@ sub readLine
                 EOM
             }
 
+            my $optionRE = qr/\$\{([a-zA-Z0-9-_]+)\}/;  # Example of matched 
string is "${option-name}" or "${_option-name}".
+            my $ctx = $self->{ctx};
+
+            # Replace reference to global option with their value.
+            my ($sub_var_name) = ($filename =~ $optionRE);
+            while ($sub_var_name)
+            {
+                my $sub_var_value = $ctx->getOption($sub_var_name) || "";
+                if(!$ctx->hasOption($sub_var_name)) {
+                    warning (" *\n * WARNING: y[$sub_var_name] used in 
$self->{current_fn}:$. is not set in global context.\n *");
+                }
+
+                debug ("Substituting \${$sub_var_name} with $sub_var_value");
+
+                $filename =~ s/\$\{$sub_var_name\}/$sub_var_value/g;
+
+                # Replace other references as well.
+                ($sub_var_name) = ($filename =~ $optionRE);
+            }
+
             open ($newFh, '<', $filename) or
                 die make_exception('Config', "Unable to open file '$filename' 
which was included from $self->{current_fn}:$.");
 

Reply via email to