Class: Francis::FileStructure

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

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ FileStructure

Returns a new instance of FileStructure.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/francis.rb', line 17

def initialize(name, options)
  @name = name
  sinatra_gem_version = Gem.latest_version_for 'sinatra'
  @structure = {
    "/views" => {},
    "/public" => {},
    "/" => {
      "config.ru" => %Q(require 'bundler'\nBundler.require\nrequire_relative 'app'\nrun #{@name.capitalize}),
      "app.rb" => %Q(require 'sinatra'\nclass #{@name.capitalize} < Sinatra::Base\n\tget '/' do\n\t\t'Hello world!'\n\tend\nend),
      "Gemfile" => %Q(source 'https://rubygems.org'\ngem 'sinatra', '#{sinatra_gem_version.approximate_recommendation}'\n)
    }
  }
  @options = options
  @wd = "#{Dir.getwd}/#{@name}"
  parse_options
  execute!
end

Instance Method Details

#add_folder(folder) ⇒ Object



34
35
36
# File 'lib/francis.rb', line 34

def add_folder(name)
  @structure["/#{name}"] = {}
end

#execute!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/francis.rb', line 47

def execute!
  # create target directory
  Dir.mkdir(@wd)
  # cycle through structure and create folders and files
  @structure.each{|folder,files|
    if folder != "/"
      Dir.mkdir("#{@wd}#{folder}")
    end
    files.each{|name,contents|
      File.open("#{@wd}#{folder}/#{name}", "w"){|f|
        f.write(contents)
      }
    }
  }
  # if git option is given, execute git init command
  if @options[:git]
    Dir.chdir(@wd)
    system "git init"
  end
  puts "#{@name} created as Sinatra app"
end

#gitObject



68
69
70
71
72
# File 'lib/francis.rb', line 68

def git
  if @options[:git]
    @structure["/"][".gitignore"] = %Q(.DS_Store\nGemfile.lock\npids/*\nlogs/*\n*.gem)
  end
end

#parse_optionsObject



40
41
42
43
44
45
46
# File 'lib/francis.rb', line 40

def parse_options
  git
  readme
  unicorn
  whenever
  require_gem
end

#readmeObject



73
74
75
76
77
# File 'lib/francis.rb', line 73

def readme
  if @options[:readme]
    @structure["/"]["readme.md"] = %Q(# #{@name.capitalize}\n\nTODO: Write a readme file)
  end
end

#remove_folder(name) ⇒ Object



37
38
39
# File 'lib/francis.rb', line 37

def remove_folder(name)
  @structure.delete("/#{name}"){|el| "#{el} not found"}
end

#require_gemObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/francis.rb', line 94

def require_gem
  if @options[:require_gem]
    @options[:require_gem].each{|r|
      version = Gem.latest_version_for(r)
      if version
        @structure["/"]["Gemfile"] << %Q(gem '#{r}', '#{version.approximate_recommendation}'\n)
      else
        warn "Could not find gem: #{r}, skipping inclusion in Gemfile"
      end
    }
  end
end

#unicornObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/francis.rb', line 78

def unicorn
  if @options[:unicorn]
    add_folder "/config"
    add_folder "/pids"
    add_folder "/logs"
    @structure["/config"]["unicorn.rb"] = %Q(working_directory '#{@wd}'\npid '#{@wd}/pids/unicorn.pid'\nstderr_path '#{@wd}/logs/unicorn.log'\nstdout_path '#{@wd}/logs/unicorn.log'\nlisten '/tmp/#{@name}.sock'\nworker_processes 2\ntimeout 30)
    @structure["/pids"]["unicorn.pid"] = ""
    @structure["/logs"]["unicorn.log"] = ""
  end
end

#wheneverObject



88
89
90
91
92
93
# File 'lib/francis.rb', line 88

def whenever
  if @options[:whenever]
    add_folder "/config"
    @structure["/config"]["schedule.rb"] = ""
  end
end