Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:
# Monthly, to catch breakage from new Ruby or dependency releases
# when nothing has been pushed.
schedule:
- cron: '0 6 1 * *'

permissions:
contents: read

jobs:
test:
name: Ruby ${{ matrix.ruby }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby: ['3.1', '3.2', '3.3', '3.4', '4.0']
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Tests
run: bundle exec rake test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ work/sandbox
DEMO.md
*.lock
*.gem
pkg
58 changes: 0 additions & 58 deletions .index

This file was deleted.

10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

9 changes: 1 addition & 8 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
source "https://rubygems.org"
gemspec :path => 'pkg'

platforms :rbx do
#gem 'racc'
gem 'rubysl', '~> 2.0'
gem 'psych'
end

gemspec
29 changes: 29 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# RELEASE HISTORY

## Unreleased (0.3.0)

Backload is revived, for Ruby 3.1 and up.

Changes:

* Add `Kernel.backloading`, called before every load. Until now there
were only callbacks for afterwards. (#4)
* Add the `:depth` option, which says how many loads a load is inside
of, so that what required what can be shown as a tree. (#2)
* Add `backload/trace`, which prints that tree:
`ruby -rbackload/trace script.rb`.
* The `require_relative` callback is always on. It no longer has to be
asked for with `backload/require_relative`. That file, and
`backload/require` and `backload/load`, now just load Backload.
* `Kernel.required_relative` is given the directory as a second
argument. A callback that takes only the feature still works. (#5)
* The hooks are put in place with `Module#prepend` instead of aliases.
* The callbacks are no longer defined as empty methods, so defining one
doesn't warn of a method redefined.
* Tests run on Spectrum.

Fixes:

* `require_relative` raised "Can't parse" on current Ruby, whose
backtrace lines have changed. The directory now comes from
`caller_locations`.


## 0.2.0 / 2012-12-22

Change the name of the project from Loaded to Backload, and
Expand Down
17 changes: 0 additions & 17 deletions MANIFEST.txt

This file was deleted.

127 changes: 82 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,111 @@
[Homepage](http://rubygems.org/gems/backload) |
[Report Issue](http://github.com/rubyworks/backload/issues) |
[Source Code](http://github.com/rubyworks/backload)
( [![Build Status](https://secure.travis-ci.org/rubyworks/backload.png)](http://travis-ci.org/rubyworks/backload) )
# Backload

[Source Code](https://github.com/rubyworks/backload) /
[Report Issue](https://github.com/rubyworks/backload/issues)    
[![CI](https://github.com/rubyworks/backload/actions/workflows/ci.yml/badge.svg)](https://github.com/rubyworks/backload/actions/workflows/ci.yml)

# Backload
**Shouldn't loading have a callback?**

Shouldn't loading have a callback?
Backload gives Ruby's `require`, `require_relative` and `load` callbacks,
before and after, so a program can see what is loaded, when, and from
inside what. It needs Ruby 3.1 or newer.

$ gem install backload

## Installation

Using RubyGems type on a command line:
## Seeing what a program loads

$ gem install backload
To print every load as a tree, without writing anything:

$ ruby -rbackload/trace script.rb

require json
require json/common
require json/version
require_relative lib/thing
require set

## Instruction
The tree goes to standard error. Each line is printed as the load starts,
and is indented by how many loads it is inside of. A feature that was
already loaded is still listed, since it was asked for.

To use Backload simply override the `Kernel.backloaded` method.

## The callbacks

For anything else, require `backload` and define the callbacks you want
as methods of `Kernel`. None of them exists until you define it.

```ruby
require 'backload'
require 'backload/require_relative'

def Kernel.backloaded(feature, options={})
if options[:require]
if rel = options[:relative]
puts "#{feature} has been required relative to #{rel}!"
else
puts "#{feature} has been required!"
end
require 'backload'

def Kernel.backloading(feature, options={})
puts "#{' ' * options[:depth]}loading #{feature}"
end

def Kernel.backloaded(feature, options={})
if options[:require]
if dir = options[:relative]
puts "#{feature} has been required relative to #{dir}"
else
if wrap = options[:wrap]
puts "#{feature} has been loaded with wrap, it's #{wrap}!"
else
puts "#{feature} has been loaded!"
end
puts "#{feature} has been required"
end
else
puts "#{feature} has been loaded"
end
end
```

Becuase of implementation details, `#require_relative` has to be reimplemented completely
to make the callback work. To be on the safe side, at least for now, it therefore has to
be required separately, as the example above demonstrates.
`Kernel.backloading` is called before every `require`, `require_relative`
and `load`. `Kernel.backloaded` is called after each one that loaded
something: not when `require` returns false because the feature was
loaded before, and not when the load raises.

Backload also provides callbacks for each type of loading. Just use the past tense
of the term for any of `#load`, `#require` and `#require_relative`. For example, to
see all features as they have been required.
The options say what happened:

| Option | Value |
|-------------|--------------------------------------------------------------|
| `:require` | true for `require` and `require_relative` |
| `:load` | true for `load` |
| `:relative` | for `require_relative`, the directory it is relative to |
| `:wrap` | for `load`, its second argument |
| `:depth` | how many loads this one is inside of; 0 at the top |

There is also a plain callback for each method, called after it, named by
its past tense:

```ruby
def Kernel.required(feature)
puts "#{feature} required!"
end
```
def Kernel.required(feature)
puts "#{feature} required"
end

## Feedback
def Kernel.loaded(path, wrap)
puts "#{path} loaded"
end

Please report issues or suggestions to
[GitHub Issues](http://github.com/rubyworks/backload/issues).
Or if you wish to discuss in real-time try [IRC #rubyworks](irc://chat.us.freenet.org/rubyworks) on freenode.net.
def Kernel.required_relative(feature, directory)
puts "#{feature} required relative to #{directory}"
end
```


## Copying
## Notes

Backload is copyrighted open source software.
* An `autoload` is seen as the `require` it makes when the constant is
first used.
* `require_relative` can't be wrapped like the others, because Ruby works
out the directory from the file that called it. Backload finds the
directory itself and requires the file by its full path, which is what
`require_relative` does. Before 0.3 this had to be asked for with
`require 'backload/require_relative'`; it is now always on.
* The hooks are put in front of Kernel's methods with `Module#prepend`,
so they work alongside RubyGems, Bundler and others that redefine
`require`.
* The nesting depth is counted for each thread.

Copyright 2012 Rubyworks.

It is modifiable and redistributable in accordance with the
**BSD-2-Clause** license.
## Copying

Copyright (c) 2012 Rubyworks

Backload is distributed under the terms of the **BSD-2-Clause** license.
See LICENSE.txt for details.
23 changes: 18 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#!/usr/bin/env ruby
require 'rake/clean'
CLOBBER.include 'pkg'

task :default => [:test]
require 'bundler/gem_helper'

desc "Run unit tests."
task :test do
sh "rubytest -Ilib test/test_*"
# Bundler tags releases as v1.2.3, but Backload's tags have always been
# plain version numbers, so keep it that way.
class Bundler::GemHelper
def version_tag
version.to_s
end
end

Bundler::GemHelper.install_tasks

desc 'run tests'
task 'test' do
sh 'rubytest -Ilib -r spectrum/testcase -r ./test/helper test/case_*.rb'
end

task 'default' => 'test'
31 changes: 0 additions & 31 deletions Toolchain

This file was deleted.

Loading
Loading