Improve project generator for CORE "poser"

optionally adds support for rattail/posterior PHP lib
This commit is contained in:
Lance Edgar 2023-05-15 19:52:23 -05:00
parent d8f953b11a
commit 8d4f3f8d9b
7 changed files with 239 additions and 45 deletions

View file

@ -5,6 +5,8 @@ CORE-POS "Poser" project generator
import os
import colander
from rattail.projects import ProjectGenerator
@ -14,25 +16,80 @@ class COREPOSPoserProjectGenerator(ProjectGenerator):
"""
key = 'corepos_poser'
def make_schema(self, **kwargs):
schema = super(COREPOSPoserProjectGenerator, self).make_schema(**kwargs)
schema.add(colander.SchemaNode(name='organization',
typ=colander.String()))
schema.add(colander.SchemaNode(name='org_slug',
typ=colander.String()))
schema.add(colander.SchemaNode(name='has_office_plugins',
typ=colander.Boolean()))
schema.add(colander.SchemaNode(name='has_lane_plugins',
typ=colander.Boolean()))
schema.add(colander.SchemaNode(name='use_posterior',
typ=colander.Boolean()))
return schema
def normalize_context(self, context):
context = super(COREPOSPoserProjectGenerator, self).normalize_context(context)
# org_studly_prefix
context['org_studly_prefix'] = context['org_slug'].capitalize()
# requires
requires = [('wikimedia/composer-merge-plugin', '^2.1')]
if context['use_posterior']:
requires.append(('rattail/posterior', '^0.1.1'))
context['requires'] = requires
return context
def generate_project(self, output, context, **kwargs):
##############################
# project root
##############################
self.generate('gitignore',
os.path.join(output, '.gitignore'))
self.generate('composer.json.mako',
os.path.join(output, 'composer.json'),
context)
##############################
# office plugins
##############################
if context['has_office_plugins']:
office_plugins = os.path.join(output, 'office_plugins')
os.makedirs(office_plugins)
demo_plugin = os.path.join(office_plugins, 'PoserDemo')
plugin_name = f"{context['org_studly_prefix']}Demo"
demo_plugin = os.path.join(office_plugins, plugin_name)
os.makedirs(demo_plugin)
self.generate('office_plugins/PoserDemo/PoserDemo.php',
os.path.join(demo_plugin, 'PoserDemo.php'))
self.generate('office_plugins/PoserDemo/PoserDemo.php.mako',
os.path.join(demo_plugin, f"{plugin_name}.php"),
context)
self.generate('office_plugins/PoserDemo/PoserDemoTask.php.mako',
os.path.join(demo_plugin, f"{plugin_name}Task.php"),
context)
##############################
# lane plugins
##############################
if context['has_lane_plugins']:
lane_plugins = os.path.join(output, 'lane_plugins')
os.makedirs(lane_plugins)

View file

@ -0,0 +1,29 @@
## -*- coding: utf-8; mode: js; -*-
{
"name": "${org_slug}/poser",
"description": "CORE-POS customizations for ${organization}",
"require": {
<% count = len(requires) %>
% for i, pkg in enumerate(requires, 1):
<% package, version = pkg %>
"${package}": "${version}"${'' if i == count else ','}
% endfor
},
"extra": {
"merge-plugin": {
"require": [
"../IS4C/composer.json"
]
}
},
"config": {
"component-dir": "../IS4C/fannie/src/javascript/composer-components",
"vendor-dir": "../IS4C/vendor",
"allow-plugins": {
"composer/installers": true,
"oomphinc/composer-installers-extender": true,
"corepos/composer-installer": true,
"wikimedia/composer-merge-plugin": true
}
}
}

View file

@ -0,0 +1,2 @@
office_plugins/GitStatus

View file

@ -1,33 +0,0 @@
<?php
/*******************************************************************************
Copyright 2014 Whole Foods Co-op
This file is part of IT CORE.
IT CORE is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
IT CORE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
in the file license.txt along with IT CORE; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*********************************************************************************/
/**
*/
class PoserDemo extends COREPOS\Fannie\API\FanniePlugin
{
public $plugin_description = 'Plugin to demo Poser customization';
public $plugin_settings = array(
'Foo' => array('default'=>'Bar', 'label'=>'Foo',
'description'=>'Some important foo-related setting'),
);
}

View file

@ -0,0 +1,43 @@
## -*- coding: utf-8; mode: php; -*-
<?php
class ${org_studly_prefix}Demo extends COREPOS\Fannie\API\FanniePlugin
{
public $plugin_description = "Demo plugin for ${organization}";
public $plugin_settings = [
% if use_posterior:
'${org_studly_prefix}DemoTailboneAPIURL' => [
'label' => 'Tailbone API URL',
'description' => 'Base URL for Tailbone API (usually ends with /api)',
],
'${org_studly_prefix}DemoTailboneAPIToken' => [
'label' => 'Tailbone API Token',
'description' => 'User auth token for use with Tailbone API',
],
'${org_studly_prefix}DemoTailboneAPIVerifySSL' => [
'label' => 'Verify SSL',
'description' => 'Validate SSL cert used by Tailbone API?',
'options' => [
"Yes, validate the SSL cert" => 'true',
"No, do not validate the SSL cert (SHOULD ONLY BE USED FOR TESTING!)" => 'false',
],
'default'=>'true',
],
% else:
'${org_studly_prefix}Foo' => [
'label' => 'Foo',
'description'=>'Some important foo setting',
'default' => 'bar',
],
% endif
];
}

View file

@ -0,0 +1,64 @@
## -*- coding: utf-8; mode: php; -*-
<?php
class ${org_studly_prefix}DemoTask extends FannieTask
{
public $name = "${organization} Demo";
public $description = 'Demo command that tries to connect to
Tailbone API, and reports success/failure.
NOTE: This task is provided by the ${org_studly_prefix}Demo plugin;
please see that for settings to control behavior.';
public $default_schedule = array(
'min' => 0,
'hour' => 5,
'day' => '*',
'month' => '*',
'weekday' => '*',
);
public function run()
{
$this->cronMsg("hello from ${org_studly_prefix}Demo!", FannieLogger::INFO);
% if use_posterior:
$settings = $this->config->get('PLUGIN_SETTINGS');
$url = $settings['PoserDemoTailboneAPIURL'];
if (!$url) {
$this->cronMsg("must define the Tailbone API URL", FannieLogger::ERROR);
return;
}
$token = $settings['PoserDemoTailboneAPIToken'];
if (!$token) {
$this->cronMsg("must define the Tailbone API token", FannieLogger::ERROR);
return;
}
$verifySSL = $settings['PoserDemoTailboneAPIVerifySSL'] === 'false' ? false : true;
$posterior = new \Rattail\Posterior\Client($url, $token, $verifySSL);
try {
$response = $posterior->get('/about');
} catch (Exception $e) {
$this->cronMsg($e, FannieLogger::ERROR);
return;
}
$body = $response->getBody();
$this->cronMsg("response body was: $body", FannieLogger::INFO);
print("$body\n");
% else:
$this->cronMsg("task is complete!", FannieLogger::INFO);
print("task is complete!\n");
% endif
}
}

View file

@ -12,6 +12,38 @@ class GeneratedProjectViewSupplement(ViewSupplement):
"""
route_prefix = 'generated_projects'
def configure_form_corepos_poser(self, f):
f.set_grouping([
("Naming", [
'organization',
'org_slug',
]),
("Options", [
'has_office_plugins',
'has_lane_plugins',
'use_posterior',
]),
])
# organization
f.set_helptext('organization', "For use with branding etc.")
f.set_default('organization', "Acme Foods Co-op")
# org_slug
f.set_helptext('org_slug', "Short name used for folders etc.")
f.set_default('org_slug', 'acmefoods')
# has_*_plugins
f.set_label('has_office_plugins', "Office Plugins")
f.set_default('has_office_plugins', True)
f.set_label('has_lane_plugins', "Lane Plugins")
f.set_default('has_lane_plugins', False)
# use_posterior
f.set_helptext('use_posterior', "Set this if you plan to integrate with Tailbone API")
f.set_default('use_posterior', False)
def configure_form_corporal(self, f):
f.set_grouping([