After a great talk on community demo
<https://www.youtube.com/watch?v=aOqA8-wpPKQ>, here is a follow up with the
points that were raised during the discussion:
Use cases:
1. Run all cops as part of community templates CI against the whole
repository
2. Run all cops against a single template invoked by the user from
template editing screen (foreman core)
3. Upgrade scenario: Preferably run cops for the next foreman version
before the actual upgrade to make sure the templates will remain valid.
Features:
1. List of rues should be pluggable [Shim]: It looks like it is a
must-have for the engine.
2. Deployment options
1. Engine as a separate gem, cops in a relevant repository - core cops
in core, plugin cops in plugins.
2. Engine with all cops in a single gem, versioned per foreman
version.
3. Engine as part of templates plugin, cops as part of relevant
plugins.
4. Separate gems for everything: foreman-cops-engine,
foreman-cops-core, foreman-cops-plugin1, foreman-cops-plugin2 e.t.c.
Engine
is versioned per foreman release version (for the sake of rubocop
version),
cops are versioned per plugin version.
General comments:
1. Cops writing should be enforced on PR's that are changing the way to
write templates [mhulan]
2. Cops are dependent on core/plugin version [gwmngilfen]
On Monday, August 14, 2017 at 2:29:02 PM UTC+3, [email protected] wrote:
>
> TL;DR: I have developed a way to scan any template and see if there are
> suspicious/incorrect code patterns in them, so the templates will remain
> valid even after foreman code changes.
>
> Recently I have started to think about user created templates and foreman
> upgrades.
>
> When user upgrades foreman, hist default templates get upgraded by the
> installer/migrations, but templates created by the user (both cloned and
> from scratch) are not touched.
> This could lead to invalid templates and broken provisioning functionality
> for the user.
> Good example for this would be the change
> <https://github.com/theforeman/foreman/commit/7b966530c9ba48b2a37416465a3c9619f7143387>
>
> from calling to <%= foreman_url %> to <%= foreman_url('built') %>
>
> I was looking for a way to inspect any template, in order to identify
> problematic code as soon as the system is upgraded.
>
> I came down to a solution based on rubocop - it's already analyzing source
> files for patterns.
> I have created a POC that analyzes a template written to a file, and
> presents the resulting errors as regular rubocop (clang style).
> All source codes are available as gist:
> https://gist.github.com/ShimShtein/341b746f15826261053e97c2f435ff1a
>
> Small explanation for the gist:
>
> Entry point: inspect_template.rb
> Usage:
> Put everything from the gist to a single folder and execute:
>
>> inspect_template /path/to/template_source.erb
>
> This script aggregates all the parts that are needed to create the
> clang-like output.
>
> The process:
>
> 1. Strip all non-ruby text from the template. This is done by
> erb_strip.rb. It turns everything that is not a ruby code into spaces, so
> the ruby code remains in the same places as it was in the original file.
> 2. Run rubocop with custom rules and erb monkey patch and produce a
> json report
> 1. foreman_callback_cop.rb custom rule file. The most interesting
> line is "def_node_matcher :foreman_url_call?, '(send nil
> :foreman_url)'". Here you define which pattern to look for in the
> AST, in our case we are looking for calls (send) for foreman_url method
> without parameters.
> 2. foreman_erb_monkey_patch.rb file: Patches rubocop engine to
> treat *.erb files as source files and not skip them.
> 3. Process the resulting json to convert it to clang style
> highlighting.
>
> Possible usages:
>
> - Scanning all template after foreman upgrade to see that they are
> still valid.
> - Linting a template while while editing.
> - Using rubocop's autocorrect feature to automatically fix offences
> found by this process.
>
> Long shot: we can create custom rules to inspect code for our plugins too,
> especially if we start creating custom rules in rubocop.
>
> I am interested in comments, opinions and usages to see how to proceed
> from here.
>
>
--
You received this message because you are subscribed to the Google Groups
"foreman-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.