<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: environment code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:15:23 GMT</pubDate>
    <description>DZone Snippets: environment code</description>
    <item>
      <title>Howto: setup the rails environment in a .rb script</title>
      <link>http://snippets.dzone.com/posts/show/3996</link>
      <description>Stolen from: http://www.railsonwave.com/railsonwave/2007/2/14/howto-setup-the-rails-environment-in-a-rb-script&lt;br /&gt;I quote:&lt;br /&gt;&lt;br /&gt;It can happen that you need to perform some operations on your rails application from a script, for example you may need to have a cron job that do something on your models. To archieve this goal you need to set up a rails environment in your script; it&#8217;s really easy, you&#8217;ve just to put these lines on the top of your .rb file.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;RAILS_ROOT = RELATIVEPATHTOYOURAPPFOLDER&lt;br /&gt;require RAILS_ROOT + "/config/environment" &lt;br /&gt;Dependencies.load_file("application.rb")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;I really don&#8217;t know why but you have to manually load application.rb, if you don&#8217;t rails translate the &#8216;ApplicationController&#8217; constant into &#8216;application_controller&#8217; and look for a file named &#8216;application_controller.rb&#8217; that doesn&#8217;t exist.&lt;br /&gt;&lt;br /&gt;Sandro.&lt;br /&gt;&lt;br /&gt;From the comments:&lt;br /&gt;edbond says:&lt;br /&gt;for script in your RAILS_ROOT folder &lt;br /&gt;&lt;code&gt;&lt;br /&gt;ENV['RAILS_ENV'] = 'development' require File.expand_path(File.dirname(__FILE__) + "/config/environment")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// insert code here..&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 11 May 2007 19:24:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3996</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>Windows Start|Run magic</title>
      <link>http://snippets.dzone.com/posts/show/3638</link>
      <description>Stolen from: http://www.hanselman.com/blog/Reflector5ReleasedWorldDominationAssured.aspx&lt;br /&gt;&lt;br /&gt;Tuesday, February 20, 2007 1:18:12 PM (Pacific Standard Time, UTC-08:00)&lt;br /&gt;The PATH might make sense if you have a single folder with a bunch of utilities. Otherwise, you can safely leave your DOS tricks behind and just add an entry for reflector.exe to:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Set the (Default) string value to the full path to the executable. Start|Run glory, the Windows way.&lt;br /&gt;Joshua Flanagan&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 06 Mar 2007 18:09:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3638</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>Windows Path</title>
      <link>http://snippets.dzone.com/posts/show/3637</link>
      <description>Stolen From: http://www.hanselman.com/blog/Reflector5ReleasedWorldDominationAssured.aspx&lt;br /&gt;Tuesday, February 20, 2007 4:54:50 PM (Pacific Standard Time, UTC-08:00)&lt;br /&gt;&lt;br /&gt;You mention that reflector should be in your path. That gives me an opportunity to exploit a (ruby) gem I created a few weeks ago which makes managing your path a breeze. Instead of going to My Computer | Properties | Blah blah and adding the path entry using that terrible dialog, just follow these steps:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;gem install patheditor&lt;br /&gt;path_editor --add c:\path\to\reflectore&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Adding items to the Windows Path is so painful, I created this utility to make it a little easier for everyone.&lt;br /&gt;&lt;br /&gt;It uses a WSH object to update your USER path setting, and the change will be permanent - current and future command prompts will see the new path. Unfortunately, slickrun does not respect path updates so you'll have to kill it and restart if you launch prompts from there ...&lt;br /&gt;Justin&lt;br /&gt;&lt;br /&gt;Also:&lt;br /&gt;Thursday, February 22, 2007 5:25:22 PM (Pacific Standard Time, UTC-08:00)&lt;br /&gt;@Jon&lt;br /&gt;&lt;br /&gt;Updating path that way (Set PATH=) only affects your current command prompt. My script uses WSH objects to propagate the change to all running processes (that respect the update), and future prompts will include the new path.&lt;br /&gt;&lt;br /&gt;The script will also not add duplicate entries, and it can even clean duplicate/non-existent directories out of your current path.&lt;br /&gt;Justin</description>
      <pubDate>Tue, 06 Mar 2007 17:45:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3637</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>Load OS X's environment.plist in your shell</title>
      <link>http://snippets.dzone.com/posts/show/586</link>
      <description>To make environment variables available to Mac OS X GUI applications, those variables must be defined in your ~/.MacOSX/environment.plist. Sometimes you want to have those same environment variables available from your shell as well. If you're using Terminal, that's no problem--the variables from environment.plist are available. But what if you need to SSH into the machine? &lt;br /&gt;&lt;br /&gt;Adhering to the DRY principle, this script can be launched from your shell's rc file to load up the environment variables from environment.plist. To load it into my tcsh environment, I use&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if ($?SSH_CLIENT) then&lt;br /&gt;    eval `~/bin/parseEnvironmentPlist.rb`&lt;br /&gt;endif&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And here's the ruby script:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;#&lt;br /&gt;# A script for parsing ~/.MacOSX/environment.plist and loading the &lt;br /&gt;# environment variables it defines into a shell environment.&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;# determine which shell the user is running&lt;br /&gt;# currently we support bash and tcsh&lt;br /&gt;if /^\/[-A-Za-z\/]+\/(bash|tcsh)$/ =~ ENV['SHELL']&lt;br /&gt;    shell = $1&lt;br /&gt;else&lt;br /&gt;    # if we can't determine the users shell, or if&lt;br /&gt;    # it's an unsupported shell, bail out here&lt;br /&gt;    exit 1&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# a regex for matching &lt;key&gt;...&lt;/key&gt; lines&lt;br /&gt;# group 1 is the name of the key&lt;br /&gt;key_re = /^\s*&lt;key&gt;([A-Za-z]+[_A-Za-z0-9]*)&lt;\/key&gt;\s*$/&lt;br /&gt;&lt;br /&gt;# a regex for matching &lt;string&gt;...&lt;/string&gt; value lines&lt;br /&gt;# group 1 is the value of the environment variable&lt;br /&gt;value_re = /^\s*&lt;string&gt;([-_:.\/0-9A-Za-z]+)&lt;\/string&gt;\s*$/&lt;br /&gt;&lt;br /&gt;File.open("#{ENV['HOME']}/.MacOSX/environment.plist", "r") do |plist|&lt;br /&gt;&lt;br /&gt;    currentKey = "" # the key we're currently processing&lt;br /&gt;    &lt;br /&gt;    # look at each line of the file to find keys&lt;br /&gt;    # followed by values&lt;br /&gt;    plist.each_line do |next_line| &lt;br /&gt;    &lt;br /&gt;        # if we find a key, hold on to it &lt;br /&gt;        if key_re =~ next_line&lt;br /&gt;            currentKey = $1&lt;br /&gt;            currentValue = ""&lt;br /&gt;        &lt;br /&gt;        # since key lines alternate with value lines,&lt;br /&gt;        # if we match a value line, we know it's a value&lt;br /&gt;        # for the previously matched key&lt;br /&gt;        elsif value_re =~ next_line&lt;br /&gt;            currentValue = $1&lt;br /&gt;        &lt;br /&gt;            if shell == "bash"&lt;br /&gt;                # output a setenv command to stdout that's &lt;br /&gt;                # suitable for running through bash's eval&lt;br /&gt;                puts "#{currentKey}=#{currentValue}; export #{currentKey};"&lt;br /&gt;            elsif shell == "tcsh"&lt;br /&gt;                # output a setenv command to stdout that's &lt;br /&gt;                # suitable for running through tcsh's eval&lt;br /&gt;                puts "setenv #{currentKey} #{currentValue};"&lt;br /&gt;            else&lt;br /&gt;                # we should never get to this point since we &lt;br /&gt;                # exit much earlier if the shell type can't be &lt;br /&gt;                # determined. But, just in case, exit here too.&lt;br /&gt;                exit 1&lt;br /&gt;            end&lt;br /&gt;        &lt;br /&gt;            currentKey = currentValue = ""&lt;br /&gt;        end&lt;br /&gt;    &lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I wrote this script back when I was first learning Ruby, so A) that's why there are so many comments and 2) it could probably be improved, but it works for me!</description>
      <pubDate>Fri, 19 Aug 2005 04:33:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/586</guid>
      <author>santry (Sean Santry)</author>
    </item>
  </channel>
</rss>
