Fire up a web browser for a folder? - ruby

Is there an easy way to fire up a web browser for a folder?
eg.
I am in a folder that contains a website (index.html and other files) and I want to browse the site through a browser. Is there a gem that I just launch to make this folder browsable?
In this way I don't have to install nginx just for a specific folder. And when you install nginx you have to bother with configuration files and so on.
Kinda how Rails does it with:
rails server

Yes, there is... Throw the following in a file called webserver:
#!/usr/bin/env ruby
require 'webrick'
include WEBrick
server = HTTPServer.new(:Port => 3000, :DocumentRoot => Dir::pwd)
trap("INT"){ server.shutdown }
server.start
Then, perform the following (This assumes Mac OSX):
$ sudo chmod 755 webserver
$ sudo chown root:wheel webserver
$ sudo cp webserver /usr/local/bin/webserver (or somewhere in your path)
Now, just run webserver from the directory you want to use as the document root. A webserver will now be running on localhost:3000.
Hope this helps!
UPDATE
I just remembered after reading a post on Phusion Passenger 3.0 progress that there will be a passenger lite option...

Easiest way I've found is this little Python one-liner:
2.x:
python -m SimpleHTTPServer
3.x:
python -m http.server 8080
Unless you want to execute Ruby dynamically, of course. But that wasn't explicit in your question. Only static HTML.

The webbrick example works great, thanks to Brian. However, I just wanted to follow up on his update.
Assuming you have a working ruby and rubygems installed:
gem install passenger
put all files in a subdirectory called public
example project dir:
.
├── any
│   ├── old crap
│   └── that will not be on the website
└── public
├── favicon.ico
├── images
│   ├── ajax-loader-large.gif
│   ├── bg.jpg
│   ├── bg_home.jpg
│   ├── bg_nav.gif
├── index.html
├── javascripts
│   ├── jquery.liveSearch.js
├── robots.txt
└── stylesheets
├── all.css
Then run passenger start
The first time it will install a bunch of things (including nginx, but you won't have to worry about configuring it), but it should work faster after that.

And, if you have PHP >= 5.4.0, you can:
php -S localhost:8000
That's pretty easy!
Reference: http://php.net/manual/en/features.commandline.webserver.php

Related

require error in ruby gem

I'm working on a Ruby gem and I'm getting an odd error. I have previously released this gem without too much trouble. I added some methods / refactored some code and wanted to release a subsequent version (from 1.1 to 1.2).
For reference, the name of the gem is Intervallum, (the word 'interval' in Latin).
I'm getting a 'require' error that's been stumping me.
The folder tree is:
.
├── Gemfile
├── LICENSE
├── README.md
├── intervallum-[version].gem
├── intervallum.gemspec
└── lib
├── intervallum
│   ├── module.scroll.rb
│   └── module.spell.rb
└── intervallum.rb
In lib/intervallum.rb I tried Dir.glob, Dir['./lib/intervallum/*'] and require '../intervallum/lib/intervallum/module.spell.rb' and what happens for each one is:
Locally, after gem build intervallum.gemspec and gem install intervallum-[version].gem, I boot up irb and require intervallum and it works fine.
push to RubyGems
remove the local copy
install from RG
load up in irb I get load errors or it cannot find class of a helper class
I'm not sure why this keeps occurring, or if there's something that I'm missing as to why this keeps occurring but any advice would be much appreciated.
The problem is that you are not including those files in the gem when it gets packaged. Your gemspec specifies that only the lib/intervallum.rb file will be included in the gem:
s.files = ["./lib/intervallum.rb"]
Change that line to include all files in lib. It's also a good idea to include the gemspec.
s.files = Dir['lib/**/*', 'intervallum.gemspec']

Perl compiled by PAR::Packer doesn't run on other machines

I have an existing 2000 LOC perl script with a Tkx GUI that I just inherited on my first day as an intern at a place where I am the sole programmer (everyone else is an IC engineer, but they do their simulations in perl).
The goal is to produce an executable for clients to run without having to install perl nor anything else. Apparently this has been possible in the past.
I've only been able to get the Program to build by installing ActiveTcl 8.5.15, ActivePerl 5.16, installing via PPM Win32::API and Win32::Exe, tk, tkx, Carp, then installing via cpanp i PAR::Packer. This very specific mix is the only one I found that produced any results that worked.
Then it's:
pp -vvv -l C:\Perl\lib\auto\Tcl\tkkit.dll -l C:\Perl\lib\auto\Tcl\Tcl.dll -l C:\Perl\lib\auto\Win32\API\API.dll --gui -o .\<THE NAME OF THE FILE.EXE> .\<PERL SOURCE>.pl
From here I get a working executable, except it will not run on any of the other machines I've tested it on. It's not an arch issue as far as I can tell.
The documentation by the previous developer is extremely lacking on the subject (he's better on documenting the actual code). I'm told he migrated it to StrawberryPerl then back to ActivePerl when that broke, but as yet it's still pretty broken over here and the existing build environment is long gone.
Any help would be appreciated.
Action: Use PAR::Packer pp to compile Windows binary from a perl script so that clients can run it without perl
Expected Result: A tk GUI Window opens and stays open.
Actual Result: A tk GUI Window does not open, no errors are produced on console nor in any prompt.
Thanks to all who responded.
I actually found the solution myself, which was that this block
BEGIN { if (exists $ENV{PAR_PROGNAME}) { use Config (); $ENV{PERL_TCL_DL_PATH} = catfile($ENV{PAR_TEMP}, 'tkkit.' . $Config::Config{dlext}, ); } }
needs to appear before a "use Tkx;" to properly instruct PAR::Packer to grab the necessary parts of Tcl to package the program. So the necessary libraries were being left out and the executable was searching for ActiveTcl installations in %path% that did not exist. But thank you for the detail about the additional module for debugging.
This is my solution on Windows 7 with strawberry perl 5.26.1 and tkx 1.09:
pp --compile --noscan --dependent --compress 6 hello.pl
Copy generated .exe and needed dlls to a directory, and rename this directory to bin
Copy tcl and tk library to lib directory on the same hierarchy as bin
Now my generated file structure:
$ tree -L 2
.
├── bin
│   ├── hello.exe
│   ├── hello.pl
│   ├── libgcc_s_dw2-1.dll
│   ├── libstdc++-6.dll
│   ├── libwinpthread-1.dll
│   ├── perl526.dll
│   ├── tcl86.dll
│   ├── tk86.dll
│   └── zlib1.dll
└── lib
├── tcl8.6
└── tk8.6
BTW, the whole folder can be compressed to about 2.7MB by 7-zip on my machine.

Reload middleman when data/ file change

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

homebrew install - share/info are gone after installation?

I wanted to install some info document of libavl. I found some weird behavior of brew command with the following brew library that I made. What I did were:
First, I compile it with "make libavl.info."
Second, I created share/info under brew KEG PREFIX.
Third, I installed some info files to that share/info.
I do not know yet how I can symbolic-link those info files in system directory such as "/usr/local/share/info", but I want to have this brew formula. To make sure before and after installing info files that I have those info files in the corresponding KEG PREFIX, I printed directory structure in a file called "log," which is also installed to the KEG PREFIX. But, those info files disappeared after the installation. I do not know what happened. I do not know where I should ask in the homebrew website Brew.sh, so I ask this question here.
THIS IS THE FORMULA FILE called "avl.rb"
require 'formula'
class Avl < Formula
homepage 'http://savannah.gnu.org/projects/avl'
url 'ftp://ftp.gnu.org/pub/gnu/avl/avl-2.0.3.tar.gz'
sha1 'ec2e025838c846fbc05fb60cf49035f2e97f37f3'
def install
system "make", "libavl.info"
info.mkpath
system "/usr/local/bin/tree " "#{prefix} > #{prefix}/log"
info.install "libavl.info"
info.install "libavl.info-1"
info.install "libavl.info-2"
info.install "libavl.info-3"
info.install "libavl.info-4"
system "/usr/local/bin/tree " "#{prefix} >> #{prefix}/log"
end
end
The "log" file content before info.install command in the brew formula:
/usr/local/Cellar/avl/2.0.3
├── log
└── share
└── info
2 directories, 1 file
This is the content of "log" file after info.install command:
/usr/local/Cellar/avl/2.0.3
├── log
└── share
└── info
├── libavl.info
├── libavl.info-1
├── libavl.info-2
├── libavl.info-3
└── libavl.info-4
2 directories, 6 files
This is the final installed KEG PREFIX directory. There is the "log" file, but no "share/info" directory in it.
/usr/local/Cellar/avl/2.0.3
├── AUTHORS
├── COPYING
├── ChangeLog
├── INSTALL_RECEIPT.json
├── NEWS
├── README
├── TODO
└── log
0 directories, 8 files
I will appreciate any answers to this mystery.
Thank you,
Homebrew does not install info files by default. You can change this by setting HOMEBREW_KEEP_INFO=1.

Test a ruby gem binary

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.

Resources