Module: SassConfig

Defined in:
ext/sass/sass_config.rb

Overview

The SassConfig module.

Class Method Summary collapse

Class Method Details

.dart_sassObject



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
# File 'ext/sass/sass_config.rb', line 21

def dart_sass
  repo = 'https://github.com/sass/dart-sass'

  tag_name = dart_sass_version

  message = "dart-sass for #{Platform::ARCH} not available at #{repo}/releases/tag/#{tag_name}"

  env = ''

  os = case Platform::OS
       when 'darwin'
         'macos'
       when 'linux'
         'linux'
       when 'linux-android'
         'android'
       when 'linux-musl'
         env = '-musl'
         'linux'
       when 'windows'
         'windows'
       else
         raise NotImplementedError, message
       end

  cpu = case Platform::CPU
        when 'x86_64'
          'x64'
        when 'aarch64'
          'arm64'
        when 'arm'
          'arm'
        when 'riscv64'
          'riscv64'
        else
          raise NotImplementedError, message
        end

  ext = Platform::OS == 'windows' ? 'zip' : 'tar.gz'

  "#{repo}/releases/download/#{tag_name}/dart-sass-#{tag_name}-#{os}-#{cpu}#{env}.#{ext}"
end

.dart_sass_versionObject



15
16
17
18
19
# File 'ext/sass/sass_config.rb', line 15

def dart_sass_version
  package_json(__dir__)['dependencies']['sass']
    # TODO: remove after https://github.com/sass/dart-sass/pull/2413
    .delete_prefix('file:sass-').delete_suffix('.tgz')
end

.development?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'ext/sass/sass_config.rb', line 146

def development?
  File.exist?('../../Gemfile')
end

.embedded_sass_protocolObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/sass/sass_config.rb', line 126

def embedded_sass_protocol
  require 'json'

  rubyarchdir = ENV.fetch('RUBYARCHDIR', nil)

  rubylibdir = ENV.fetch('RUBYLIBDIR', nil)

  version = Utils.capture(RbConfig.ruby,
                          "-I#{File.absolute_path('../../lib', __dir__)}",
                          *("-I#{rubyarchdir}" if rubyarchdir),
                          *("-I#{rubylibdir}" if rubylibdir),
                          File.absolute_path('../../exe/sass', __dir__),
                          '--embedded',
                          '--version')

  tag_name = JSON.parse(version)['protocolVersion']

  "https://github.com/sass/sass/raw/embedded-protocol-#{tag_name}/spec/embedded_sass.proto"
end

.gem_platformObject



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
# File 'ext/sass/sass_config.rb', line 156

def gem_platform
  platform = Gem::Platform.new("#{Platform::CPU}-#{Platform::HOST_OS}")
  case Platform::OS
  when 'darwin'
    case platform.cpu
    when 'aarch64'
      Gem::Platform.new(['arm64', platform.os])
    else
      platform
    end
  when 'linux'
    if platform.version&.start_with?('gnu')
      platform
    else
      Gem::Platform.new([platform.cpu, platform.os, "gnu#{platform.version}"])
    end
  when 'windows'
    case platform.cpu
    when 'x86_64'
      Gem::Platform.new('x64-mingw-ucrt')
    else
      Gem::Platform.new([platform.cpu, 'mingw', 'ucrt'])
    end
  else
    platform
  end
end

.gem_versionObject



150
151
152
153
154
# File 'ext/sass/sass_config.rb', line 150

def gem_version
  require_relative '../../lib/sass/embedded/version'

  development? ? dart_sass_version : Sass::Embedded::VERSION
end

.package_json(path = '.') ⇒ Object



9
10
11
12
13
# File 'ext/sass/sass_config.rb', line 9

def package_json(path = '.')
  require 'json'

  JSON.parse(File.read(File.absolute_path('package.json', path)))
end

.protocObject



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
# File 'ext/sass/sass_config.rb', line 64

def protoc
  repo = 'https://repo.maven.apache.org/maven2/com/google/protobuf/protoc'

  dependency = Gem::Dependency.new('google-protobuf')

  spec = dependency.to_spec

  version = spec.version

  message = "protoc for #{Platform::ARCH} not available at #{repo}/#{version}"

  os = case Platform::OS
       when 'darwin'
         'osx'
       when 'linux', 'linux-android', 'linux-musl', 'linux-none', 'linux-uclibc'
         'linux'
       when 'windows'
         'windows'
       else
         raise NotImplementedError, message
       end

  cpu = case Platform::CPU
        when 'i386'
          'x86_32'
        when 'x86_64'
          'x86_64'
        when 'aarch64'
          Platform::OS == 'windows' ? 'x86_64' : 'aarch_64'
        when 'ppc64le'
          'ppcle_64'
        when 's390x'
          's390_64'
        else
          raise NotImplementedError, message
        end

  uri = "#{repo}/#{version}/protoc-#{version}-#{os}-#{cpu}.exe"

  Utils.fetch_https("#{uri}.sha1")

  uri
rescue Gem::RemoteFetcher::FetchError
  dependency_request = Gem::Resolver::DependencyRequest.new(dependency, nil)

  versions = Gem::Resolver::BestSet.new.find_all(dependency_request).filter_map do |s|
    s.version if s.platform == Gem::Platform::RUBY
  end

  versions.sort.reverse_each do |v|
    uri = "#{repo}/#{v}/protoc-#{v}-#{os}-#{cpu}.exe"

    Utils.fetch_https("#{uri}.sha1")

    return uri
  rescue Gem::RemoteFetcher::FetchError
    next
  end

  raise NotImplementedError, message
end