Hog Hog2025.2-3
generate_yaml.tcl
Go to the documentation of this file.
1 #!/usr/bin/env tclsh
2 # Copyright 2018-2025 The University of Birmingham
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 #parsing command options
17 
18 ##nagelfar variable CI_STAGES
19 ##nagelfar variable CI_PROPS
20 
21 if {[catch {package require yaml} ERROR]} {
22  puts "$ERROR\n If you are running this script on tclsh, you can fix this by installing 'tcllib'"
23  return
24 }
25 
26 if {[catch {package require cmdline} ERROR]} {
27  puts "$ERROR\n If you are running this script on tclsh, you can fix this by installing 'tcllib'"
28  return
29 }
30 
31 set parameters {
32  {runall "If set, it will generate a gitlab-ci.yml file for all projects in the Top folder, \
33  even if it has not been modified with respect to the target branch."}
34  {static "Normally the content of the hog-child.yml file is added at the beginning of the generated yml file. If this flag is set, this will not be done."}
35  {external_path.arg "" "Path for external files not stored in the git repository."}
36 }
37 
38 set usage "Generate a gitlab-ci.yml config file for the child pipeline - USAGE: generate_yaml.tcl \[options\]"
39 
40 set old_path [pwd]
41 set tcl_path [file normalize "[file dirname [info script]]/.."]
42 
43 set repo_path [file normalize $tcl_path/../..]
44 source $tcl_path/hog.tcl
45 
46 if {[catch {array set options [cmdline::getoptions ::argv $parameters $usage]}]} {
47  Msg Info [cmdline::usage $parameters $usage]
48  exit 1
49 }
50 
51 if {$options(runall) == 1} {
52  set runall 1
53 } else {
54  set runall 0
55 }
56 if {$options(static) == 1} {
57  set static 1
58  set runall 1
59 } else {
60  set static 0
61 }
62 
63 if {$options(external_path) != ""} {
64  set ext_path $options(external_path)
65  Msg Info "External path set to $ext_path"
66 } else {
67  set ext_path ""
68 }
69 
70 
71 set stage_list $CI_STAGES
72 set prop_list $CI_PROPS
73 
74 if {$static == 1} {
75  if {[file exists "$repo_path/.gitlab-ci.yml"]} {
76  set created_yml "$repo_path/new_gitlab-ci.yml"
77  Msg Warning "$repo_path/.gitlab-ci.yml, will create (and possibly repleace) $created_yml, please rename it if you want Hog-CI to work."
78  } else {
79  set created_yml "$repo_path/.gitlab-ci.yml"
80  }
81  Msg Info "Creating new file $created_yml..."
82  set fp [open $created_yml w]
83 
84  Msg Info "Evaluating the current version of Hog to use in the ref in the yml file..."
85  cd $tcl_path
86  set ref [Git describe]
87  cd $old_path
88  # adding include hog.yml and ref
89  #set outer [huddle create "inculde" [huddle list [huddle string "project: 'hog/Hog'" "file" "'/hog.yml'" "ref" "'$ref'" ]]]
90  #puts $fp [ string trimleft [ yaml::huddle2yaml $outer ] "-" ]
91  puts $fp "include:\n - project: 'hog/Hog'\n file: 'hog.yml'\n ref: '$ref'\n"
92 } else {
93  set created_yml "$repo_path/generated-config.yml"
94  Msg Info "Copying $repo_path/Hog/YAML/hog-common.yml to $created_yml..."
95  file copy -force $repo_path/Hog/YAML/hog-common.yml $created_yml
96  set fp [open $created_yml a]
97  Msg Info "Copying $repo_path/Hog/YAML/hog-child.yml to $created_yml..."
98  set fp2 [open "$repo_path/Hog/YAML/hog-child.yml" r]
99  set file_data [read $fp2]
100  close $fp2
101  regsub -all {\-\-\-} $file_data "" file_data
102  puts $fp $file_data
103  puts $fp "\n"
104  if {[file exists "$repo_path/hog-ci-users.yml"] == 1} {
105  Msg Info "Copying $repo_path/hog-ci-users.yml to $created_yml..."
106  set fp3 [open "$repo_path/hog-ci-users.yml" r]
107  set file_data [read $fp3]
108  close $fp3
109  regsub -all {\-\-\-} $file_data "" file_data
110  puts $fp $file_data
111  puts $fp "\n"
112  }
113 }
114 
115 set projects_list [SearchHogProjects $repo_path/Top]
116 foreach proj $projects_list {
117  set proj_name [file tail $proj]
118  set dir $repo_path/Top/$proj
119  set ver [GetProjectVersion $dir $repo_path $ext_path 1]
120  set no_ver_check 0
121 
122  if {[file exists "$dir/ci.conf"] == 1} {
123  Msg Info "Foung CI configuration file $dir/ci.conf, reading configuration for $proj..."
124  set ci_confs [ReadConf $dir/ci.conf]
125  set f [open $dir/ci.conf "r"]
126  set line [gets $f]
127  close $f
128  if {[string first "NO_VER_CHECK" $line]} {
129  set no_ver_check 1
130  }
131  }
132 
133  if {$ver == 0 || $ver == -1 || $runall == 1 || $no_ver_check == 1} {
134  if {$runall == 0 && $no_ver_check == 0} {
135  Msg Info "$proj was modified, adding it to CI..."
136  } else {
137  Msg Info "$proj is set to always run, adding it to CI..."
138  }
139  if {[file exists "$dir/ci.conf"] == 1} {
140  Msg Info "Found CI configuration file $dir/ci.conf, reading configuration for $proj..."
141  puts $fp [WriteGitLabCIYAML $proj $dir/ci.conf]
142  } else {
143  Msg Info "No CI configuration file found ($dir/ci.conf) for $proj, creating all jobs..."
144  puts $fp [WriteGitLabCIYAML $proj]
145  }
146  } else {
147  Msg Info "$proj was not modified since version: $ver, skipping."
148  #Here we should provide the link to the tag in $ver
149  }
150 }
151 close $fp
152 Msg Info "$created_yml generated correctly."