How do I force middleman to reload when I edit the data file?
For example. This is my app:
├── Gemfile
├── Gemfile.lock
├── config.rb
├── data
│ └── products.yml
└── source
├── ...
When I edit data/products.yml the data does not appear on the site until I manually restart the server with bundle exec middleman.
It works if you manually add data to the reload paths
middleman server --reload-paths data/
https://github.com/middleman/middleman/issues/726
Related
Here is current gem structure:
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── mygem.gemspec
├── images
│ ├── 1b1d4bde376084011d027bba1c047a4b.jpg
│ ├── 1d468d064d2e26b5b5de9a0241ef2d4b.jpg
│ ├── 309666c7b45ecbf8f13e85a0bd6b0a4c.jpg
│ ├── 3f9f3db06db20d1d9f8188cd753f6ef4.jpg
│ ├── 679634ff89a31279a39f03e278bc9a01.jpg
│ ├── 6d97739b4a08f965dc9239dd24382e96.jpg
│ ├── 71662d4d4029a3b41d47d5baf681ab9a.jpg
│ ├── 92d90b8977f813af803c78107e7f698e.jpg
│ ├── ad8a37f872956666c3077a3e9e737984.jpg
│ └── df0a3b93e9412536ee8a11255f974141.jpg
├── lib
│ └── mygem.rb
└── spec
└── _spec.rb
The ./images folder does not really exist until you run rspec the first time -- tests do download these binary files from a remote storage, use them and cache them here for later.
There are also rake tasks that use these files and instead of leaving the developer a memo "run this rake task only after you ran rspec" I would like to put the download_and_keep() procedure in some common place callable by both rspec and rake. The question is what is the recommended place to put such method?
There is a folder ./spec/support by rspec design but I'm thinking about switching to minitest so I need something more universal.
P.S.: can't think up more tags for this question.
There isn't really a standard for such a directory. The important part is to pick a name that, at a glance, doesn't sound like it's supposed to be included in the shipped gem.
For some inspiration, rails/rails has a tools, and rubygems/rubygems has util. A top-level support seems like it'd make a fair choice too.
My problem is the following:
I can't require a ruby file from any directory other than the one with all the main files, i.e. requires like this: ./file work fine, but requires like this: dir_name/file fail.
Here's my project structure:
├── bin
│ ├── console
│ └── setup
├── CODE_OF_CONDUCT.md
├── Gemfile
├── lib
│ ├── riverbattle
│ │ ├── base.rb <------- main file that starts everything
│ │ ├── colorful.rb
│ │ ├── computer.rb
│ │ ├── constants.rb
│ │ ├── exit_error.rb
│ │ ├── field.rb
│ │ ├── game.rb
│ │ ├── human.rb
│ │ ├── invalid_move_error.rb
│ │ ├── move.rb
│ │ ├── player.rb
│ │ ├── version.rb
│ │ └── victory_error.rb
│ └── riverbattle.rb <------ the file from where I get the error
├── LICENSE
├── LICENSE.txt
├── Rakefile
├── README.md
├── spec
│ ├── spec_helper.rb
│ └── riverbattle_spec.rb
├── riverbattle-0.1.2.gem
└── riverbattle.gemspec
So, when I launch the app like this: ruby base.rb when I am in the lib/riverbattle/ directory, everything launches fine, but when I try to do ruby riverbattle.rb from the directory lib/, I get the following error:
➜ lib git:(master) ✗ ruby riverbattle.rb
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require': cannot load such file -- ./field (LoadError) from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from
/home/denis/.rvm/gems/ruby-2.2.1/gems/riverbattle-0.1.2/lib/riverbattle/game.rb:1:in
<top (required)>' from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require' from
/home/denis/.rvm/gems/ruby-2.2.1/gems/riverbattle-0.1.2/lib/riverbattle/base.rb:1:in
' from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require' from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from riverbattle.rb:2:in `'
I just don't get it why it cannot require one single file.
By the way, here's the content of the file (riverbattle.rb) from which I get the error:
require "riverbattle/version"
module Riverbattle
require "riverbattle/base"
end
and nothing else.
I was told that require actually requires an absolute path to the file and I need to do something with my $LOAD_PATH or something, but this information doesn't help at all to solve the issue. Why is it okay to require files from the same directory, but not okay, if I require them from another one?
1) Could anybody help me with solving that problem?
2) Is it the problem with only my computer and I can safely publish it as a gem (i.e. it may work properly on another computer), or it is the wrong code that doesn't work?
require searches in your $LOAD_PATH. require should generally be used if you want to use a gem that is installed on your system. For your purposes you want to use require_relative:
require_relative 'riverbattle/version'
Note that at first glance
require './riverbattle/version'
might also seem to work. However, . here points to the current directory from where you run the process, not the directory where the file resides, obviously this is not what you want.
If lib/base.rb is your main file, then perhaps it should be the "executable" file? If that is the case, should it be in bin?
$LOAD_PATH can be added to by using -I option with the ruby executable.
ruby -Ipath_to_project_root/lib lib/main.rb
would then let any file that requires files in your project look in the lib folder.
It can also be used like the answer in your other question with the $: or $LOAD_PATH.
Look at how other gems are set up, and you will find that your base.rb file can live in the bin folder, just like your setup and console files are. If it is the main script that starts it all, it could be named riverbattle as is common with gems to have an executable of the same or similar name.
require is not used to load gems, it is used to load files and without any explicit path given, will look in the load path. It doesn't matter if it is a gem or not.
require_relative is used to load files that are always local relative to the file that is using the require_relative method.
Of course, the documentation is your friend.
I'm new in ruby and I'm trying to create a GEM. The "problem" is that I found really hard to execute the code I'm building, without having to repeat a lot of steps.
This is basically my gem structure
├── Gemfile
├── Gemfile.lock
├── Guardfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│ └── hermes
├── hermes.gemspec
├── lib
│ ├── hermes
│ │ ├── code.rb
│ │ ├── issue.rb
│ │ └── version.rb
│ └── hermes.rb
├── requirements.md
└── spec
├── hermes_spec.rb
└── spec_helper.rb
So I'm running bundle exec irb, then require 'hermes'and then Hermes.init, but if I change any of the files code, I have to exit irb and repeat the steps. Is there a better way of doing it? Which is the "correct" way of doing it (without using rspec) ?
Thanks!
You can use pry and then load "path/to/file.rb" every time you change your code. You can also give it a simple web interface with sinatra and use shotgun to have your code reloaded on the fly.
But, the correct way is using rspec. Nothing better than to start fresh and effective employing test driven development. Trust me, you'll be ahead of 90% of the market by just starting with tdd from scratch.
I am developing a ruby gem which will have a binary.
I am trying to develop the binary but i am worried its not finding my requires because the gem isnt installed as a gem is there a way to test the binary without packaging it as a gem?
#!/usr/bin/env ruby
require "middleman_ember_scaffold/load_paths"
# Start the CLI
MiddlemanEmberScaffold::Cli::Base.start
sits in a file named mse and ive added my bin folder of gem to path
.
└── middleman_ember_scaffold
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│ └── mes
├── lib
│ ├── middleman_ember_scaffold
│ │ ├── cli.rb
│ │ ├── load_paths.rb
│ │ └── version.rb
│ └── middleman_ember_scaffold.rb
└── middleman_ember_scaffold.gemspec
4 directories, 10 files
when i run mes i get
/Users/justin/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- middleman_ember_scaffold/load_paths (LoadError)
from /Users/justin/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/justin/middleman-generator/middleman_ember_scaffold/bin/mes:7:in `<main>'
i'd like to be able to run and develop mes without re-packaging everytime i make a change.
Probably a "better" way would be to do the following:
$ ruby -I./lib bin/mes
It does the same as changing your load path, but it only does it for the command you're executing.
Use RUBYLIB Environment Variable
The problem you're facing is that your source directory isn't getting some of magic applied to installed gems, and therefore doesn't have your lib directory in the $LOAD_PATH. While there are other ways to deal with this, for testing I'd recommend just adding your lib directory to the RUBYLIB environment variable. For example:
RUBYLIB="/path/to/middleman_ember_scaffold/lib:$RUBYLIB"
export RUBYLIB
bin/mes
should work for any Bourne-compatible shell. If you're running Bash, and don't have anything else stored in RUBYLIB, you might even be able to shorten the invocation to:
RUBYLIB="/path/to/middleman_ember_scaffold/lib" bin/mes
Either way, once Ruby knows what directories it should add to the $LOAD_PATH everything should work just fine.
I have a weird problem when trying to package a Firefox add-on built using version 1.9 of the SDK. The extensions directory structure is something like this:
├── data
│ ├── file1.js
│ ├── file2.js
│ ├── jquery.min.js
│ └── uri.js
├── lib
│ ├── file3.js
│ ├── main.js
│ ├── services
│ │ ├── file4.js
│ │ ├── file5.js
│ │ └── file6.js
│ └── uri.js
├── package.json
└── package.json.backup
As part of the build process, I am running the data and 'lib` directories through uglify.js. This appears to work fine. Basically I copy the codebase to a different location, run it through uglify and I get the same directory structure except the JS files are compressed.
Next, I run cfx xpi --pkgdir=path/to/ugly/codebase to package the code into an xpi.
If I then move the produced .xpi to a new directory, unzip it with unzip and inspect the contents, most of my lib directory has been deleted. Files in the data directory are fine.
tree resources/addon_name
resources/addon_name
├── data
│ ├── file1.js
│ ├── file2.js
│ ├── jquery.min.js
│ └── uri.js
└── lib
└── main.js
If I don't uglify the JS files then everything seems to work fine and when I unzip the xpi I will have a full lib directory as I would expect.
Note that this is not a problem with the uglifying process (that was the first thing I checked). When I copy the codebase and uglify it, I can stop the process at that point and list the lib directory. It will contain all the uglified JS files I would expect. It's only after packaging and subsequent unzipping that they are gone.
I have tried reproducing this issue with a brand new extension but I get a slightly different problem. Basically, files in the lib directory are deleted on packaging regardless of whether they are compressed or not. Basically my steps are:
mkdir test_extension
cd test_extension && cfx init
touch lib/uri.js // this is
cd .. && cfx xpi pkgdir=test_extension // Have to run this part twice to get ID
mkdir unpack && mv test_textension.xpi unpack
cd unpack
unzip test_extension.xpi
ls resources/test_extension/lib
=> main.js // the uri.js file is missing
If lib/uri.js is not required from any js file of your add-on, it will
be removed from the final XPI. So if you have require('./uri.js') in
your main.js, the file should be there after the packaging.
My guessing is that uglifying the libraries makes
impossible for the current cfx tool generates the proper manifest with
all dependencies. See Manifest Generation.
Note original post on mozilla-labs-jetpack mailing list, copied the answer here to be useful to someone else that doesn't know the ML.