Class: Crate::Dependency

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Utils
Defined in:
lib/crate/dependency.rb

Overview

Create a build task that will download, checksum and build and install an upstream source

This task will create the following targets:

Direct Known Subclasses

GemIntegration, Ruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, version = nil) {|_self| ... } ⇒ Dependency

Create a Crate Dependency with the given name and version

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
38
39
40
41
42
# File 'lib/crate/dependency.rb', line 34

def initialize( name = nil, version = nil )
  @name = name
  @version = version
  @install_commands = []
  @build_commands = []
  yield self if block_given?
  @upstream_source = URI.parse( @upstream_source )
  define unless name.nil? or version.nil?
end

Instance Attribute Details

#build_commandsObject

array of shell commands for building



26
27
28
# File 'lib/crate/dependency.rb', line 26

def build_commands
  @build_commands
end

#install_commandsObject

array of shell commands for installing



29
30
31
# File 'lib/crate/dependency.rb', line 29

def install_commands
  @install_commands
end

#nameObject

Name of the task, this is also the Rake namespace underwhich all other tasks will follow



17
18
19
# File 'lib/crate/dependency.rb', line 17

def name
  @name
end

#upstream_sourceObject

Upstream location



23
24
25
# File 'lib/crate/dependency.rb', line 23

def upstream_source
  @upstream_source
end

#versionObject

Version of the upstream version



20
21
22
# File 'lib/crate/dependency.rb', line 20

def version
  @version
end

Instance Method Details

#buildObject

Execute all the build commands



237
238
239
# File 'lib/crate/dependency.rb', line 237

def build
  cd_and_sh( pkg_dir, build_commands )
end

#build_dirObject

The build directory for this particular task



47
48
49
# File 'lib/crate/dependency.rb', line 47

def build_dir
  @build_dir ||= File.join(Crate.project.build_dir, name )
end

#cd_and_sh(dir, cmds) ⇒ Object

Change to a directory and execute a sequence of commands



251
252
253
254
255
256
257
# File 'lib/crate/dependency.rb', line 251

def cd_and_sh( dir, cmds )
  Dir.chdir( dir ) do
    cmds.each do |cmd|
      sh cmd
    end
  end
end

#defineObject

Define all the tasks in the namespace of the name of this task.

The dependency chain is:

:install => :build => :patch => :unpack => :verify => :download


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/crate/dependency.rb', line 130

def define
  logger.debug "Defining tasks for #{name} #{version}"

  namespace "#{name}" do
    define_download
    define_verify
    define_unpack
    define_patch
    define_build
    define_install

    task :done    => "#{name}:install"
    task :default => "#{name}:done"
  end

  desc "Build and Install #{name} #{version}"
  task name => "#{name}:default"
end

#define_buildObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/crate/dependency.rb', line 200

def define_build
  desc "Build #{name} #{version}"
  task :build => dotfile( 'build' ) do
    logger.info "#{name} #{version} built"
  end

  file dotfile( 'build' ) => "#{name}:patch" do
    logger.info "Bulding #{name} #{version}"
    Dir.chdir( pkg_dir ) do
      build
    end
    dotfile!( 'build' )
  end
  ::CLEAN << dotfile( 'build' )
end

#define_downloadObject



149
150
151
152
153
154
155
156
157
# File 'lib/crate/dependency.rb', line 149

def define_download
  file local_source do |t|
    logger.info "Downloading #{upstream_source} to #{t.name}"
    download( upstream_source, t.name )
  end

  desc "Download #{File.basename( local_source )}"
  task :download => local_source
end

#define_installObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/crate/dependency.rb', line 216

def define_install
  desc "Install #{name} into #{Crate.project.install_dir}"
  task :install => dotfile('install')  do
    logger.info "#{name} #{version} is installed"
  end

  file dotfile( 'install' ) => "#{name}:build" do 
    logger.info "Installing #{name} #{version}"
    Dir.chdir( pkg_dir ) do
      install
    end
    dotfile!( 'install' )
  end
  ::CLEAN << dotfile( 'install' )

end

#define_patchObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/crate/dependency.rb', line 183

def define_patch
  desc "Apply patches to #{name}"
  task :patch => dotfile( 'patch' )  do
    logger.info "#{name} #{version} is patched"
  end
  file dotfile( 'patch' ) => "#{name}:unpack" do
    logger.info "Patching #{name} #{version}"
    patch_files.each do |pfile|
      logger.info "applying patch #{File.basename( pfile ) }"
      apply_patch( pfile, pkg_dir )
    end
    dotfile!( 'patch' )
  end

  ::CLEAN << dotfile( 'patch' )
end

#define_unpackObject



172
173
174
175
176
177
178
179
180
181
# File 'lib/crate/dependency.rb', line 172

def define_unpack
  #-- unpack
  desc "Unpack #{name} into #{build_dir}"
  task :unpack => "#{name}:verify" do 
    logger.info "Unpacking"
    unpack( local_source, build_dir )
    FileUtils.rm_f dotfile( 'patch' )
  end
  ::CLEAN << pkg_dir
end

#define_verifyObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/crate/dependency.rb', line 159

def define_verify
  desc "Verify source against its checksum"
  task :verify => "#{name}:download" do 
    if @digest then 
      if @digest.valid?( local_source ) then
          logger.info "#{local_source} validates against #{@digest.hex}"
      else
        raise "#{local_source} does not have checksum #{@digest.hex}" 
      end
    end
  end
end

#depends_on(other_dependency) ⇒ Object

allow this task to say it depends on something else. This is a build dependency



293
294
295
296
297
# File 'lib/crate/dependency.rb', line 293

def depends_on( other_dependency )
  namespace name do
    task :build => "#{other_dependency}:done"
  end
end

#dotfile(name) ⇒ Object

return the full path to a named dotfile



275
276
277
# File 'lib/crate/dependency.rb', line 275

def dotfile( name )
  File.join( build_dir, ".#{name}" )
end

#dotfile!(name) ⇒ Object

make the given dotfile



282
283
284
285
286
287
# File 'lib/crate/dependency.rb', line 282

def dotfile!( name )
  File.open( dotfile( name ), "w" ) do |f|
    h = { 'name' => self.name, 'version' => self.version, "#{name}_timestsamp" => Time.now }
    f.puts h.to_yaml
  end
end

#installObject

Execute all the install commands



244
245
246
# File 'lib/crate/dependency.rb', line 244

def install
  cd_and_sh( pkg_dir, install_commands )
end

#install_dirObject

The fake root directory to install into



61
62
63
# File 'lib/crate/dependency.rb', line 61

def install_dir
  Crate.project.install_dir
end

#local_sourceObject

The local on disk copy of the upstream source



90
91
92
# File 'lib/crate/dependency.rb', line 90

def local_source
  @local_source ||= File.join( build_dir, File.basename( upstream_source.path ) )
end

#loggerObject

handle to the top level logger



68
69
70
# File 'lib/crate/dependency.rb', line 68

def logger
  Crate.project.logger
end

#patch_filesObject

patch the upacked source with files that are in the recipe directory



302
303
304
# File 'lib/crate/dependency.rb', line 302

def patch_files
  Dir[File.join( recipe_dir, "*.patch" )].sort
end

#pkg_dirObject

The directory this task unpacks into



75
76
77
# File 'lib/crate/dependency.rb', line 75

def pkg_dir
  @pkg_dir ||= File.join( self.build_dir, "#{name + ( version ? "-#{version}" : "" ) }" )
end

#pkg_dir=(pd) ⇒ Object

override the directory that the local source unpacks into if it is not name-version



83
84
85
# File 'lib/crate/dependency.rb', line 83

def pkg_dir=( pd )
  @pkg_dir = File.join( self.build_dir, pd )
end

#recipe_dirObject

The recipe directory for this particular task



54
55
56
# File 'lib/crate/dependency.rb', line 54

def recipe_dir
  @recipe_dir ||= File.join( Crate.project.recipe_dir, name )
end

#sh(cmd) ⇒ Object

Execute a shell command, sending the command name to the logger at info level and all the output to the logger at the debug level



263
264
265
266
267
268
269
270
# File 'lib/crate/dependency.rb', line 263

def sh( cmd )
  logger.info( cmd )

  io = IO.popen( "#{cmd} 2>&1" )
  io.each_line do |l|
    logger.debug( l.strip )
  end
end

#upstream_md5=(checksum) ⇒ Object

record the upstream md5 checksum



111
112
113
# File 'lib/crate/dependency.rb', line 111

def upstream_md5=( checksum )
  @digest = Crate::Digest.md5( checksum )
end

#upstream_sha1Object



100
101
102
103
104
105
106
# File 'lib/crate/dependency.rb', line 100

def upstream_sha1
  if @digest then
    return @digest.hex
  else
    return nil
  end
end

#upstream_sha1=(checksum) ⇒ Object

record the upstream sha1 checksum



97
98
99
# File 'lib/crate/dependency.rb', line 97

def upstream_sha1=( checksum )
  @digest = Crate::Digest.sha1( checksum )
end

#usptream_md5Object



114
115
116
117
118
119
120
# File 'lib/crate/dependency.rb', line 114

def usptream_md5
  if @digest then
    return @digest.hex
  else
    return nil
  end
end