Skip to content

Commit

Permalink
Added support for managing keybindings.json (#226)
Browse files Browse the repository at this point in the history
Co-authored-by: Oskar Moge <[email protected]>
  • Loading branch information
freemanjp and mowge authored Oct 6, 2022
1 parent ac9327f commit 0c96805
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions molecule/code-only/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions molecule/code-only/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"')
35 changes: 35 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions molecule/default/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"')
35 changes: 35 additions & 0 deletions molecule/ubuntu-min/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions tasks/write-settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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))
6 changes: 6 additions & 0 deletions templates/keybindings.json.j2
Original file line number Diff line number Diff line change
@@ -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 -%}

0 comments on commit 0c96805

Please sign in to comment.