Method: Bundler::Thor::Runner#install

Defined in:
lib/bundler/vendor/thor/lib/thor/runner.rb

#install(name) ⇒ Object

rubocop:disable Metrics/MethodLength

[View source]

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bundler/vendor/thor/lib/thor/runner.rb', line 46

def install(name) # rubocop:disable Metrics/MethodLength
  initialize_thorfiles

  is_uri  = name =~ %r{^https?\://}

  if is_uri
    base = name
    package = :file
    require "open-uri"
    begin
      contents = URI.open(name, &:read)
    rescue OpenURI::HTTPError
      raise Error, "Error opening URI '#{name}'"
    end
  else
    # If a directory name is provided as the argument, look for a 'main.thor'
    # command in said directory.
    begin
      if File.directory?(File.expand_path(name))
        base = File.join(name, "main.thor")
        package = :directory
        contents = File.open(base, &:read)
      else
        base = name
        package = :file
        require "open-uri"
        contents = URI.open(name, &:read)
      end
    rescue Errno::ENOENT
      raise Error, "Error opening file '#{name}'"
    end
  end

  say "Your Thorfile contains:"
  say contents

  unless options["force"]
    return false if no?("Do you wish to continue [y/N]?")
  end

  as = options["as"] || begin
    first_line = contents.split("\n")[0]
    (match = first_line.match(/\s*#\s*module:\s*([^\n]*)/)) ? match[1].strip : nil
  end

  unless as
    basename = File.basename(name)
    as = ask("Please specify a name for #{name} in the system repository [#{basename}]:")
    as = basename if as.empty?
  end

  location = if options[:relative] || is_uri
    name
  else
    File.expand_path(name)
  end

  thor_yaml[as] = {
    filename: Digest::SHA256.hexdigest(name + as),
    location: location,
    namespaces: Bundler::Thor::Util.namespaces_in_content(contents, base)
  }

  save_yaml(thor_yaml)
  say "Storing thor file in your system repository"
  destination = File.join(thor_root, thor_yaml[as][:filename])

  if package == :file
    File.open(destination, "w") { |f| f.puts contents }
  else
    require "fileutils"
    FileUtils.cp_r(name, destination)
  end

  thor_yaml[as][:filename] # Indicate success
end