Added puppet function for creating new classes

This commit is contained in:
Ben Vincent 2023-11-10 19:21:56 +11:00
parent 3aa3e4b247
commit 0302cda360
3 changed files with 48 additions and 5 deletions

View File

@ -14,6 +14,8 @@ vim.cmd [[
autocmd!
autocmd FileType puppet nnoremap <buffer> <Leader>g :call OpenPuppetClassOrTemplate()<CR>
autocmd FileType puppet nnoremap <buffer> <Leader>t :call OpenPuppetTestMode()<CR>
autocmd BufNewFile site/roles/manifests/**.pp call ApplyPuppetTemplate()
autocmd BufNewFile site/profiles/manifests/**.pp call ApplyPuppetTemplate()
augroup END
]]

View File

@ -123,8 +123,45 @@ function! OpenPuppetClassOrTemplate()
endfunction
]])
-- -- keybindings
-- vim.cmd([[
-- nnoremap <Leader>g :call OpenPuppetClassOrTemplate()<CR>
-- nnoremap <Leader>t :call OpenPuppetTestMode()<CR>
-- ]])
vim.cmd([[
function! GeneratePuppetClassName()
" Get the full file path
let l:filepath = expand('%:p')
" Check if the file path ends with the desired patterns
if l:filepath =~ 'site/roles/manifests/.\+\.pp$' || l:filepath =~ 'site/profiles/manifests/.\+\.pp$'
" Extract the part of the path after 'manifests/'
let l:manifest_path = matchstr(l:filepath, 'manifests/.\+\.pp$')
" Remove 'manifests/' and '.pp' to get the class path
let l:class_path = substitute(l:manifest_path, 'manifests/\(.\+\)\.pp', '\1', '')
" Replace slashes with double colons to form the class name
let l:class_name = substitute(l:class_path, '/', '::', 'g')
" Detect if it is a role or profile based on the path
let l:type = l:filepath =~ 'site/roles/manifests/' ? 'roles' : 'profiles'
" Return the generated class name with the type prefix
return l:type . '::' . l:class_name
endif
" If the file path doesn't match the pattern, return an empty string
return ''
endfunction
]])
vim.cmd([[
function! ApplyPuppetTemplate()
" Generate the class name based on the file path
let l:class_name = GeneratePuppetClassName()
" If a class name was successfully generated, substitute the placeholder
if l:class_name != ''
" Read the template content
execute '0r $HOME/.config/nvim/templates/puppet/newclass.pp'
execute "1,\$s/%CLASS_NAME%/" . l:class_name . "/g"
endif
endfunction
]])

View File

@ -0,0 +1,4 @@
# %CLASS_NAME%
class %CLASS_NAME% {
# enter some class code here
}