Skip to content

Commit

Permalink
First run of CLI. Refactor some bits to look for environment variable…
Browse files Browse the repository at this point in the history
… to get paths to work with the CLI. Only piece not implemented on the CLI is 'install' which I'll finish today (hopefully)
  • Loading branch information
jm committed Apr 22, 2012
1 parent 01098b5 commit 7796b99
Show file tree
Hide file tree
Showing 10 changed files with 473 additions and 38 deletions.
20 changes: 18 additions & 2 deletions Railcar.xcodeproj/project.pbxproj
Expand Up @@ -42,7 +42,9 @@
883ADB3715295F11000F55D5 /* RCVersionManagementWindowController.rb in Resources */ = {isa = PBXBuildFile; fileRef = 883ADB3615295F11000F55D5 /* RCVersionManagementWindowController.rb */; };
883ADB3F152A1635000F55D5 /* RCApplicationLaunchWindowController.rb in Resources */ = {isa = PBXBuildFile; fileRef = 883ADB3E152A1635000F55D5 /* RCApplicationLaunchWindowController.rb */; };
883ADB41152A1642000F55D5 /* ApplicationLaunchWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 883ADB40152A1642000F55D5 /* ApplicationLaunchWindow.xib */; };
88436FE5153FC9CC0069C4E0 /* 512-Railcar.png in Resources */ = {isa = PBXBuildFile; fileRef = 88436FE4153FC9CC0069C4E0 /* 512-Railcar.png */; };
88436FE9154058FE0069C4E0 /* RCCli.rb in Resources */ = {isa = PBXBuildFile; fileRef = 88436FE8154058FE0069C4E0 /* RCCli.rb */; };
88436FEA15426C0B0069C4E0 /* 512-Railcar.png in Resources */ = {isa = PBXBuildFile; fileRef = 88436FE4153FC9CC0069C4E0 /* 512-Railcar.png */; };
88436FEC154270210069C4E0 /* cli.sh in Resources */ = {isa = PBXBuildFile; fileRef = 88436FEB154270210069C4E0 /* cli.sh */; };
884C524F152A865E006B8C90 /* RCApplicationManager.rb in Resources */ = {isa = PBXBuildFile; fileRef = 884C524E152A865E006B8C90 /* RCApplicationManager.rb */; };
884C5251152A8A14006B8C90 /* error.png in Resources */ = {isa = PBXBuildFile; fileRef = 884C5250152A8A14006B8C90 /* error.png */; };
884C5253152A9BE7006B8C90 /* RCApplicationInstance.rb in Resources */ = {isa = PBXBuildFile; fileRef = 884C5252152A9BE7006B8C90 /* RCApplicationInstance.rb */; };
Expand Down Expand Up @@ -114,6 +116,8 @@
883ADB3E152A1635000F55D5 /* RCApplicationLaunchWindowController.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = RCApplicationLaunchWindowController.rb; sourceTree = "<group>"; };
883ADB40152A1642000F55D5 /* ApplicationLaunchWindow.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ApplicationLaunchWindow.xib; sourceTree = "<group>"; };
88436FE4153FC9CC0069C4E0 /* 512-Railcar.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "512-Railcar.png"; sourceTree = "<group>"; };
88436FE8154058FE0069C4E0 /* RCCli.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = RCCli.rb; sourceTree = "<group>"; };
88436FEB154270210069C4E0 /* cli.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = cli.sh; sourceTree = "<group>"; };
884C524E152A865E006B8C90 /* RCApplicationManager.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = RCApplicationManager.rb; sourceTree = "<group>"; };
884C5250152A8A14006B8C90 /* error.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = error.png; sourceTree = "<group>"; };
884C5252152A9BE7006B8C90 /* RCApplicationInstance.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = RCApplicationInstance.rb; sourceTree = "<group>"; };
Expand Down Expand Up @@ -259,6 +263,15 @@
name = "Rails App Launch UI";
sourceTree = "<group>";
};
88436FE7154058E40069C4E0 /* CLI */ = {
isa = PBXGroup;
children = (
88436FEB154270210069C4E0 /* cli.sh */,
88436FE8154058FE0069C4E0 /* RCCli.rb */,
);
name = CLI;
sourceTree = "<group>";
};
886564761525E17800B63502 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -292,6 +305,7 @@
8865648C1525E17800B63502 /* Railcar */ = {
isa = PBXGroup;
children = (
88436FE7154058E40069C4E0 /* CLI */,
889842CD152C758200D9B797 /* App Generator UI */,
883ADB3C152A1610000F55D5 /* Rails App Launch UI */,
883ADB2815295636000F55D5 /* Ruby Manager UI */,
Expand Down Expand Up @@ -492,7 +506,9 @@
889C4E3B153BDCF900E69C0C /* servicesTemplate.png in Resources */,
889C4E3D153BDCFF00E69C0C /* rubyTemplate.png in Resources */,
889C4E3F153BDD0300E69C0C /* packageTemplate.png in Resources */,
88436FE5153FC9CC0069C4E0 /* 512-Railcar.png in Resources */,
88436FE9154058FE0069C4E0 /* RCCli.rb in Resources */,
88436FEA15426C0B0069C4E0 /* 512-Railcar.png in Resources */,
88436FEC154270210069C4E0 /* cli.sh in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
27 changes: 19 additions & 8 deletions Railcar/RCApplicationInstance.rb
Expand Up @@ -6,6 +6,7 @@
# Copyright 2012 Arcturo. All rights reserved.
#
require 'yaml'
require 'fileutils'

class RCApplicationInstance
attr_accessor :name, :path, :rubyVersion, :port, :environment, :key, :processId
Expand All @@ -20,26 +21,35 @@ def initialize(key, data)
@environment = data[:environment]
end

# CLI will feed environment variable if it's available
def initializerPath
File.join(NSBundle.mainBundle.bundlePath, "initializers", "rbenv_init_#{rubyVersion}.sh")
File.join((ENV['RAILCAR_PATH'] || NSBundle.mainBundle.bundlePath), "initializers", "rbenv_init_#{rubyVersion}.sh")
end

def launch
begin
@processId = Process.spawn("source #{initializerPath} \n ruby #{path}/script/rails server -e #{environment} -p #{port}")

bundleProcessId = Process.spawn("source #{initializerPath}\n bundle install --gemfile=#{path}/Gemfile > /dev/null 2>&1")
Process.waitpid(bundleProcessId)

@processId = Process.spawn("source #{initializerPath}\n ruby #{path}/script/rails server -e #{environment} -p #{port}")

File.open(File.join(path, "tmp", "railcar.pid"), "w") do |f|
f.write(@processId.to_s)
end

@launched = true
rescue
@launched = false
end
end

def stop
if @processId
if (theProcessId = (@processId || File.read(File.join(path, "tmp", "railcar.pid"))))
begin
Process.kill("KILL", @processId)
@processId = nil

Process.kill("KILL", theProcessId.to_i)
theProcessId = nil
FileUtils.rm(File.join(path, "tmp", "railcar.pid")) if File.exists?(File.join(path, "tmp", "railcar.pid"))

@launched = false
return true
rescue
Expand All @@ -50,7 +60,7 @@ def stop
end

def launched?
@launched
@launched || File.exist?(File.join(path, "tmp", "railcar.pid"))
end

def environments
Expand All @@ -71,6 +81,7 @@ def databaseInfo
end
end

# TODO: switch this to a delegate setup so I can toss it to another thread
def versionControlStatus
if (File.exist?(File.join(@path, ".git")))
gitStatus
Expand Down
36 changes: 25 additions & 11 deletions Railcar/RCApplicationLaunchWindowController.rb
Expand Up @@ -51,23 +51,14 @@ def stopApplication
controlButton.setEnabled(false)

@app.stop

appLink.setHidden(true)
appLinkLabel.setHidden(true)

controlButton.setTitle("Launch")
controlButton.setEnabled(true)
refreshControl
end

def launchApplication
controlButton.setEnabled(false)

if @app.launch
appLink.setStringValue("http://localhost:#{@app.port}/")
appLink.setHidden(false)
appLinkLabel.setHidden(false)

controlButton.setTitle("Stop")
refreshControl
else
appLink.setHidden(true)
appLinkLabel.setHidden(true)
Expand Down Expand Up @@ -100,4 +91,27 @@ def refreshVersionControl
versionControlLabel.setStringValue(info[:name])
versionControlStatusLabel.setStringValue(info[:status])
end

def refreshControl
if @app.launched?
appLink.setStringValue("http://localhost:#{@app.port}/")
appLink.setHidden(false)
appLinkLabel.setHidden(false)

controlButton.setTitle("Stop")
else
appLink.setHidden(true)
appLinkLabel.setHidden(true)

controlButton.setTitle("Launch")
controlButton.setEnabled(true)
end
end

def windowDidBecomeKey(notification)
refreshVersionControl
refreshEnvironments
refreshVersions
refreshControl
end
end
31 changes: 27 additions & 4 deletions Railcar/RCApplicationManager.rb
Expand Up @@ -12,24 +12,47 @@ def applications
NSUserDefaults.standardUserDefaults['railcar.linkedApplications']
end

# We do the persistentDomainForName song and dance so the CLI can use this same code
def addApplicationToList(data)
hsh = NSUserDefaults.standardUserDefaults['railcar.linkedApplications']
hsh[uniqueId(data)] = data
NSUserDefaults.standardUserDefaults['railcar.linkedApplications'] = hsh
defaults = NSUserDefaults.standardUserDefaults.persistentDomainForName("com.arcturo.railcar")

hsh = defaults.dup
hsh['railcar.linkedApplications'][uniqueId(data)] = data

NSUserDefaults.standardUserDefaults.setPersistentDomain(hsh, forName: "com.arcturo.railcar")
end

def appDataForPath(path)
appList = NSUserDefaults.standardUserDefaults.persistentDomainForName("com.arcturo.railcar")['railcar.linkedApplications']

appList.values.select do |data|
data[:path] == path
end.first
end

def appDataForName(name)
appList = NSUserDefaults.standardUserDefaults.persistentDomainForName("com.arcturo.railcar")['railcar.linkedApplications']

appList.values.select do |data|
data[:name] == name
end.first
end

def uniqueId(data)
"#{Time.now.to_i}-#{data[:name].gsub(/\W/, '')}-#{data[:path].gsub(/\W/, '')}"
end

# We do the persistentDomainForName song and dance so the CLI can use this same code
def add(path, data = {})
if isRailsApp?(path)
defaults = NSUserDefaults.standardUserDefaults.persistentDomainForName("com.arcturo.railcar")

addApplicationToList({
:name => discernAppName(path),
:path => path,
:rubyVersion => DEFAULT_RUBY_VERSION,
:environment => "development",
:port => (NSUserDefaults.standardUserDefaults['railcar.linkedApplications'].length + 3001).to_s
:port => (defaults['railcar.linkedApplications'].length + 3001).to_s
}.merge(data))

true
Expand Down

0 comments on commit 7796b99

Please sign in to comment.