How do you get LiveReload to work with Octopress using Guard - octopress

I am using this guide to add liveReload to Octopress.
http://www.erikzaadi.com/2012/09/16/using-live-reload-with-octopress/
Once I get to 'rake generate && rake watch',
The command line spits out I am missing a specific gem; i.e.'rake-0.9.6', which is strange because I thought by installing the bundle took care of that.
I suspect that bundle is being saved elsewhere; but shouldn't that bundle be saved in the directory of my choosing (i.e. Sites/myproject?)

Add these two entries to your Gemfile, in the :development group:
gem 'guard'
gem 'guard-livereload'
Create a file called Guardfile containing something like:
guard 'livereload' do
watch(%r{public/generated})
watch(%r{public/.+\.(css|js|html)})
end
Start 2 shell tabs running these commands: rake generate && rake watch and guard
`
rake generate && rake watch
`
start guard LiveReload
`
guard
`
It’s neat to get LiveReload working with Octopress. However, the generation can finish after your page does a reload, so you won’t see your latest changes. I’ll update this blog post when I figure out a solution to that one. Until then, you may find it more convenient to manually refresh the blog page yourself.
It’s worth noting that if you’re running any other instance of guard- LiveReload, then one of these two copies will win and one won’t work. If you run a rails server this way, then this can bite you. It took me a bit of time to figure out why guard wasn’t working.
source
http://www.railsonmaui.com/blog/2013/04/27/octopress-setup-with-github-and-org-mode/#sec-4

Related

How to start Middleman? cannot load such file -- less (LoadError)

I am learning Middleman. I installed Vagrant and have set up everything about that. But I have a problem at the end, when I call "bundle exec middleman", the Middleman should start after that regularly. But I get this message:
"var/lib/gems/2.2.0/gems/tilt-1.4.1/lib/tilt/template.rb:144:in 'require':cannot load such file -- less (LoadError)"
What should I do? I am working on Linux.
Maybe you forgot to add less to your Gemfile:
gem 'less'
As #tadman has pointed out adding
gem 'less'
is (possibly) a solution, but here is a little more detail on why you might have this problem migrating or developing a site:
If you initialise a middleman project with middleman init, then start working on your site with 'middleman serve' everything should be fine. But if, at some point you add a file with a .less extension you will get this error when you next restart middleman. Note that you might not knowingly have added less files - fontawsome for example can add .less versions of the css deep in it's directory structure.
I say that installing less is only possibly a solution since the 'less' gem was broken when I tried it (it was not able to install due to an old dependency on therubyracer
Deleting .less files that you didn't want or know you had is also a solution.

Automatically run RSPec when plain-old Ruby (not Rails) files change

I am writing a Ruby script designed to run from the command line. The script has a corresponding RSpec file that verifies its functionality. The folder structure is:
./main_script.rb
./spec/main_script_spec.rb
Running rspec spec in the top level directory works as expected. Test results from the ./spec/main_script_spec.rb file are shown. I'd like to avoid running this manually every time I change either the main script file or the spec file. All my search results turn up things like guard which (as far as I can tell) are all designed for Rails apps.
How do I setup RSpec to watch for script or spec changes and run automatically with non-Rails Ruby code?
Like David said, Guard can be used to watch a wide variety of files and perform actions when those files are modified. It does not have to be used with a Rails app. I have set up something similar in the past using guard. Here is what I did:
Place the following in your Gemfile:
source 'https://rubygems.org'
gem 'guard'
gem 'guard-shell'
gem 'rspec'
gem 'rb-fsevent', '~> 0.9'
Then run:
$ bundle install
Create a Guardfile in your home directory with:
$ guard init
In the Guardfile, comment out the examples and add this:
guard :shell do
watch(%r{^*\.rb}) { `bundle exec rspec spec/` }
end
This tells guard to watch for modifications to any ruby files in the directory and execute the command bundle exec rspec spec/ when they change (the backticks are used to execute the command in the shell).
Then open up a new terminal window in your current directory and start a guard server to start watching the files:
$ bundle exec guard
Now your Rspec test suite should automatically run when you modify ruby files in the directory.
I used guard at the past, but now I'm using a combination of rspec focus feature and watch command.
It's very simple, just add an f before a describe of it block you want to run the test. So it would becomes fdescribe or fit block. This is the same as adding a tag :focus => true to your block.
We can then filter specs with the focus tag: rspec -t focus
Now, to keeping running theses specs (every 0.5 seconds) with focus tag we call it with watch command:
watch -n 0.5 rspec -t focus
But with that the output won't show colors. So, we need to use with unbuffer.
sudo apt-get install expect
With a little customization:
watch -n 0.5 --color 'unbuffer bundle exec rspec -t focus'
Since it's annoying to type this all, I made two alias at my ~/.bash_aliases file (your can use .bashrc as well):
alias focus="watch -n 0.5 --color 'unbuffer bundle exec rspec -t focus'"
alias focuss="bundle exec rspec -t focus"
Now I can type focus to keep running it, or for a single focus execution I type focuss
Guard can be used for plain old ruby. I generally have trouble with guard so I like to use watchr, another gem. With a few lines of code you can tell watchr to watch for changes to your files and run a command when they change.
For an example of guard with plain ruby, see the shuhari gem.
update on watchr gem: There appears to be an issue with this gem, perhaps with versions of ruby >= 2.0. The observr gem addresses this issue and works as expected in ruby 2.3.
I have used guard and the guard-rspec addition with great results, and I don't believe it to be Rails-specific. Other Ruby/RSpec projects should work equally well.
The guard documentation recommends the use of Bundler and to "always run Guard through Bundler to avoid errors". I.e. you install it through your Gemfile and always run it with bundle exec guard (or use rubygems-bundler to avoid the bundle exec part).

How to develop a Ruby GEM without having to install it first?

I'm developing a GEM that I've forked and I'm trying to modify it slightly for my app.
I'm finding it difficult and time consuming because for every change I make I have to
uninstall
build
re-install
run the app
Is there an easier way of which doesn't require repeating all steps above?
To use it in some app using bundler
If what you mean is for using it in a app to test it / use it, you can just specify a path for your gem or even point to a git repo in the Gemfile http://gembundler.com/gemfile.html
Like
gem "mygem", :path => "~/code/gems/mygem"
To use it as a standalone gem. i.e: like rspec or rake that can run outside of an app.
Just specify the path to your gem binary when running the gem command, like:
$ ~/path_to_my_gem/bin/mygem some args
If you can execute inside your gem directory (i.e: the command does not create files in the current directory, or needs any specific files from the current directory), just do this:
$ ./bin/mygem some args
Note that this last one is just for future reference, I think it's not applicable in the OP context.
use require_relative to include your files:
require_relative 'yourgem/yourclass'
This is the documentation for the function.

Guard running outside of Bundler warning

When I run the guard command it gives the following warning:
Guard here! It looks like your project has a Gemfile, yet you are
running guard outside of Bundler. If this is your intent, feel free
to ignore this message. Otherwise, consider using bundle exec guard
to ensure your dependencies are loaded correctly.
Is this hinting to me that Rails is not configured to work with Bundler correctly, or is it normal? It's not the expected behavior in the tutorial I'm following.
You should run bundle exec guard instead. Or, alternatively, run bundle install --binstubs, then you may run guard with bin/guard (it creates a script at this location). This is the recommended way of running all commands coming from gems installed with bundle install.
(If I understand it correctly) It ensures that you run the specific version of the gem specified in the bundle as well as that this gem will not be able to run gems which are installed on you computer but not included in the Gemfile (which could fool you into believing that you project is fine until you try to run it on a different computer, or a production server, where the other gem would be missing). It also does a lot of of stuff which, frankly, I have no idea about.
More info in the docs.

Using Live Reload with Jekyll

I'm getting started with Jekyll static site generator and I would like to use Live Reload with it. I know Jekyll has a generator and server commands, and Live Reload can run various compilers and custom commands. How do I configure these to work together?
LiveReload is built into Jekyll 3.7+.
jekyll serve --livereload
You can also set LiveReload's port, delay, and ignored files. See jekyll help serve.
UPDATE: As pointed out in other answers, LiveReload is built into Jekyll 3.7+.
jekyll serve --livereload
For older versions:
The simplest approach I've found that works is to use two terminal windows: One for jekyll serve --watch and one for guard.
I tried the guard-jekyll-plus approach suggested by Nobu but I had a bunch of errors.
As shumushin pointed out, Jekyll can handle the automatic rebuilding process, you simply launch it using jekyll serve --watch
Now to get LiveReload working run guard with guard-livereload in a second terminal window. This is basically the same as Jan Segre's answer, but without guard-jekyll.
My Guardfile looks like this:
guard 'livereload' do
watch(/^_site/)
end
And my Gemfile:
gem 'jekyll'
gem 'guard'
gem 'guard-livereload'
Note: You still need to include the livereload script in your index.html page; it is the "glue" that binds guard-livereload and the browser together.
<script src="http://localhost:35729/livereload.js"></script>
There's guard-livereload which you can use with guard-jekyll and centralize the watching process with guard, an example would be (I haven't tested it):
Install guard-jekyll, either through gem or bundler
Install guard-livereload, either through gem or bundler
Init guard-jekyll
guard init jekyll
Add this to your Guardfile:
guard 'livereload' do
watch(%r{_site/.+})
end
You can adapt the above to suit better your project, and
you probably already know you have to include the livereload
script on your page:
<script src="http://localhost:35729/livereload.js"></script>
Oh, and to start the whole watching mess:
guard
For jekyll 1.0+ use:
jekyll serve --watch
See Jekyll: Basic Usage for more details and options.
UPDATE: this no longer works with the latest version of Jekyll
cd your/site/folder
jekyll --server --auto
This post explains a cleaner way - Setting Up LiveReload With Jekyll
Gemfile:
gem 'jekyll'
gem 'guard'
gem 'guard-jekyll-plus'
gem 'guard-livereload'
Guardfile:
guard 'jekyll-plus', :serve => true do
watch /.*/
ignore /^_site/
end
guard 'livereload' do
watch /.*/
end
Install any LiveReload browser extension. Then run guard.
I wrote a Jekyll plugin called Hawkins that incorporates LiveReload into the Jekyll watch process. It works with Jekyll 3.1 and up.
Simply add
group :jekyll_plugins do
gem 'hawkins'
end
to your Gemfile (and then a bundle install). From there you can run jekyll liveserve. Hawkins will modify the head sections of your pages to include the necessary components for LiveReload, and when Jekyll detects a page change, Hawkins will push a message to your browser via WebSockets. Please note that you will need a browser that supports WebSockets. For very fast reloads, you can use Jekyll's new --incremental option that will only regenerate the changed pages.
Start by running jekyll normally in your site folder:
cd your/site/folder
jekyll
By default Jekyll generates a folder called _site inside it (your/site/folder/_site).
Tell LiveReload to watch that _site folder.
This command will open your website in the browser and uses jekyll built-in livereload server.
bundle exec jekyll serve -l -o
You need a latest jekyll version.
I just started using GitHub Pages today, and wanted to be able to use live reload with Jekyll. Got it working & written my first post on Creating GitHub Pages with Jekyll & LiveReload.
It uses Grunt with the grunt-contrib-watch plugin instead of Jekyll's serve command - works well for me. Hope it works for you as well.
You can use just jekyll serve -w, an option I prefer as I am lazy.
For Live Reload,
Remove Jekyll Admin from Gemfile in the root directory of your project and it works like charm.
If you're running it frequently, the Repla macOS app makes it easy to startup Jekyll so it automatically refreshes. After Repla is installed, you run it from the Jekyll blog's root directory and pass it the jekyll serve command. For example:
repla server "bundle exec jekyll serve --watch --drafts" -r "...done"
Repla will be configured to refresh each time ...done is printed in the console, which Jekyll prints when it finishes compiling your site.
Repla runs the Jekyll server process in a split below a browser split showing your site:
After Jekyll is running in Repla, you can also save the configuration to a file with ⌘S, shut it down by closing the window, and run it again just by double-clicking the file. In other words, you can start your Jekyll blog again next time just by opening the file, without involving the terminal at all.
Disclosure: I maintain the Repla app.

Resources