Class: Distil::Library

Inherits:
Configurable show all
Defined in:
lib/distil/library.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Configurable

alias_config_key, #configure_with, #key_for_alias

Constructor Details

#initialize(config, project) ⇒ Library

Returns a new instance of Library.



10
11
12
13
14
15
16
17
18
19
20
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
63
64
65
66
67
68
69
70
71
# File 'lib/distil/library.rb', line 10

def initialize(config, project)
  @project= project
  
  if config.is_a?(String)
    config= {
      :href=>config
    }
  end

  configure_with(config) do |c|
    
    c.with :href do |href|
      @href= URI.parse(href)
      case when svn_url?
        @protocol= :svn
      when git_url?
        @protocol= :git
      when http_folder?
        @protocol= :http_recursive
      else
        @protocol= :http
      end
    end
    
  end
  
  @name ||= File.basename(href.path, ".*")

  if @path.nil?
    parts= [LIBRARY_CACHE_FOLDER, href.host, File.dirname(href.path)]
    case when svn_url? || git_url?
      parts << File.basename(href.path, ".*")
    when http_folder?
      parts << File.basename(href.path)
    end
    @path= File.join(*parts)
    @path << "-#{@version}" unless @version.nil?
  end
  
  @path= File.expand_path(@path)
  
  update if !up_to_date?
  
  Dir.chdir(path) do
    case
    when File.exist?("Buildfile") || File.exists?("buildfile") || File.exist?("#{name}.jsproj")
      @build_command= APP_SCRIPT
      remote_project= Project.find(path)
      @product_path= remote_project ? remote_project.output_path : File.join(path, 'build')
    when File.exist?("Makefile") || File.exist?("makefile")
      @build_command= "make"
    when File.exists?("Rakefile") || File.exists?("rakefile")
      @build_command= "rake"
    when File.exists?("Jakefile") || File.exists?("jakefile")
      @build_command= "jake"
    else
      @build_command= ""
    end
  end
  
  # build
end

Instance Attribute Details

#build_commandObject (readonly)

Returns the value of attribute build_command.



8
9
10
# File 'lib/distil/library.rb', line 8

def build_command
  @build_command
end

#hrefObject (readonly)

Returns the value of attribute href.



7
8
9
# File 'lib/distil/library.rb', line 7

def href
  @href
end

#include_pathObject (readonly)

Returns the value of attribute include_path.



7
8
9
# File 'lib/distil/library.rb', line 7

def include_path
  @include_path
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/distil/library.rb', line 7

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/distil/library.rb', line 7

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



7
8
9
# File 'lib/distil/library.rb', line 7

def project
  @project
end

#protocolObject (readonly)

Returns the value of attribute protocol.



8
9
10
# File 'lib/distil/library.rb', line 8

def protocol
  @protocol
end

#revisionObject (readonly)

Returns the value of attribute revision.



8
9
10
# File 'lib/distil/library.rb', line 8

def revision
  @revision
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/distil/library.rb', line 7

def version
  @version
end

Instance Method Details

#buildObject



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/distil/library.rb', line 196

def build
  update unless up_to_date?

  unless build_command.empty?
    Dir.chdir(path) do
      exit 1 if !system("#{build_command}")
    end
  end
  
  File.unlink(output_path) if File.symlink?(output_path)
  File.symlink(project.relative_output_path_for(product_path), output_path)
end

#content_for(content_type, variant = RELEASE_VARIANT) ⇒ Object



209
210
211
212
213
# File 'lib/distil/library.rb', line 209

def content_for(content_type, variant=RELEASE_VARIANT)
  file= file_for(content_type, variant)
  return nil if !file
  File.read(file)
end

#fetchObject



145
146
147
# File 'lib/distil/library.rb', line 145

def fetch
  self.send "fetch_with_#{@protocol}"
end

#fetch_with_gitObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/distil/library.rb', line 108

def fetch_with_git
  require_git
  FileUtils.mkdir_p(path)
  Dir.chdir path do
    clone_cmd  = "git clone"
    clone_cmd += " -b #{version}" unless version.nil?
    clone_cmd += " #{href} ."
    clone_cmd += " -q"
    if !system(clone_cmd)
      raise ValidationError.new("Failed to clone external project: #{href}")
    end
  end          
end

#fetch_with_httpObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/distil/library.rb', line 122

def fetch_with_http
  if !href.respond_to?(:read)
    raise ValidationError, "Cannot read from project source url: #{href}"
  end

  FileUtils.mkdir_p(path)
  begin
    text= href.read
  rescue OpenURI::HTTPError => http_error
    raise ValidationError, "Unable to fetch remote project: status=#{http_error.io.status[0]} url=#{href}"
  end
  File.open(File.join(path, File.basename(href.path)), "w") { |output|
    output.write text
  }
end

#fetch_with_http_recursiveObject



138
139
140
141
142
143
# File 'lib/distil/library.rb', line 138

def fetch_with_http_recursive
  dir= File.dirname(path)
  FileUtils.mkdir_p(dir)
  fetcher= Distil::RecursiveHTTPFetcher.new(href, 1, dir)
  fetcher.fetch
end

#file_for(content_type, language = nil, variant = RELEASE_VARIANT) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/distil/library.rb', line 215

def file_for(content_type, language=nil, variant=RELEASE_VARIANT)
  if language
    file= File.join(output_path, "#{name}-#{language}-#{variant}.#{content-type}")
    return file if File.exists?(file)
    file= File.join(output_path, "#{name}-#{language}-release.#{content-type}")
    return file if File.exists?(file)
    file= File.join(output_path, "#{name}-#{language}.#{content-type}")
    return file if File.exists?(file)
  end
  
  file= File.join(output_path, "#{name}-#{variant}.#{content_type}")
  return file if File.exists?(file)
  file= File.join(output_path, "#{name}-release.#{content_type}")
  return file if File.exists?(file)
  file= File.join(output_path, "#{name}.#{content_type}")
  return file if File.exists?(file)
  
  candidates= Dir.glob(File.join(output_path, "*.#{content_type}"))
  return candidates.first if 1==candidates.length
  
  nil
end

#git_url?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/distil/library.rb', line 89

def git_url?
  "#{@href}" =~ /^git:\/\// || "#{@href}" =~ /\.git$/
end

#http_folder?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/distil/library.rb', line 93

def http_folder?
  File.extname(href.path).empty?
end

#output_pathObject



170
171
172
# File 'lib/distil/library.rb', line 170

def output_path
  File.join(project.output_path, name)
end

#product_pathObject



81
82
83
# File 'lib/distil/library.rb', line 81

def product_path
  @product_path || path
end

#require_gitObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/distil/library.rb', line 97

def require_git
  begin
    `git --version 2>/dev/null`
  rescue
    nil
  end
  if $?.nil? || !$?.success?
    raise ValidationError.new("The git version control tool is required to pull this repository: #{uri}")
  end
end

#svn_url?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/distil/library.rb', line 85

def svn_url?
  "#{@href}" =~ /svn(?:\+ssh)?:\/\/*/
end

#to_sObject



73
74
75
# File 'lib/distil/library.rb', line 73

def to_s
  "Library: #{name} @ #{path}"
end

#up_to_date?Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/distil/library.rb', line 174

def up_to_date?
  return false unless File.exists?(path)
  return @up_to_date unless @up_to_date.nil?
  
  case protocol
  when :git
    require_git
    Dir.chdir path do
      current_sha1= `git rev-parse #{version}`
      origin_sha1= `git ls-remote #{href} #{version}`
      if $?.exitstatus!=0
        project.error("Could not determine whether library is up to date: #{name}")
        return @up_to_date=true
      end
      origin_sha1= origin_sha1.split(/\s/)[0]
      return @up_to_date= (current_sha1.strip==origin_sha1.strip)
    end
  else
    @up_to_date= true
  end
end

#updateObject



162
163
164
165
166
167
168
# File 'lib/distil/library.rb', line 162

def update
  if File.exists?(path)
    self.send "update_with_#{@protocol}"
  else
    fetch
  end
end

#update_with_gitObject



149
150
151
152
153
154
155
156
# File 'lib/distil/library.rb', line 149

def update_with_git
  require_git
  Dir.chdir path do
    command = "git pull"
    command << " origin #{version}" if version
    `#{command}`
  end
end

#update_with_httpObject



158
159
160
# File 'lib/distil/library.rb', line 158

def update_with_http
  fetch_with_http
end