Module: Adhearsion::CLI::AhnCommand::CommandHandler

Defined in:
lib/adhearsion/cli.rb

Defined Under Namespace

Classes: CLIException, ComponentError, PathInvalid, UnknownCommand, UnknownProject

Class Method Summary collapse

Class Method Details

.create(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/adhearsion/cli.rb', line 76

def create(*args)
  if args.size.zero?
    raise CommandHandler::UnknownCommand.new("Must specify something to create!")
  elsif args.size == 1
    # We're creating a project
    path = args.first
    require 'rubigen'
    require 'rubigen/scripts/generate'
    source = RubiGen::PathSource.new(:application,
      File.join(File.dirname(__FILE__), "../../app_generators"))
    RubiGen::Base.reset_sources
    RubiGen::Base.append_sources source
    RubiGen::Scripts::Generate.new.run([path], :generator => 'ahn')
  elsif args.size == 2
    # We're creating a feature (e.g. a component)
    feature_type, component_name = args

    if feature_type != "component"
      # At the moment, only components can be created.
      raise CommandHandler::UnknownCommand.new("Don't know how to create '#{feature_type}'")
    end

    if component_name !~ /^[a-z][\w_]+$/
      raise CommandHandler::ComponentError.new("Component name must be lowercase alphanumeric characters " +
          "and begin with a character")
    end

    app_path = PathString.from_application_subdirectory Dir.pwd

    raise PathInvalid.new(Dir.pwd) if app_path.nil?

    new_component_dir = app_path + "/components/#{component_name}"
    raise ComponentError.new("Component #{component_name} already exists!") if File.exists?(new_component_dir)

    # Everything's good. Let's create the component
    Dir.mkdir new_component_dir
    File.open(new_component_dir + "/#{component_name}.rb","w") do |file|
      file.puts <<-RUBY
# See http://docs.adhearsion.com for more information on how to write components or
# look at the examples in newly-created projects.
      RUBY
    end
    File.open(new_component_dir + "/#{component_name}.yml","w") do |file|
      file.puts '# You can use this file for component-specific configuration.'
    end
    puts "Created blank component '#{component_name}' at components/#{component_name}"
  else
    raise CommandHandler::UnknownCommand.new("Provided too many arguments to 'create'")
  end
end

.disable(type, name) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/adhearsion/cli.rb', line 166

def disable(type, name)
  case type
    when "component"
      app_path = PathString.from_application_subdirectory Dir.pwd
      if app_path
        disabled_dir = File.join app_path, "components", "disabled"

        disabled_component_path = File.join disabled_dir, name
        enabled_component_path  = File.join app_path, "components", name

        Dir.mkdir disabled_dir unless File.directory?(disabled_dir)

        if File.directory? enabled_component_path
          if File.directory?(disabled_component_path)
            raise ComponentError.new("There is already a disabled component at #{disabled_component_path}")
          else
            FileUtils.mv enabled_component_path, disabled_component_path
            puts "Disabled component #{name}"
          end
        else
          raise ComponentError.new("Could not find component #{name} at #{enabled_component_path} !")
        end
      else
        raise PathInvalid.new(Dir.pwd)
      end
    else
      raise UnknownCommand.new("disable #{type}")
  end
end

.enable(type, name) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/adhearsion/cli.rb', line 140

def enable(type, name)
  case type
    when "component"
      app_path = PathString.from_application_subdirectory Dir.pwd
      if app_path
        disabled_component_path = File.join app_path, "components", "disabled", name
        disabled_components_path = File.join app_path, "components", "disabled"
        enabled_component_path  = File.join app_path, "components", name
        if File.directory? disabled_component_path
          FileUtils.mv disabled_component_path, enabled_component_path
          puts "Enabled component #{name}"
        elsif File.directory? enabled_component_path
          raise ComponentError.new("This component is already enabled.")
        elsif !File.directory? disabled_components_path
          raise ComponentError.new("There is no components/disabled directory!")
        else
          raise ComponentError.new("The requested component was not found.")
        end
      else
        raise PathInvalid.new(Dir.pwd)
      end
    else
      raise UnknownCommand.new("enable #{type}")
  end
end

.helpObject



136
137
138
# File 'lib/adhearsion/cli.rb', line 136

def help
  puts USAGE
end

.method_missing(action, *args) ⇒ Object

Raises:



196
197
198
# File 'lib/adhearsion/cli.rb', line 196

def method_missing(action, *args)
  raise UnknownCommand, [action, *args] * " "
end

.start(path, daemon = false, pid_file = nil) ⇒ Object

Raises:



127
128
129
130
# File 'lib/adhearsion/cli.rb', line 127

def start(path, daemon=false, pid_file=nil)
  raise PathInvalid, path unless File.exists? path + "/.ahnrc"
  Adhearsion::Initializer.start path, :daemon => daemon, :pid_file => pid_file
end

.versionObject



132
133
134
# File 'lib/adhearsion/cli.rb', line 132

def version
  puts "Adhearsion v#{Adhearsion::VERSION::STRING}"
end