From 0c96805e8fc64ecf9843510e8f21525572606966 Mon Sep 17 00:00:00 2001 From: John Freeman Date: Thu, 6 Oct 2022 08:25:22 +0100 Subject: [PATCH] Added support for managing keybindings.json (#226) Co-authored-by: Oskar Moge --- README.md | 16 +++++++++- molecule/code-only/converge.yml | 12 +++++++ molecule/code-only/tests/test_settings.py | 13 ++++++++ molecule/default/converge.yml | 35 +++++++++++++++++++++ molecule/default/tests/test_settings.py | 38 +++++++++++++++++++++++ molecule/ubuntu-min/converge.yml | 35 +++++++++++++++++++++ tasks/write-settings.yml | 17 ++++++++++ templates/keybindings.json.j2 | 6 ++++ 8 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 templates/keybindings.json.j2 diff --git a/README.md b/README.md index e4f1489..8d0eeab 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ users: - # extension 2 visual_studio_code_settings_overwrite: # Overwrite the settings file if it exists. Options: boolean "yes" or "no" (defaults to "no"). visual_studio_code_settings: # JSON object + visual_studio_code_keybindings_overwrite: # Overwrite the keybindings file if it exists. Options: boolean "yes" or "no" (defaults to "no"). + visual_studio_code_keybindings: # JSON array ``` Example Playbooks @@ -102,7 +104,7 @@ Minimal playbook: - role: gantsign.visual-studio-code ``` -Playbook with extensions installed that overwrites settings: +Playbook with extensions installed that overwrites settings and keybindings: ```yaml - hosts: servers @@ -122,6 +124,18 @@ Playbook with extensions installed that overwrites settings: "Vagrantfile": "ruby" } } + visual_studio_code_keybindings_overwrite: yes + visual_studio_code_keybindings: [ + { + "key": "ctrl+'", + "command": "workbench.action.terminal.focus" + }, + { + "key": "ctrl+'", + "command": "workbench.action.focusActiveEditorGroup", + "when": "terminalFocus" + } + ] ``` More Roles From GantSign diff --git a/molecule/code-only/converge.yml b/molecule/code-only/converge.yml index 64f41ef..2727ee6 100644 --- a/molecule/code-only/converge.yml +++ b/molecule/code-only/converge.yml @@ -56,9 +56,21 @@ "Vagrantfile": "ruby" } } + visual_studio_code_keybindings: [ + { + "key": "ctrl+'", + "command": "workbench.action.terminal.focus" + }, + { + "key": "ctrl+'", + "command": "workbench.action.focusActiveEditorGroup", + "when": "terminalFocus" + } + ] - username: test_usr2 visual_studio_code_extensions: [] visual_studio_code_settings: {} + visual_studio_code_keybindings: [] - username: test_usr3 post_tasks: diff --git a/molecule/code-only/tests/test_settings.py b/molecule/code-only/tests/test_settings.py index 9c664f8..abf1784 100644 --- a/molecule/code-only/tests/test_settings.py +++ b/molecule/code-only/tests/test_settings.py @@ -7,3 +7,16 @@ def test_settings(host): assert settings_file.group == 'test_usr' assert settings_file.mode == 0o600 assert settings_file.contains('"Vagrantfile": "ruby"') + + +def test_keybindings(host): + settings_file = host.file( + '/home/test_usr/.config/Code/User/keybindings.json') + + assert settings_file.exists + assert settings_file.is_file + assert settings_file.user == 'test_usr' + assert settings_file.group == 'test_usr' + assert settings_file.mode == 0o600 + assert settings_file.contains( + '"command": "workbench.action.terminal.focus"') diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index e6859ca..bf43088 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -60,6 +60,15 @@ force: no mode: 'u=rw,go=' + - name: install default key bindings + become: yes + become_user: test_usr4 + copy: + content: '[{"key":"ctrl+r","command": "remove_me"}]' + dest: /home/test_usr4/.config/Code/User/keybindings.json + force: no + mode: 'u=rw,go=' + roles: - role: ansible-role-visual-studio-code users: @@ -74,13 +83,27 @@ "Vagrantfile": "ruby" } } + visual_studio_code_keybindings: [ + { + "key": "ctrl+'", + "command": "workbench.action.terminal.focus" + }, + { + "key": "ctrl+'", + "command": "workbench.action.focusActiveEditorGroup", + "when": "terminalFocus" + } + ] - username: test_usr2 visual_studio_code_extensions: [] visual_studio_code_settings: {} + visual_studio_code_keybindings: [] - username: test_usr3 - username: test_usr4 visual_studio_code_settings: {} visual_studio_code_settings_overwrite: yes + visual_studio_code_keybindings: [] + visual_studio_code_keybindings_overwrite: yes - role: ansible-role-visual-studio-code visual_studio_code_build: 'insiders' users: @@ -95,9 +118,21 @@ "Vagrantfile": "ruby" } } + visual_studio_code_keybindings: [ + { + "key": "ctrl+'", + "command": "workbench.action.terminal.focus" + }, + { + "key": "ctrl+'", + "command": "workbench.action.focusActiveEditorGroup", + "when": "terminalFocus" + } + ] - username: test_usr2 visual_studio_code_extensions: [] visual_studio_code_settings: {} + visual_studio_code_keybindings: [] - username: test_usr3 post_tasks: diff --git a/molecule/default/tests/test_settings.py b/molecule/default/tests/test_settings.py index 4a76398..81f4ebe 100644 --- a/molecule/default/tests/test_settings.py +++ b/molecule/default/tests/test_settings.py @@ -31,3 +31,41 @@ def test_settings_overwrite(host): assert settings_file.group == 'test_usr4' assert settings_file.mode == 0o600 assert not settings_file.contains('remove_me') + + +def test_keybindings(host): + settings_file = host.file( + '/home/test_usr/.config/Code/User/keybindings.json') + + assert settings_file.exists + assert settings_file.is_file + assert settings_file.user == 'test_usr' + assert settings_file.group == 'test_usr' + assert settings_file.mode == 0o600 + assert settings_file.contains( + '"command": "workbench.action.terminal.focus"') + + +def test_keybindings_overwrite(host): + settings_file = host.file( + '/home/test_usr4/.config/Code/User/keybindings.json') + + assert settings_file.exists + assert settings_file.is_file + assert settings_file.user == 'test_usr4' + assert settings_file.group == 'test_usr4' + assert settings_file.mode == 0o600 + assert not settings_file.contains('remove_me') + + +def test_keybindings_insiders(host): + settings_file = host.file( + '/home/test_usr/.config/Code - Insiders/User/keybindings.json') + + assert settings_file.exists + assert settings_file.is_file + assert settings_file.user == 'test_usr' + assert settings_file.group == 'test_usr' + assert settings_file.mode == 0o600 + assert settings_file.contains( + '"command": "workbench.action.terminal.focus"') diff --git a/molecule/ubuntu-min/converge.yml b/molecule/ubuntu-min/converge.yml index 5e01ce9..718b48c 100644 --- a/molecule/ubuntu-min/converge.yml +++ b/molecule/ubuntu-min/converge.yml @@ -75,6 +75,15 @@ force: no mode: 'u=rw,go=' + - name: install default key bindings + become: yes + become_user: test_usr4 + copy: + content: '[{"key":"ctrl+r","command": "remove_me"}]' + dest: /home/test_usr4/.config/Code/User/keybindings.json + force: no + mode: 'u=rw,go=' + roles: - role: ansible-role-visual-studio-code visual_studio_code_skip_add_repo: yes @@ -90,13 +99,27 @@ "Vagrantfile": "ruby" } } + visual_studio_code_keybindings: [ + { + "key": "ctrl+'", + "command": "workbench.action.terminal.focus" + }, + { + "key": "ctrl+'", + "command": "workbench.action.focusActiveEditorGroup", + "when": "terminalFocus" + } + ] - username: test_usr2 visual_studio_code_extensions: [] visual_studio_code_settings: {} + visual_studio_code_keybindings: [] - username: test_usr3 - username: test_usr4 visual_studio_code_settings: {} visual_studio_code_settings_overwrite: yes + visual_studio_code_keybindings: [] + visual_studio_code_keybindings_overwrite: yes - role: ansible-role-visual-studio-code visual_studio_code_skip_add_repo: yes visual_studio_code_build: 'insiders' @@ -112,9 +135,21 @@ "Vagrantfile": "ruby" } } + visual_studio_code_keybindings: [ + { + "key": "ctrl+'", + "command": "workbench.action.terminal.focus" + }, + { + "key": "ctrl+'", + "command": "workbench.action.focusActiveEditorGroup", + "when": "terminalFocus" + } + ] - username: test_usr2 visual_studio_code_extensions: [] visual_studio_code_settings: {} + visual_studio_code_keybindings: [] - username: test_usr3 post_tasks: diff --git a/tasks/write-settings.yml b/tasks/write-settings.yml index 691f9af..a553a1e 100644 --- a/tasks/write-settings.yml +++ b/tasks/write-settings.yml @@ -41,3 +41,20 @@ (user.visual_studio_code_settings_overwrite | default(False) | bool) or (user.visual_studio_code_settings is defined and user.visual_studio_code_settings not in ({}, '', None, omit)) + +- name: write keybindings + become: yes + become_user: '{{ user.username }}' + template: + src: keybindings.json.j2 + dest: '~{{ user.username }}/{{ visual_studio_code_config_path }}/User/keybindings.json' + force: '{{ user.visual_studio_code_keybindings_overwrite | default(False) | bool }}' + mode: 'u=rw,go=' + with_items: '{{ users }}' + loop_control: + loop_var: user + label: '{{ user.username }}' + when: >- + (user.visual_studio_code_keybindings_overwrite | default(False) | bool) + or (user.visual_studio_code_keybindings is defined + and user.visual_studio_code_keybindings not in ([], '', None, omit)) diff --git a/templates/keybindings.json.j2 b/templates/keybindings.json.j2 new file mode 100644 index 0000000..ca39a4c --- /dev/null +++ b/templates/keybindings.json.j2 @@ -0,0 +1,6 @@ +// Place your key bindings in this file to override the defaults +{% if user.visual_studio_code_keybindings is iterable -%} +{{ user.visual_studio_code_keybindings | to_nice_json }} +{% else -%} +{{ '[]' }} +{% endif -%}