Skip to content

Commit

Permalink
link command
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Jul 28, 2023
1 parent 99bf40c commit dc35eaa
Show file tree
Hide file tree
Showing 15 changed files with 373 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ gemspec
gem "rake", "~> 13.0"

gem "minitest", "~> 5.0"

gem "steep", require: false
gem "rbs"
72 changes: 72 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
PATH
remote: .
specs:
rbs-src (0.1.0)

GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.6)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
concurrent-ruby (1.2.2)
csv (3.2.7)
ffi (1.15.5)
fileutils (1.7.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.3)
language_server-protocol (3.17.0.3)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.5.3)
minitest (5.19.0)
parser (3.2.2.3)
ast (~> 2.4.1)
racc
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbs (3.1.2)
securerandom (0.2.2)
steep (1.5.2)
activesupport (>= 5.1)
concurrent-ruby (>= 1.1.10)
csv (>= 3.0.9)
fileutils (>= 1.1.0)
json (>= 2.1.0)
language_server-protocol (>= 3.15, < 4.0)
listen (~> 3.0)
logger (>= 1.3.0)
parser (>= 3.1)
rainbow (>= 2.2.2, < 4.0)
rbs (>= 3.1.0)
securerandom (>= 0.1)
strscan (>= 1.0.0)
terminal-table (>= 2, < 4)
strscan (3.0.6)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)

PLATFORMS
arm64-darwin-22

DEPENDENCIES
minitest (~> 5.0)
rake (~> 13.0)
rbs
rbs-src!
steep

BUNDLED WITH
2.4.10
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
# Rbs::Src
# rbs-src

TODO: Delete this and the text below, and describe your gem

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rbs/src`. To experiment with that code, run `bin/console` for an interactive prompt.
rbs-src allows using and editing RBS files from `rbs collection`.

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
$ gem install rbs-src

Install the gem and add to the application's Gemfile by executing:
## Usage

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
### Downloading the latest RBS files

If bundler is not being used to manage dependencies, install the gem by executing:
$ rbs-src setup --repo-prefix=tmp/rbs_collection --rbs-prefix=sig/gems

$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
### Making a symlink

## Usage
$ rbs-src link https://github.com/ruby/gem_rbs_collection.git activesupport 6.0
$ rbs-src link --repo-prefix=... --rbs-prefix=... --force https://github.com/ruby/gem_rbs_collection.git activesupport 6.0

### Opening the git repository

$ rbs-src path activesupport
$ rbs-src path --absolute activesupport

TODO: Write usage instructions here
$ rbs-src open --with=open activesupport
$ rbs-src open --with="subl -w" activesupport

## Development

Expand Down
15 changes: 15 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
D = Steep::Diagnostic

target :lib do
signature "sig"

check "lib" # Directory name

configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
# configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
# configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
# configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
# configure_code_diagnostics do |hash| # You can setup everything yourself
# hash[D::Ruby::NoMethod] = :information
# end
end
8 changes: 8 additions & 0 deletions exe/rbs-src
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby

$LOAD_PATH << File.join(__dir__, "../lib")

require "rbs/src"
require "rbs/src/cli"

exit Rbs::Src::CLI.start(ARGV)
4 changes: 4 additions & 0 deletions lib/rbs/src.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

require_relative "src/version"

require "rbs"

require "rbs/src/link"

module Rbs
module Src
class Error < StandardError; end
Expand Down
50 changes: 50 additions & 0 deletions lib/rbs/src/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "optparse"

module Rbs
module Src
class CLI
def self.start(argv)
command = argv.shift

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

OptionParser.new do |opts|
opts.on("--repo-prefix=PREFIX") { repository_prefix = Pathname(_1) }
opts.on("--rbs-prefix=PREFIX") { rbs_prefix = Pathname(_1) }
opts.on("--force") { force = true }
end.parse!(argv)

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

link = Link.new(
stdout: STDOUT,
base_path: Pathname.pwd,
repo_url: repo_url,
repository_prefix: repository_prefix,
rbs_prefix: rbs_prefix,
gem_name: gem_name,
gem_version: gem_version,
force: force,
)
link.run
0
when "open"

RBS::Collection::Config::Lockfile
1
when "setup"
1
else
puts "Unknown command: #{command}"
1
end
end
end
end
end
70 changes: 70 additions & 0 deletions lib/rbs/src/link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Rbs
module Src
class Link
attr_reader :repo_url, :base_path, :repository_prefix, :rbs_prefix, :gem_name, :gem_version, :force, :stdout

def initialize(stdout:, repo_url:, base_path:, repository_prefix:, rbs_prefix:, gem_name:, gem_version:, force:)
@stdout = stdout
@repo_url = repo_url
@base_path = base_path
@repository_prefix = repository_prefix
@rbs_prefix = rbs_prefix
@gem_name = gem_name
@gem_version = gem_version
@force = force
end

def rbs_path
rbs_prefix + "#{gem_name}-#{gem_version}"
end

def repository_path
repository_prefix + "#{gem_name}-#{gem_version}"
end

def clean
rbs_path.delete
repository_path.rmtree
end

def repository_sig_path
repository_path.join("gems", gem_name, gem_version)
end

def run
repository_prefix.mkpath
rbs_prefix.mkpath

sh!(*clone_command)

unless repository_sig_path.directory?
unless force
stdout.puts "Cannot find a directory for #{gem_name}-#{gem_version} in #{repository_path}"
return
end
end

File.symlink(repository_sig_path.relative_path_from(rbs_path.parent), rbs_path)
end

def sh!(*args, chdir: base_path)
pid = spawn(*args, chdir: chdir.to_s)
_, status = Process.waitpid2(pid)

unless status.success?
raise "Failed to execute a command in `#{chdir}`: #{args.inspect} => #{status.inspect}"
end
end

def clone_command
[
"git",
"clone",
"--filter=blob:none",
repo_url,
repository_path.to_s
]
end
end
end
end
Empty file added lib/rbs/src/lockfile_loader.rb
Empty file.
12 changes: 5 additions & 7 deletions rbs-src.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ Gem::Specification.new do |spec|
spec.authors = ["Soutaro Matsumoto"]
spec.email = ["[email protected]"]

spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = "rbs-src"
spec.description = "rbs-src"
spec.homepage = "https://github.com/soutaro/rbs-src"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"

spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
spec.metadata["source_code_uri"] = "https://github.com/soutaro/rbs-src.git"
spec.metadata["changelog_uri"] = "https://github.com/soutaro/rbs-src"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand Down
54 changes: 54 additions & 0 deletions rbs_collection.lock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
sources:
- type: git
name: ruby/gem_rbs_collection
revision: aeede7258d020bfd509541e0c8075ce833292409
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
path: ".gem_rbs_collection"
gems:
- name: abbrev
version: '0'
source:
type: stdlib
- name: json
version: '0'
source:
type: stdlib
- name: logger
version: '0'
source:
type: stdlib
- name: minitest
version: '0'
source:
type: stdlib
- name: monitor
version: '0'
source:
type: stdlib
- name: mutex_m
version: '0'
source:
type: stdlib
- name: optparse
version: '0'
source:
type: stdlib
- name: pathname
version: '0'
source:
type: stdlib
- name: rbs
version: 3.1.2
source:
type: rubygems
- name: rdoc
version: '0'
source:
type: stdlib
- name: tsort
version: '0'
source:
type: stdlib
gemfile_lock_path: Gemfile.lock
17 changes: 17 additions & 0 deletions rbs_collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Download sources
sources:
- type: git
name: ruby/gem_rbs_collection
remote: https://github.com/ruby/gem_rbs_collection.git
revision: main
repo_dir: gems

# You can specify local directories as sources also.
# - type: local
# path: path/to/your/local/repository

# A directory to install the downloaded RBSs
path: .gem_rbs_collection

gems:
- name: pathname
7 changes: 7 additions & 0 deletions sig/rbs/cli.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Rbs
module Src
class CLI
def self.start: (Array[String]) -> Integer
end
end
end
Loading

0 comments on commit dc35eaa

Please sign in to comment.