Class: Diggit::Dig
- Inherits:
-
Object
- Object
- Diggit::Dig
- Defined in:
- lib/dgit/core.rb
Overview
Constant Summary collapse
- DGIT_FOLDER =
".dgit".freeze
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
The config.
-
#folder ⇒ String
readonly
The folder in which diggit is running.
-
#journal ⇒ Journal
readonly
The journal.
-
#options ⇒ Hash<String,Object>
readonly
The options.
-
#plugin_loader ⇒ PluginLoader
readonly
Utility classes to load plugins.
Class Method Summary collapse
-
.init(folder = '.') ⇒ Dig
Initialize and return the diggit instance into the given folder.
-
.init_dir(folder = '.') ⇒ void
Initialize a folder to be a diggit folder by creating an empty configuration.
-
.it ⇒ Dig
Returns the diggit instance.
Instance Method Summary collapse
-
#analyze(source_ids = [], analyses = [], mode = :run) ⇒ void
Perform the given analyses on sources with the given ids using the given mode.
-
#clone(*source_ids) ⇒ void
Clone the repository of all sources with the given source ids.
-
#config_path(name) ⇒ String
Return the path of the given config file.
-
#file_path(name) ⇒ String
Return the path of the given file in the diggit folder.
-
#initialize(folder) ⇒ Dig
constructor
Constructor.
-
#join(source_ids = [], joins = [], mode = :run) ⇒ void
Perform the given joins on sources with the given ids using the given mode.
-
#load_config ⇒ void
Load the config from
.dgit/config. -
#load_journal ⇒ void
Load the journal from
.dgit/journal. -
#load_options ⇒ void
Load the options from
.dgit/options. -
#save_config ⇒ void
Save the config to
.dgit/config. -
#save_journal ⇒ void
Save the journal to
.dgit/journal. -
#save_options ⇒ void
Save the options to
.dgit/options.
Constructor Details
#initialize(folder) ⇒ Dig
369 370 371 372 373 |
# File 'lib/dgit/core.rb', line 369 def initialize(folder) raise "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end |
Instance Attribute Details
#config ⇒ Config (readonly)
Returns the config.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/dgit/core.rb', line 298 class Dig DGIT_FOLDER = ".dgit".freeze DGIT_SOURCES = "sources".freeze DGIT_CONFIG = "config".freeze DGIT_OPTIONS = "options".freeze DGIT_JOURNAL = "journal".freeze private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it raise "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder [String] the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a `.dgit` folder containing a `journal`, `config`, `options` files. # It also creates an empty `sources` folder and an empty `plugins` folder. # Directory creation is skipped if the `.dgit` folder already exists. # @param folder [String] the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), Journal.new) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) return if File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) raise "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from `.dgit/journal` # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } @journal = Oj.load_file(config_path(DGIT_JOURNAL)) url_array.each do |url| @journal.add_source(url) end end # Save the journal to `.dgit/journal` # @return [void] def save_journal File.open(config_path(DGIT_SOURCES), "w") { |f| @journal.sources.each { |source| f.puts(source.url) } } Oj.to_file(config_path(DGIT_JOURNAL), @journal) end # Load the options from `.dgit/options` # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to `.dgit/options` # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from `.dgit/config` # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to `.dgit/config` # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select { |s| s.entry.new? }.each do |s| Log.fine("cloning #{s.url} (#{s.oid})") s.clone end ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select { |s| s.entry.cloned? }.each do |s| @config.get_analyses(*analyses).each do |klass| Log.fine("Performing analysis #{klass} (#{mode}) on #{s.url}") a = klass.new() s.load_repository a.source = s clean(a, s.entry) if clean_mode?(mode) && s.entry.has?(a) run(a, s.entry) if run_mode?(mode) && !s.entry.has?(a) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| Log.fine("Performing join #{klass}") j = klass.new() clean(j, @journal.workspace) if clean_mode?(mode) && @journal.workspace.has?(j) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.entry.cloned? && (klass.required_analyses - s.entry.performed.map(&:name)).empty? end j.sources = source_array run(j, @journal.workspace) if run_mode?(mode) && !source_array.empty? && !@journal.workspace.has?(j) end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean(runnable, placeholder) placeholder.clean(runnable) entry = RunnableEntry.new(runnable) begin runnable.clean rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error cleaning #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } ensure save_journal end end def run(runnable, placeholder) entry = RunnableEntry.new(runnable) begin runnable.run rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error running #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } else entry.toc placeholder.performed << entry ensure save_journal end end end |
#folder ⇒ String (readonly)
Returns the folder in which diggit is running.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/dgit/core.rb', line 298 class Dig DGIT_FOLDER = ".dgit".freeze DGIT_SOURCES = "sources".freeze DGIT_CONFIG = "config".freeze DGIT_OPTIONS = "options".freeze DGIT_JOURNAL = "journal".freeze private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it raise "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder [String] the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a `.dgit` folder containing a `journal`, `config`, `options` files. # It also creates an empty `sources` folder and an empty `plugins` folder. # Directory creation is skipped if the `.dgit` folder already exists. # @param folder [String] the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), Journal.new) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) return if File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) raise "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from `.dgit/journal` # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } @journal = Oj.load_file(config_path(DGIT_JOURNAL)) url_array.each do |url| @journal.add_source(url) end end # Save the journal to `.dgit/journal` # @return [void] def save_journal File.open(config_path(DGIT_SOURCES), "w") { |f| @journal.sources.each { |source| f.puts(source.url) } } Oj.to_file(config_path(DGIT_JOURNAL), @journal) end # Load the options from `.dgit/options` # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to `.dgit/options` # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from `.dgit/config` # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to `.dgit/config` # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select { |s| s.entry.new? }.each do |s| Log.fine("cloning #{s.url} (#{s.oid})") s.clone end ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select { |s| s.entry.cloned? }.each do |s| @config.get_analyses(*analyses).each do |klass| Log.fine("Performing analysis #{klass} (#{mode}) on #{s.url}") a = klass.new() s.load_repository a.source = s clean(a, s.entry) if clean_mode?(mode) && s.entry.has?(a) run(a, s.entry) if run_mode?(mode) && !s.entry.has?(a) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| Log.fine("Performing join #{klass}") j = klass.new() clean(j, @journal.workspace) if clean_mode?(mode) && @journal.workspace.has?(j) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.entry.cloned? && (klass.required_analyses - s.entry.performed.map(&:name)).empty? end j.sources = source_array run(j, @journal.workspace) if run_mode?(mode) && !source_array.empty? && !@journal.workspace.has?(j) end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean(runnable, placeholder) placeholder.clean(runnable) entry = RunnableEntry.new(runnable) begin runnable.clean rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error cleaning #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } ensure save_journal end end def run(runnable, placeholder) entry = RunnableEntry.new(runnable) begin runnable.run rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error running #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } else entry.toc placeholder.performed << entry ensure save_journal end end end |
#journal ⇒ Journal (readonly)
Returns the journal.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/dgit/core.rb', line 298 class Dig DGIT_FOLDER = ".dgit".freeze DGIT_SOURCES = "sources".freeze DGIT_CONFIG = "config".freeze DGIT_OPTIONS = "options".freeze DGIT_JOURNAL = "journal".freeze private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it raise "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder [String] the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a `.dgit` folder containing a `journal`, `config`, `options` files. # It also creates an empty `sources` folder and an empty `plugins` folder. # Directory creation is skipped if the `.dgit` folder already exists. # @param folder [String] the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), Journal.new) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) return if File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) raise "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from `.dgit/journal` # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } @journal = Oj.load_file(config_path(DGIT_JOURNAL)) url_array.each do |url| @journal.add_source(url) end end # Save the journal to `.dgit/journal` # @return [void] def save_journal File.open(config_path(DGIT_SOURCES), "w") { |f| @journal.sources.each { |source| f.puts(source.url) } } Oj.to_file(config_path(DGIT_JOURNAL), @journal) end # Load the options from `.dgit/options` # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to `.dgit/options` # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from `.dgit/config` # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to `.dgit/config` # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select { |s| s.entry.new? }.each do |s| Log.fine("cloning #{s.url} (#{s.oid})") s.clone end ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select { |s| s.entry.cloned? }.each do |s| @config.get_analyses(*analyses).each do |klass| Log.fine("Performing analysis #{klass} (#{mode}) on #{s.url}") a = klass.new() s.load_repository a.source = s clean(a, s.entry) if clean_mode?(mode) && s.entry.has?(a) run(a, s.entry) if run_mode?(mode) && !s.entry.has?(a) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| Log.fine("Performing join #{klass}") j = klass.new() clean(j, @journal.workspace) if clean_mode?(mode) && @journal.workspace.has?(j) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.entry.cloned? && (klass.required_analyses - s.entry.performed.map(&:name)).empty? end j.sources = source_array run(j, @journal.workspace) if run_mode?(mode) && !source_array.empty? && !@journal.workspace.has?(j) end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean(runnable, placeholder) placeholder.clean(runnable) entry = RunnableEntry.new(runnable) begin runnable.clean rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error cleaning #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } ensure save_journal end end def run(runnable, placeholder) entry = RunnableEntry.new(runnable) begin runnable.run rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error running #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } else entry.toc placeholder.performed << entry ensure save_journal end end end |
#options ⇒ Hash<String,Object> (readonly)
Returns the options.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/dgit/core.rb', line 298 class Dig DGIT_FOLDER = ".dgit".freeze DGIT_SOURCES = "sources".freeze DGIT_CONFIG = "config".freeze DGIT_OPTIONS = "options".freeze DGIT_JOURNAL = "journal".freeze private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it raise "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder [String] the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a `.dgit` folder containing a `journal`, `config`, `options` files. # It also creates an empty `sources` folder and an empty `plugins` folder. # Directory creation is skipped if the `.dgit` folder already exists. # @param folder [String] the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), Journal.new) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) return if File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) raise "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from `.dgit/journal` # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } @journal = Oj.load_file(config_path(DGIT_JOURNAL)) url_array.each do |url| @journal.add_source(url) end end # Save the journal to `.dgit/journal` # @return [void] def save_journal File.open(config_path(DGIT_SOURCES), "w") { |f| @journal.sources.each { |source| f.puts(source.url) } } Oj.to_file(config_path(DGIT_JOURNAL), @journal) end # Load the options from `.dgit/options` # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to `.dgit/options` # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from `.dgit/config` # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to `.dgit/config` # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select { |s| s.entry.new? }.each do |s| Log.fine("cloning #{s.url} (#{s.oid})") s.clone end ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select { |s| s.entry.cloned? }.each do |s| @config.get_analyses(*analyses).each do |klass| Log.fine("Performing analysis #{klass} (#{mode}) on #{s.url}") a = klass.new() s.load_repository a.source = s clean(a, s.entry) if clean_mode?(mode) && s.entry.has?(a) run(a, s.entry) if run_mode?(mode) && !s.entry.has?(a) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| Log.fine("Performing join #{klass}") j = klass.new() clean(j, @journal.workspace) if clean_mode?(mode) && @journal.workspace.has?(j) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.entry.cloned? && (klass.required_analyses - s.entry.performed.map(&:name)).empty? end j.sources = source_array run(j, @journal.workspace) if run_mode?(mode) && !source_array.empty? && !@journal.workspace.has?(j) end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean(runnable, placeholder) placeholder.clean(runnable) entry = RunnableEntry.new(runnable) begin runnable.clean rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error cleaning #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } ensure save_journal end end def run(runnable, placeholder) entry = RunnableEntry.new(runnable) begin runnable.run rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error running #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } else entry.toc placeholder.performed << entry ensure save_journal end end end |
#plugin_loader ⇒ PluginLoader (readonly)
Returns utility classes to load plugins.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/dgit/core.rb', line 298 class Dig DGIT_FOLDER = ".dgit".freeze DGIT_SOURCES = "sources".freeze DGIT_CONFIG = "config".freeze DGIT_OPTIONS = "options".freeze DGIT_JOURNAL = "journal".freeze private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it raise "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder [String] the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a `.dgit` folder containing a `journal`, `config`, `options` files. # It also creates an empty `sources` folder and an empty `plugins` folder. # Directory creation is skipped if the `.dgit` folder already exists. # @param folder [String] the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), Journal.new) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) return if File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) raise "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from `.dgit/journal` # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } @journal = Oj.load_file(config_path(DGIT_JOURNAL)) url_array.each do |url| @journal.add_source(url) end end # Save the journal to `.dgit/journal` # @return [void] def save_journal File.open(config_path(DGIT_SOURCES), "w") { |f| @journal.sources.each { |source| f.puts(source.url) } } Oj.to_file(config_path(DGIT_JOURNAL), @journal) end # Load the options from `.dgit/options` # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to `.dgit/options` # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from `.dgit/config` # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to `.dgit/config` # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select { |s| s.entry.new? }.each do |s| Log.fine("cloning #{s.url} (#{s.oid})") s.clone end ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select { |s| s.entry.cloned? }.each do |s| @config.get_analyses(*analyses).each do |klass| Log.fine("Performing analysis #{klass} (#{mode}) on #{s.url}") a = klass.new() s.load_repository a.source = s clean(a, s.entry) if clean_mode?(mode) && s.entry.has?(a) run(a, s.entry) if run_mode?(mode) && !s.entry.has?(a) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: `:run`, `:rerun` or `:clean`. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| Log.fine("Performing join #{klass}") j = klass.new() clean(j, @journal.workspace) if clean_mode?(mode) && @journal.workspace.has?(j) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.entry.cloned? && (klass.required_analyses - s.entry.performed.map(&:name)).empty? end j.sources = source_array run(j, @journal.workspace) if run_mode?(mode) && !source_array.empty? && !@journal.workspace.has?(j) end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean(runnable, placeholder) placeholder.clean(runnable) entry = RunnableEntry.new(runnable) begin runnable.clean rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error cleaning #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } ensure save_journal end end def run(runnable, placeholder) entry = RunnableEntry.new(runnable) begin runnable.run rescue StandardError => e entry.toc entry.error = e placeholder.canceled << entry Log.error "Error running #{runnable.name}: #{e}" e.backtrace.each { |l| Log.debug(l) } else entry.toc placeholder.performed << entry ensure save_journal end end end |
Class Method Details
.init(folder = '.') ⇒ Dig
Initialize and return the diggit instance into the given folder.
321 322 323 324 325 326 327 |
# File 'lib/dgit/core.rb', line 321 def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end |
.init_dir(folder = '.') ⇒ void
This method returns an undefined value.
Initialize a folder to be a diggit folder by creating an empty configuration. It creates a .dgit folder containing a journal, config, options files. It also creates an empty sources folder and an empty plugins folder. Directory creation is skipped if the .dgit folder already exists.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/dgit/core.rb', line 335 def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), Journal.new) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) return if File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end |
.it ⇒ Dig
Returns the diggit instance.
313 314 315 316 |
# File 'lib/dgit/core.rb', line 313 def self.it raise "Diggit has not been initialized." if @diggit.nil? @diggit end |
Instance Method Details
#analyze(source_ids = [], analyses = [], mode = :run) ⇒ void
This method returns an undefined value.
Perform the given analyses on sources with the given ids using the given mode.
435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/dgit/core.rb', line 435 def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select { |s| s.entry.cloned? }.each do |s| @config.get_analyses(*analyses).each do |klass| Log.fine("Performing analysis #{klass} (#{mode}) on #{s.url}") a = klass.new() s.load_repository a.source = s clean(a, s.entry) if clean_mode?(mode) && s.entry.has?(a) run(a, s.entry) if run_mode?(mode) && !s.entry.has?(a) end end end |
#clone(*source_ids) ⇒ void
This method returns an undefined value.
Clone the repository of all sources with the given source ids.
421 422 423 424 425 426 427 428 |
# File 'lib/dgit/core.rb', line 421 def clone(*source_ids) @journal.sources_by_ids(*source_ids).select { |s| s.entry.new? }.each do |s| Log.fine("cloning #{s.url} (#{s.oid})") s.clone end ensure save_journal end |
#config_path(name) ⇒ String
Return the path of the given config file
355 356 357 |
# File 'lib/dgit/core.rb', line 355 def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end |
#file_path(name) ⇒ String
Return the path of the given file in the diggit folder
362 363 364 |
# File 'lib/dgit/core.rb', line 362 def file_path(name) File.(name, @folder) end |
#join(source_ids = [], joins = [], mode = :run) ⇒ void
This method returns an undefined value.
Perform the given joins on sources with the given ids using the given mode.
453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/dgit/core.rb', line 453 def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| Log.fine("Performing join #{klass}") j = klass.new() clean(j, @journal.workspace) if clean_mode?(mode) && @journal.workspace.has?(j) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.entry.cloned? && (klass.required_analyses - s.entry.performed.map(&:name)).empty? end j.sources = source_array run(j, @journal.workspace) if run_mode?(mode) && !source_array.empty? && !@journal.workspace.has?(j) end end |
#load_config ⇒ void
This method returns an undefined value.
Load the config from .dgit/config
407 408 409 |
# File 'lib/dgit/core.rb', line 407 def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end |
#load_journal ⇒ void
This method returns an undefined value.
Load the journal from .dgit/journal
377 378 379 380 381 382 383 384 |
# File 'lib/dgit/core.rb', line 377 def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } @journal = Oj.load_file(config_path(DGIT_JOURNAL)) url_array.each do |url| @journal.add_source(url) end end |
#load_options ⇒ void
This method returns an undefined value.
Load the options from .dgit/options
395 396 397 |
# File 'lib/dgit/core.rb', line 395 def = Oj.load_file(config_path(DGIT_OPTIONS)) end |
#save_config ⇒ void
This method returns an undefined value.
Save the config to .dgit/config
413 414 415 416 |
# File 'lib/dgit/core.rb', line 413 def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end |
#save_journal ⇒ void
This method returns an undefined value.
Save the journal to .dgit/journal
388 389 390 391 |
# File 'lib/dgit/core.rb', line 388 def save_journal File.open(config_path(DGIT_SOURCES), "w") { |f| @journal.sources.each { |source| f.puts(source.url) } } Oj.to_file(config_path(DGIT_JOURNAL), @journal) end |
#save_options ⇒ void
This method returns an undefined value.
Save the options to .dgit/options
401 402 403 |
# File 'lib/dgit/core.rb', line 401 def Oj.to_file(config_path(DGIT_OPTIONS), ) end |