├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── bin ├── compile-twig-templates ├── extract-locales └── licence-headers-check ├── composer.json ├── github-actions └── build-package │ ├── Dockerfile │ ├── README.md │ ├── action.yml │ └── entrypoint.sh ├── src ├── CompileTwigTemplatesCommand.php ├── LicenceHeadersCheckCommand.php └── PHPStan │ └── Rules │ └── GlobalVarTypeRule.php └── tools └── plugin-release /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## [0.7.5] - 2025-05-22 8 | - gettext sort-output flag is deprecated 9 | 10 | ## [0.7.4] - 2024-09-18 11 | - Fix compatibility with Twig 3.12. 12 | 13 | ## [0.7.3] - 2024-06-20 14 | - Do not check licence headers of docker compose config files. 15 | 16 | ## [0.7.2] - 2024-01-10 17 | - Fix copyright unduplicate logic in licence headers check command. 18 | 19 | ## [0.7.1] - 2023-10-16 20 | - Fix default project directory on licence headers check command. 21 | 22 | ## [0.7.0] - 2023-10-16 23 | - Update PHP namespace to prevent conflicts with GLPI core tools. 24 | - Add a PHPStan rule to ensure that global variable types are defined in PHPDoc. 25 | 26 | ## [0.6.4] - 2023-07-27 27 | - Allow usage of `/*!` starting line in JS and PHP files license headers. 28 | 29 | ## [0.6.3] - 2023-07-07 30 | - Exclude all hidden files from license headers checks. 31 | 32 | ## [0.6.2] - 2023-01-12 33 | - Prevent locale extract from failing when some files types are not existing. 34 | - Fix handling of copyright dates updates. 35 | 36 | ## [0.6.1] - 2022-11-07 37 | - Fix licence headers check command on PHP 8.x. 38 | 39 | ## [0.6.0] - 2022-11-07 40 | - Accept `.licence-header` as source file for licence headers. 41 | - Allow usage of `symfony/console` 6.x. 42 | 43 | ## [0.5.1] - 2022-06-28 44 | - Fix locales extraction on `alpine` context 45 | 46 | ## [0.5.0] - 2022-05-03 47 | - Add ability to preserve tagged data in license headers 48 | - Drop old locales extraction script 49 | - Exit gracefully if external tools not found in plugin release script 50 | - Fix composer handling on Windows env in plugin release script 51 | - Fix handling of `bin/*` files in license headers checks 52 | - Prevent too many empty lines creation in license headers fix 53 | 54 | ## [0.4.5] - 2022-04-20 55 | - Prevent unsafe git dir exception on `build-package` action 56 | 57 | ## [0.4.4] - 2022-03-10 58 | - Fix locales extraction from Twig templates 59 | 60 | ## [0.4.3] - 2022-03-09 61 | - Fix locales extraction when installed using Composer 2+ 62 | 63 | ## [0.4.2] - 2022-01-28 64 | - Fix licence header ending detection 65 | 66 | ## [0.4.1] - 2021-12-20 67 | - Improve locales extraction 68 | - Improve headers checks 69 | - Fix PSR-12 compliance 70 | 71 | ## [0.4.0] - 2021-11-17 72 | - Drop `glpi-project/coding-standard` dependency 73 | - Do not check `.gitlab-ci.yml` in licence-headers-check command 74 | 75 | ## [0.3.1] - 2021-10-28 76 | - Handle *.twig files in license headers check command 77 | - Fix PHP 8.1 compatibility 78 | 79 | ## [0.3.0] - 2021-10-07 80 | - Latest coding standards 81 | 82 | ## [0.2.0] - 2021-09-28 83 | - Do not check `css/lib` and `tests/config` in licence-headers-check command 84 | - Handle CSSO preserved comments 85 | - Remove consolidation/robo task runner 86 | - Fix "is not" with literal SyntaxWarning 87 | 88 | ## [0.1.16] - 2021-03-03 89 | - Enhance license headers check 90 | - Enable usage of Robo 3.x, drop usage of Robo 1.x 91 | - Remove disabled minification tasks 92 | 93 | ## [0.1.15] - 2020-01-18 94 | - Add licence-headers-check command in replacement of modify_headers.pl script 95 | - Add plugin package building Github action 96 | - Latest coding standards 97 | 98 | ## [0.1.14] - 2020-10-26 99 | - Permit usage of consolidation/robo 2.x (PHP 7.4 compatibility) 100 | - Call npm install if package.json exists 101 | 102 | ## [0.1.13] - 2020-06-17 103 | - Fix release build when using --commit option 104 | - Disable minification (bugged) 105 | 106 | ## [0.1.12] - 2020-04-06 107 | - Fix missing test classes in classmap 108 | 109 | ## [0.1.11] - 2020-04-03 110 | - Remove vendor useless files 111 | 112 | ## [0.1.10] - 2020-04-02 113 | - Fix do not check detection 114 | 115 | ## [0.1.9] - 2020-02-07 116 | - Fix versions order 117 | - Add parametrable exclusions 118 | - Python3 compatible 119 | 120 | ## [0.1.8] - 2019-06-07 121 | - Fix encoding issues with release script 122 | 123 | ## [0.1.7] - 2019-05-02 124 | - Add switch to ignore version checking 125 | - Fix help 126 | 127 | ## [0.1.6] - 2019-03-01 128 | - Fix composer deprecation messages 129 | 130 | ## [0.1.5] - 2018-06-21 131 | - Generate en_GB po file on strings extraction 132 | - Use stable libraries versions 133 | 134 | ## [0.1.4] - 2018-02-23 135 | - Latest coding standards 136 | 137 | ## [0.1.3] - 2018-01-05 138 | 139 | - Coding standards are still in 0.5 for projects using tools 140 | 141 | ## [0.1.3] - 2018-01-02 142 | 143 | - Upgrade coding standards to 0.6 144 | 145 | ## [0.1.2] - 2017-03-03 146 | 147 | - Do not check for gh token for mo compilation, version proposal or minify 148 | 149 | ## [0.1.1] - 2017-02-08 150 | 151 | - Fix a bug in standalone minify 152 | 153 | ## [0.1] - 2017-02-07 154 | 155 | First version 156 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GLPI tools 2 | 3 | A set of tools used in the core GLPI project and also from plugins. 4 | 5 | ## Github Actions 6 | 7 | Following Gihub Actions are available: 8 | - [Plugin package building](github-actions/build-package). 9 | -------------------------------------------------------------------------------- /bin/compile-twig-templates: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | . 31 | * 32 | * --------------------------------------------------------------------- 33 | */ 34 | 35 | require __DIR__.'/../../../autoload.php'; 36 | 37 | use GlpiProject\Tools\CompileTwigTemplatesCommand; 38 | use Symfony\Component\Console\Application; 39 | 40 | $command = new CompileTwigTemplatesCommand(); 41 | 42 | $application = new Application($command->getName()); 43 | $application->add($command); 44 | $application->setDefaultCommand($command->getName(), true); 45 | $application->run(); 46 | -------------------------------------------------------------------------------- /bin/extract-locales: -------------------------------------------------------------------------------- 1 | #!/bin/bash -eu 2 | 3 | # 4 | # --------------------------------------------------------------------- 5 | # 6 | # GLPI tools 7 | # 8 | # @copyright 2017-2022 Teclib' and contributors. 9 | # @licence https://www.gnu.org/licenses/gpl-3.0.html 10 | # @link https://github.com/glpi-project/tools 11 | # 12 | # --------------------------------------------------------------------- 13 | # 14 | # LICENSE 15 | # 16 | # This file is part of GLPI tools. 17 | # 18 | # This program is free software: you can redistribute it and/or modify 19 | # it under the terms of the GNU General Public License as published by 20 | # the Free Software Foundation, either version 3 of the License, or 21 | # (at your option) any later version. 22 | # 23 | # This program is distributed in the hope that it will be useful, 24 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | # GNU General Public License for more details. 27 | # 28 | # You should have received a copy of the GNU General Public License 29 | # along with this program. If not, see . 30 | # 31 | # --------------------------------------------------------------------- 32 | # 33 | 34 | SCRIPT_DIR=$(dirname $0) 35 | if [[ "$SCRIPT_DIR" == *"/vendor/glpi-project/tools"* ]]; then 36 | # Script is executed from "vendor/glpi-project/tools/bin" directory 37 | WORKING_DIR=$(readlink -f "$SCRIPT_DIR/../../../..") 38 | else 39 | # Script is executed from "vendor/bin" directory 40 | WORKING_DIR=$(readlink -f "$SCRIPT_DIR/../..") 41 | fi; 42 | 43 | # Define translate function args 44 | F_ARGS_N="1,2" 45 | F_ARGS__S="1" 46 | F_ARGS__="1" 47 | F_ARGS_X="1c,2" 48 | F_ARGS_SX="1c,2" 49 | F_ARGS_NX="1c,2,3" 50 | F_ARGS_SN="1,2" 51 | 52 | # Compute POT filename 53 | if [ -f "$WORKING_DIR/setup.php" ]; then 54 | # setup.php found: it's a plugin. 55 | NAME="$(grep -m1 "PLUGIN_.*_VERSION" $WORKING_DIR/setup.php | cut -d _ -f 2)" 56 | EXCLUDE_REGEX="^.\/\(\..*\|\(libs?\|node_modules\|tests\|vendor\)\/\).*" 57 | 58 | # Only strings with domain specified are extracted (use Xt args of keyword param to set number of args needed) 59 | F_ARGS_N="$F_ARGS_N,4t" 60 | F_ARGS__S="$F_ARGS__S,2t" 61 | F_ARGS__="$F_ARGS__,2t" 62 | F_ARGS_X="$F_ARGS_X,3t" 63 | F_ARGS_SX="$F_ARGS_SX,3t" 64 | F_ARGS_NX="$F_ARGS_NX,5t" 65 | F_ARGS_SN="$F_ARGS_SN,4t" 66 | else 67 | # using core most probably 68 | NAME="GLPI" 69 | EXCLUDE_REGEX="^.\/\(\..*\|\(config\|files\|lib\|marketplace\|node_modules\|plugins\|public\|tests\|tools\|vendor\)\/\).*" 70 | fi; 71 | POTFILE="$WORKING_DIR/locales/${NAME,,}.pot" 72 | 73 | if [ ! -d "$WORKING_DIR/locales" ]; then 74 | mkdir $WORKING_DIR/locales 75 | fi 76 | 77 | # Clean existing POT file 78 | rm -f $POTFILE && touch $POTFILE 79 | 80 | # Append locales from Twig templates 81 | # It have to be executed first as the use of `--add-location=file` will remove line numbers 82 | # from all previous locations, including those which that may be added from PHP/JS files. 83 | 84 | if [ -d "$WORKING_DIR/templates" ]; then 85 | ## 1. Transform twig files and save them into temp dir 86 | TEMP_TWIG_DIR=$(mktemp -d -t glpi-locales-XXXXXXXX) 87 | mkdir -p "$TEMP_TWIG_DIR/templates" 88 | $SCRIPT_DIR/compile-twig-templates --quiet $WORKING_DIR/templates $TEMP_TWIG_DIR/templates 89 | 90 | ## 2. Extract string from transformed files 91 | cd $TEMP_TWIG_DIR 92 | TWIG_FILES=`find -type f -name "*.twig"` 93 | if [ ! -z "$TWIG_FILES" ]; then 94 | xgettext $TWIG_FILES \ 95 | -o $POTFILE \ 96 | -L PHP \ 97 | --add-comments=TRANS \ 98 | --add-location=file \ 99 | --from-code=UTF-8 \ 100 | --force-po \ 101 | --join-existing \ 102 | --keyword=_n:$F_ARGS_N \ 103 | --keyword=__:$F_ARGS__ \ 104 | --keyword=_x:$F_ARGS_X \ 105 | --keyword=_nx:$F_ARGS_NX 106 | fi 107 | 108 | ## 3. Clean temporary dir 109 | cd $SCRIPT_DIR 110 | rm -r $TEMP_TWIG_DIR 111 | fi 112 | 113 | # Append locales from PHP 114 | cd $WORKING_DIR 115 | PHP_FILES=`find -not -regex $EXCLUDE_REGEX -type f -name "*.php"` 116 | if [ ! -z "$PHP_FILES" ]; then 117 | xgettext $PHP_FILES \ 118 | -o $POTFILE \ 119 | -L PHP \ 120 | --add-comments=TRANS \ 121 | --from-code=UTF-8 \ 122 | --force-po \ 123 | --join-existing \ 124 | --keyword=_n:$F_ARGS_N \ 125 | --keyword=__s:$F_ARGS__S \ 126 | --keyword=__:$F_ARGS__ \ 127 | --keyword=_x:$F_ARGS_X \ 128 | --keyword=_sx:$F_ARGS_SX \ 129 | --keyword=_nx:$F_ARGS_NX \ 130 | --keyword=_sn:$F_ARGS_SN 131 | fi 132 | 133 | # Append locales from JavaScript 134 | cd $WORKING_DIR 135 | JS_FILES=`find -not -regex $EXCLUDE_REGEX -type f -name "*.js" -not -name "*.min.js"` 136 | if [ ! -z "$JS_FILES" ]; then 137 | xgettext $JS_FILES \ 138 | -o $POTFILE \ 139 | -L JavaScript \ 140 | --add-comments=TRANS \ 141 | --from-code=UTF-8 \ 142 | --force-po \ 143 | --join-existing \ 144 | --keyword=_n:$F_ARGS_N \ 145 | --keyword=__:$F_ARGS__ \ 146 | --keyword=_x:$F_ARGS_X \ 147 | --keyword=_nx:$F_ARGS_NX \ 148 | --keyword=i18n._n:$F_ARGS_N \ 149 | --keyword=i18n.__:$F_ARGS__ \ 150 | --keyword=i18n._p:$F_ARGS_X \ 151 | --keyword=i18n.ngettext:$F_ARGS_N \ 152 | --keyword=i18n.gettext:$F_ARGS__ \ 153 | --keyword=i18n.pgettext:$F_ARGS_X 154 | fi 155 | 156 | # Update main language 157 | LANG=C msginit --no-translator -i $POTFILE -l en_GB -o $WORKING_DIR/locales/en_GB.po 158 | -------------------------------------------------------------------------------- /bin/licence-headers-check: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | . 31 | * 32 | * --------------------------------------------------------------------- 33 | */ 34 | 35 | require __DIR__.'/../../../autoload.php'; 36 | 37 | use GlpiProject\Tools\LicenceHeadersCheckCommand; 38 | use Symfony\Component\Console\Application; 39 | 40 | $command = new LicenceHeadersCheckCommand(); 41 | 42 | $application = new Application($command->getName()); 43 | $application->add($command); 44 | $application->setDefaultCommand($command->getName(), true); 45 | $application->run(); 46 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glpi-project/tools", 3 | "description": "Various tools for GLPI and its plugins", 4 | "keywords": ["GLPI", "plugins", "tools"], 5 | "license": "GPL-3.0-or-later", 6 | "authors": [ 7 | { 8 | "name": "Teclib'", 9 | "email": "glpi@teclib.com", 10 | "homepage": "http://teclib-group.com" 11 | } 12 | ], 13 | "support" : { 14 | "source": "https://github.com/glpi-project/tools", 15 | "issues": "https://github.com/glpi-project/tools/issues" 16 | }, 17 | "autoload" : { 18 | "psr-4" : { 19 | "GlpiProject\\Tools\\" : "src/" 20 | } 21 | }, 22 | "bin" : [ 23 | "bin/extract-locales", 24 | "bin/licence-headers-check", 25 | "tools/plugin-release" 26 | ], 27 | "require": { 28 | "symfony/console": "^5.4 || ^6.0", 29 | "twig/twig": "^3.3" 30 | }, 31 | "require-dev": { 32 | "nikic/php-parser": "^4.13", 33 | "phpstan/phpstan-src": "^1.10" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /github-actions/build-package/Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # --------------------------------------------------------------------- 3 | # 4 | # GLPI tools 5 | # 6 | # @copyright 2017-2022 Teclib' and contributors. 7 | # @licence https://www.gnu.org/licenses/gpl-3.0.html 8 | # @link https://github.com/glpi-project/tools 9 | # 10 | # --------------------------------------------------------------------- 11 | # 12 | # LICENSE 13 | # 14 | # This file is part of GLPI tools. 15 | # 16 | # This program is free software: you can redistribute it and/or modify 17 | # it under the terms of the GNU General Public License as published by 18 | # the Free Software Foundation, either version 3 of the License, or 19 | # (at your option) any later version. 20 | # 21 | # This program is distributed in the hope that it will be useful, 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | # GNU General Public License for more details. 25 | # 26 | # You should have received a copy of the GNU General Public License 27 | # along with this program. If not, see . 28 | # 29 | # --------------------------------------------------------------------- 30 | # 31 | 32 | FROM ghcr.io/glpi-project/plugin-builder 33 | 34 | COPY entrypoint.sh /entrypoint.sh 35 | RUN chmod +x /entrypoint.sh 36 | 37 | ENTRYPOINT ["/entrypoint.sh"] 38 | -------------------------------------------------------------------------------- /github-actions/build-package/README.md: -------------------------------------------------------------------------------- 1 | # Build package Github action 2 | 3 | This action can be used to build a plugin's release package. 4 | 5 | ## Usage 6 | 7 | ### Inputs 8 | 9 | - `plugin-version`: The tag name corresponding to the release. 10 | 11 | ### Outputs 12 | 13 | - `package-basename`: Basename of the built package. 14 | - `package-path`: Path of the built package. 15 | 16 | ### Example workflow 17 | 18 | Following workflow will do these steps each time a tag is pushed: 19 | - build the plugin's package; 20 | - draft a new release; 21 | - attach package to the release. 22 | 23 | ```yaml 24 | name: "Plugin release" 25 | 26 | on: 27 | push: 28 | tags: 29 | - '*' 30 | 31 | jobs: 32 | create-release: 33 | name: "Create release" 34 | runs-on: "ubuntu-latest" 35 | steps: 36 | - name: "Checkout" 37 | uses: "actions/checkout@v2" 38 | - name: "Build package" 39 | id: "build-package" 40 | uses: "glpi-project/tools/github-actions/build-package" 41 | with: 42 | plugin-version: ${{ github.ref }} 43 | - name: "Upload package artifact" 44 | uses: "actions/upload-artifact@v2" 45 | with: 46 | name: ${{ steps.build-package.outputs.package-basename }} 47 | path: ${{ steps.build-package.outputs.package-path }} 48 | - name: "Create release" 49 | id: "create-release" 50 | uses: "actions/create-release@v1" 51 | env: 52 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 53 | with: 54 | tag_name: ${{ github.ref }} 55 | release_name: ${{ github.ref }} 56 | draft: true 57 | - name: "Attach package to release" 58 | uses: "actions/upload-release-asset@v1" 59 | env: 60 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 61 | with: 62 | upload_url: ${{ steps.create-release.outputs.upload_url }} 63 | asset_path: ${{ steps.build-package.outputs.package-path }} 64 | asset_name: ${{ steps.build-package.outputs.package-basename }} 65 | asset_content_type: " application/x-bzip2" 66 | ``` 67 | -------------------------------------------------------------------------------- /github-actions/build-package/action.yml: -------------------------------------------------------------------------------- 1 | # 2 | # --------------------------------------------------------------------- 3 | # 4 | # GLPI tools 5 | # 6 | # @copyright 2017-2022 Teclib' and contributors. 7 | # @licence https://www.gnu.org/licenses/gpl-3.0.html 8 | # @link https://github.com/glpi-project/tools 9 | # 10 | # --------------------------------------------------------------------- 11 | # 12 | # LICENSE 13 | # 14 | # This file is part of GLPI tools. 15 | # 16 | # This program is free software: you can redistribute it and/or modify 17 | # it under the terms of the GNU General Public License as published by 18 | # the Free Software Foundation, either version 3 of the License, or 19 | # (at your option) any later version. 20 | # 21 | # This program is distributed in the hope that it will be useful, 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | # GNU General Public License for more details. 25 | # 26 | # You should have received a copy of the GNU General Public License 27 | # along with this program. If not, see . 28 | # 29 | # --------------------------------------------------------------------- 30 | # 31 | 32 | name: "Build package" 33 | description: "Build a plugin's package" 34 | inputs: 35 | plugin-version: 36 | description: "Plugin version to release" 37 | required: true 38 | outputs: 39 | package-basename: 40 | description: "Built package basename" 41 | package-path: 42 | description: "Built package path" 43 | runs: 44 | using: "docker" 45 | image: "Dockerfile" 46 | env: 47 | PLUGIN_VERSION: ${{ inputs.plugin-version }} 48 | -------------------------------------------------------------------------------- /github-actions/build-package/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # --------------------------------------------------------------------- 5 | # 6 | # GLPI tools 7 | # 8 | # @copyright 2017-2022 Teclib' and contributors. 9 | # @licence https://www.gnu.org/licenses/gpl-3.0.html 10 | # @link https://github.com/glpi-project/tools 11 | # 12 | # --------------------------------------------------------------------- 13 | # 14 | # LICENSE 15 | # 16 | # This file is part of GLPI tools. 17 | # 18 | # This program is free software: you can redistribute it and/or modify 19 | # it under the terms of the GNU General Public License as published by 20 | # the Free Software Foundation, either version 3 of the License, or 21 | # (at your option) any later version. 22 | # 23 | # This program is distributed in the hope that it will be useful, 24 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | # GNU General Public License for more details. 27 | # 28 | # You should have received a copy of the GNU General Public License 29 | # along with this program. If not, see . 30 | # 31 | # --------------------------------------------------------------------- 32 | # 33 | 34 | composer install --no-progress --no-suggest --no-interaction --prefer-dist 35 | 36 | # Check that "plugin-release" script is fetched with project dependencies. 37 | # TODO Embed the release script in "ghcr.io/glpi-project/plugin-builder" docker image. 38 | if [ ! -f "vendor/bin/plugin-release" ] 39 | then 40 | echo "Project must have 'glpi-project/tools' in its composer dependencies." 41 | exit 1 42 | fi 43 | 44 | # plugin-release requires tags to be fetched. 45 | # TODO Fix this ! 46 | git config --global --add safe.directory $(pwd) 47 | git fetch --tags 48 | 49 | # --assume-yes Prevent interactions. 50 | # --dont-check Do not check if $PLUGIN_VERSION is a valid name for a version. 51 | # --nogithub Do not create Github release draft. 52 | # --nosign Do not sign package with gpg. 53 | vendor/bin/plugin-release --assume-yes --dont-check --nogithub --nosign --release $PLUGIN_VERSION --verbose 54 | 55 | # Defines output variables. 56 | echo "package-basename=$(find dist/ -type f -name '*.tar.bz2' | xargs -n 1 basename)" >> $GITHUB_OUTPUT 57 | echo "package-path=$(find dist/ -type f -name '*.tar.bz2')" >> $GITHUB_OUTPUT 58 | -------------------------------------------------------------------------------- /src/CompileTwigTemplatesCommand.php: -------------------------------------------------------------------------------- 1 | . 30 | * 31 | * --------------------------------------------------------------------- 32 | */ 33 | 34 | namespace GlpiProject\Tools; 35 | 36 | use Symfony\Component\Console\Command\Command; 37 | use Symfony\Component\Console\Helper\ProgressBar; 38 | use Symfony\Component\Console\Input\InputArgument; 39 | use Symfony\Component\Console\Input\InputInterface; 40 | use Symfony\Component\Console\Output\OutputInterface; 41 | use Twig\Environment; 42 | use Twig\TwigFilter; 43 | use Twig\TwigFunction; 44 | use Twig\TwigTest; 45 | use Twig\Cache\CacheInterface; 46 | use Twig\Cache\FilesystemCache; 47 | use Twig\Loader\FilesystemLoader; 48 | use Twig\Loader\LoaderInterface; 49 | use RecursiveDirectoryIterator; 50 | use RecursiveFilterIterator; 51 | use RecursiveIteratorIterator; 52 | use SplFileInfo; 53 | 54 | class CompileTwigTemplatesCommand extends Command 55 | { 56 | 57 | protected function configure() 58 | { 59 | parent::configure(); 60 | 61 | $this->setName('glpi:tools:compile_twig_templates'); 62 | $this->setDescription('Compile twig templates into php files.'); 63 | 64 | $this->addArgument( 65 | 'templates-directory', 66 | InputArgument::REQUIRED, 67 | 'Templates directory' 68 | ); 69 | 70 | $this->addArgument( 71 | 'output-directory', 72 | InputArgument::REQUIRED, 73 | 'Output directory' 74 | ); 75 | } 76 | 77 | protected function execute(InputInterface $input, OutputInterface $output) 78 | { 79 | $tpl_dir = $input->getArgument('templates-directory'); 80 | $output_dir = $input->getArgument('output-directory'); 81 | 82 | $loader = new FilesystemLoader($tpl_dir, dirname($tpl_dir)); 83 | $twig = $this->getMockedTwigEnvironment($loader); 84 | $twig->setCache($this->getTwigCacheHandler($output_dir)); 85 | 86 | $files = $this->getTemplatesFiles($tpl_dir); 87 | 88 | $progress_bar = new ProgressBar($output); 89 | foreach ($progress_bar->iterate($files) as $file) { 90 | $twig->load($file); 91 | } 92 | 93 | $output->writeln(''); // New to next line after progress bar display 94 | 95 | return 0; // Success 96 | } 97 | 98 | /** 99 | * Return template files. 100 | * 101 | * @param string $directory 102 | * 103 | * @return array 104 | */ 105 | private function getTemplatesFiles(string $directory): array 106 | { 107 | $directory = realpath($directory); 108 | 109 | if (!is_dir($directory) || !is_readable($directory)) { 110 | throw new \Symfony\Component\Console\Exception\InvalidOptionException( 111 | sprintf('Unable to read directory "%s"', $directory) 112 | ); 113 | } 114 | 115 | $dir_iterator = new RecursiveDirectoryIterator($directory); 116 | 117 | $filter_iterator = new class($dir_iterator) extends RecursiveFilterIterator { 118 | public function accept(): bool 119 | { 120 | if ($this->isFile() && !preg_match('/^twig$/', $this->getExtension())) { 121 | return false; 122 | } 123 | return true; 124 | } 125 | }; 126 | 127 | $recursive_iterator = new RecursiveIteratorIterator( 128 | $filter_iterator, 129 | RecursiveIteratorIterator::SELF_FIRST 130 | ); 131 | 132 | $files = []; 133 | 134 | /** @var SplFileInfo $file */ 135 | foreach ($recursive_iterator as $file) { 136 | if (!$file->isFile()) { 137 | continue; 138 | } 139 | 140 | $files[] = preg_replace( 141 | '/^' . preg_quote($directory . DIRECTORY_SEPARATOR, '/') . '/', 142 | '', 143 | $file->getRealPath() 144 | ); 145 | } 146 | 147 | return $files; 148 | } 149 | 150 | /** 151 | * Return a mocked Twig environment. 152 | * This mocked environment will prevent exceptions to be thrown when custom 153 | * functions, filters or tests are used in templates. 154 | * 155 | * @param LoaderInterface $loader 156 | * 157 | * @return Environment 158 | */ 159 | private function getMockedTwigEnvironment(LoaderInterface $loader): Environment 160 | { 161 | return new class ($loader) extends Environment { 162 | 163 | public function getFunction(string $name): ?TwigFunction 164 | { 165 | if (in_array($name, ['__', '_n', '_x', '_nx'], true)) { 166 | // Return a function that has its own name as callback 167 | // for translation functions, so Twig will generate code following this pattern: 168 | // $name($parameter, ...)`, e.g. `__('str')` or `_n('str', 'strs', 5)`. 169 | return new TwigFunction($name, $name); 170 | } 171 | return parent::getFunction($name) ?? new TwigFunction($name, function () {}); 172 | } 173 | 174 | public function getFilter(string $name): ?TwigFilter 175 | { 176 | return parent::getFilter($name) ?? new TwigFilter($name, function () {}); 177 | } 178 | 179 | public function getTest(string $name): ?TwigTest 180 | { 181 | if (in_array($name, ['divisible', 'same'])) { 182 | // `same as` and `divisible by` will be search in 2 times. 183 | // First check will be done on first word, should return `null` to 184 | // trigger second search that will be done on full name. 185 | return null; 186 | } 187 | return parent::getTest($name) ?? new TwigTest($name, function () {}); 188 | } 189 | }; 190 | } 191 | 192 | /** 193 | * Return a custom Twig cache handler. 194 | * This handler is usefull to be able to preserve filenames of compiled files. 195 | * 196 | * @param string $directory 197 | * 198 | * @return CacheInterface 199 | */ 200 | private function getTwigCacheHandler(string $directory): CacheInterface 201 | { 202 | return new class($directory) extends FilesystemCache { 203 | 204 | private $directory; 205 | 206 | public function __construct(string $directory, int $options = 0) 207 | { 208 | $this->directory = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; 209 | parent::__construct($directory, $options); 210 | } 211 | 212 | public function generateKey(string $name, string $className): string 213 | { 214 | return $this->directory . $name; 215 | } 216 | }; 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /src/LicenceHeadersCheckCommand.php: -------------------------------------------------------------------------------- 1 | . 30 | * 31 | * --------------------------------------------------------------------- 32 | */ 33 | 34 | namespace GlpiProject\Tools; 35 | 36 | use RecursiveDirectoryIterator; 37 | use RecursiveFilterIterator; 38 | use RecursiveIterator; 39 | use RecursiveIteratorIterator; 40 | use SplFileInfo; 41 | use Symfony\Component\Console\Command\Command; 42 | use Symfony\Component\Console\Input\InputInterface; 43 | use Symfony\Component\Console\Input\InputOption; 44 | use Symfony\Component\Console\Output\OutputInterface; 45 | 46 | class LicenceHeadersCheckCommand extends Command { 47 | 48 | /** 49 | * Result code returned when some headers are missing or are outdated. 50 | * 51 | * @var integer 52 | */ 53 | const ERROR_FOUND_MISSING_OR_OUTDATED = 1; 54 | 55 | /** 56 | * Result code returned when some files cannot be updated. 57 | * 58 | * @var integer 59 | */ 60 | const ERROR_UNABLE_TO_FIX_FILES = 2; 61 | 62 | /** 63 | * Header lines. 64 | * 65 | * @var array 66 | */ 67 | private $header_lines; 68 | 69 | protected function configure() { 70 | parent::configure(); 71 | 72 | $this->setName('glpi:tools:licence_headers_check'); 73 | $this->setDescription('Check licence header in code source files.'); 74 | 75 | $project_dir = realpath(__DIR__ . str_repeat(DIRECTORY_SEPARATOR . '..', 4)); 76 | if ($project_dir === false || !is_readable($project_dir)) { 77 | $project_dir = null; 78 | } 79 | 80 | $this->addOption( 81 | 'directory', 82 | 'd', 83 | InputOption::VALUE_REQUIRED, 84 | 'Directory to parse', 85 | $project_dir 86 | ); 87 | 88 | $header_file = null; 89 | if ($project_dir !== null) { 90 | $path = implode(DIRECTORY_SEPARATOR, [$project_dir, '.licence-header']); 91 | $legacy_path = implode(DIRECTORY_SEPARATOR, [$project_dir, 'tools', 'HEADER']); 92 | if (file_exists($path)) { 93 | $header_file = realpath($path); 94 | } elseif (file_exists($legacy_path)) { 95 | $header_file = realpath($legacy_path); 96 | } 97 | } 98 | 99 | $this->addOption( 100 | 'header-file', 101 | null, 102 | InputOption::VALUE_REQUIRED, 103 | 'Header file to use', 104 | $header_file 105 | ); 106 | 107 | $this->addOption( 108 | 'fix', 109 | 'f', 110 | InputOption::VALUE_NONE, 111 | 'Fix missing and outdated headers' 112 | ); 113 | 114 | $this->addOption( 115 | 'discard-extra-tags', 116 | null, 117 | InputOption::VALUE_NONE, 118 | 'Discard extra tags found in headers' 119 | ); 120 | } 121 | 122 | protected function execute(InputInterface $input, OutputInterface $output) { 123 | 124 | $files = $this->getFilesToParse($input->getOption('directory')); 125 | 126 | $output->writeln( 127 | '' . sprintf('%s files to process.', count($files)) . '', 128 | OutputInterface::VERBOSITY_VERBOSE 129 | ); 130 | 131 | $missing_found = 0; 132 | $missing_errors = 0; 133 | $outdated_found = 0; 134 | $outdated_errors = 0; 135 | 136 | foreach ($files as $filename) { 137 | $output->writeln( 138 | '' . sprintf('Processing "%s".', $filename) . '', 139 | OutputInterface::VERBOSITY_VERY_VERBOSE 140 | ); 141 | 142 | if (($file_lines = file($filename)) === false) { 143 | throw new \Exception(sprintf('Unable to read file.', $filename)); 144 | } 145 | 146 | $header_start_pattern = null; 147 | $header_end_pattern = null; 148 | $header_content_pattern = null; 149 | 150 | $extension = pathinfo($filename, PATHINFO_EXTENSION); 151 | if ($extension === '') { 152 | // No extension, file is probably a binary. 153 | // Try to compute extension from shebang. 154 | $first_line = $file_lines[0]; 155 | if (preg_match('/^#!/', $first_line)) { 156 | $shebang_matches = []; 157 | if ( 158 | // `#!/usr/bin/env php [options]` format 159 | preg_match('/^#!\/usr\/bin\/env\s+(?[^\s]+)(\s+.*)?$/', $first_line, $shebang_matches) 160 | // `#!/bin/bash [options]` format 161 | || preg_match('/^#!(.{0}|\/([^\/]+\/)*(?[^\/\s]+))(\s+.*)?$/', $first_line, $shebang_matches) 162 | ) { 163 | $binary = $shebang_matches['binary']; 164 | switch ($shebang_matches['binary']) { 165 | case 'bash': 166 | $extension = 'sh'; 167 | break; 168 | case 'perl': 169 | $extension = 'pl'; 170 | break; 171 | case 'php': 172 | default: 173 | $extension = $binary; 174 | break; 175 | } 176 | } 177 | } 178 | } 179 | switch ($extension) { 180 | case 'pl': 181 | case 'sh': 182 | case 'yaml': 183 | case 'yml': 184 | $header_line_prefix = '# '; 185 | $header_prepend_line = "#\n"; 186 | $header_append_line = "#\n"; 187 | $header_start_pattern = '/^#[^!]/'; // Any commented line except shebang (#!) 188 | $header_content_pattern = '/^#/'; 189 | break; 190 | case 'sql': 191 | $header_line_prefix = '-- '; 192 | $header_prepend_line = "--\n"; 193 | $header_append_line = "--\n"; 194 | $header_content_pattern = '/^(--|#)/'; // older headers were prefixed by "#" 195 | break; 196 | case 'css': 197 | case 'scss': 198 | $header_line_prefix = ' * '; 199 | $header_prepend_line = "/*!\n"; 200 | $header_append_line = " */\n"; 201 | $header_start_pattern = '/^\/\*(\!|\*)?$/'; // older headers were starting by "/**" or "/*!" 202 | $header_end_pattern = '/\*\//'; 203 | break; 204 | case 'twig': 205 | $header_line_prefix = ' # '; 206 | $header_prepend_line = "{#\n"; 207 | $header_append_line = " #}\n"; 208 | $header_start_pattern = '/^\{#$/'; 209 | $header_end_pattern = '/#}/'; 210 | break; 211 | default: 212 | $header_line_prefix = ' * '; 213 | $header_prepend_line = "/**\n"; 214 | $header_append_line = " */\n"; 215 | $header_start_pattern = '/^\/\*(\!|\*)?$/'; // accept "/*", "/**" and "/*!" 216 | $header_end_pattern = '/\*\//'; 217 | break; 218 | } 219 | 220 | if ($header_start_pattern === null) { 221 | // If there is no specific "start pattern", then first regular comment line is consider are header start. 222 | $header_start_pattern = $header_content_pattern; 223 | } 224 | 225 | $header_found = false; 226 | $header_missing = false; 227 | $is_header_line = false; 228 | $is_last_header_line = false; 229 | $pre_header_lines = []; 230 | $current_header_lines = []; 231 | $post_header_lines = []; 232 | 233 | foreach ($file_lines as $line) { 234 | if (!$header_found && !$header_missing) { 235 | if (preg_match($header_start_pattern, $line)) { 236 | // Line matches header opening line 237 | $header_found = true; 238 | $is_header_line = true; 239 | } else if (!$this->shouldLineBeLocatedBeforeHeader($line)) { 240 | // Line does not match allowed lines before header, 241 | // consider that header is missing. 242 | $header_missing = true; 243 | } 244 | } else if ($is_last_header_line) { 245 | // Previous line was "last header line", so current line is the first line after licence header 246 | $is_last_header_line = false; 247 | $is_header_line = false; 248 | } else if ($is_header_line && $header_end_pattern !== null && preg_match($header_end_pattern, $line)) { 249 | // Line matches header end pattern 250 | $is_last_header_line = true; 251 | } else if ($is_header_line && $header_content_pattern !== null && !preg_match($header_content_pattern, $line)) { 252 | // Line does not match header, so it is the first line after licence header 253 | $is_header_line = false; 254 | } 255 | 256 | if ($header_missing || ($header_found && !$is_header_line)) { 257 | $post_header_lines[] = $line; 258 | } else if ($is_header_line) { 259 | $current_header_lines[] = $line; 260 | } else { 261 | $pre_header_lines[] = $line; 262 | } 263 | } 264 | 265 | $preserved_tagged_data = $input->getOption('discard-extra-tags') 266 | ? [] 267 | : $this->extractTaggedData($current_header_lines, $header_line_prefix); 268 | 269 | $updated_header_lines = $this->getLicenceHeaderLines( 270 | $input->getOption('header-file'), 271 | $header_line_prefix, 272 | $header_prepend_line, 273 | $header_append_line, 274 | $preserved_tagged_data 275 | ); 276 | 277 | $header_outdated = array_slice($updated_header_lines, 1, -1) !== array_slice($current_header_lines, 1, -1); 278 | 279 | if (!$header_missing && !$header_outdated) { 280 | continue; 281 | } 282 | 283 | if ($header_missing) { 284 | $output->writeln( 285 | '' . sprintf('Missing licence header in file "%s".', $filename) . '', 286 | OutputInterface::VERBOSITY_NORMAL 287 | ); 288 | $missing_found++; 289 | } else { 290 | $output->writeln( 291 | '' . sprintf('Licence header outdated in file "%s".', $filename) . '', 292 | OutputInterface::VERBOSITY_NORMAL 293 | ); 294 | $outdated_found++; 295 | } 296 | 297 | if ($input->getOption('fix')) { 298 | $pre_header_lines = $this->stripEmptyLines($pre_header_lines, false, true); 299 | $post_header_lines = $this->stripEmptyLines($post_header_lines, true, false); 300 | 301 | $file_contents = ''; 302 | if (!empty($pre_header_lines)) { 303 | $file_contents .= implode('', $pre_header_lines) . "\n"; 304 | } 305 | $file_contents .= implode('', $updated_header_lines); 306 | if (!empty($post_header_lines)) { 307 | $file_contents .= "\n" . implode('', $post_header_lines); 308 | } 309 | 310 | if (strlen($file_contents) !== file_put_contents($filename, $file_contents)) { 311 | $output->writeln( 312 | '' . sprintf('Unable to update licence header in file "%s".', $filename) . '', 313 | OutputInterface::VERBOSITY_QUIET 314 | ); 315 | if ($header_missing) { 316 | $missing_errors++; 317 | } else { 318 | $outdated_errors++; 319 | } 320 | } 321 | } 322 | } 323 | 324 | if ($missing_found === 0 && $outdated_found === 0) { 325 | $output->writeln('Files headers are valid.', OutputInterface::VERBOSITY_QUIET); 326 | return 0; // Success 327 | } 328 | 329 | if (!$input->getOption('fix')) { 330 | $msg = sprintf( 331 | 'Found %d file(s) without header and %d file(s) with outdated header. Use --fix option to fix these files.', 332 | $missing_found, 333 | $outdated_found 334 | ); 335 | $output->writeln('' . $msg . '', OutputInterface::VERBOSITY_QUIET); 336 | return self::ERROR_FOUND_MISSING_OR_OUTDATED; 337 | } 338 | 339 | $msg = sprintf( 340 | 'Fixed %d file(s) without header and %d file(s) with outdated header.', 341 | $missing_found - $missing_errors, 342 | $outdated_found - $outdated_errors 343 | ); 344 | $output->writeln('' . $msg . '', OutputInterface::VERBOSITY_QUIET); 345 | 346 | if ($missing_errors > 0 || $outdated_errors > 0) { 347 | $output->writeln( 348 | '' . sprintf('%s file(s) cannot be updated.', $missing_errors + $outdated_errors) . '', 349 | OutputInterface::VERBOSITY_QUIET 350 | ); 351 | return self::ERROR_UNABLE_TO_FIX_FILES; 352 | } 353 | 354 | return 0; // Success 355 | } 356 | 357 | /** 358 | * Get licence header lines. 359 | * 360 | * @param string $header_file_path 361 | * @param string $line_prefix 362 | * @param string $prepend_line 363 | * @param string $append_line 364 | * @param array $extra_tagged_data 365 | * 366 | * @return array 367 | */ 368 | private function getLicenceHeaderLines( 369 | string $header_file_path, 370 | string $line_prefix, 371 | string $prepend_line, 372 | string $append_line, 373 | array $extra_tagged_data = [] 374 | ): array { 375 | if ($this->header_lines === null) { 376 | if (($lines = file($header_file_path)) === false) { 377 | throw new \Exception('Unable to read header file.'); 378 | } 379 | $this->header_lines = $lines; 380 | } 381 | 382 | $lines = []; 383 | $lines[] = $prepend_line; 384 | foreach ($this->header_lines as $line) { 385 | $lines[] = (preg_match('/^\s+$/', $line) ? rtrim($line_prefix) : $line_prefix) . $line; 386 | } 387 | $lines[] = $append_line; 388 | 389 | $lines = $this->appendTaggedData($lines, $extra_tagged_data, $line_prefix); 390 | 391 | return $this->stripEmptyLines($lines, true, true); 392 | } 393 | 394 | /** 395 | * Return files to parse. 396 | * 397 | * @param string $directory 398 | * 399 | * @return array 400 | */ 401 | private function getFilesToParse(string $directory): array { 402 | $directory = realpath($directory); 403 | 404 | if (!is_dir($directory) || !is_readable($directory)) { 405 | throw new \Symfony\Component\Console\Exception\InvalidOptionException( 406 | sprintf('Unable to read directory "%s"', $directory) 407 | ); 408 | } 409 | 410 | $dir_iterator = new RecursiveDirectoryIterator($directory); 411 | $exclusion_pattern = $this->getExclusionPattern($directory); 412 | 413 | $filter_iterator = new class($dir_iterator, $exclusion_pattern) extends RecursiveFilterIterator { 414 | private $exclusion_pattern; 415 | 416 | public function __construct(RecursiveIterator $iterator, ?string $exclusion_pattern) { 417 | $this->exclusion_pattern = $exclusion_pattern; 418 | parent::__construct($iterator); 419 | } 420 | 421 | public function accept(): bool { 422 | if ($this->exclusion_pattern !== null && preg_match($this->exclusion_pattern, $this->getRealPath())) { 423 | return false; 424 | } 425 | if ($this->isDir()) { 426 | return true; // parse subdirectories 427 | } 428 | if (preg_match('/^(css|js|php|pl|scss|sh|sql|twig|ya?ml)$/', $this->getExtension())) { 429 | return true; // handled extensions 430 | } 431 | if (basename($this->getPath()) === 'bin') { 432 | return true; // executable 433 | } 434 | return false; 435 | } 436 | 437 | public function getChildren(): ?RecursiveFilterIterator { 438 | return new self($this->getInnerIterator()->getChildren(), $this->exclusion_pattern); 439 | } 440 | }; 441 | 442 | $recursive_iterator = new RecursiveIteratorIterator( 443 | $filter_iterator, 444 | RecursiveIteratorIterator::SELF_FIRST 445 | ); 446 | 447 | $files = []; 448 | 449 | /** @var SplFileInfo $file */ 450 | foreach ($recursive_iterator as $file) { 451 | if (!$file->isFile()) { 452 | continue; 453 | } 454 | 455 | $files[] = $file->getRealPath(); 456 | } 457 | 458 | return $files; 459 | } 460 | 461 | /** 462 | * Indicates if a line can/should be located before licence header. 463 | * 464 | * @param string $line 465 | * 466 | * @return bool 467 | */ 468 | private function shouldLineBeLocatedBeforeHeader(string $line): bool { 469 | // PHP opening tag 470 | if (rtrim($line) === 'getTagPattern($line_prefix); 545 | 546 | foreach ($lines as $line) { 547 | $tag = null; 548 | if (preg_match($tag_pattern, $line, $tag)) { 549 | $tag_name = $tag['name']; 550 | $tag_value = $tag['value']; 551 | 552 | if (!array_key_exists($tag_name, $tagged_data)) { 553 | $tagged_data[$tag_name] = []; 554 | } 555 | $tagged_data[$tag_name][] = $tag_value; 556 | } 557 | } 558 | 559 | return $tagged_data; 560 | } 561 | 562 | /** 563 | * Append tagged data to header lines. 564 | * 565 | * @param array $lines 566 | * @param array $data_to_append 567 | * @param string|null $line_prefix 568 | * 569 | * @return array 570 | */ 571 | private function appendTaggedData(array $lines, array $data_to_append, ?string $line_prefix = null): array { 572 | 573 | $existing_data = $this->extractTaggedData($lines, $line_prefix); 574 | 575 | if (count($existing_data) === 0) { 576 | $existing_tag_lines_nums = []; 577 | $append_line_num = count($lines); // There is no tag in given lines, append new tags to the end. 578 | } else { 579 | $data_to_append = array_merge_recursive($existing_data, $data_to_append); 580 | $data_to_append = array_map('array_unique', $data_to_append); 581 | ksort($data_to_append); 582 | 583 | $existing_tag_lines_nums = array_keys(preg_grep($this->getTagPattern($line_prefix), $lines)); 584 | $append_line_num = $existing_tag_lines_nums[0]; 585 | } 586 | 587 | // Deduplicate tagged data 588 | foreach ($data_to_append as $tag_name => $tag_values) { 589 | if (preg_match('/^copy(right|left)$/', $tag_name) !== 1) { 590 | continue; 591 | } 592 | $data_to_append[$tag_name] = $this->unduplicateCopyTag($tag_values); 593 | } 594 | 595 | // Drop existing tag lines and re-append merged tagged data entirely 596 | $result_lines = []; 597 | foreach ($lines as $num => $line) { 598 | if (!in_array($num, $existing_tag_lines_nums)) { 599 | $result_lines[] = $line; // Line is ot a tag line, keep it. 600 | } 601 | if ($num === $append_line_num) { 602 | // Append entire tag data 603 | $pad = max(array_map('strlen', array_keys($data_to_append))); 604 | foreach ($data_to_append as $tag_name => $tag_values) { 605 | foreach ($tag_values as $tag_value) { 606 | $result_lines[] = $line_prefix . sprintf('@%s %s', str_pad($tag_name, $pad), $tag_value) . "\n"; 607 | } 608 | } 609 | } 610 | } 611 | 612 | return $result_lines; 613 | } 614 | 615 | /** 616 | * Get regex pattern used to detect/extract tagged data. 617 | * 618 | * @param string $line_prefix 619 | * 620 | * @return string 621 | */ 622 | private function getTagPattern(?string $line_prefix = null): string { 623 | return '/^' 624 | . ($line_prefix !== null ? '(?:' . preg_quote($line_prefix, '/') .')?' : '') // may be prefixed by line prefix 625 | . '\s*' // may be prefixed by whitespace 626 | . '@(?[a-z]+)' // @tagname 627 | . '\s+' // space between tag and value 628 | . '(?.+)' // value 629 | . '$/i'; 630 | } 631 | 632 | /** 633 | * Unduplicate copyright/copyleft tags values. 634 | * 635 | * @param array $values 636 | * 637 | * @return array 638 | */ 639 | private function unduplicateCopyTag(array $values): array { 640 | $copy_dates_pattern = '/^' 641 | . '(?.+\s+)?' // capture everything before dates 642 | . '(?\d{4})' // mandatory date (unique year or starting year) 643 | . '(-(?\d{4}))?' // optionnal ending date with `-` separator 644 | . '(?\s+.+)?' // capture everything after dates 645 | . '$/'; 646 | 647 | $preserved_values = []; 648 | 649 | foreach ($values as $value) { 650 | $dates_matches = []; 651 | if (preg_match($copy_dates_pattern, $value, $dates_matches) !== 1) { 652 | continue; 653 | } 654 | 655 | $before = trim($dates_matches['before'] ?? ''); 656 | $before_pattern = strlen($before) > 0 657 | ? '\s*' . preg_quote($before, '/') . '\s+' 658 | : ''; 659 | $after = trim($dates_matches['after'] ?? ''); 660 | $after_pattern = strlen($after) > 0 661 | ? '\s+' . preg_quote($after, '/') . '\s*' 662 | : ''; 663 | 664 | $similar_pattern = '/^' 665 | . $before_pattern 666 | . '(?\d{4})(-(?\d{4}))?' 667 | . $after_pattern 668 | . '$/'; 669 | 670 | if (count(preg_grep($similar_pattern, $preserved_values)) > 0) { 671 | // similar value already computed 672 | continue; 673 | } 674 | 675 | $similar_values = preg_grep($similar_pattern, $values); 676 | 677 | if (count($similar_values) === 1) { 678 | // found only current value, no need to deduplicate 679 | $preserved_values[] = $value; 680 | continue; 681 | } 682 | 683 | // Compute min starting and max ending dates 684 | $starting_date = $dates_matches['starting_date']; 685 | $ending_date = !empty($dates_matches['ending_date']) ? $dates_matches['ending_date'] : $starting_date; 686 | foreach ($similar_values as $similar_value) { 687 | $similar_dates_matches = []; 688 | preg_match($copy_dates_pattern, $similar_value, $similar_dates_matches); 689 | if ($similar_dates_matches['starting_date'] < $starting_date) { 690 | $starting_date = $similar_dates_matches['starting_date']; 691 | } elseif ($similar_dates_matches['starting_date'] > $ending_date) { 692 | $ending_date = $similar_dates_matches['starting_date']; 693 | } 694 | if (!empty($similar_dates_matches['ending_date']) && $similar_dates_matches['ending_date'] > $ending_date) { 695 | $ending_date = $similar_dates_matches['ending_date']; 696 | } 697 | } 698 | $preserved_values[] = ($dates_matches['before'] ?? '') 699 | . $starting_date 700 | . ($ending_date !== $starting_date ? '-' . $ending_date : '') 701 | . ($dates_matches['after'] ?? ''); 702 | } 703 | 704 | return $preserved_values; 705 | } 706 | 707 | /** 708 | * Get files exclusion pattern. All files matching this pattern will be excluded from checks. 709 | * 710 | * @param string $directory 711 | * 712 | * @return string 713 | */ 714 | protected function getExclusionPattern(string $directory): ?string { 715 | $excluded_elements = [ 716 | '(\.|.*\/\.).+', // Any hidden file/directory 717 | 718 | '(docker-)?compose(\.override)?\.ya?ml', // docker compose configuration files 719 | 720 | 'node_modules', // npm imported libs 721 | 'vendor', // composer imported libs 722 | 723 | 'public\/lib', // libs packaged using webpack 724 | ]; 725 | if (file_exists($directory . DIRECTORY_SEPARATOR . 'setup.php') 726 | && file_exists($directory . DIRECTORY_SEPARATOR . 'hook.php')) { 727 | // Directory is a plugin root directory 728 | $excluded_elements = array_merge( 729 | $excluded_elements, 730 | [ 731 | 'lib', // Manually included libs 732 | 'dist', // Plugin archives 733 | ] 734 | ); 735 | } else if (file_exists($directory . DIRECTORY_SEPARATOR . 'composer.json') 736 | && preg_match('/"name"\s*:\s*"glpi\/glpi"/', file_get_contents($directory . DIRECTORY_SEPARATOR . 'composer.json'))) { 737 | // Directory is GLPI root directory 738 | $excluded_elements = array_merge( 739 | $excluded_elements, 740 | [ 741 | 'config', 742 | 'css\/lib', 743 | 'lib\/(?!(bundles|index\.php)).+', // Manually included libs, but do not exclude "bundles" subdir or "index.php" 744 | 'files', 745 | 'marketplace', 746 | 'plugins', 747 | 'tests\/config', 748 | 'tests\/config_db\.php', 749 | 'tests\/files', 750 | ] 751 | ); 752 | } 753 | 754 | if (empty($excluded_elements)) { 755 | return null; 756 | } 757 | 758 | return '/^' 759 | . preg_quote($directory . DIRECTORY_SEPARATOR, '/') 760 | . '(' . implode('|', $excluded_elements) . ')' 761 | . '$/'; 762 | } 763 | } 764 | -------------------------------------------------------------------------------- /src/PHPStan/Rules/GlobalVarTypeRule.php: -------------------------------------------------------------------------------- 1 | . 30 | * 31 | * --------------------------------------------------------------------- 32 | */ 33 | 34 | namespace GlpiProject\Tools\PHPStan\Rules; 35 | 36 | use PhpParser\Comment\Doc; 37 | use PhpParser\Node; 38 | use PhpParser\Node\Expr\Variable; 39 | use PhpParser\Node\Stmt; 40 | use PhpParser\Node\Stmt\Global_; 41 | use PHPStan\Analyser\Scope; 42 | use PHPStan\Rules\Rule; 43 | use PHPStan\Rules\RuleErrorBuilder; 44 | use PHPStan\Type\FileTypeMapper; 45 | 46 | class GlobalVarTypeRule implements Rule 47 | { 48 | private FileTypeMapper $fileTypeMapper; 49 | 50 | public function __construct( 51 | FileTypeMapper $fileTypeMapper 52 | ) { 53 | $this->fileTypeMapper = $fileTypeMapper; 54 | } 55 | 56 | public function getNodeType(): string 57 | { 58 | return Stmt::class; 59 | } 60 | 61 | public function processNode(Node $node, Scope $scope): array 62 | { 63 | if (!($node instanceof Global_)) { 64 | return []; 65 | } 66 | 67 | $variablesTypes = []; 68 | foreach ($node->vars as $var) { 69 | if (!$var instanceof Variable) { 70 | continue; 71 | } 72 | if (!is_string($var->name)) { 73 | continue; 74 | } 75 | 76 | $variablesTypes[$var->name] = null; 77 | } 78 | 79 | $function = $scope->getFunction(); 80 | foreach ($node->getComments() as $comment) { 81 | if (!$comment instanceof Doc) { 82 | continue; 83 | } 84 | $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( 85 | $scope->getFile(), 86 | $scope->isInClass() ? $scope->getClassReflection()->getName() : null, 87 | $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, 88 | $function !== null ? $function->getName() : null, 89 | $comment->getText(), 90 | ); 91 | foreach ($resolvedPhpDoc->getVarTags() as $key => $varTag) { 92 | if (array_key_exists($key, $variablesTypes)) { 93 | $variablesTypes[$key] = $varTag->getType()->toPhpDocNode(); 94 | } 95 | } 96 | } 97 | 98 | $errors = []; 99 | 100 | foreach ($variablesTypes as $variableName => $variableType) { 101 | if ($variableType === null) { 102 | $errors[] = RuleErrorBuilder::message( 103 | sprintf( 104 | 'Missing PHPDoc tag @var for global variable $%s', 105 | $variableName 106 | ) 107 | )->identifier('varTag.noType')->build(); 108 | } 109 | } 110 | 111 | return $errors; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /tools/plugin-release: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # 3 | # --------------------------------------------------------------------- 4 | # 5 | # GLPI tools 6 | # 7 | # @copyright 2017-2022 Teclib' and contributors. 8 | # @copyright 2012-2017 The Galette Team 9 | # @licence https://www.gnu.org/licenses/gpl-3.0.html 10 | # @link https://github.com/glpi-project/tools 11 | # 12 | # --------------------------------------------------------------------- 13 | # 14 | # LICENSE 15 | # 16 | # This file is part of GLPI tools. 17 | # 18 | # This program is free software: you can redistribute it and/or modify 19 | # it under the terms of the GNU General Public License as published by 20 | # the Free Software Foundation, either version 3 of the License, or 21 | # (at your option) any later version. 22 | # 23 | # This program is distributed in the hope that it will be useful, 24 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | # GNU General Public License for more details. 27 | # 28 | # You should have received a copy of the GNU General Public License 29 | # along with this program. If not, see . 30 | # 31 | # --------------------------------------------------------------------- 32 | # 33 | 34 | import os, sys, argparse, re, git, subprocess 35 | import tarfile, shutil, gitdb, time, json, fnmatch 36 | if sys.version_info[0] == 3: 37 | import urllib.request, urllib.error, urllib.parse 38 | else: 39 | import urllib2 40 | from datetime import datetime 41 | from termcolor import colored 42 | from lxml import etree 43 | 44 | plugin_dir = os.path.abspath(__file__).split(os.path.sep + 'vendor', 1)[0] 45 | dist_dir = os.path.join( 46 | plugin_dir, 47 | 'dist' 48 | ) 49 | verbose = False 50 | tagrefs = None 51 | force = False 52 | commit = None 53 | extra = None 54 | sign = True 55 | github = True 56 | assume_yes = False 57 | banned = [ 58 | '.git*', 59 | '.gh_token', 60 | '.tx/', 61 | 'tools/', 62 | 'tests/', 63 | '.atoum.php', 64 | '.travis.yml', 65 | '.circleci/', 66 | '.ignore-release', 67 | ] 68 | gh_orga = 'pluginsGLPI' 69 | script_version = '1.0.5' 70 | 71 | def print_err(msg): 72 | """ 73 | Display colored error message 74 | """ 75 | print(colored(msg, 'red', attrs=['bold'])) 76 | 77 | def get_numeric_version(ver): 78 | """ 79 | Returns all numeric version 80 | """ 81 | return re.findall(r'\d+', ver) 82 | 83 | def valid_version(ver): 84 | """ 85 | Check if provided version is valid. 86 | 87 | Takes all digits in passed version, then reassemble them with dots 88 | to check if it is the same as original given one. 89 | """ 90 | return '.'.join(get_numeric_version(ver)) == ver 91 | 92 | def incr_version(ver): 93 | """ 94 | Increment version number 95 | """ 96 | version = get_numeric_version(ver) 97 | version[-1] = str(int(version[-1]) + 1) 98 | return version 99 | 100 | def propose_version(): 101 | """ 102 | Propose new minor and major versions, 103 | according to existing git tags 104 | """ 105 | last_major = '0' 106 | last_minor = '0' 107 | 108 | tagrefs.sort(key=lambda tagref: list(map(int, tagref.name.split('.'))) if valid_version(tagref.name) else [0]) 109 | last_minor = tagrefs[-1].name 110 | 111 | for tagref in tagrefs: 112 | if valid_version(tagref.name): 113 | #last major version 114 | if int(tagref.name.split('.')[-1]) == 0: 115 | last_major = tagref.name.replace('.0', '') 116 | 117 | if verbose: 118 | print('last minor: %s | last major %s' % (last_minor, last_major)) 119 | 120 | #no version provided. propose one 121 | new_minor = None 122 | new_major = None 123 | 124 | if int(last_minor.split('.')[-1]) == 0: 125 | #if the latest is a major version 126 | new_minor = last_minor + ('.1') 127 | else: 128 | new_minor = '.'.join(incr_version(last_minor)) 129 | 130 | new_major = '.'.join(incr_version(last_major)) + '.0' 131 | 132 | print("""Proposed versions: 133 | minor: %s 134 | major: %s 135 | """ % (new_minor, new_major)) 136 | 137 | def get_latest_version(): 138 | """ 139 | Look for latest version 140 | """ 141 | tagrefs.sort(key=lambda tagref: list(map(int, tagref.name.split('.'))) if valid_version(tagref.name) else [0]) 142 | last = tagrefs[-1] 143 | return last 144 | 145 | def is_existing_version(ver): 146 | """ 147 | Look specified version exists 148 | """ 149 | for tagref in tagrefs: 150 | if tagref.name == ver: 151 | return True 152 | return False 153 | 154 | def ask_user_confirm(msg): 155 | """ 156 | Ask user his confirmation 157 | """ 158 | if assume_yes: 159 | return True 160 | else: 161 | while True: 162 | sys.stdout.write(msg) 163 | if sys.version_info[0] == 3: 164 | choice = input().lower() 165 | else: 166 | choice = raw_input().lower() 167 | if choice == 'y' or choice == 'yes': 168 | return True 169 | elif choice == 'n' or choice == 'no': 170 | return False 171 | else: 172 | print_err( 173 | "Invalid input. Please enter 'yes' or 'no' (or 'y' or 'n')." 174 | ) 175 | 176 | def get_rel_name(buildver): 177 | """ 178 | Build archive name from command line parameters 179 | That would be used for git archiving prefix and archive name 180 | """ 181 | archive_name = None 182 | 183 | if commit and extra: 184 | now = datetime.now() 185 | archive_name = 'glpi-%s-%s-%s-%s-%s' % ( 186 | plugin_name, 187 | buildver, 188 | extra, 189 | now.strftime('%Y%m%d'), 190 | commit 191 | ) 192 | else: 193 | archive_name = 'glpi-%s-%s' % (plugin_name, buildver) 194 | 195 | return archive_name 196 | 197 | def _do_build(repo, ver): 198 | """ 199 | Proceed build 200 | """ 201 | exists = False 202 | ascexists = False 203 | rel_name = get_rel_name(ver) 204 | archive_name = rel_name + '.tar.bz2' 205 | archive = os.path.join( 206 | dist_dir, 207 | archive_name 208 | ) 209 | 210 | if not force: 211 | #first check if a version 212 | local = False 213 | ascLocal = False 214 | 215 | #check if a release exists upstream 216 | #FIXME: this retrieve only publicated release, not drafts 217 | url = 'https://api.github.com/repos/%s/%s/releases/tags/%s' % (gh_orga, plugin_name, ver) 218 | 219 | exists = False 220 | gh_id = None 221 | 222 | if github: 223 | if sys.version_info[0] == 3: 224 | try: 225 | request = urllib.request.Request(url) 226 | handle = urllib.request.urlopen(request) 227 | contents = json.loads(handle.read()) 228 | 229 | for asset in contents['assets']: 230 | if archive_name == asset['name']: 231 | exists = True 232 | gh_id = contents['id'] 233 | break 234 | except (urllib.error.URLError, urllib.error.HTTPError): 235 | pass 236 | else: 237 | try: 238 | request = urllib2.Request(url) 239 | handle = urllib2.urlopen(request) 240 | contents = json.loads(handle.read()) 241 | 242 | for asset in contents['assets']: 243 | if archive_name == asset['name']: 244 | exists = True 245 | gh_id = contents['id'] 246 | break 247 | except (urllib2.URLError, urllib2.HTTPError): 248 | pass 249 | 250 | 251 | if exists: 252 | #we know a release exists for this tag. Check if files have been uploaded yet 253 | pass 254 | 255 | if not exists: 256 | #also check from local repo 257 | exists = os.path.exists(archive) 258 | if exists: 259 | local = True 260 | 261 | #also check from local repo 262 | ascexists = os.path.exists( 263 | os.path.join( 264 | dist_dir, 265 | archive_name + '.asc' 266 | ) 267 | ) 268 | 269 | if exists or ascexists: 270 | msg = '' 271 | if exists: 272 | loctxt = '' 273 | if local: 274 | loctxt = 'locally ' 275 | msg = 'Release %s already %sexists' % (rel_name, loctxt) 276 | 277 | if ascexists: 278 | loctxt = '' 279 | if ascLocal: 280 | loctxt = ' locally' 281 | if msg != '': 282 | msg += ' and has been %ssigned!' % loctxt 283 | else: 284 | msg += 'Release has been %ssigned!' % loctxt 285 | 286 | msg += '\n\nYou will *NOT* build another one :)' 287 | print_err(msg) 288 | else: 289 | print('Building %s...' % rel_name) 290 | 291 | typestr = 'Tag' 292 | typever = ver 293 | 294 | if commit and extra: 295 | typestr = 'Commit' 296 | typever = commit 297 | 298 | if verbose: 299 | print('Release name: %s, %s: %s, Dest: %s' % ( 300 | rel_name, 301 | typestr, 302 | typever, 303 | archive 304 | )) 305 | 306 | ls_files = subprocess.check_output(['git', 'ls-tree', '-r', str(typever), '--name-only']) 307 | if sys.version_info[0] == 3: 308 | ls_files = ls_files.decode() 309 | 310 | paths = [] 311 | 312 | if os.path.exists(os.path.join(plugin_dir, '.ignore-release')): 313 | f = open(os.path.join(plugin_dir, '.ignore-release'),'r') 314 | for line in f: 315 | if line.strip() != '': 316 | banned.append(line.strip()) 317 | 318 | for ls_file in ls_files.split('\n'): 319 | if ls_file != '': 320 | append = True 321 | for ban in banned: 322 | if re.match(ban, ls_file): 323 | append = False 324 | break 325 | if append: 326 | paths.append(ls_file) 327 | 328 | archive_cmd_pattern = 'git archive --prefix=%s/ %s %s | bzip2 > %s' 329 | if commit and extra: 330 | print('Archiving GIT commit %s' % commit) 331 | archive_cmd = archive_cmd_pattern % ( 332 | plugin_name+'/', 333 | commit, 334 | ' '.join(paths), 335 | archive 336 | ) 337 | else: 338 | print('Archiving GIT tag %s' % ver) 339 | archive_cmd = archive_cmd_pattern % ( 340 | plugin_name+'/', 341 | ver, 342 | ' '.join(paths), 343 | archive 344 | ) 345 | 346 | res = subprocess.check_call(archive_cmd, shell=True) 347 | if res > 0: 348 | print_err('Archiving has failed!') 349 | else: 350 | prepare(plugin_name, archive) 351 | 352 | if sign: 353 | do_sign(archive) 354 | 355 | if github: 356 | create_gh_release(archive, gh_id, plugin_name, ver) 357 | 358 | def do_sign(archive): 359 | sign_cmd = 'gpg --no-use-agent --detach-sign --armor %s' % archive 360 | p1 = subprocess.Popen(sign_cmd, shell=True) 361 | p1.communicate() 362 | 363 | def create_gh_release(archive, gh_id, plugin_name, ver): 364 | with open(gh_cred_file, 'r') as fd: 365 | token = fd.readline().strip() 366 | 367 | gh = github.Github(token) 368 | gh_user = gh.get_user() 369 | 370 | for gh_repo in gh_user.get_repos(): 371 | if gh_repo.full_name == '%s/%s' % (gh_orga, plugin_name): 372 | break 373 | 374 | gh_release = None 375 | 376 | #check in all releases (including drafts) if nothing has been found yet 377 | if gh_id is None: 378 | for gh_rel in gh_repo.get_releases(): 379 | if gh_rel.tag_name == ver: 380 | gh_release = gh_rel 381 | break 382 | 383 | #create release if it does not exists 384 | if gh_id is None and gh_release is None: 385 | is_prerelease = True if commit else False 386 | #TODO: retrieve ChangeLog from MD file if exists, and add it to gh release 387 | gh_release = gh_repo.create_git_release( 388 | str(ver), 389 | 'GLPI %s %s' % (plugin_name, ver), 390 | 'Automated release from release script', 391 | True, 392 | is_prerelease 393 | ) 394 | elif gh_id is not None: 395 | gh_release = gh_repo.get_release(gh_id) 396 | 397 | #upload = ask_user_confirm( 398 | # 'Do you want to upload archive %s? [yes/No] ' % archive 399 | #) 400 | 401 | #if upload: 402 | # do_upload(archive, gh_id, plugin_name, ver) 403 | 404 | #def do_upload(archive, gh_id, plugin_name, ver): 405 | #from uritemplate import URITemplate 406 | #import requests 407 | #import mimetypes 408 | 409 | #Upload asset 410 | #template = URITemplate(gh_release.upload_url) 411 | 412 | #headers = {'Content-Type': 'application/octet-stream', 'Authorization': 'token %s' % token} 413 | #params = {'name': '%s-%s.tar.bz2' % (plugin_name, ver)} 414 | #url = template.expand(params) 415 | 416 | ## Bad request :'( 417 | #f = open('/var/www/webapps/glpi/plugins/order/dist/glpi-order-1.9.5.tar.bz2', 'rb') 418 | #r = requests.post( 419 | # url, 420 | # data=f, 421 | # headers=headers 422 | #) 423 | #print r.json() 424 | #r.raise_for_status() 425 | 426 | def cmd_exists(cmd): 427 | try: 428 | subprocess.check_call(cmd, 429 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) 430 | return True 431 | except subprocess.CalledProcessError as e: 432 | return False 433 | except OSError as e: 434 | return False 435 | 436 | def prepare(rel_name, archive): 437 | """ 438 | Add external libraries to the archive, if any 439 | """ 440 | 441 | plugin = tarfile.open(archive, 'r') 442 | src_dir = os.path.join(dist_dir, 'src') 443 | if not os.path.exists(src_dir): 444 | os.makedirs(src_dir) 445 | plugin.extractall(path=src_dir) 446 | plugin.close() 447 | 448 | build_dir = os.path.join(src_dir, plugin_name) 449 | 450 | print('Adding vendor libraries') 451 | if os.path.exists(os.path.join(build_dir, 'composer.json')): 452 | composer_cmd = 'composer' 453 | if not cmd_exists([composer_cmd, '--version']): 454 | composer_cmd = 'composer.bat' 455 | if not cmd_exists([composer_cmd, '--version']): 456 | print_err('composer not found!') 457 | sys.exit(1) 458 | 459 | composer = [composer_cmd, 'install', '-o', '--no-dev'] 460 | 461 | if not verbose: 462 | composer.insert(-1, '-q') 463 | 464 | p1 = subprocess.Popen( 465 | composer, 466 | cwd=build_dir 467 | ) 468 | p1.communicate() 469 | 470 | #cleanup vendors 471 | for root, dirnames, filenames in os.walk(os.path.join(build_dir, 'vendor')): 472 | #remove git directories 473 | for dirname in fnmatch.filter(dirnames, '.git*'): 474 | remove_dir = os.path.join(build_dir, root, dirname) 475 | shutil.rmtree(remove_dir) 476 | #remove test directories 477 | for dirname in fnmatch.filter(dirnames, 'test?'): 478 | remove_dir = os.path.join(build_dir, root, dirname) 479 | shutil.rmtree(remove_dir) 480 | #remove examples directories 481 | for dirname in fnmatch.filter(dirnames, 'example?'): 482 | remove_dir = os.path.join(build_dir, root, dirname) 483 | shutil.rmtree(remove_dir) 484 | #remove doc directories 485 | for dirname in fnmatch.filter(dirnames, 'doc?'): 486 | remove_dir = os.path.join(build_dir, root, dirname) 487 | shutil.rmtree(remove_dir) 488 | #remove composer stuff 489 | for filename in fnmatch.filter(filenames, 'composer*'): 490 | remove_file = os.path.join(build_dir, root, filename) 491 | os.remove(remove_file) 492 | 493 | p2 = subprocess.Popen( 494 | [composer_cmd, 'dump-autoload', '-o', '--no-dev'], 495 | cwd=build_dir 496 | ) 497 | p2.communicate() 498 | 499 | if os.path.exists(os.path.join(build_dir, 'composer.lock')): 500 | os.remove(os.path.join(build_dir, 'composer.lock')) 501 | 502 | if os.path.exists(os.path.join(build_dir, 'package.json')): 503 | if not cmd_exists(['npm', '--version']): 504 | print_err('npm not found!') 505 | sys.exit(1) 506 | 507 | npm = ['npm', 'install'] 508 | 509 | p = subprocess.Popen( 510 | npm, 511 | cwd=build_dir 512 | ) 513 | p.communicate() 514 | 515 | # assume that npm install triggers build of required libs on postinstall event (using webpack for instance) 516 | # node_modules directory should not be packages into plugin 517 | shutil.rmtree(os.path.join(build_dir, 'node_modules')) 518 | 519 | if os.path.exists(os.path.join(build_dir, 'package-lock.json')): 520 | os.remove(os.path.join(build_dir, 'package-lock.json')) 521 | 522 | compile_mo(build_dir) 523 | 524 | plugin = tarfile.open(archive, 'w|bz2') 525 | 526 | for i in os.listdir(src_dir): 527 | plugin.add( 528 | os.path.join(src_dir, i), 529 | arcname=rel_name 530 | ) 531 | 532 | plugin.close() 533 | shutil.rmtree(src_dir) 534 | 535 | def compile_mo(build_dir): 536 | locales_dir = os.path.join(build_dir, 'locales') 537 | if verbose: 538 | print('Locales dir: %s' % locales_dir) 539 | if os.path.exists(locales_dir): 540 | for file in os.listdir(locales_dir): 541 | if file.endswith('.po'): 542 | if verbose: 543 | print('Compiling %s...' % file) 544 | if not cmd_exists(['msgfmt', '--version']): 545 | print_err('msgfmt not found!') 546 | sys.exit(1) 547 | p1 = subprocess.Popen( 548 | ['msgfmt', file, '-o', file.replace('.po', '.mo')], 549 | cwd=locales_dir 550 | ) 551 | p1.communicate() 552 | 553 | def valid_commit(repo, c): 554 | """ 555 | Validate commit existance in repository 556 | """ 557 | global commit 558 | 559 | try: 560 | dformat = '%a, %d %b %Y %H:%M' 561 | repo_commit = repo.commit(c) 562 | 563 | commit = repo_commit.hexsha[:10] 564 | print(colored("""Commit informations: 565 | Hash: %s 566 | Author: %s 567 | Authored date: %s 568 | Commiter: %s 569 | Commit date: %s 570 | Message: %s""" % ( 571 | commit, 572 | repo_commit.author, 573 | time.strftime(dformat, time.gmtime(repo_commit.authored_date)), 574 | repo_commit.committer, 575 | time.strftime(dformat, time.gmtime(repo_commit.committed_date)), 576 | repo_commit.message 577 | ), None, 'on_grey', attrs=['bold']).encode('utf-8')) 578 | return True 579 | except gitdb.exc.BadObject: 580 | return False 581 | 582 | def guess_plugin_name(): 583 | """ 584 | Tries to guess plugin name, from version constant, take directory name at last 585 | """ 586 | name = None 587 | 588 | filename = os.path.join(plugin_dir, 'setup.php') 589 | 590 | #try to get configured plugin name 591 | with open(filename) as input: 592 | for count, line in enumerate(input): 593 | regexp = ".*('|\")PLUGIN_(.+)_VERSION('|\"), ('|\")(.+)('|\")" 594 | results = re.match(regexp, line) 595 | if results: 596 | name = results.group(2) 597 | break 598 | 599 | if name is None: 600 | #No configured name found. Let's use current directory name 601 | name = os.path.split(plugin_dir)[-1] 602 | 603 | return name.lower() 604 | 605 | def check_version(buildver): 606 | if verbose: 607 | print('Checking for version %s' % buildver) 608 | 609 | filename = os.path.join( 610 | plugin_dir, 611 | 'setup.php' 612 | ) 613 | 614 | found = None 615 | #find version constant 616 | if os.path.exists(filename): 617 | with open(filename, 'r') as input: 618 | for count, line in enumerate(input): 619 | regexp = ".*('|\")PLUGIN_%s_VERSION('|\"), ('|\")(.+)('|\")" % plugin_name.upper() 620 | results = re.match(regexp, line) 621 | if results: 622 | found = results.group(4) 623 | break 624 | 625 | if not (str(found) == str(buildver)): 626 | print_err('Plugin version check has failed (%s but %s found)!' % (buildver, found)) 627 | return False 628 | 629 | #check plugins website XML file 630 | xmlfile = os.path.join(plugin_dir, '%s.xml' % plugin_name) 631 | if not os.path.exists(xmlfile): 632 | xmlfile = os.path.join(plugin_dir, 'plugin.xml') 633 | if not os.path.exists(xmlfile): 634 | xmlfile = None 635 | 636 | if xmlfile != None: 637 | if verbose: 638 | print('XML file found in %s' % xmlfile) 639 | try: 640 | xmldoc = etree.parse(xmlfile) 641 | for version in xmldoc.getiterator('num'): 642 | if str(version.text) == str(buildver): 643 | if verbose: 644 | print('%s found in the XML file!' % buildver) 645 | return True 646 | print_err('%s *NOT* found in the XML file %s' % (buildver, xmlfile)) 647 | except etree.XMLSyntaxError as err: 648 | print_err('%s is *NOT* XML valid!' % (xmlfile)) 649 | if verbose: 650 | print(format(err)) 651 | return False 652 | else: 653 | print_err('Plugins website configuration file has not been found!') 654 | return False 655 | 656 | def main(): 657 | """ 658 | Main method 659 | """ 660 | global verbose, tagrefs, force, extra, assume_yes, sign, plugin_name, github, gh_cred_file 661 | 662 | parser = argparse.ArgumentParser(description='GLPI plugins release script') 663 | parser.add_argument('--version', action='version', version=script_version) 664 | group = parser.add_mutually_exclusive_group() 665 | group.add_argument( 666 | '-r', 667 | '--release', 668 | help='Version to release' 669 | ) 670 | parser.add_argument( 671 | '-g', 672 | '--nogithub', 673 | help="DO NOT Create github draft release", 674 | action='store_false' 675 | ) 676 | parser.add_argument( 677 | '-C', 678 | '--check-only', 679 | help="Only do chec, does not release anything", 680 | action='store_true' 681 | ) 682 | parser.add_argument( 683 | '-d', 684 | '--dont-check', 685 | help="DO NOT check version (usefull to built archive for commit in a ci)", 686 | action='store_true' 687 | ) 688 | group.add_argument( 689 | '-p', 690 | '--propose', 691 | help='Calculate and propose next possible versions', 692 | action='store_true' 693 | ) 694 | parser.add_argument( 695 | '-c', 696 | '--commit', 697 | help='Specify commit to archive (-r required)' 698 | ) 699 | parser.add_argument( 700 | '-e', 701 | '--extra', 702 | help='Extra version informations (-c required)' 703 | ) 704 | parser.add_argument( 705 | '-m', 706 | '--compile-mo', 707 | help="Compile MO files from PO files (exclusive)", 708 | action='store_true' 709 | ) 710 | parser.add_argument( 711 | '-S', 712 | '--nosign', 713 | help="Do not sign release tarball", 714 | action="store_false" 715 | ) 716 | parser.add_argument( 717 | '-Y', 718 | '--assume-yes', 719 | help='Assume YES to all questions. Be sure to understand what you are doing!', 720 | action='store_true' 721 | ) 722 | parser.add_argument( 723 | '-V', 724 | '--verbose', 725 | help='Be more verbose', 726 | action="store_true" 727 | ) 728 | parser.add_argument('-f', action='store_true') 729 | args = parser.parse_args() 730 | 731 | verbose=args.verbose 732 | sign=args.nosign 733 | github=args.nogithub 734 | 735 | if verbose: 736 | print(args) 737 | 738 | if not args.compile_mo and not args.propose: 739 | if github: 740 | import github 741 | gh_cred_file = os.path.join(plugin_dir, '.gh_token') 742 | if not os.path.exists(gh_cred_file): 743 | print_err('GitHub credential file does not exists! Either create it or use the --nogithub option.') 744 | sys.exit(1) 745 | 746 | plugin_name = guess_plugin_name() 747 | 748 | repo = git.Repo(plugin_dir) 749 | tagrefs = repo.tags 750 | 751 | if args.f == True: 752 | force = ask_user_confirm( 753 | 'Are you *REALLY* sure you mean -f when you typed -f? [yes/No] ' 754 | ) 755 | assume_yes=args.assume_yes 756 | 757 | if args.check_only: 758 | print('*** Entering *check-only* mode ***') 759 | 760 | #check if dist_dir exists 761 | if not os.path.exists(dist_dir): 762 | os.makedirs(dist_dir) 763 | 764 | build = False 765 | buildver = None 766 | if args.compile_mo: 767 | compile_mo(plugin_dir) 768 | elif (args.extra or args.commit) and (not args.extra or not args.commit or not args.release): 769 | print_err('You have to specify --version --commit and --extra all together') 770 | sys.exit(1) 771 | elif args.commit and args.release and args.extra: 772 | if valid_commit(repo, args.commit): 773 | if verbose: 774 | print('Commit is valid') 775 | build = True 776 | buildver = args.release 777 | extra = args.extra 778 | else: 779 | print_err('Invalid commit ref %s' % args.commit) 780 | elif args.release: 781 | if not args.dont_check and not valid_version(args.release): 782 | print_err('%s is not a valid version number!' % args.release) 783 | sys.exit(1) 784 | else: 785 | #check if specified version exists 786 | if not is_existing_version(args.release): 787 | print_err('%s does not exist!' % args.release) 788 | sys.exit(1) 789 | else: 790 | build = True 791 | buildver = args.release 792 | elif args.propose: 793 | propose_version() 794 | else: 795 | buildver = get_latest_version() 796 | if force: 797 | build = True 798 | else: 799 | build = ask_user_confirm( 800 | 'Do you want to build version %s? [Yes/no] ' % buildver 801 | ) 802 | 803 | if build: 804 | if args.dont_check or (check_version(buildver) and args.check_only == False): 805 | _do_build(repo, buildver) 806 | 807 | if __name__ == "__main__": 808 | main() 809 | --------------------------------------------------------------------------------