Skip to content

Instantly share code, notes, and snippets.

@dubgeiser
Created May 8, 2018 11:23
Show Gist options
  • Select an option

  • Save dubgeiser/462aa1f4494bb7363fa0ee1ba67a54ad to your computer and use it in GitHub Desktop.

Select an option

Save dubgeiser/462aa1f4494bb7363fa0ee1ba67a54ad to your computer and use it in GitHub Desktop.
Script, depending on PyYaml to parse a GrumPHP config to Vim ale variable settings. (basic POC)
#!/usr/bin/env python3
""" Script that will convert a grumphp.yml file to Vimscript.
Basically it will convert the Yaml to Vimscript assignments that can configure
the Vim Ale plugin.
The script will extract the relevant settings for phpcs and phpstan from
grumphp.yml and spit out Vimscript.
Dependencies:
- pyyamli 3.12: https://github.com/yaml/pyyaml/releases
Setup with Python3!
THIS IS VERY MUCH STILL A WORK IN PROGRESS!
"""
import yaml
fn_grumphp = 'grumphp.yml'
settings_map = {
'phpcs' : {
'standard' : 'g:ale_php_phpcs_standard'
},
'phpstan' : {
'level' : 'g:ale_php_phpstan_level'
}
}
def extract_ale_php_settings(fn_grumphp):
""" Given a file name, assume it is a valid GrumPHP Yaml file and extract
all relevant settings that apply to Vim Ale plugin configuration and return
them as a dictionary that maps task names (as the keys in settings_map) to
a their corresponding configuration (which will be a dict).
"""
grumphp_stream = open(fn_grumphp, "r")
grumphp_config = yaml.safe_load(grumphp_stream)
grumphp_stream.close()
tasks = grumphp_config['parameters']['tasks']
settings = {
'phpcs' : tasks.get('phpcs', []),
'phpstan' : tasks.get('phpstan', [])
}
return settings
def build_ale_config(settings, settings_map):
""" Given a bunch of settings that map task names in a GrumPHP configuration
file to their corresponding config, return a dict that represents the
corresponding configuration for Vim Ale.
The mapping for GrumPHP config => Ale config is in settings_map.
"""
ale_config = {}
for task_name, config in settings.items():
if task_name in settings_map:
for name, value in config.items():
if name in settings_map[task_name]:
ale_config[settings_map[task_name][name]] = value
return ale_config
def dump_ale_config(config):
""" Simple dumper for the ale configuration.
"""
for name, value in config.items():
print("let %s = '%s'" % (name, value))
def main(fn__grumphp, settings_map):
settings = extract_ale_php_settings(fn_grumphp)
ale_config = build_ale_config(settings, settings_map)
dump_ale_config(ale_config)
if __name__ == '__main__':
main(fn_grumphp, settings_map)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment