Class: Rbs::Src::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/src/cli.rb

Class Method Summary collapse

Class Method Details

.has_gem?(name) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
244
245
246
# File 'lib/rbs/src/cli.rb', line 241

def self.has_gem?(name)
  ::Gem::Specification.find_by_name(name)
  true
rescue
  false
end

.start(argv, stdout: STDOUT) ⇒ Object



6
7
8
9
10
11
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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rbs/src/cli.rb', line 6

def self.start(argv, stdout: STDOUT)
  command = argv.shift

  case command
  when "link"
    repository_prefix = Pathname("tmp/rbs-src")
    rbs_prefix = Pathname("sig/rbs-src")
    repository_dir = Pathname("gems")

    OptionParser.new do |opts|
      opts.on("--repo-prefix=PREFIX", "The location to put repository in (default: #{repository_prefix})") { repository_prefix = Pathname(_1) }
      opts.on("--repo-dir=DIR", "The collection repository dir name (default: #{repository_dir})") { repository_dir = Pathname(_1) }
      opts.on("--rbs-prefix=PREFIX", "The location to put symlinks in (default: #{rbs_prefix})") { rbs_prefix = Pathname(_1) }

      opts.banner = <<~BANNER
        Usage: rbs-src link [options] repo_url gem_name gem_version

        Clone the repository and set up a symlink.

        Example:
            $ rbs-src link https://github.com/ruby/gem_rbs_collection.git activerecord 7.0
            $ rbs-src link https://github.com/ruby/gem_rbs_collection.git parseg 1.0

        Options:
      BANNER
    end.parse!(argv)

    repository_prefix.mkpath
    rbs_prefix.mkpath

    repo_url = argv.shift or raise
    gem_name = argv.shift or raise
    gem_version = argv.shift or raise

    gem = Gem.new(
      name: gem_name,
      version: gem_version,
      repository_prefix: repository_prefix,
      repository_dir: repository_dir,
      rbs_prefix: rbs_prefix
    )

    runner = CommandRunner.new(stdout: stdout)

    runner.push "Setting up #{gem.name}-#{gem.version}" do
      runner.push "Cloning git repository into #{gem.repository_root}" do
        if gem.repository_root.directory?
          runner.puts "Skipping already exist"
        else
          gem.clone(runner, repository_url: repo_url, commit: nil)
        end
      end

      runner.push "Linking to #{gem.rbs_path}" do
        unless gem.rbs_path.exist?
          runner.puts "File.symlink..."
          gem.link()
        else
          runner.puts "Skipping already exist"
        end
      end
    end

    0

  when "status"
    rbs_collection_lock_path = RBS::Collection::Config.to_lockfile_path(RBS::Collection::Config::PATH)
    repository_prefix = Pathname("tmp/rbs-src")

    OptionParser.new do |opts|
      opts.on("--rbs-collection-lock=PATH", "The path to rbs_collection.lock.yaml (default: #{rbs_collection_lock_path})") { rbs_collection_lock_path = Pathname(_1) }
      opts.on("--repo-prefix=PREFIX", "The location to put repository in (default: #{repository_prefix})") { repository_prefix = Pathname(_1) }

      opts.banner = <<~BANNER
        Usage: rbs-src status

        Prints the status of git repositories.

        Example:
            $ rbs-src status [GEMs...]

        Options:
      BANNER
    end.parse!(argv)

    unless argv.empty?
      gems = Set.new(argv)
    end

    repository_prefix.mkpath

    lockfile = RBS::Collection::Config::Lockfile.from_lockfile(
      lockfile_path: rbs_collection_lock_path,
      data: YAML.safe_load(rbs_collection_lock_path.read)
    )

    loader = LockfileLoader.new(
      lockfile: lockfile,
      repository_prefix: repository_prefix,
      rbs_prefix: rbs_prefix
    )

    non_ok_count = 0

    runner = CommandRunner.new(stdout: stdout)

    loader.each_gem do |gem, repo, commit|
      if !gems || gems.include?(gem.name)
        status = gem.status(runner, commit: commit)
        case status
        when :ok
          string = Rainbow("ok").blue
        when :dirty
          string = Rainbow("dirty").red
        when :commit_mismatch
          non_ok_count = non_ok_count + 1
          string = Rainbow("commit_mismatch").yellow
        end

        stdout.puts "[#{string}] #{gem.name} #{gem.repository_root}"
      end
    end

    non_ok_count == 0 ? 0 : 1

  when "setup"
    rbs_collection_lock_path = RBS::Collection::Config.to_lockfile_path(RBS::Collection::Config::PATH)
    repository_prefix = Pathname("tmp/rbs-src")
    rbs_prefix = Pathname("sig/rbs-src")
    output_path= nil #: Pathname?

    OptionParser.new do |opts|
      opts.on("--rbs-collection-lock=PATH", "The path to rbs_collection.lock.yaml (default: #{rbs_collection_lock_path})") { rbs_collection_lock_path = Pathname(_1) }
      opts.on("--repo-prefix=PREFIX", "The location to put repository in (default: #{repository_prefix})") { repository_prefix = Pathname(_1) }
      opts.on("--rbs-prefix=PREFIX", "The location to put symlinks in (default: #{rbs_prefix})") { rbs_prefix = Pathname(_1) }
      opts.on("--output[=PATH]", "-o[PATH]", "The path to write the list of stdlib dependencies (default: rbs_src.dep)") {|path| output_path = Pathname(path || "rbs_src.dep") }

      opts.banner = <<~BANNER
        Usage: rbs-src setup [options]

        Set up git repositories and symlinks in rbs_collection.yaml.

        Example:
            $ rbs-src setup

        Options:
      BANNER
    end.parse!(argv)

    repository_prefix.mkpath
    rbs_prefix.mkpath

    lockfile = RBS::Collection::Config::Lockfile.from_lockfile(
      lockfile_path: rbs_collection_lock_path,
      data: YAML.safe_load(rbs_collection_lock_path.read)
    )

    loader = LockfileLoader.new(
      lockfile: lockfile,
      repository_prefix: repository_prefix,
      rbs_prefix: rbs_prefix
    )

    runner = CommandRunner.new(stdout: stdout)

    loader.each_gem do |gem, repo, commit|
      runner.push "Setting up #{gem.name}-#{gem.version}" do
        if gem.repository_root.directory?
          runner.push "Checking out commit in #{gem.repository_root}" do
            gem.checkout(runner, repository_url: repo, commit: commit)
        end
        else
          runner.push "Cloning git repository into #{gem.repository_root}" do
            gem.clone(runner, repository_url: repo, commit: commit)
          end
        end

        runner.push "Linking to #{gem.rbs_path}" do
          unless gem.rbs_path.exist?
            runner.puts "File.symlink..."
            gem.link()
          else
            runner.puts "Skipping already exist"
          end
        end
      end
    end

    other_libs = [] #: Array[[String, String]]
    lockfile.gems.each_value do |gem|
      case source = gem[:source]
      when RBS::Collection::Sources::Git
        # skip
      else
        other_libs << [gem[:name], gem[:version]]
      end
    end

    other_libs.sort_by! { _1[0] }

    if output_path
      runner.push "Writing dependencies to #{output_path}:" do
        output_path.open("w") do |io|
          other_libs.each do |name, _|
            io.puts name
          end
        end
      end
    else
      unless other_libs.empty?
        runner.push "You have to load other libraries without rbs-collection:" do
          if has_gem?("steep")
            runner.puts "Add the following lines in your Steepfile:"

            other_libs.each do |name, version|
              runner.puts "  library('#{name}')"
            end
          else
            other_libs.each do |name, version|
              runner.puts "-r #{name} \\"
            end
          end
        end
      end
    end

    0
  else
    puts "Unknown command: #{command}"
    puts
    puts "  known commands: setup, status, link"
    1
  end
end