Class: Crate::Project

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/crate/project.rb

Overview

the Crate top level task, there should only be one of these in existence at a time. This task is accessible via Crate.project, and is what is defined in the Rakefile in the project directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) {|_self| ... } ⇒ Project

Returns a new instance of Project.

Yields:

  • (_self)

Yield Parameters:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/crate/project.rb', line 46

def initialize( name ) 
  raise "Crate Project already initialized" if ::Crate.project
  @name         = name
  @project_root = File.expand_path( File.dirname( Rake.application.rakefile ) )
  @recipe_dir   = File.join( @project_root, 'recipes' )
  @build_dir    = File.join( @project_root, 'build' )
  @install_dir  = File.join( @project_root, 'fakeroot' )
  @dist_dir     = File.join( @project_root, 'dist' )
  yield self if block_given?
  ::Crate.project = self
  define
end

Instance Attribute Details

#build_dirObject

subdirectory of project_root where recipes’ are built. default: ‘build’



24
25
26
# File 'lib/crate/project.rb', line 24

def build_dir
  @build_dir
end

#dist_dirObject

the directory where the final products are stored



31
32
33
# File 'lib/crate/project.rb', line 31

def dist_dir
  @dist_dir
end

#extensionsObject

The list of extensions to compile



34
35
36
# File 'lib/crate/project.rb', line 34

def extensions
  @extensions
end

#install_dirObject

subdirectory of project_root representing a fake installation root. default ‘fakeroot’



28
29
30
# File 'lib/crate/project.rb', line 28

def install_dir
  @install_dir
end

#main_classObject

The ‘main’ class which is instantiated to start the application



40
41
42
# File 'lib/crate/project.rb', line 40

def main_class
  @main_class
end

#main_fileObject

The ‘main’ file



37
38
39
# File 'lib/crate/project.rb', line 37

def main_file
  @main_file
end

#nameObject (readonly)

Name of the project



14
15
16
# File 'lib/crate/project.rb', line 14

def name
  @name
end

#project_rootObject (readonly)

Top level directory of the project.



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

def project_root
  @project_root
end

#recipe_dirObject

subdirectory of project_root in which the recipe’s are stored. default: ‘recipes’



21
22
23
# File 'lib/crate/project.rb', line 21

def recipe_dir
  @recipe_dir
end

#run_methodObject

The method on an instance of ‘main_class’ to invoke with ARGV, ENV arguments to start the crate based program.



44
45
46
# File 'lib/crate/project.rb', line 44

def run_method
  @run_method
end

Instance Method Details

#compile_crate_bootObject

Compile the crate_boot stub to an object file



146
147
148
149
150
151
152
153
# File 'lib/crate/project.rb', line 146

def compile_crate_boot
  create_crate_boot_h
  compile_options = %w[ CFLAGS XCFLAGS CPPFLAGS ].collect { |c| compile_params[c] }.join(' ')
  cmd = "#{compile_params['CC']} #{compile_options} -I#{Crate.ruby.pkg_dir} -o crate_boot.o -c crate_boot.c"
  logger.debug cmd
  sh cmd
  ::CLEAN << "crate_boot.o"
end

#compile_paramsObject

Load upthe compile params we may need to compile the project. This method is usless until after the :ruby task has been completed



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/crate/project.rb', line 112

def compile_params 
  unless @compile_params
    @compile_params = {}
    Dir.chdir( ::Crate.ruby.pkg_dir ) do
      %w[ CC CFLAGS XCFLAGS LDFLAGS CPPFLAGS LIBS ].each do |p|
        @compile_params[p] = %x( ./miniruby -I. -rrbconfig -e 'puts Config::CONFIG["#{p}"]' ).strip
      end
    end
  end
  return @compile_params
end

#create_crate_boot_hObject

Create the crate_boot.h file



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/crate/project.rb', line 127

def create_crate_boot_h
  File.open( "crate_boot.h", "w+" ) do |h|
    h.puts <<-CRATE_BOOT_H
/**
 * Automatcially generated.  Do not edit.  To change the contents of
 * this file, update your project main_file, main_class and run_method
 * options and rebuild.
 */
#define CRATE_MAIN_FILE  "#{Crate.project.main_file}"
#define CRATE_MAIN_CLASS "#{Crate.project.main_class}"
#define CRATE_RUN_METHOD "#{Crate.project.run_method}"
CRATE_BOOT_H
  end
end

#defineObject

define the project task



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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/crate/project.rb', line 174

def define
  lib_db = File.join( dist_dir, "lib.db" )
  app_db = File.join( dist_dir, "app.db" )
  directory dist_dir
  packer_cmd = "amalgalite-pack"

  task :pack_ruby => dist_dir do
    prefix = File.join( ::Crate.ruby.pkg_dir, "lib" )
    
    logger.info "Packing ruby standard lib into #{lib_db}"
    cmd = "#{packer_cmd} --drop-table --db #{lib_db} --compress --strip-prefix #{prefix} #{prefix}" 
    logger.debug cmd
    sh "#{cmd} > /dev/null"
  end

  task :pack_ruby_ext => dist_dir do
    logger.info "Packing ruby extension libs into #{lib_db}"
    File.open( ::Crate.ruby.ext_setup_file ) do |f|
      f.each_line do |line|
        next if line =~ /\Aoption/
        next if line.strip.length == 0
        next if line =~ /\A#/

        prefix = File.join( ::Crate.ruby.ext_dir, line.strip, "lib" )
        next unless File.directory?( prefix )

        cmd = "#{packer_cmd} --merge --db #{lib_db} --compress --strip-prefix #{prefix} #{prefix}"
        logger.debug cmd
        sh "#{cmd} > /dev/null"
      end
    end
  end

  task :pack_amalgalite => dist_dir do
    logger.info "Packing amalgalite into #{lib_db}"
    cmd = "#{packer_cmd} --drop-table --db #{lib_db} --self"
    logger.debug cmd
    sh "#{cmd} > /dev/null"
  end

  task :pack_app => [ :pack_amalgalite, :pack_ruby, :pack_ruby_ext ] do
    logger.info "Packing project packing lists lists into #{app_db}"
    Crate.project.packing_lists.each_with_index do |pl,idx|
      pc = ( idx == 0 ) ? "#{packer_cmd} --drop-table" : packer_cmd.dup
      cmd = "#{pc} --db #{app_db} --merge --compress --strip-prefix #{pl.prefix} #{pl.file_list.join(' ')} "
      logger.debug cmd
      sh "#{cmd} > /dev/null"
    end
  end

  file "crate_boot.o" => "crate_boot.c" do
    compile_crate_boot
  end

  app_path = File.join( dist_dir, name )
  file app_path => [ "crate_boot.o", dist_dir ] do
    link_project
  end

  desc "Build #{name}"
  #task :default => [ :ruby ] do 
  task :default => [ app_path, :pack_app ] do
    logger.info "Build #{name}"
    compile_crate_boot
    link_project
  end
  ::CLEAN << self.install_dir
  ::CLEAN << "project.log"
  ::CLEAN << self.dist_dir
  load_rakefiles
end

Run the link command to create the final executable



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

def link_project
  link_options = %w[ CFLAGS XCFLAGS LDFLAGS ].collect { |c| compile_params[c] }.join(' ')
  Dir.chdir( ::Crate.ruby.pkg_dir ) do
    dot_a = FileList[ "ext/**/*.a" ]
    dot_a << %w[ libssl.a libcrypto.a libz.a libruby-static.a  ] # order is important on the last 4
    dot_o = [ "ext/extinit.o", File.join( project_root, "crate_boot.o" )]
    libs = compile_params['LIBS']
    cmd = "#{compile_params['CC']} #{link_options} #{dot_o.join(' ')} #{libs} #{dot_a.join(' ')} -o #{File.join( dist_dir, name) }"
    logger.debug cmd
    sh cmd
  end
end

#load_rakefilesObject

Load all .rake files that are in a recipe sub directory



249
250
251
252
253
254
# File 'lib/crate/project.rb', line 249

def load_rakefiles
  Dir["#{recipe_dir}/*/*.rake"].each do |recipe|
    logger.debug "loading #{recipe}"
    import recipe
  end
end

#loggerObject

Create a logger for the project



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/crate/project.rb', line 92

def logger
  unless @logger 
    @logger = Logging::Logger[name]
    @logger.level = :debug
    @logger.add_appenders

    @logger.add_appenders( 
        Logging::Appenders::File.new( File.join( project_root, "project.log" ), :layout => Logging::Layouts::Pattern.new( :pattern => "%d %5l: %m\n" )),
        Logging::Appenders::Stdout.new( 'stdout', :level => :info,
                                      :layout => Logging::Layouts::Pattern.new( :pattern      => "%d %5l: %m\n",
                                                                                :date_pattern => "%H:%M:%S") )
    )
  end
  return @logger
end

#packing_listsObject

The list of application files to pack into the app.db This is an array of PackingList



81
82
83
# File 'lib/crate/project.rb', line 81

def packing_lists
  @packing_lists ||= []
end

#packing_lists=(list) ⇒ Object



85
86
87
# File 'lib/crate/project.rb', line 85

def packing_lists=( list )
  @packing_lists = [ list ].flatten
end