|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var path = require( 'path' ); |
| 24 | +var logger = require( 'debug' ); |
| 25 | +var readFile = require( '@stdlib/fs/read-file' ).sync; |
| 26 | +var writeFile = require( '@stdlib/fs/write-file' ).sync; |
| 27 | +var uppercase = require( '@stdlib/string/uppercase' ); |
| 28 | +var currentYear = require( '@stdlib/time/current-year' ); |
| 29 | +var replace = require( '@stdlib/string/replace' ); |
| 30 | +var format = require( '@stdlib/string/format' ); |
| 31 | + |
| 32 | + |
| 33 | +// VARIABLES // |
| 34 | + |
| 35 | +var FOPTS = { |
| 36 | + 'encoding': 'utf8' |
| 37 | +}; |
| 38 | + |
| 39 | +// Logger: |
| 40 | +var debug = logger( 'script:ndarray-unary-loops:create-header-files' ); |
| 41 | + |
| 42 | +// Templates: |
| 43 | +var TMPL_HEADER = readFile( path.resolve( __dirname, '..', 'templates', 'header.txt' ), FOPTS ); |
| 44 | + |
| 45 | +// Output directory: |
| 46 | +var INCLUDE_DIR = path.resolve( __dirname, '..', '..', 'include', 'stdlib', 'ndarray', 'base', 'unary' ); |
| 47 | + |
| 48 | +// Get the current year: |
| 49 | +var CURRENT_YEAR = currentYear().toString(); |
| 50 | + |
| 51 | +// Specify the copyright holder: |
| 52 | +var COPYRIGHT = 'The Stdlib Authors'; |
| 53 | + |
| 54 | + |
| 55 | +// FUNCTIONS // |
| 56 | + |
| 57 | +/** |
| 58 | +* Creates a header file for a provided loop signature. |
| 59 | +* |
| 60 | +* @private |
| 61 | +* @param {string} signature - loop signature |
| 62 | +* @throws {Error} unexpected error |
| 63 | +*/ |
| 64 | +function createHeaderFile( signature ) { |
| 65 | + var fpath; |
| 66 | + var file; |
| 67 | + var err; |
| 68 | + |
| 69 | + file = replace( TMPL_HEADER, '{{YEAR}}', CURRENT_YEAR ); |
| 70 | + file = replace( file, '{{COPYRIGHT}}', COPYRIGHT ); |
| 71 | + file = replace( file, '{{INCLUDE_GUARD}}', uppercase( signature ) ); |
| 72 | + file = replace( file, '{{SIGNATURE}}', signature ); |
| 73 | + |
| 74 | + fpath = path.join( INCLUDE_DIR, format( '%s.h', signature ) ); |
| 75 | + |
| 76 | + debug( 'Creating header file: %s', fpath ); |
| 77 | + err = writeFile( fpath, file, FOPTS ); |
| 78 | + if ( err ) { |
| 79 | + throw err; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +// MAIN // |
| 85 | + |
| 86 | +/** |
| 87 | +* Creates header files for a list of loop signatures. |
| 88 | +* |
| 89 | +* @private |
| 90 | +* @param {Array<string>} signatures - list of loop signatures |
| 91 | +*/ |
| 92 | +function createHeaderFiles( signatures ) { |
| 93 | + var i; |
| 94 | + for ( i = 0; i < signatures.length; i++ ) { |
| 95 | + createHeaderFile( signatures[ i ] ); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +// EXPORTS // |
| 101 | + |
| 102 | +module.exports = createHeaderFiles; |
0 commit comments