Module: EasyHtmlGenerator

Defined in:
lib/easy_html_generator.rb

Overview

main module loads projects and dispatches rack requests

Defined Under Namespace

Modules: Generator Classes: Checksum, Config, Generators, Project, RackDispatcher, Workspace

Constant Summary collapse

EHC_WORKSPACE_PATH =
File.expand_path File.join(File.dirname(__FILE__), '..')
EHC_SRC_PATH =
File.join(EHC_WORKSPACE_PATH, 'src')
EHC_SHARED_PATH =
File.join(EHC_SRC_PATH, 'shared')
EHC_SHARED_HELPER_PATH =
File.join(EHC_SHARED_PATH, 'lib', 'helper')
EHC_SHARED_GENERATORS_PATH =
File.join(EHC_SHARED_PATH, 'lib', 'generators')
EHC_SHARED_STYLES_PATH =
File.join(EHC_SHARED_PATH, 'assets', 'styles')
WORKSPACE_PATH =
Bundler.root
SRC_PATH =
File.join(WORKSPACE_PATH, 'src')
DIST_PATH =
File.join(WORKSPACE_PATH, 'dist')
SHARED_PATH =
File.join(SRC_PATH, 'shared')
SHARED_HELPER_PATH =
File.join(SHARED_PATH, 'lib', 'helper')
SHARED_GENERATORS_PATH =
File.join(SHARED_PATH, 'lib', 'generators')
SHARED_STYLES_PATH =
File.join(SHARED_PATH, 'assets', 'styles')
CREATE_ON_INIT =
[
  SRC_PATH,
  DIST_PATH
]
COPY_ON_INIT =
{
  EHC_SHARED_PATH            => SHARED_PATH,
  "#{EHC_SRC_PATH}/template" => "#{SRC_PATH}/demo"
}

Class Method Summary collapse

Class Method Details

.clean_path(path) ⇒ Object



71
72
73
74
75
# File 'lib/easy_html_generator.rb', line 71

def self.clean_path(path)
  path = path.slice(1..-1) if path.start_with? '/'
  path.sub!("#{project_name_by_path(path)}/", '')
  path
end

.dispatch(request) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/easy_html_generator.rb', line 50

def self.dispatch(request)
  path         = request.env['REQUEST_PATH'] || request.env['PATH_INFO']
  cleaned_path = clean_path path
  project_name = project_name_by_path path

  project = Workspace.project_by_name project_name

  return path unless project

  project.generate if project.should_generate_for? cleaned_path

  # return absolute dist path
  File.join(project.dist_path, cleaned_path)
end

.generate_allObject



77
78
79
# File 'lib/easy_html_generator.rb', line 77

def self.generate_all
  Workspace.projects.each{|name, project| project.generate }
end

.generate_project(project) ⇒ Object



81
82
83
# File 'lib/easy_html_generator.rb', line 81

def self.generate_project(project)
  Workspace.projects[project].generate
end

.project_name_by_path(path) ⇒ Object



65
66
67
68
69
# File 'lib/easy_html_generator.rb', line 65

def self.project_name_by_path(path)
  path = path.slice(1..-1) if path.start_with? '/'
  path = path.split('/').first if path.include? '/'
  path
end

.projectsObject



46
47
48
# File 'lib/easy_html_generator.rb', line 46

def self.projects
  Workspace.projects
end

.rack_appObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/easy_html_generator.rb', line 85

def self.rack_app
  Rack::Builder.new do
    use Rack::ShowExceptions
    use Rack::Reloader, 0
    use EasyHtmlGenerator::RackDispatcher
    use Rack::Deflater
    use Rack::ContentLength
    use Rack::StaticCache, urls: [''], root: EasyHtmlGenerator::DIST_PATH
    use Rack::Static,
      urls: [''],
      root: EasyHtmlGenerator::DIST_PATH,
      index: 'index.html'

    run lambda { |env| [404, {'Content-Type' => 'text/plain'}, ['404 File not found!']] }
  end
end

.start_server(host, port) ⇒ Object



102
103
104
105
106
107
# File 'lib/easy_html_generator.rb', line 102

def self.start_server(host, port)
  config = { Host: host,
             Port: port}

  Rack::Handler.get('thin').run(rack_app, config)
end