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
34
35
# 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
  ar = RbConfig::MAKEFILE_CONFIG['AR']
  ar = " AR=#{ar}" if ar
  sh "cd #{src} && make clean &&#{ar} make libtree-sitter.a"
end

#downloadObject



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

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)


37
38
39
# File 'ext/tree_sitter/repo.rb', line 37

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

#extract?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'ext/tree_sitter/repo.rb', line 41

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

#include_and_lib_dirsObject



63
64
65
# File 'ext/tree_sitter/repo.rb', line 63

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

#sh(cmd) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'ext/tree_sitter/repo.rb', line 67

def sh(cmd)
  return if system(cmd)

  abort <<~MSG

    Failed to run: #{cmd}

    exiting …

  MSG
end

#sources_from_curl?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'ext/tree_sitter/repo.rb', line 79

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_git?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/tree_sitter/repo.rb', line 93

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

  sh <<~SHELL.chomp
    mkdir #{src} \\
    && cd #{src} \\
    && git init \\
    && git remote add origin "#{url[:git]}" \\
    && git fetch origin --depth 1 tags/v#{version} \\
    && git reset --hard FETCH_HEAD
  SHELL
  true
end

#sources_from_wget?Boolean

Returns:

  • (Boolean)


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