Gulp function to print out less verbose console log message? - gulp

I'm new to Gulp (or Grunt) tools. I created a function to print out the version value in my package.json file, like this
const {dest, parallel, series, src } = require('gulp');
const semver = require('semver');
const package_json = path.join(path.dirname(__filename),'package.json');
const pkg = JSON.parse(fs.readFileSync(package_json));
const version = semver.parse(pkg.version);
function get_version(cb) {
console.log(version.version);
cb();
}
exports.get_version = get_version
and I get
$ gulp get_version -f gulpfile-version.js
[16:07:08] Using gulpfile ~/some_path/gulpfile-version.js
[16:07:08] Starting 'get_version'...
1.4.6
[16:07:08] Finished 'get_version' after 1.91 ms
How can I modify my function to be less verbose and only print out the 1.4.6 string, and not the other time-stamped lines?

You can use --silent flag in your command to suppress the gulp logs.
Refer the doc
https://github.com/gulpjs/gulp/blob/master/docs/CLI.md
Your example
$ gulp get_version -f gulpfile-version.js --silent

Related

TypeError: JSON5.parse is not a function

I have installed JSON5 by npm install -d -g json5 and trying to parse the json string. But it is showing me [[object Object]]JSON5.parse is not a function. What the problem actually here is? and how we can resolve this? I am using the following code-
const JSON5 = require("json5")
var jsonV = JSON5.parse(fileStr)
Thanks!

write all parameters jenkins in JSON file using ${params}

I'm a beginner with shell scripting and i have some issues while a jenkins job parametrized. I want to write all parameters of jenkins job pipeline build with parameters into a JSON file using ${params}!
In my case i have 4 parameters(apis:multi-select,name:string,version:single-select and status:Boolean), there is the script Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
script{
sh "./test.sh ${params}"
}
}
}
}
}
Content of test.sh
#!/bin/bash
echo $# > file.json
The output in jenkins
+ ./test.sh [apis:dev,qa,prod, name:CC, version:g3, status:true]
Result in file.json
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
My question is how format the output to obtain a clean result in file.json ? please i need help.
Add this to the top of your script:
import groovy.json.JsonOutput
Then use this line instead of sh "./test.sh ${params}":
writeFile file: 'params.json', text: JsonOutput.toJson(params)
This uses a Groovy library and a native Jenkins method for writing files, which means you don't need to use the sh method.

Octave: how to specify arguments for mkoctfile

Im am using octave under windows (native) and try to compile a c++ program into a mex file and link some libraries to it:
% compile for octave
cmd = sprintf("mex main.cpp -I\"%s\\Winnt\\Include\" -L\"%s\\Winnt\\lib_x64\\msc\" -lvisa64.lib", ...
getenv('VXIPNPPATH'), getenv('VXIPNPPATH'))
eval(cmd);
When run, the output of the command is:
>> mex main.cpp -I'C:\Program Files (x86)\IVI Foundation\VISA\\Winnt\Include' -L'C:\Program Files (x86)\IVI Foundation\VISA\\Winnt\lib_x64\msc' -lvisa64.lib
g++: error: Files: No such file or directory
g++: error: (x86)\IVI: No such file or directory
g++: error: Foundation\VISA\\Winnt\lib_x64\msc: No such file or directory
warning: mkoctfile: building exited with failure status
I also tried to run the string directly from the command line:
mex main.cpp -I'C:\Program Files (x86)\IVI Foundation\VISA\\Winnt\Include' -L'C:\Program Files (x86)\IVI Foundation\VISA\\Winnt\lib_x64\msc' -lvisa64.lib
with the same result.
While the -I command appears to work well, why does the -L argument causes problems? What would be the right way to escape the path names with spaces?
Double quotes also won't work.
EDIT
Based on the answers, I am using mex() in its functional form, but the result is still the same:
vxipath = getenv('VXIPNPPATH');
params={};
params{1} = sprintf('-I%s', fullfile(vxipath, 'Winnt', 'Include'));
params{2} = sprintf('-L%s', fullfile(vxipath, 'Winnt', 'lib_x64', 'msc'));
params{3} = sprintf('-lvisa64.lib');
% replace \ with /
for i1=1:length(params)
s = params{i1};
s(s=='\') = '/';
params{i1} = s;
end
params
mex("main.cpp", params{:});
Gives the output:
params =
{
[1,1] = -IC:/Program Files (x86)/IVI Foundation/VISA/Winnt/Include
[1,2] = -LC:/Program Files (x86)/IVI Foundation/VISA/Winnt/lib_x64/msc
[1,3] = -lvisa64.lib
}
g++: error: Files: No such file or directory
g++: error: (x86)/IVI: No such file or directory
g++: error: Foundation/VISA/Winnt/lib_x64/msc: No such file or directory
warning: mkoctfile: building exited with failure status
Which is the same result as before. Additional observations are:
'/' or '\' does not make a difference
if I omit all parameters, I get a missing-include-file-error: OK
if I omit the '-L' argument, I get a missing-lib-file-error: OK
if I add the '-L' argument, I get the error shown above: It appears that the -L argument behaves differently than the -I argument.
I also tried it directly from the bash shell with the corresponding command with the same result.
Replace backslashes with slashes and place each argument inside single quotes.
mex 'main.cpp' '-IC:/Program Files (x86)/IVI Foundation/VISA//Winnt/Include' '-LC:/Program Files (x86)/IVI Foundation/VISA//Winnt/lib_x64/msc' '-lvisa64.lib'
or
mex ('main.cpp', '-IC:/Program Files (x86)/IVI Foundation/VISA//Winnt/Include', '-LC:/Program Files (x86)/IVI Foundation/VISA//Winnt/lib_x64/msc', '-lvisa64.lib')
This doesn't answer the how to fix it, as rahnema1 already did that. But I'll show you how to simplify your code.
Do not use eval. eval is evil.
Instead of evaluating a string function paramA paramB, call function directly with string input arguments. function paramA paramB is translated by the interpreter to a call function('paramA','paramB'). But it is a lot easier to generate the latter form, and you get to avoid eval to boot:
params = {};
params{1} = '-IC:/Program Files (x86)/IVI Foundation/VISA//Winnt/Include';
params{2} = '-LC:/Program Files (x86)/IVI Foundation/VISA//Winnt/lib_x64/msc';
params{2} = '-lvisa64.lib';
mex('main.cpp', params{:});
Properly generate paths using fullfile. It adds / or \ depending on which platform you're on, plus I find it easier to read:
include_path = fullfile(getenv('VXIPNPPATH'), 'Winnt', 'Include');
params{1} = ['-I', include_path];
mkoctfile does not escape the arguments properly if they contain spaces and it does not like backslashes in Octave's own paths.
It creates the following two commands:
g++ -c -I/release/mxe-octave-w64/usr/x86_64-w64-mingw32/include -IC:\Octave\OCTAVE~1.0\\mingw64\include\octave-5.1.0\octave\.. -IC:\Octave\OCTAVE~1.0\\mingw64\include\octave-5.1.0\octave -IC:\Octave\OCTAVE~1.0\\mingw64\include -fopenmp -g -O2 -I. "-IC:\Program Files (x86)\IVI Foundation\VISA\Winnt\Include" -DMEX_DEBUG main.cpp -o C:\Octave\OCTAVE~1.0\tmp/oct-u4r15I.o
g++ -IC:\Octave\OCTAVE~1.0\\mingw64\include\octave-5.1.0\octave\.. -IC:\Octave\OCTAVE~1.0\\mingw64\include\octave-5.1.0\octave -IC:\Octave\OCTAVE~1.0\\mingw64\include -fopenmp -g -O2 -shared -Wl,-rpath-link,/release/mxe-octave-w64/usr/x86_64-w64-mingw32/lib -L/release/mxe-octave-w64/usr/x86_64-w64-mingw32/lib -L/release/mxe-octave-w64/usr/x86_64-w64-mingw32/qt5/lib -Wl,--export-all-symbols -o main.mex C:\Octave\OCTAVE~1.0\tmp/oct-u4r15I.o -lvisa64.lib -LC:\Program Files (x86)\IVI Foundation\VISA\Winnt\lib_x64\msc -LC:\Octave\OCTAVE~1.0\\mingw64\lib\octave\5.1.0 -LC:\Octave\OCTAVE~1.0\\mingw64\lib -LC:\Octave\OCTAVE~1.0\\mingw64\lib\octave\5.1.0 -loctinterp -loctave -Wl,-rpath-link,/release/mxe-octave-w64/usr/x86_64-w64-mingw32/lib -L/release/mxe-octave-w64/usr/x86_64-w64-mingw32/lib -L/release/mxe-octave-w64/usr/x86_64-w64-mingw32/qt5/lib -Wl,--export-all-symbols
When I change it to the following:
replace \ with /
specify the library name without .lib extension
escape -LC:\Program Files... to "-LC:\Program Files..."
g++ -c -I/release/mxe-octave-w64/usr/x86_64-w64-mingw32/include -IC:/Octave/OCTAVE~1.0//mingw64/include/octave-5.1.0/octave/.. -IC:/Octave/OCTAVE~1.0//mingw64/include/octave-5.1.0/octave -IC:/Octave/OCTAVE~1.0//mingw64/include -fopenmp -g -O2 -I. "-IC:/Program Files (x86)/IVI Foundation/VISA/Winnt/Include" -DMEX_DEBUG main.cpp -o C:/Octave/OCTAVE~1.0/tmp/oct-u4r15I.o
g++ -IC:/Octave/OCTAVE~1.0//mingw64/include/octave-5.1.0/octave/.. -IC:/Octave/OCTAVE~1.0//mingw64/include/octave-5.1.0/octave -IC:/Octave/OCTAVE~1.0//mingw64/include -fopenmp -g -O2 -shared -Wl,-rpath-link,/release/mxe-octave-w64/usr/x86_64-w64-mingw32/lib -L/release/mxe-octave-w64/usr/x86_64-w64-mingw32/lib -L/release/mxe-octave-w64/usr/x86_64-w64-mingw32/qt5/lib -Wl,--export-all-symbols -o main.mex C:/Octave/OCTAVE~1.0/tmp/oct-u4r15I.o "-LC:/Program Files (x86)/IVI Foundation/VISA/Winnt/lib_x64/msc" -lvisa64 -LC:/Octave/OCTAVE~1.0//mingw64/lib/octave/5.1.0 -LC:/Octave/OCTAVE~1.0//mingw64/lib -LC:/Octave/OCTAVE~1.0//mingw64/lib/octave/5.1.0 -loctinterp -loctave -Wl,-rpath-link,/release/mxe-octave-w64/usr/x86_64-w64-mingw32/lib -L/release/mxe-octave-w64/usr/x86_64-w64-mingw32/lib -L/release/mxe-octave-w64/usr/x86_64-w64-mingw32/qt5/lib -Wl,--export-all-symbols
It will compile without error.

BitBake: How to use shell script content as body of pkg_postinst or pkg_preinst functions?

I want to add the contents of a shell script into the body of pkg_preinst_${PN} or pkg_postinst_${PN} function (BitBake recipe of a software package).
For example, let's consider this "PREINST" shell script:
$ cat PREINST
#! /bin/sh
chmod +x /usr/bin/mybin
Executing a simple 'cat' command inside pkg_preinst function doesn't work:
pkg_preinst_${PN}() {
cat ${S}/path/to/PREINST
}
In this way, the contents for the .spec file for the generated rpm package are not the expected:
%pre
cat /Full/Path/To/Variable/S/path/to/PREINST
As you can see, %pre section doesn't include real contents of PREINST file, just includes the 'cat' command.
Is it possible to include the contents of PREINST file into the generated .spec file in some way?
Thank you in advance!
Finally I solved this issue by prepending this code to the do_package task:
do_package_prepend() {
PREINST_path = "${S}/${MYMODULE}/PREINST"
POSTINST_path = "${S}/${MYMODULE}/POSTINST"
PREINST = open(PREINST_path, "r")
POSTINST = open(POSTINST_path, "r")
d.setVar("pkg_preinst", PREINST.read())
d.setVar("pkg_postinst", POSTINST.read())
}
It modifies "pkg_preinst" and "pkg_postinst" keys in 'd' global dictionary with the content of each PREINST and POSTINST file as value. Now it works! :)

piping the output of gulp to a variable

I'm using the gulp-run plugin to get the git hash using the following code:
run('git log -1 --pretty=format:%H').exec();
I can add a pipe to save the output to a file, like so:
run('git log -1 --pretty=format:%H').exec().pipe(gulp.dest('some/dir'));
From there, I can read in the contents of the file using readFile() to get the git hash.
Is there a way for me to skip this step, and get the output of gulp-run into a variable directly?
Two comments:
I would recommend
git rev-parse HEAD
Instead of git log -1 --pretty=format:%H
And you can easily do this with https://github.com/mgutz/execSync, gulp-run is not necessary.
var sh = require('execSync');
var revision = sh.run('git rev-parse HEAD');
console.log(revision); // => 93004330f14fd502e1568a0c2c1a645eae269e1b