Class: TreeSitter::Repo

Inherits:
Object
  • Object
show all
Defined in:
ext/tree_sitter/repo.rb

Overview

Fetches tree-sitter sources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepo

Returns a new instance of Repo.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'ext/tree_sitter/repo.rb', line 10

def initialize
  @version = TREESITTER_VERSION

  # `tree-sitter-@version` is the name produced by tagged releases of sources
  # by git, so we use it everywhere, including when cloning from git.
  @src = Pathname.pwd / "tree-sitter-#{@version}"

  @url = {
    git: 'https://github.com/tree-sitter/tree-sitter',
    tar: "https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v#{@version}.tar.gz",
    zip: "https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v#{@version}.zip"
  }

  @exe = {}
  %i[curl git tar wget zip].each do |cmd|
    @exe[cmd] = find_executable(cmd.to_s)
  end
end

Instance Attribute Details

#exeObject (readonly)

Returns the value of attribute exe.



8
9
10
# File 'ext/tree_sitter/repo.rb', line 8

def exe
  @exe
end

#srcObject (readonly)

Returns the value of attribute src.



8
9
10
# File 'ext/tree_sitter/repo.rb', line 8

def src
  @src
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'ext/tree_sitter/repo.rb', line 8

def url
  @url
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'ext/tree_sitter/repo.rb', line 8

def version
  @version
end

Instance Method Details

#compileObject



29
30
31
32
33
# File 'ext/tree_sitter/repo.rb', line 29

def compile
  # We need to clean because the same folder is used over and over
  # by rake-compiler-dock
  sh "cd #{src} && make clean && make"
end

#downloadObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'ext/tree_sitter/repo.rb', line 43

def download
  # TODO: should we force re-download? Maybe with a flag?
  return true if Dir.exist? src

  res = false
  %w[git curl wget].each do |cmd|
    res =
      if find_executable(cmd)
        send("sources_from_#{cmd}")
      else
        false
      end
    break if res
  end

  res
end

#exe?(name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'ext/tree_sitter/repo.rb', line 35

def exe?(name)
  @exe[name]
end

#extract?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'ext/tree_sitter/repo.rb', line 39

def extract?
  !exe.filter { |k, v| %i[tar zip].include?(k) && v }.empty?
end

#include_and_lib_dirsObject



61
62
63
# File 'ext/tree_sitter/repo.rb', line 61

def include_and_lib_dirs
  [[src / 'lib' / 'include'], [src.to_s]]
end

#keep_static_libObject



65
66
67
68
69
70
# File 'ext/tree_sitter/repo.rb', line 65

def keep_static_lib
  src
    .children
    .filter { |f| /\.(dylib|so)/ =~ f.basename.to_s }
    .each(&:unlink)
end

#sh(cmd) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'ext/tree_sitter/repo.rb', line 72

def sh(cmd)
  return if system(cmd)

  abort <<~MSG

    Failed to run: #{cmd}

    exiting …

  MSG
end

#sources_from_curlObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'ext/tree_sitter/repo.rb', line 84

def sources_from_curl
  return false if !exe?(:curl) || !extract?

  if exe?(:tar)
    sh "curl -L #{url[:tar]} -o tree-sitter-v#{version}.tar.gz"
    sh "tar -xf tree-sitter-v#{version}.tar.gz"
  elsif exe?(:zip)
    sh "curl -L #{url[:zip]} -o tree-sitter-v#{version}.zip"
    sh "unzip -q tree-sitter-v#{version}.zip"
  end

  true
end

#sources_from_gitObject



98
99
100
101
102
103
104
105
# File 'ext/tree_sitter/repo.rb', line 98

def sources_from_git
  return false if !exe?(:git)

  sh "git clone #{url[:git]} #{src}"
  sh "cd #{src} && git checkout tags/v#{version}"

  true
end

#sources_from_wgetObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/tree_sitter/repo.rb', line 107

def sources_from_wget
  return false if !exe?(:wget) || !extract?

  if exe?(:tar)
    sh "wget #{url[:tar]} -O tree-sitter-v#{version}.tar.gz"
    sh "tar -xf tree-sitter-v#{version}.tar.gz"
  elsif exe?(:zip)
    sh "wget #{url[:zip]} -O tree-sitter-v#{version}.zip"
    sh "unzip -q tree-sitter-v#{version}.zip"
  end

  true
end