Module: Appjam::Generators::Actions

Included in:
Gist, Help, Lib, Search
Defined in:
lib/appjam/generators/actions.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/appjam/generators/actions.rb', line 7

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#apply_component_for(choice, component) ⇒ Object

Returns the related module for a given component and option generator_module_for(‘rr’, :mock)



22
23
24
25
26
27
28
29
# File 'lib/appjam/generators/actions.rb', line 22

def apply_component_for(choice, component)
  # I need to override Thor#apply because for unknow reason :verobse => false break tasks.
  path = File.expand_path(File.dirname(__FILE__) + "/components/#{component.to_s.pluralize}/#{choice}.rb")
  say_status :apply, "#{component.to_s.pluralize}/#{choice}"
  shell.padding += 1
  instance_eval(open(path).read)
  shell.padding -= 1
end

#ask(statement, default = nil, color = nil) ⇒ Object

Ask something to the user and receives a response.

Example

ask("What is your app name?")


110
111
112
113
114
115
# File 'lib/appjam/generators/actions.rb', line 110

def ask(statement, default=nil, color=nil)
  default_text = default ? " (leave blank for #{default}):" : nil
  say("#{statement}#{default_text} ", color)
  result = $stdin.gets.strip
  result.blank? ? default : result
end

#check_app_existence(app) ⇒ Object

Raise SystemExit if the app is iexistent



118
119
120
121
122
123
124
125
126
127
# File 'lib/appjam/generators/actions.rb', line 118

def check_app_existence(app)
  unless File.exist?(destination_root(app))
    say
    say "================================================================="
    say "We didn't found #{app.underscore.camelize}!                      "
    say "================================================================="
    say
    # raise SystemExit
  end
end

#destination_root(*paths) ⇒ Object

Returns the root for this thor class (also aliased as destination root).



83
84
85
# File 'lib/appjam/generators/actions.rb', line 83

def destination_root(*paths)
  File.expand_path(File.join(@destination_stack.last, paths))
end

#execute_component_setup(component, choice) ⇒ Object

Performs the necessary generator for a given component choice execute_component_setup(:mock, ‘rr’)



13
14
15
16
17
18
# File 'lib/appjam/generators/actions.rb', line 13

def execute_component_setup(component, choice)
  return true && say("Skipping generator for #{component} component...", :yellow) if choice.to_s == 'none'
  say "Applying '#{choice}' (#{component})...", :yellow
  apply_component_for(choice, component)
  send("setup_#{component}") if respond_to?("setup_#{component}")
end

#fetch_app_name(app = 'app') ⇒ Object

Returns the app_name for the application at root



100
101
102
103
# File 'lib/appjam/generators/actions.rb', line 100

def fetch_app_name(app='app')
  app_path = destination_root(app, 'app.rb')
  @app_name ||= File.read(app_path).scan(/class\s(.*?)\s</).flatten[0]
end

#fetch_component_choice(component) ⇒ Object

Returns the component choice stored within the .component file of an application fetch_component_choice(:mock)



33
34
35
# File 'lib/appjam/generators/actions.rb', line 33

def fetch_component_choice(component)
  retrieve_component_config(destination_root('.components'))[component]
end

#in_app_root?Boolean

Returns true if inside a Appjam application

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/appjam/generators/actions.rb', line 88

def in_app_root?
  File.exist?(destination_root('Classes'))
  # Dir.glob("*.xcodeproj").count >= 1
end

#invalid_fields(fields) ⇒ Object

Returns the field with an unacceptable name(for symbol) else returns nil



94
95
96
97
# File 'lib/appjam/generators/actions.rb', line 94

def invalid_fields(fields)
  results = fields.select { |field| field.split(":").first =~ /\W/ }
  results.empty? ? nil : results
end

#resolve_valid_choice(component) ⇒ Object

Prompts the user if necessary until a valid choice is returned for the component resolve_valid_choice(:mock) => ‘rr’



55
56
57
58
59
60
61
62
63
# File 'lib/appjam/generators/actions.rb', line 55

def resolve_valid_choice(component)
  available_string = self.class.available_choices_for(component).join(", ")
  choice = options[component]
  until valid_choice?(component, choice)
    say("Option for --#{component} '#{choice}' is not available.", :red)
    choice = ask("Please enter a valid option for #{component} (#{available_string}):")
  end
  choice
end

#retrieve_component_config(target) ⇒ Object

Loads the component config back into a hash i.e retrieve_component_config(…) => { :mock => ‘rr’, :test => ‘riot’, … }



49
50
51
# File 'lib/appjam/generators/actions.rb', line 49

def retrieve_component_config(target)
  YAML.load_file(target)
end

#store_component_choice(key, value) ⇒ Object

Set the component choice and store it in the .component file of the application store_component_choice(:renderer, :haml)



39
40
41
42
43
44
45
# File 'lib/appjam/generators/actions.rb', line 39

def store_component_choice(key, value)
  path        = destination_root('.components')
  config      = retrieve_component_config(path)
  config[key] = value
  create_file(path, :force => true) { config.to_yaml }
  value
end

#store_component_config(destination) ⇒ Object

Creates a component_config file at the destination containing all component options Content is a yamlized version of a hash containing component name mapping to chosen value



73
74
75
76
77
78
79
80
# File 'lib/appjam/generators/actions.rb', line 73

def store_component_config(destination)
  components = @_components || options
  create_file(destination) do
    self.class.component_types.inject({}) { |result, comp|
      result[comp] = components[comp].to_s; result
    }.to_yaml
  end
end

#valid_choice?(component, choice) ⇒ Boolean

Returns true if the option passed is a valid choice for component valid_option?(:mock, ‘rr’)

Returns:

  • (Boolean)


67
68
69
# File 'lib/appjam/generators/actions.rb', line 67

def valid_choice?(component, choice)
  choice.present? && self.class.available_choices_for(component).include?(choice.to_sym)
end

#valid_constant?(name) ⇒ Boolean

Ensure that project name is valid, else raise an NameError

Returns:

  • (Boolean)


130
131
132
133
134
135
136
# File 'lib/appjam/generators/actions.rb', line 130

def valid_constant?(name)
  if name =~ /^\d/
    raise ::NameError, "Project name #{name} cannot start with numbers"
  elsif name =~ /^\W/
    raise ::NameError, "Project name #{name} cannot start with non-word character"
  end
end

#which(cmd) ⇒ Object

Detect command and dump it’s path



139
140
141
142
143
144
145
146
147
148
# File 'lib/appjam/generators/actions.rb', line 139

def which cmd
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    }
  end
  return nil
end