plugins/runnables/lib/runnables.rb in redcar-0.3.7.1 vs plugins/runnables/lib/runnables.rb in redcar-0.3.8

- old
+ new

@@ -1,25 +1,47 @@ +require File.dirname(__FILE__) + "/../vendor/session-2.4.0/lib/session" +Session.use_open4 = true + +require 'runnables/command_output_controller' +require 'runnables/running_process_checker' + module Redcar class Runnables + TREE_TITLE = "Runnables" + def self.run_process(command) + tab = Redcar.app.focussed_window.new_tab(HtmlTab) + controller = CommandOutputController.new(command) + tab.html_view.controller = controller + tab.focus + end + class TreeMirror include Redcar::Tree::Mirror def initialize(project) - runnables = project.config_file(:runnables) - if runnables - @top = runnables.map do |name, info| - Runnable.new(name, info) + runnable_file_paths = project.config_files("runnables/*.json") + + runnables = [] + runnable_file_paths.each do |path| + json = File.read(path) + this_runnables = JSON(json)["commands"] + runnables += this_runnables || [] + end + + if runnables.any? + @top = runnables.map do |runnable| + Runnable.new(runnable["name"], runnable) end else @top = [HelpItem.new] end end def title - "Runnables" + TREE_TITLE end def top @top end @@ -44,29 +66,27 @@ def text @name end def leaf? - @info[:command] + @info["command"] end def icon if leaf? File.dirname(__FILE__) + "/../icons/cog.png" + else + :directory end end def children - return [] if leaf? - - @info.map do |name, info| - Runnable.new(name, info) - end + [] end def command - @info[:command] + @info["command"] end end class TreeController include Redcar::Tree::Controller @@ -74,26 +94,63 @@ def initialize(project) @project = project end def activated(tree, node) - command = node.command - output = `#{command}` - tab = Redcar.app.focussed_window.new_tab(EditTab) - tab.edit_view.document.text = output - tab.title = node.text - tab.focus + case node + when Runnable + Runnables.run_process(node.command) + when HelpItem + tab = Redcar.app.focussed_window.new_tab(HtmlTab) + tab.go_to_location("http://wiki.github.com/danlucraft/redcar/users-guide-runnables") + tab.title = "Runnables Help" + tab.focus + end end end class ShowRunnables < Redcar::Command def execute + if tree = win.treebook.trees.detect {|tree| tree.tree_mirror.title == TREE_TITLE } + win.treebook.focus_tree(tree) + else + project = Project::Manager.in_window(win) + tree = Tree.new( + TreeMirror.new(project), + TreeController.new(project) + ) + win.treebook.add_tree(tree) + end + end + end + + class RunEditTabCommand < Redcar::EditTabCommand + def file_mappings project = Project::Manager.in_window(win) - tree = Tree.new( - TreeMirror.new(project), - TreeController.new(project) - ) - win.treebook.add_tree(tree) + runnable_file_paths = project.config_files("runnables/*.json") + + file_runners = [] + runnable_file_paths.each do |path| + json = File.read(path) + this_file_runners = JSON(json)["file_runners"] + file_runners += this_file_runners || [] + end + file_runners end + + def execute + file_mappings.each do |file_mapping| + regex = Regexp.new(file_mapping["regex"]) + if tab.edit_view.document.mirror.path =~ regex + command_schema = file_mapping["command"] + command = command_schema.gsub("__PATH__", tab.edit_view.document.mirror.path) + puts command + Runnables.run_process(command) + end + end + end end end end + + +