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
373 374 375 376 377 |
# File 'lib/dgit/core.rb', line 373 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.
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 |
# File 'lib/dgit/core.rb', line 302 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, indent: 2) end # Load the options from `.dgit/options` # @return [void] def @options = 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(&:clone) 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| a = klass.new(@options) 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| j = klass.new(@options) 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.
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 |
# File 'lib/dgit/core.rb', line 302 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, indent: 2) end # Load the options from `.dgit/options` # @return [void] def @options = 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(&:clone) 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| a = klass.new(@options) 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| j = klass.new(@options) 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.
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 |
# File 'lib/dgit/core.rb', line 302 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, indent: 2) end # Load the options from `.dgit/options` # @return [void] def @options = 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(&:clone) 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| a = klass.new(@options) 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| j = klass.new(@options) 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.
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 |
# File 'lib/dgit/core.rb', line 302 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, indent: 2) end # Load the options from `.dgit/options` # @return [void] def @options = 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(&:clone) 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| a = klass.new(@options) 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| j = klass.new(@options) 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.
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 |
# File 'lib/dgit/core.rb', line 302 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, indent: 2) end # Load the options from `.dgit/options` # @return [void] def @options = 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(&:clone) 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| a = klass.new(@options) 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| j = klass.new(@options) 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.
325 326 327 328 329 330 331 |
# File 'lib/dgit/core.rb', line 325 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.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/dgit/core.rb', line 339 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.
317 318 319 320 |
# File 'lib/dgit/core.rb', line 317 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.
436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/dgit/core.rb', line 436 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| a = klass.new(@options) 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.
425 426 427 428 429 |
# File 'lib/dgit/core.rb', line 425 def clone(*source_ids) @journal.sources_by_ids(*source_ids).select { |s| s.entry.new? }.each(&:clone) ensure save_journal end |
#config_path(name) ⇒ String
Return the path of the given config file
359 360 361 |
# File 'lib/dgit/core.rb', line 359 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
366 367 368 |
# File 'lib/dgit/core.rb', line 366 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 |
# File 'lib/dgit/core.rb', line 453 def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| j = klass.new(@options) 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`
411 412 413 |
# File 'lib/dgit/core.rb', line 411 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`
381 382 383 384 385 386 387 388 |
# File 'lib/dgit/core.rb', line 381 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`
399 400 401 |
# File 'lib/dgit/core.rb', line 399 def @options = Oj.load_file(config_path(DGIT_OPTIONS)) end |
#save_config ⇒ void
This method returns an undefined value.
Save the config to ‘.dgit/config`
417 418 419 420 |
# File 'lib/dgit/core.rb', line 417 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`
392 393 394 395 |
# File 'lib/dgit/core.rb', line 392 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, indent: 2) end |
#save_options ⇒ void
This method returns an undefined value.
Save the options to ‘.dgit/options`
405 406 407 |
# File 'lib/dgit/core.rb', line 405 def Oj.to_file(config_path(DGIT_OPTIONS), ) end |