Creating a custom generator with Yeoman - scaffolding

I'd like to use Yeoman for scaffolding a few different projects I'm working on. For now, however, I'd like to start with something as simple as having a generator that creates the following folder structure:
/build/
/src/
/spec/
/spec/test/
/spec/buster.js
/.jshintrc
/readme.md
The .jshintrc, readme.md file and buster.js files can have have just a random line of text.
I've tried reading through the generator readme and countless examples but clearly I'm missing something as none of the generators I end up with seem to work. At the most basic level I've even tried running:
yo generator:app
Thinking I could at least start there but then when I run yo --help my generator isn't listed.
EDIT:
Here are the steps I take when trying to create a generator using the generator-generator library:
> mkdir somegen
> cd somegen
> yo generator:app
... <answer a couple of questions> ...
> cd ..
> npm install -g somegen
Then, when I try to run yo somegen it fails saying:
You don't seem to have a generator with the name somegen installed.

Naming convention I believe.
You need to name your generator directory "generator-somegen"
then install it or use $ npm link. and use it the same as you were trying to $ yo somegen

Related

Custom Cmake handling functions for build/clean

i'm new to cmake and I'm trying to find an option to commit the following
cmake .. && make && ./(project_name) --> in order to run my code using 1 commnad
and
rm -rf * --> in order to clean my build directory.
As custom commands to add to my cmake to use like MakeFile uses make all / make clean.
How would I approach this with building a custom command in Cmake to run ? is it even possible to do ?

cmake3 keeps using old CXXFLAGS value

I'm trying to build the azure storage sdk and have hit a build failure. Looking at the build commands, I see that it is trying to use an old CXXFLAGS value (not sure if this is the reason for the failure but would like to clean this up).
My steps were essentially:
export CXXFLAGS="-fPIC"
run cmake3 and make on cpprestsdk
unset CXXFLAGS
run cmake3 on azure-storage-cpp
Then when I look at my compile_commands.json file, I still see the -fPIC option being used. I tried deleting the directory and cloning again but it is still there.
CXXFLAGS is not set anymore, even tried using a new shell but still no luck:
$ echo $CXXFLAGS
Any ideas what is going on?
Edit:
I'm using the following command:
cmake3 .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
and am following CentOS instructions from https://github.com/Azure/azure-storage-cpp

npm run script, can't access command line args

I'm an electron beginner, and I'm trying to use it to package up a react-based app. I'm trying to run electron via a script entry in my package.json:
"electron-dev": "concurrently \"cross-env BROWSER=none npm start\" \"wait-on http://localhost:3000 && electron . $npm_config_input \"",
That will run electron.js (which is what "main" is defined as earlier in the package.json file), but I need to pass in a command line argument. I've seen references that indicate $npm_config_input will have the argument passed in this way:
% npm run electron-dev --input=file.tif
But that $npm_config_input doesn't seem to get expanded for me. electron.js gets the literal string $npm_config_input. I'm confused why this isn't working.
It seems I could avoid this problem by doing this:
% npm run electron-dev -- --input=file.tif
But I don't know how to associate the input argument to the second command I'm starting using concurrently. It would be nice if I could use something like $1 or $npm_config_input in its definition. Does anyone have a solution for this?
I'm running this on Windows 10 using git bash. Other things generally work. I have nodejs 12.16.2 installed. TIA!
you almost got it right, just use process.argv.
for instance, create a file named cmd.js that looks like this
console.log(process.argv.slice(2));
now create a script hook for it by adding the following to package.json
"scripts": {
"foo": "node cmd.js"
}
now you can try it...
$ npm run foo -- arg1 arg2
> foo#1.0.0 foo /tmp/foo
> node cmd.js "arg1" "arg2"
[ 'arg1', 'arg2' ]
I believe I have found the answer. In electron.js, I printed process.argv to see if there was anything of use there. It turns out process.env.npm_config_input contains file.tif when I run this:
% npm run electron-dev --input=file.tif
That should work for me. I still don't understand why the other things I read about and tried didn't work.

How to set the CMAKE_PREFIX_PATH?

I have a problem with the global environmental variable CMAKE_PREFIX_PATH. I already set this and I can see it is set when I type env, but when I run cmake . to build HipHop, it tells me that the variable isn't set.
Is there a way I can hard-code this into the makefiles?
Try to run cmake -DCMAKE_PREFIX_PATH=/your/path .
CMAKE_PREFIX_PATH works as a build directive, rather than as an environment variable. Moreover, you may perform the build into a dedicated temporary directory (it's cleaner, because when done, you can remove that temporary directory and you get back a clean pristine source tree).
$ mkdir -p tmpbuild && cd tmpbuild
$ cmake -DCMAKE_PREFIX_PATH=~/deliveries/hiphop ..
$ make install
$ cd ..
On MacOS it's different. I had to use:
make -i CMAKE_PREFIX_PATH="/the/path"
This was while installing VMQT, and this error was shown:
CMake Error at CMakeLists.txt:87 (find_package): By not providing
"FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked CMake
to find a package configuration file provided by "OpenCV", but CMake
did not find one.
Could not find a package configuration file provided by "OpenCV"
with any of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If
"OpenCV" provides a separate development package or SDK, be sure it
has been installed.
Used this to solve it: make -i CMAKE_PREFIX_PATH="/opt/homebrew/Cellar/opencv/4.6.0_1/lib/cmake/opencv4/"

What is CMake equivalent of 'configure --prefix=DIR && make all install '?

I do cmake . && make all install. This works, but installs to /usr/local.
I need to install to a different prefix (for example, to /usr).
What is the cmake and make command line to install to /usr instead of /usr/local?
You can pass in any CMake variable on the command line, or edit cached variables using ccmake/cmake-gui. On the command line,
cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr . && make all install
Would configure the project, build all targets and install to the /usr prefix. The type (PATH) is not strictly necessary, but would cause the Qt based cmake-gui to present the directory chooser dialog.
Some minor additions as comments make it clear that providing a simple equivalence is not enough for some. Best practice would be to use an external build directory, i.e. not the source directly. Also to use more generic CMake syntax abstracting the generator.
mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr .. && cmake --build . --target install --config Release
You can see it gets quite a bit longer, and isn't directly equivalent anymore, but is closer to best practices in a fairly concise form... The --config is only used by multi-configuration generators (i.e. MSVC), ignored by others.
The ":PATH" part in the accepted answer can be omitted. This syntax may be more memorable:
cmake -DCMAKE_INSTALL_PREFIX=/usr . && make all install
...as used in the answers here.
Note that in both CMake and Autotools you don't always have to set the installation path at configure time. You can use DESTDIR at install time (see also here) instead as in:
make DESTDIR=<installhere> install
See also this question which explains the subtle difference between DESTDIR and PREFIX.
This is intended for staged installs and to allow for storing programs in a different location from where they are run e.g. /etc/alternatives via symbolic links.
However, if your package is relocatable and doesn't need any hard-coded (prefix) paths set via the configure stage you may be able to skip it.
So instead of:
cmake -DCMAKE_INSTALL_PREFIX=/usr . && make all install
you would run:
cmake . && make DESTDIR=/usr all install
Note that, as user7498341 points out, this is not appropriate for cases where you really should be using PREFIX.
The way I build CMake projects cross platform is the following:
/project-root> mkdir build
/project-root> cd build
/project-root/build> cmake -G "<generator>" -DCMAKE_INSTALL_PREFIX=stage ..
/project-root/build> cmake --build . --target=install --config=Release
The first two lines create the out-of-source build directory
The third line generates the build system specifying where to put the installation result (which I always place in ./project-root/build/stage - the path is always considered relative to the current directory if it is not absolute)
The fourth line builds the project configured in . with the buildsystem configured in the line before. It will execute the install target which also builds all necessary dependent targets if they need to be built and then copies the files into the CMAKE_INSTALL_PREFIX (which in this case is ./project-root/build/stage. For multi-configuration builds, like in Visual Studio, you can also specify the configuration with the optional --config <config> flag.
The good part when using the cmake --build command is that it works for all generators (i.e. makefiles and Visual Studio) without needing different commands.
Afterwards I use the installed files to create packages or include them in other projects...
Starting with CMake 3.15, the correct way of achieving this would be using:
cmake --install <dir> --prefix "/usr"
Official Documentation
Starting with CMake 3.21 you can use the --install-prefix option instead of manually setting CMAKE_INSTALL_PREFIX.
The modern equivalent of configure --prefix=DIR && make all install would now be:
cmake -B build --install-prefix=DIR
cmake --build build
cmake --install build
Regarding Bruce Adams answer:
Your answer creates dangerous confusion. DESTDIR is intended for
installs out of the root tree. It allows one to see what would be
installed in the root tree if one did not specify DESTDIR.
PREFIX is the base directory upon which the real installation is
based.
For example, PREFIX=/usr/local indicates that the final destination
of a package is /usr/local. Using DESTDIR=$HOME will install the files
as if $HOME was the root (/). If, say DESTDIR, was /tmp/destdir, one
could see what 'make install' would affect. In that spirit, DESTDIR
should never affect the built objects.
A makefile segment to explain it:
install:
cp program $DESTDIR$PREFIX/bin/program
Programs must assume that PREFIX is the base directory of the final
(i.e. production) directory. The possibility of symlinking a program
installed in DESTDIR=/something only means that the program does not
access files based upon PREFIX as it would simply not work. cat(1)
is a program that (in its simplest form) can run from anywhere.
Here is an example that won't:
prog.pseudo.in:
open("#prefix#/share/prog.db")
...
prog:
sed -e "s/#prefix#/$PREFIX/" prog.pseudo.in > prog.pseudo
compile prog.pseudo
install:
cp prog $DESTDIR$PREFIX/bin/prog
cp prog.db $DESTDIR$PREFIX/share/prog.db
If you tried to run prog from elsewhere than $PREFIX/bin/prog,
prog.db would never be found as it is not in its expected location.
Finally, /etc/alternatives really does not work this way. There are
symlinks to programs installed in the root tree (e.g. vi -> /usr/bin/nvi,
vi -> /usr/bin/vim, etc.).
It is considered bad practice to invoke the actual build system (e.g. via the make command) if using CMake. It is highly recommended to do it like this:
Configure + Generation stages:
cmake -S foo -B _builds/foo/debug -G "Unix Makefiles" -D CMAKE_BUILD_TYPE:STRING=Debug -D CMAKE_DEBUG_POSTFIX:STRING=d -D CMAKE_INSTALL_PREFIX:PATH=/usr
Build and Install stages:
cmake --build _builds/foo/debug --config Debug --target install
When following this approach, the generator can be easily switched (e.g. -G Ninja for Ninja) without having to remember any generator-specific commands.
Note that the CMAKE_BUILD_TYPE variable is only used by single-config generators and the --config argument of the build command is only used by multi-config generators.
Lots of answer, but I figured I'd do a summary to properly group them and explain the differences.
First of all, you can define that prefix one of two ways: during configuration time, or when installing, and that's really up to your needs.
During configuration time
Two options:
cmake -S $src_dir -B $build_dir -D CMAKE_INSTALL_PREFIX=$install_dir
cmake -S $src_dir -B $build_dir --install-prefix=$install_dir # Since CMake 3.21
During install time
Advantage: no need to reconfigure if you want to change it.
Two options:
cmake DESTDIR=$install_dir --build $build_dir --target=install # Makefile only
cmake --install $build_dir --prefix=$install_dir