Class: Blufin::GenerateUiRoutes

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

Constant Summary collapse

SCHEMA_FILE =
"#{Blufin::Base::get_base_path}#{Blufin::Base::OPT_PATH}/schema/routes.yml"
HOME_TERM =
'dashboard'

Class Method Summary collapse

Class Method Details

.run(project) ⇒ Object

Generates routes in the UI.

Returns:

  • void

Raises:

  • (RuntimeError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/generate/generate_ui_routes.rb', line 10

def self.run(project)
    raise RuntimeError, "Expected project type to be: #{Blufin::Projects::TYPE_UI}, instead got: #{project[Blufin::Projects::TYPE]}" unless project[Blufin::Projects::TYPE] == Blufin::Projects::TYPE_UI
    @errors          = []
    @warnings        = []
    @files           = []
    @files_to_create = []
    begin
        @output = "const routes = [\n    {\n"
        project_path      = Blufin::Projects::get_project_path(project[Blufin::Projects::PROJECT_ID], true)
        routes_file       = "#{project_path}/#{project[Blufin::Projects::UI][Blufin::Projects::ROUTES_FILE]}"
        routes_yml        = Blufin::Yml::read_file(routes_file, SCHEMA_FILE)
        target_file       = "#{project_path}/src/#{Blufin::Strings::remove_surrounding_slashes(routes_yml['RoutesFile'])}"
        layout            = routes_yml['Layout']
        pages_root        = routes_yml['PagesRoot']
        pages_not_found   = routes_yml['Pages404']
        path_pages_ignore = []
        path_to_pages     = "#{project_path}/src/#{pages_root}"
        # Add ignore path(s) -- if exists.
        routes_yml['Ignore'].each { |ignore_path| path_pages_ignore << "#{Blufin::Strings::remove_surrounding_slashes(ignore_path)}" } if routes_yml.has_key?('Ignore')
        routes = routes_yml['Routes']
        routes.each_with_index do |route, idx|
            comma       = route.has_key?('Children') ? ',' : nil
            parent_path = route['Parent']['Path']
            @output     += "path: '/\#{route['Parent'].has_key?('RootPath') && route['Parent']['RootPath'] ? nil : parent_path}',\nsectionName: '\#{route['Parent']['Name']}',\nsectionIcon: '\#{route['Parent']['Icon']}',\ncomponent: () => import('./../\#{layout}')\#{comma}\n"
            file   = parent_path == HOME_TERM ? "#{pages_root}/#{parent_path}/#{HOME_TERM}.vue" : "#{pages_root}/#{parent_path}/#{parent_path}-#{HOME_TERM}.vue"
            prefix = parent_path == HOME_TERM ? 'dashboard/' : nil
            process_file(file, project_path)
            if route.has_key?('Children')
                @output += "children: [\n    {\n        path: '',\n        component: () => import('./../\#{file}')\n    },\n"
                children = route['Children']
                children.each_with_index do |child, idx|
                    comma    = idx == children.length - 1 ? nil : ','
                    file     = "#{pages_root}/#{parent_path}/#{child['Path']}.vue"
                    disabled = child.has_key?('Disabled') && child['Disabled'] ? "\n                disabled: true," : nil
                    process_file(file, project_path)
                    @output += "    {\n        path: '\#{prefix}\#{child['Path']}',\n        name: '\#{child['Name']}',\#{disabled}\n        component: () => import('./../\#{file}')\n    }\#{comma}\n"
                end
                @output += "        ]\n"
            end

            if idx == routes.length - 1
                @output += "    }\n"
            else
                @output += "    },\n    {\n"
            end
        end

        @output += "];\n\nif (process.env.MODE !== 'ssr') {\n    routes.push({\npath: '*',\ncomponent: () => import('./../\#{pages_not_found}')\n    });\n}\n\nexport default routes;\n"
    rescue => e
        Blufin::Terminal::print_exception(e)
    end

    # Write the output to the file.
    Blufin::Files::write_file(target_file, Blufin::Arrays::convert_string_to_line_array(@output))

    # Output message.
    Blufin::Terminal::info('Generating route file(s):') if @files_to_create.any?

    # Write all the blank route files.
    @files_to_create.each do |ftc|
        if Blufin::Files::file_exists(ftc)
            Blufin::GenerateBase::output(ftc, Blufin::GenerateBase::OUTPUT_SKIP)
        else
            Blufin::Files::write_file(ftc, Blufin::Arrays::convert_string_to_line_array(get_blank_file_content))
            Blufin::GenerateBase::output(ftc, Blufin::GenerateBase::OUTPUT_GENERATE)
        end
    end

    puts if @files_to_create.any?

    # Check for rogue files.
    Blufin::Files::get_files_in_dir(path_to_pages).each do |file|
        next if file =~ /#{path_to_pages}\/(#{path_pages_ignore.join('|')})/
        unless @files_to_create.include?(file)
            @warnings << "Found rogue file: #{Blufin::Terminal::format_invalid(file)}"
        end
    end

    return @errors, @warnings

end