Yeoman Generator in CoffeeScript - HookFor Warning - coffeescript

I'm trying to develop a custom generator for Yeoman using CoffeeScript but I'm facing a problem. When I use the hookFor method in the constructor of my class Generator, I get a warning hookFor must be used within the constructor when I try to init my project with Yeoman and my custom generator. Here is the code of my generator in index.coffee :
path = require 'path'
util = require 'util'
yeoman = require '../../../../'
module.exports = class Generator extends yeoman.generators.Base
constructor: ->
super()
#directories = ['controllers', 'helpers', 'models', 'templates', 'views']
#hookFor 'artefact:controller', args: ['App']
deploy: ->
#directory '.', '.'
#mkdir path.join 'dev', directory for directory in #directories
Any help will be appreciated. Thanks.

Apparently, the error comes from Yeoman Generators code in the yeoman-generators/lib/base.js file.
Here is how I led to that conclusion :
The warning is caused by the variable _running set to true in hookFor function (line 296)
This variable is set to true in run function (line 78) and just after that, methods of the class Generator are iterated (line 81-137)
the constructor defined in CoffeeScript for the class Generator is called during the iteration, so #hookFor is called whereas _running is true : warning!
But, the constructor should not be called because a test is done during the iteration to prevent it (line 92) :
if ( method.constructor === '-' )
However, this test, in my opinion, should be :
if ( method === 'constructor' )
The hack does the trick. Feel free to add comment if I'm wrong.

Related

Yocto variable not defined but set with _ operator

I'm struggling with something I'm not sure to address correctly.
In a Yocto environment (for STM32MP1 by the way) I have to configure a new target.
Hence I added to meta-st/meta-st-stm32mp/conf/machine/include/st-machine-extlinux-config-stm32mp.inc this section, that looks like the other already available:
EXTLINUX_BOOTDEVICE_EMMC = "mmc1"
EXTLINUX_BOOTDEVICE_SDCARD = "mmc0"
EXTLINUX_ROOT_EMMC = "${#bb.utils.contains('ST_VENDORFS','1','root=/dev/mmcblk1p4','root=/dev/mmcblk1p3',d)}"
EXTLINUX_ROOT_NAND = "ubi.mtd=UBI rootfstype=ubifs root=ubi0:rootfs"
# Define available targets to use
UBOOT_EXTLINUX_CONFIGURED_TARGETS += "mp151a_sdcard"
UBOOT_EXTLINUX_CONFIGURED_TARGETS += "mp151a_emmc"
# Define bootprefix for each target
UBOOT_EXTLINUX_BOOTPREFIXES_mp151a_sdcard = "${EXTLINUX_BOOTDEVICE_SDCARD}_stm32mp151a_"
UBOOT_EXTLINUX_BOOTPREFIXES_mp151a_emcc = "${EXTLINUX_BOOTDEVICE_EMCC}_stm32mp151a_"
# Define labels for each target
UBOOT_EXTLINUX_LABELS_mp151a_sdcard = "stm32mp151a-sdcard"
UBOOT_EXTLINUX_LABELS_mp151a_emcc = "stm32mp151a-emcc"
# Define default boot config for each target
UBOOT_EXTLINUX_DEFAULT_LABEL_mp151a_sdcard ?= "stm32mp151a-sdcard"
UBOOT_EXTLINUX_DEFAULT_LABEL_mp151a_emcc ?= "stm32mp151a-emcc"
# Define FDT overrides for all labels
UBOOT_EXTLINUX_FDT_stm32mp151a-sdcard = "/stm32mp151a.dtb"
UBOOT_EXTLINUX_FDT_stm32mp151a-emcc = "/stm32mp151a.dtb"
# Define ROOT overrides for all labels
UBOOT_EXTLINUX_ROOT_stm32mp151a-sdcard = "${EXTLINUX_ROOT_SDCARD}"
UBOOT_EXTLINUX_ROOT_stm32mp151a-emcc = "${EXTLINUX_ROOT_EMCC}"
But when I bitbake <image> (that includes the file above) I get this output:
DEBUG: Executing python function update_extlinuxconf_targets
NOTE: UBOOT_EXTLINUX_CONFIGURED_TARGETS: mp157a-dk1_sdcard mp157a-dk1_sdcard-optee mp157c-dk2_sdcard mp157c-dk2_sdcard-optee mp157c-ed1_emmc mp157c-ed1_emmc-optee mp157c-ed1_sdcard mp157c-ed1_sdcard-optee mp157c-ev1_emmc mp157c-ev1_emmc-optee mp157c-ev1_nand mp157c-ev1_nor-sdcard mp157c-ev1_nor-emmc mp157c-ev1_sdcard mp157c-ev1_sdcard-optee mp151a_sdcard mp151a_emmc
NOTE: UBOOT_EXTLINUX_CONFIG_FLAGS: emmc sdcard
NOTE: *** Loop for config_label: emmc
NOTE: *** Loop for devicetree: stm32mp151a
NOTE: >>> New target label: mp151a_emmc
NOTE: >>> Append mp151a_emmc to UBOOT_EXTLINUX_TARGETS
NOTE: *** Loop for config_label: sdcard
NOTE: *** Loop for devicetree: stm32mp151a
NOTE: >>> New target label: mp151a_sdcard
NOTE: >>> Append mp151a_sdcard to UBOOT_EXTLINUX_TARGETS
NOTE: >>> UBOOT_EXTLINUX_TARGETS (updated): mp151a_emmc mp151a_sdcard
DEBUG: Python function update_extlinuxconf_targets finished
DEBUG: Executing python function do_create_multiextlinux_config
ERROR: UBOOT_EXTLINUX_ROOT not defined
DEBUG: Python function do_create_multiextlinux_config finished
ERROR: Function failed: do_create_multiextlinux_config
As you can see, the file is actually processed because it added the targets I've defined.
But it doesn't find the UBOOT_EXTLINUX_ROOT even if it's "set" with the _ operator:
UBOOT_EXTLINUX_ROOT_stm32mp151a-sdcard = "${EXTLINUX_ROOT_SDCARD}"
UBOOT_EXTLINUX_ROOT_stm32mp151a-emcc = "${EXTLINUX_ROOT_EMCC}"
I also tried to set the main variable to something like:
UBOOT_EXTLINUX_ROOT = ""
or
UBOOT_EXTLINUX_ROOT = "root=/dev/mmcblk1p4"
to see if it was the problem but it doesn't change nothing.
Is this something related to Yocto itself (I mean, something wrong in my syntax) or it's very specific to the SDK (meta-st) ?
The error above should be raised by this file:
root = localdata.getVar('UBOOT_EXTLINUX_ROOT')
if not root:
bb.fatal('UBOOT_EXTLINUX_ROOT not defined')
UPDATE
I checked the (huge) output of bitbake -e and among other targets I see:
# $UBOOT_EXTLINUX_ROOT [41 operations]
[...]
# "${EXTLINUX_ROOT_NOREMMC}"
# override[stm32mp157c-ev1-m4-examples-sdcard]:set /local/STM32MP15-Ecosystem-v1.1.0/Distribution-Package/openstlinux-4.19-thud-mp1-19-10-09/layers/meta-st/meta-st-stm32mp/conf/machine/include/st-machine-extlinux-config-stm32mp.inc:274
# "${EXTLINUX_ROOT_SDCARD}"
# override[stm32mp157c-ev1-m4-examples-sdcard-optee]:set /local/STM32MP15-Ecosystem-v1.1.0/Distribution-Package/openstlinux-4.19-thud-mp1-19-10-09/layers/meta-st/meta-st-stm32mp/conf/machine/include/st-machine-extlinux-config-stm32mp.inc:275
# "${EXTLINUX_ROOT_SDCARD_OPTEE}"
# override[stm32mp151a-sdcard]:set /local/STM32MP15-Ecosystem-v1.1.0/Distribution-Package/openstlinux-4.19-thud-mp1-19-10-09/layers/meta-st/meta-st-stm32mp/conf/machine/include/st-machine-extlinux-config-stm32mp.inc:296
# "${EXTLINUX_ROOT_SDCARD}"
# override[stm32mp151a-emcc]:set /local/STM32MP15-Ecosystem-v1.1.0/Distribution-Package/openstlinux-4.19-thud-mp1-19-10-09/layers/meta-st/meta-st-stm32mp/conf/machine/include/st-machine-extlinux-config-stm32mp.inc:297
[...]
# pre-expansion value:
# ""
UBOOT_EXTLINUX_ROOT=""
# $UBOOT_EXTLINUX_ROOT_cubemx-nor-sdcard
UBOOT_EXTLINUX_ROOT_cubemx-nor-sdcard="root=/dev/mmcblk0p3"
# $UBOOT_EXTLINUX_ROOT_cubemx-sdcard
UBOOT_EXTLINUX_ROOT_cubemx-sdcard="root=/dev/mmcblk0p6"
# $UBOOT_EXTLINUX_ROOT_stm32mp151a-emcc
UBOOT_EXTLINUX_ROOT_stm32mp151a-emcc="\${EXTLINUX_ROOT_EMCC}"
# $UBOOT_EXTLINUX_ROOT_stm32mp151a-sdcard
UBOOT_EXTLINUX_ROOT_stm32mp151a-sdcard="root=/dev/mmcblk0p6"
So far, if I understand correctly, the override values are correctly assigned (but not the ${EXTLINUX_ROOT_EMCC} - I don't understand where the \ comes from) but the main variable is still empty.
Adding UBOOT_EXTLINUX_ROOT = "root=/dev/mmcblk1p4" at the beginning of the above file, seems to do the trick (even if before I wrote the opposite, perhaps I forgot to clear the cache?) but I don't think it's the right way to do it.
You should specify the wanted name of the machine as a target to build, i.e.:
MACHINE=stm32mp151a-sdcard bitbake <image>
This way, the UBOOT_EXTLINUX_ROOT gets the non-empty value "root=/dev/mmcblk0p6" (from the UBOOT_EXTLINUX_ROOT_stm32mp151a-sdcard variant of the variable).

All Dingo routes return 404 when phpunit testing

original posted at https://github.com/dingo/api/issues/1472
I'm using Lumen 5.1 and DingoApi 1.0.x to do my api development, and now I'm trying to do some acceptance testing. Following the documentation of Lumen, here is how I do it:
Here is a simplified routes definition in app\Http\routes.php:
$app->get('/', function () use ($app) {
return "Welcome to mysite.com";
});
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->group([
'prefix' => 'dealer',
'middleware' => 'checkH5ApiSign'
], function ($api) {
$api->get('list', 'App\Http\Controllers\Credit\DealerController#index');
$api->get('staff_list', 'App\Http\Controllers\Credit\DealerController#getStaffList');
});
}
I can access both routes defined using $app or $api(dingo) in browser or via postman, they both can return a 200 response. But whenever I'm trying to access those routes in phpunit, the $app defined route like / is responding okay with 200 code, but all routes defined with $api(dingo) will response with 404 status code. Here is my test code:
class DealerTest extends TestCase
{
public function testTest()
{
$this->get('/')->assertResponseOk();
$this->get('/dealer/list')->assertResponseOk();
$this->get('/dealer/staff_list')->assertResponseOk();
}
}
and ran result:
PHPUnit 5.7.5 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 590 ms, Memory: 6.00MB
There was 1 failure:
1) DealerTest::testTest
Expected status code 200, got 404.
Failed asserting that false is true.
E:\Gitrepos\api.fin.youxinjinrong.com\vendor\laravel\lumen-framework\src\Testing\AssertionsTrait.php:19
E:\Gitrepos\api.fin.youxinjinrong.com\tests\DealerTest.php:8
FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
I tried ran through Dingo package code to find the cause, but failed. All other related issue could not solve my problem either. So please help me.
update
I followed the code flow, and see that FastRoute\DataGenerator\RegexBasedAbstract.php is doing the addRoute() operation, I dumped $this->staticRoutes) in that addRoute() method, see that it's doing okay both inside browser and under phpunit. But weird enough, the following call of ->getData() is behaving differenctly: in browser all static routes are returned, but not in phpunit.
Hope this can somehow be helpful. I'm still digging this problem...
So I got mine to work by doing this;
Using the example in the example used in creating the issue:
class DealerTest extends TestCase
{
public function testTest()
{
$this->get('/')->assertResponseOk();
$this->get('/dealer/list')->assertResponseOk();
$this->get('/dealer/staff_list')->assertResponseOk();
}
}
becomes
class DealerTest extends TestCase
{
public function testTest()
{
$this->get(getenv('API_DOMAIN') . '/v1/')->assertResponseOk();
$this->get(getenv('API_DOMAIN') . '/v1/dealer/list')->assertResponseOk();
$this->get(getenv('API_DOMAIN') . '/v1/dealer/staff_list')->assertResponseOk();
}
}
I hope this helps

How to fix error "Cannot dispatch middleware Application\Middleware\IndexMiddleware"?

I've set up a Zend Application as normal, except in my case the difference is that I set it up over an existing legacy web application.
I still want to call my existing legacy application over the ZF3 app. It was suggested I can do so using Middleware. I went over https://docs.zendframework.com/zend-mvc/middleware/ and set up my routing as described there.
However, when I run the application, I am greeted by this:
Cannot dispatch middleware Application\Middleware\IndexMiddleware
#0 zend-mvc\src\MiddlewareListener.php(146):
Zend\Mvc\Exception\InvalidMiddlewareException::fromMiddlewareName('Application\\Mid...')
Here is where the exception happens:
https://github.com/zendframework/zend-mvc/blob/release-3.1.0/src/MiddlewareListener.php#L146
Just to note:
$middlewareToBePiped; //'Application\Middleware\IndexMiddleware'
is_string($middlewareToBePiped); // true
$serviceLocator->has($middlewareToBePiped);//false
$middlewareToBePiped instanceof MiddlewareInterface; //false
is_callable($middlewareToBePiped);//false
My class is:
namespace Application\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Zend\Http\Response;
class IndexMiddleware implements MiddlewareInterface
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{}
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{}
}
I am thinking that my issue is that my IndexMiddleware class is not being found in ServiceLocator... (line 142 of linked API). How do I get it in there?
I put this into my application.config.php file:
'service_manager' => [
'invokables' => array(
'middleware' => IndexMiddleware::class
)
]
onto the next error it is.. (Last middleware executed did not return a response.)
but looks like it has executed)

KIE workbench integration with Java: mismatched input '<' expecting one of the following tokens: '[package, import, global, declare,..]'

I'm trying to integrate drools kie workbench with java application. I'm Using jboss-as-7.1.1.Final as my workbench. Here is the rule definition:
package com.sample;
rule "set isEligible"
ruleflow-group "sample"
lock-on-active true
when
$p : Student(gpa > 2.0)
then
System.out.println("GPA is greater than 2..");
$p.setEligible(true);
end
From the java application i'm passing the student POJO object to the kieSession object and firing up the rules.
Here is the code:
Student s = new Student();
s.setName("Virat");
s.setGpa(5.0f);
URL u = new URL("http://localhost:8080/kie-wb-distribution-wars-6.2.0.Final-jboss-as7/kie-wb.html#%5Bnull%5D?path_uri=default://master#com/sample/src/main/resources/com/sample/sampleDRL.drl&file_name=sampleDRL.drl&has_version_support=true");
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newUrlResource(u), ResourceType.DRL );
kbuilder.hasErrors();
if (kbuilder.hasErrors() ) {
System.out.println( kbuilder.getErrors() );
}
kbuilder.getKnowledgePackages();
KieBase kieBase = kieContainer.getKieBase();
KieSession kieSession = kieBase.newKieSession();
kieSession.insert(s);
kieSession.fireAllRules();
I'm able to connect to the workbench repository (com:sample:1.0) from the java app. But the problem is, i'm getting the following exception while executing the rule:
[8,0]: [ERR 107] Line 8:0 mismatched input '<' expecting one of the following tokens: '[package, import, global, declare, function, rule, query]'.
[0,0]: Parser returned a null Package
I don't know where i went wrong (because no where i have used '<' symbol).
Ask me for any missing detail. Any help on this would be greatly appreciated. Thank you.

Errors in codeigniter-restserver library

I want to use restful in my ci 3.03 application:
I found this tutplus tutorial
I downloaded codeigniter-restserver-master.zip file and copied Format.php and REST_Controller.php(#version 3.0.0) files into /application/libraries/REST directory
I created control application/controllers/api/Users.php :
require_once("application/libraries/REST/REST_Controller.php");
require_once("application/libraries/REST/Format.php");
class Users extends REST_Controller
{
//protected $rest_format = 'json';
function users_get()
{
//$users = $this->user_model->get_all();
$filter_username= $this->get('filter_username');
$filter_user_group= $this->get('filter_user_group');
$filter_active= $this->get('filter_active');
$sort= $this->get('sort');
$sort_direction= $this->get('sort_direction');
//, $filter_user_group, $filter_active, $sort, $sort_direction
$users_list = $this->muser->getUsersList(false, ''/*, $filter_username, $filter_user_group, $filter_active, $sort, $sort_direction, ''*/);
echo '<pre>'.count($users_list).'::$users_lists::'.print_r($users_list,true).'</pre>';
if($users_list)
{
$this->response($users, 200);
}
else
{
$this->response(NULL, 404);
}
}
AND RUNNING URL http://local-ci3.com/api/users I got many errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$format
Filename: REST/REST_Controller.php
Line Number: 734
Backtrace:
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 734
Function: _error_handler
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 649
Function: response
File: /mnt/diskD_Work/wwwroot/ci3/index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$format
Filename: REST/REST_Controller.php
Line Number: 752
Backtrace:
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 752
Function: _error_handler
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 649
Function: response
File: /mnt/diskD_Work/wwwroot/ci3/index.php
Line: 292
Function: require_once
Actually I wanted to get some workable library to help me with REST api creation. I think that is preferable way istead of making from zero.
But is this library not workable or does it needs for some fixing? Sorry, what I missed is if this library only for ci 2?
I made search on this forum and found such hint :
I have the same problem when I load both Format.php and
Rest_Controller.php into a controller. After have a quick glance at
Format.php, it appears to be a standalone format conversion helper.
Try to just load Rest_Controller.php and see if your problem goes
away.
I commented line
//require_once("application/libraries/REST/Format.php");
in my controller, but I still get errors like :
Message: Undefined property: Users::$format.
I tried to review code of this library and see that invalid block when data are converted to json format, line 731-757 :
elseif ($data !== NULL)
{
// If the format method exists, call and return the output in that format
if (method_exists($this->format, 'to_' . $this->response->format))
{
// Set the format header
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
$output = $this->format->factory($data)->{'to_' . $this->response->format}();
// An array must be parsed as a string, so as not to cause an array to string error
// Json is the most appropriate form for such a datatype
if ($this->response->format === 'array')
{
$output = $this->format->factory($output)->{'to_json'}();
}
}
else
{
// If an array or object, then parse as a json, so as to be a 'string'
if (is_array($data) || is_object($data))
{
$data = $this->format->factory($data)->{'to_json'}();
}
// Format is not supported, so output the raw data as a string
$output = $data;
}
}
If I tried to commented this block, but get error
Message: Array to string conversion
Looks like data are not converted in this case...
Is is possible to fix these errors?
Or can you, please, to tell me advice some codeigniter 3 REST api workable library with similar interface like library above?
Thanks!
I use that lib, work just fine. My suggestion is follow the more relevant installation instruction on github .
you also wrong place the lib file :
Tutorial say :
require(APPPATH'.libraries/REST_Controller.php');
You try :
require_once("application/libraries/REST/REST_Controller.php");
require_once("application/libraries/REST/Format.php");
No need to include the format because on line 407 the lib will load it. And also good to know on line 404 it will load the configuration (application/config/rest.php) it will be your default configuration, and also you can change it to suit your need.
Please let me know if you still got error using my answer :)