Class: Syntaxer::Wizzard

Inherits:
Object
  • Object
show all
Defined in:
lib/syntaxer/wizzard.rb

Constant Summary collapse

LANG =

Interactive console wizzard

[:ruby, :haml, :sass, :javascript]
FOLDERS_RAILS =
{
  :ruby => ["app/*", "app/**/*", "lib/*", "lib/**/*"],
  :haml => ["app/views/*", "app/views/**/*"],
  :sass => ["public/stylesheets/sass/*", "public/stylesheets/sass/**/*"],
  :javascript => ["public/javascript/*", "public/javascript/**/*"]
}
DEFAULT_GLOB =
["*/*", "**/*"]
FOLDERS =
{
  :ruby => DEFAULT_GLOB,
  :haml => DEFAULT_GLOB,
  :sass => DEFAULT_GLOB,
  :javascript => DEFAULT_GLOB
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Wizzard

Returns a new instance of Wizzard.



21
22
23
# File 'lib/syntaxer/wizzard.rb', line 21

def initialize options
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/syntaxer/wizzard.rb', line 19

def options
  @options
end

Class Method Details

.startObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# File 'lib/syntaxer/wizzard.rb', line 51

def start
  options = {}
  options[:root_path] = Dir.getwd
  rails = false
  say("Some greeting message. Hello God of this computer! Please answer in couple of question:".color(:green))
  
  # ask for the languages to install
  to_install = []
  LANG.each do |lang|
    if agree("Install support of #{lang.to_s.capitalize}? (y/n)".color(:yellow))
      default_paths = rails? ? FOLDERS_RAILS[lang] : FOLDERS[lang]
      paths = ask("Paths to check separated by comma (default are #{default_paths.inspect}):".color(:yellow))
      if paths.empty?
        paths = default_paths
      else
        paths = paths.split(',').map{|l| l.gsub(/\s/,'')}
      end
      to_install.push([lang, paths])
    end
  end

  options[:languages] = to_install

  # ask for path to save config
  if rails?
    say("Rails application detected. Config file saved to config/synaxer.rb".color(:green))
    rails = true
    config_path = "config"
  else
    config_path = ask("Specify where to save syntaxer's config file (./ by default):".color(:yellow))
    config_path = '.' if config_path.empty?
    expanded_config_path = File.expand_path(config_path)
    options[:config_dir_exists] = FileTest.directory?(expanded_config_path)
    options[:create_config_dir] = agree("No such directory found #{expanded_config_path}. Create it?".color(:green)) unless options[:config_dir_not_exists]
  end

  options[:rails] = rails
  options[:config_path] = config_path

  # trying to detect GIT
  begin
    g = ::Git.open(options[:root_path])
    options[:git] =  agree("Found git repo in #{File.expand_path(options[:root_path])}. Would you like install hook to check syntax before every commit? (y/n)".color(:yellow))
  rescue
    options[:git] = false
  end
 
  Wizzard.new(options).run
  
  # buy message
  say("Syntaxer is configured and installed. You can edit config in #{File.join(config_path, Syntaxer::SYNTAXER_CONFIG_FILE_NAME)}".color(:green))

rescue Interrupt => e
  # external quit
  puts "\nBuy"
end

Instance Method Details

#configObject



43
44
45
46
47
# File 'lib/syntaxer/wizzard.rb', line 43

def config
  reader = Reader::DSLReader.build
  writer = Writer.new(@options[:languages], reader.rules)
  writer.get_config
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/syntaxer/wizzard.rb', line 25

def run
  FileUtils.mkdir_p(@options[:config_path]) if !@options[:config_dir_exists] && @options[:create_config_dir]
  config_full_path = File.join(@options[:config_path], Syntaxer::SYNTAXER_CONFIG_FILE_NAME)
  File.open(config_full_path, 'w') do |f|
    f.write(config)
  end

  if @options[:git]
    options = {:root_path => @options[:root_path], :repository => :git, :config_file => File.join(@options[:config_path], Syntaxer::SYNTAXER_CONFIG_FILE_NAME)}
    options.merge!({:rails => true}) if options[:rails]
    Syntaxer.make_hook(options)
  end

  if @options[:rails] && @options[:languages].include?(:javascript)
    system('rake jslint:copy_config')
  end
end