geode.thor
# Throws exit code 1 on errors
Throws exit code 1 on errors
Throws exit code 1 on errors
def destroy(type, *args)
    raise Error, 'ERROR: At least one name must be given' if args.empty?
    case type
    when 'crystal'
      all_crystal_paths = Dir['app/main/*.rb'] + Dir['app/dev/*.rb']
      crystals_to_delete = args.map do |crystal_name|
        if (crystal_path = all_crystal_paths.find { |p| File.basename(p, '.*').camelize == crystal_name })
          [crystal_name, crystal_path]
        else raise Error, "ERROR: Crystal #{crystal_name} not found"
        end
      end
      crystals_to_delete.each do |crystal_name, crystal_path|
        File.delete(crystal_path)
        puts "- Deleted crystal #{crystal_name}"
      end
    when 'model'
      raise Error, 'ERROR: Only one model can be deleted at a time' unless args.size == 1
      model_name = args[0]
      model_path = if File.exists? "app/models/#{model_name.underscore}.rb"
                   singleton = false
                   "app/models/#{model_name.underscore}.rb"
                 elsif File.exists? "app/models/#{model_name.underscore}_singleton.rb"
                   singleton = true
                   "app/models/#{model_name.underscore}_singleton.rb"
                 end
      raise Error, "ERROR: Model #{model_name} not found" unless model_path
      table_name = singleton ? model_name.underscore : model_name.tableize
      File.delete model_path
      puts "- Deleted model file for model #{model_name}"
      Sequel.sqlite(ENV['DB_PATH']) do |db|
        if db.table_exists?(table_name.to_sym)
          generator = Generators::ModelDestroyMigrationGenerator.new(model_name, db, singleton)
          generator.generate_in('db/migrations')
        else
          initial_migration_index = Dir['db/migrations/*.rb'].index do |path|
            path.include? "add_#{table_name}_table_to_database"
          end
          Dir['db/migrations/*.rb'][initial_migration_index..-1].each do |migration_path|
            migration_name = File.basename(migration_path)[15..-4].camelize
            migration_version = File.basename(migration_path).to_i
            File.delete(migration_path)
            puts "- Deleted migration version #{migration_version} (#{migration_name})"
          end
        end
      end
    when 'migration'
      all_migration_paths = Dir['db/migrations/*.rb']
      migrations_to_delete = args.map do |migration_key|
        migration_path = all_migration_paths.find do |path|
          filename = File.basename(path)
          filename.to_i == migration_key.to_i || filename[15..-4].camelize == migration_key
        end
        if (migration_path)
          migration_name = File.basename(migration_path)[15..-4].camelize
          migration_version = File.basename(migration_path).to_i
          [migration_name, migration_version, migration_path]
        else raise Error, "ERROR: Migration #{migration_key} not found"
        end
      end
      migrations_to_delete.each do |migration_name, migration_version, migration_path|
        File.delete(migration_path)
        puts "- Deleted migration version #{migration_version} (#{migration_name})"
      end
    else raise Error, 'ERROR: Generation type must be crystal, model or migration'
    end
  end
def destroy ( type , * args ) raise Error , 'ERROR: At least one name must be given' if args . empty? case type when 'crystal' all_crystal_paths = Dir [ 'app/main/*.rb' ] + Dir [ 'app/dev/*.rb' ] crystals_to_delete = args . map do | crystal_name | if ( crystal_path = all_crystal_paths . find { | p | File . basename ( p , '.*' ) . camelize == crystal_name } ) [ crystal_name , crystal_path ] else raise Error , "ERROR: Crystal #{crystal_name} not found" end end crystals_to_delete . each do | crystal_name , crystal_path | File . delete ( crystal_path ) puts "- Deleted crystal #{crystal_name}" end when 'model' raise Error , 'ERROR: Only one model can be deleted at a time' unless args . size == 1 model_name = args [ 0 ] model_path = if File . exists? "app/models/#{model_name.underscore}.rb" singleton = false "app/models/#{model_name.underscore}.rb" elsif File . exists? "app/models/#{model_name.underscore}_singleton.rb" singleton = true "app/models/#{model_name.underscore}_singleton.rb" end raise Error , "ERROR: Model #{model_name} not found" unless model_path table_name = singleton ? model_name . underscore : model_name . tableize File . delete model_path puts "- Deleted model file for model #{model_name}" Sequel . sqlite ( ENV [ 'DB_PATH' ] ) do | db | if db . table_exists? ( table_name . to_sym ) generator = Generators :: ModelDestroyMigrationGenerator . new ( model_name , db , singleton ) generator . generate_in ( 'db/migrations' ) else initial_migration_index = Dir [ 'db/migrations/*.rb' ] . index do | path | path . include? "add_#{table_name}_table_to_database" end Dir [ 'db/migrations/*.rb' ] [ initial_migration_index .. - 1 ] . each do | migration_path | migration_name = File . basename ( migration_path ) [ 15 .. - 4 ] . camelize migration_version = File . basename ( migration_path ) . to_i File . delete ( migration_path ) puts "- Deleted migration version #{migration_version} (#{migration_name})" end end end when 'migration' all_migration_paths = Dir [ 'db/migrations/*.rb' ] migrations_to_delete = args . map do | migration_key | migration_path = all_migration_paths . find do | path | filename = File . basename ( path ) filename . to_i == migration_key . to_i || filename [ 15 .. - 4 ] . camelize == migration_key end if ( migration_path ) migration_name = File . basename ( migration_path ) [ 15 .. - 4 ] . camelize migration_version = File . basename ( migration_path ) . to_i [ migration_name , migration_version , migration_path ] else raise Error , "ERROR: Migration #{migration_key} not found" end end migrations_to_delete . each do | migration_name , migration_version , migration_path | File . delete ( migration_path ) puts "- Deleted migration version #{migration_version} (#{migration_name})" end else raise Error , 'ERROR: Generation type must be crystal, model or migration' end end
Throws exit code 1 on errors
Throws exit code 1 on errors
# Validates that arguments have been given # Cases destruction type # Validates that crystals with the given names all exist and gets their file paths # Deletes all given crystals, printing deletions to console # Validates that only one model name is given # Validates that model exists # Deletes model, printing deletion to console # Loads the database # If model's table exists in the database, generates new migration dropping the model's table # Otherwise, deletes the migration adding the model's table and every migration that follows # Validates that migrations with the given names or versions all exist and gets their names, versions # and file paths # Deletes all given migrations, printing deletions to console
type
<not_specific>
args
<not_specific>