Module: Formatic::VscodeSnippets

Defined in:
lib/formatic/vscode_snippets.rb

Overview

Installs the gem's VS Code snippets as a symlink inside the host application's .vscode directory.

This runs in every process that boots the host application (Spring preloader, Puma workers, rake tasks, ...), so those processes race each other. A non-atomic remove-then-create (FileUtils.ln_s(force: true)) lets two processes both pass the "does it exist?" check before either creates the link, so one of them dies with Errno::EEXIST on symlink(2).

To stay race-free the link is created under a temporary name in the system temp directory and then rename(2)-ed over the target, which atomically replaces whatever is already there.

NOTE: Formatic::File (a ViewComponent) shadows File inside this module, so we refer to Ruby's file class as ::File.

Class Method Summary collapse

Class Method Details

.install(target, source) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/formatic/vscode_snippets.rb', line 24

def install(target, source)
  target = target.to_s
  source = source.to_s

  return if already_linked?(target, source)

  install_atomically(target, source)
end