Class: TRMNLP::Commands::Init

Inherits:
Base
  • Object
show all
Defined in:
lib/trmnlp/commands/init.rb

Defined Under Namespace

Classes: Options

Instance Method Summary collapse

Methods inherited from Base

#initialize, options_from, run

Constructor Details

This class inherits a constructor from TRMNLP::Commands::Base

Instance Method Details

#call(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/trmnlp/commands/init.rb', line 12

def call(name)
  destination_dir = Pathname.new(options.dir).join(name)

  unless destination_dir.exist?
    reporter.info "Creating #{destination_dir}"
    destination_dir.mkpath
  end

  # NOTE: FNM_DOTMATCH so the glob descends into hidden template
  # directories (e.g. .github/); without it those files are skipped.
  template_dir.glob('**/{*,.*}', File::FNM_DOTMATCH).each do |source_pathname|
    next if source_pathname.directory?
    next if options.skip_liquid && source_pathname.extname == '.liquid'

    relative_pathname = source_pathname.relative_path_from(template_dir)
    destination_pathname = destination_dir.join(relative_pathname)
    destination_pathname.dirname.mkpath

    if destination_pathname.exist?
      answer = prompt("#{destination_pathname} already exists. Overwrite? (y/n): ").downcase
      if answer != 'y'
        reporter.info "Skipping #{destination_pathname}"
        next
      end
    end

    reporter.info "Creating #{destination_pathname}"
    FileUtils.cp(source_pathname, destination_pathname)
    # NOTE: cp preserves the source mode. Templates installed read-only
    # (e.g. NixOS /nix/store is 0444) would leave the author unable to
    # edit their own project. Add owner-write; keep any exec bit.
    destination_pathname.chmod(destination_pathname.stat.mode | 0o200)
  end

  init_git_repo(destination_dir) unless options.skip_git

  reporter.info <<~HEREDOC

    To start the local server:

        cd #{Pathname.new(destination_dir).relative_path_from(Dir.pwd)}
        trmnlp serve

    To publish the plugin:

        trmnlp login
        trmnlp push
  HEREDOC
end