Class: ReaPack::Index::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/reapack/index/scanner.rb

Constant Summary collapse

PROVIDES_VALIDATOR =
proc {|value|
  begin
    Provides.parse_each(value).to_a and nil
  rescue Error => e
    e.message
  end
}
VERSION_SEGMENT_MAX =
(2 ** 16) - 1
SIMPLE_TAG =
[MetaHeader::VALUE, MetaHeader::SINGLELINE].freeze
HEADER_RULES =
{
  # package-wide tags
  version: [
    MetaHeader::REQUIRED, MetaHeader::VALUE, MetaHeader::SINGLELINE, /\A\d/,
    proc {|v|
      s = v.scan(/\d+/).find {|s| s.to_i > VERSION_SEGMENT_MAX }
      'segment overflow (%d > %d)' % [s, VERSION_SEGMENT_MAX] if s
    }
  ],
  about: MetaHeader::VALUE,
  description: SIMPLE_TAG,
  donation: MetaHeader::VALUE,
  link: MetaHeader::VALUE,
  noindex: MetaHeader::BOOLEAN,
  screenshot: MetaHeader::VALUE,

  # version-specific tags
  author: SIMPLE_TAG,
  changelog: MetaHeader::VALUE,
  metapackage: MetaHeader::BOOLEAN,
  provides: [MetaHeader::VALUE, PROVIDES_VALIDATOR],
}.freeze
HEADER_ALIASES =
{
  [:reascript_name, :jsfx_name, :theme_name,
    :extension_name, :langpack_name, :webinterface_name,
    :desc, :name] => :description,
  [:links, :website] => :link,
  :donate => :donation,
  :screenshots => :screenshot,
}.freeze
META_TYPES =
[:extension, :data, :theme, :webinterface].freeze

Instance Method Summary collapse

Constructor Details

#initialize(cat, pkg, mh, index) ⇒ Scanner

Returns a new instance of Scanner.



49
50
51
52
53
# File 'lib/reapack/index/scanner.rb', line 49

def initialize(cat, pkg, mh, index)
  @cat, @pkg, @mh, @index = cat, pkg, mh, index
  @cselector = @index.cdetector[pkg.path]
  @self_overriden = false
end

Instance Method Details



216
217
218
219
220
221
222
223
224
225
# File 'lib/reapack/index/scanner.rb', line 216

def eval_links(type, tag: nil)
  tag ||= type

  @pkg..replace_links type do
    @mh[tag].to_s.each_line {|line|
      line.strip!
      @pkg..push_link type, *Link.split(line) unless line.empty?
    }
  end
end

#make_url(path, template = nil) ⇒ Object



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
# File 'lib/reapack/index/scanner.rb', line 102

def make_url(path, template = nil)
  unless template
    unless template = @index.url_template
      raise Error, 'unable to generate download links: empty url template'
    end

    unless @index.files.include? path
      raise Error, "file not found '#{path}'"
    end
  end

  substitutions = {
    '$path'    => path,
    '$commit'  => @index.commit || 'master',
    '$version' => @ver.name,
    '$package' => @pkg.path,
  }

  uri = Addressable::URI.parse template
  [:host, :path, :query].each {|segment|
    value = uri.send segment

    if value
      value.gsub! /\$\w+/, substitutions
      uri.send "#{segment}=", value
    end
  }
  uri.normalize.to_s
end

#metapackage?Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
# File 'lib/reapack/index/scanner.rb', line 227

def metapackage?
  @metapackage ||= if @mh[:metapackage].nil?
    META_TYPES.include? @pkg.type
  else
    @mh[:metapackage]
  end
end

#parse_provides(provides) ⇒ Object



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
# File 'lib/reapack/index/scanner.rb', line 132

def parse_provides(provides)
  pathdir = Pathname.new @pkg.category

  Provides.parse_each(provides).map {|line|
    line.file_pattern = @pkg.name if line.file_pattern == '.'

    expanded = ReaPack::Index.expand line.file_pattern, @pkg.category

    if expanded == @pkg.path
      # always resolve path even when an url template is set
      files = [expanded]
    elsif line.url_template.nil?
      files = @index.files.select {|f|
        File.fnmatch expanded, f, File::FNM_PATHNAME | File::FNM_EXTGLOB
      }
      raise Error, "file not found '#{line.file_pattern}'" if files.empty?
    else
      # use the relative path for external urls
      if line.file_pattern.start_with? '/'
        prefix = []
        dirs = File.split(@pkg.category).size - 1
        dirs.times { prefix << '..' }

        files = [File.join(*prefix, line.file_pattern)]
      else
        files = [line.file_pattern]
      end
    end

    files.map {|file|
      if line.target
        if line.target =~ /[\/\\\.]+\z/
          new_dir = ReaPack::Index.expand line.target, ''
          base_file = File.basename file
          target = new_dir.empty? ? base_file : File.join(new_dir, base_file)
        else
          target = line.target
        end

        expanded = ReaPack::Index.expand target, @pkg.category
      else
        target = file
      end

      src = Source.new make_url(file, line.url_template)
      src.platform = line.platform
      src.type = line.type

      case line.main?
      when true
        src.detect_sections @pkg
      when Array
        src.sections = line.main?
      end

      @cselector.push src.type || @pkg.type, src.platform,
        line.url_template || line.target ? expanded : target

      if file == @pkg.path
        if metapackage?
          # the current file is still added as a source even if @metapackage
          # is enabled but won't be added to the Action List unless specified
        elsif line.main.nil?
          # not a metapackage? then the current file is registered by default
          src.detect_sections @pkg
        end

        src.file = target if line.target && expanded != file
        @self_overriden = true
      else
        if line.url_template
          src.file = file
        elsif line.target
          src.file = target
        else
          src.file = Pathname.new(file).relative_path_from(pathdir).to_s
        end
      end

      src
    }
  }.flatten
end

#run(strict = false) ⇒ Object



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
# File 'lib/reapack/index/scanner.rb', line 55

def run(strict = false)
  @mh.alias HEADER_ALIASES

  errors = @mh.validate(HEADER_RULES, strict)

  unless errors.empty?
    raise Error, errors.join("\n")
  end

  @pkg.description = @mh[:description]
  @pkg..about = @mh[:about]

  eval_links :website, tag: :link
  eval_links :screenshot
  eval_links :donation

  @pkg.version @mh[:version] do |ver|
    next unless ver.is_new? || @index.amend

    @ver = ver

    ver.author = @mh[:author]
    ver.time = @index.time if @index.time && ver.is_new?
    ver.changelog = @mh[:changelog]

    ver.replace_sources do
      @cselector.clear
      sources = parse_provides @mh[:provides]

      unless metapackage? || @self_overriden
        # add the package itself as a main source
        src = Source.new make_url(@pkg.path)
        src.detect_sections @pkg
        sources.unshift src

        @cselector.push @pkg.type, src.platform, @pkg.path
      end

      sources.each {|src| ver.add_source src }
    end
  end

  if cons = @cselector.resolve
    raise Error, cons.first unless cons.empty?
  end
end